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 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 12 | #include "clinic/longobject.c.h" |
| 13 | /*[clinic input] |
| 14 | class int "PyObject *" "&PyLong_Type" |
| 15 | [clinic start generated code]*/ |
| 16 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=ec0275e3422a36e3]*/ |
| 17 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 18 | #ifndef NSMALLPOSINTS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 19 | #define NSMALLPOSINTS 257 |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 20 | #endif |
| 21 | #ifndef NSMALLNEGINTS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 22 | #define NSMALLNEGINTS 5 |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 23 | #endif |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 24 | |
Serhiy Storchaka | 196a7bc | 2017-02-02 16:54:45 +0200 | [diff] [blame] | 25 | _Py_IDENTIFIER(little); |
| 26 | _Py_IDENTIFIER(big); |
| 27 | |
Mark Dickinson | e441674 | 2009-02-15 15:14:57 +0000 | [diff] [blame] | 28 | /* convert a PyLong of size 1, 0 or -1 to an sdigit */ |
Victor Stinner | 08a80b1 | 2013-07-17 22:33:42 +0200 | [diff] [blame] | 29 | #define MEDIUM_VALUE(x) (assert(-1 <= Py_SIZE(x) && Py_SIZE(x) <= 1), \ |
| 30 | Py_SIZE(x) < 0 ? -(sdigit)(x)->ob_digit[0] : \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 31 | (Py_SIZE(x) == 0 ? (sdigit)0 : \ |
| 32 | (sdigit)(x)->ob_digit[0])) |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 33 | |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 34 | PyObject *_PyLong_Zero = NULL; |
| 35 | PyObject *_PyLong_One = NULL; |
| 36 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 37 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 38 | /* Small integers are preallocated in this array so that they |
| 39 | can be shared. |
| 40 | The integers that are preallocated are those in the range |
| 41 | -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive). |
| 42 | */ |
| 43 | static PyLongObject small_ints[NSMALLNEGINTS + NSMALLPOSINTS]; |
| 44 | #ifdef COUNT_ALLOCS |
Mark Dickinson | c286e58 | 2012-09-20 21:29:28 +0100 | [diff] [blame] | 45 | Py_ssize_t quick_int_allocs, quick_neg_int_allocs; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 46 | #endif |
| 47 | |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 48 | static PyObject * |
Mark Dickinson | e441674 | 2009-02-15 15:14:57 +0000 | [diff] [blame] | 49 | get_small_int(sdigit ival) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 50 | { |
Benjamin Peterson | 45c9dce | 2014-03-14 21:53:51 -0500 | [diff] [blame] | 51 | PyObject *v; |
Benjamin Peterson | 041c38a | 2014-03-14 21:47:23 -0500 | [diff] [blame] | 52 | assert(-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS); |
Benjamin Peterson | 45c9dce | 2014-03-14 21:53:51 -0500 | [diff] [blame] | 53 | v = (PyObject *)&small_ints[ival + NSMALLNEGINTS]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 54 | Py_INCREF(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 55 | #ifdef COUNT_ALLOCS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 56 | if (ival >= 0) |
| 57 | quick_int_allocs++; |
| 58 | else |
| 59 | quick_neg_int_allocs++; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 60 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 61 | return v; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 62 | } |
| 63 | #define CHECK_SMALL_INT(ival) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 64 | do if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { \ |
| 65 | return get_small_int((sdigit)ival); \ |
| 66 | } while(0) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 67 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 68 | static PyLongObject * |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 69 | maybe_small_long(PyLongObject *v) |
| 70 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 71 | if (v && Py_ABS(Py_SIZE(v)) <= 1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 72 | sdigit ival = MEDIUM_VALUE(v); |
| 73 | if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { |
| 74 | Py_DECREF(v); |
| 75 | return (PyLongObject *)get_small_int(ival); |
| 76 | } |
| 77 | } |
| 78 | return v; |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 79 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 80 | #else |
| 81 | #define CHECK_SMALL_INT(ival) |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 82 | #define maybe_small_long(val) (val) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 83 | #endif |
| 84 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 85 | /* If a freshly-allocated int is already shared, it must |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 86 | be a small integer, so negating it must go to PyLong_FromLong */ |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 87 | Py_LOCAL_INLINE(void) |
| 88 | _PyLong_Negate(PyLongObject **x_p) |
| 89 | { |
| 90 | PyLongObject *x; |
| 91 | |
| 92 | x = (PyLongObject *)*x_p; |
| 93 | if (Py_REFCNT(x) == 1) { |
| 94 | Py_SIZE(x) = -Py_SIZE(x); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | *x_p = (PyLongObject *)PyLong_FromLong(-MEDIUM_VALUE(x)); |
| 99 | Py_DECREF(x); |
| 100 | } |
| 101 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 102 | /* For int multiplication, use the O(N**2) school algorithm unless |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 103 | * both operands contain more than KARATSUBA_CUTOFF digits (this |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 104 | * being an internal Python int digit, in base BASE). |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 105 | */ |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 106 | #define KARATSUBA_CUTOFF 70 |
| 107 | #define KARATSUBA_SQUARE_CUTOFF (2 * KARATSUBA_CUTOFF) |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 108 | |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 109 | /* For exponentiation, use the binary left-to-right algorithm |
| 110 | * unless the exponent contains more than FIVEARY_CUTOFF digits. |
| 111 | * In that case, do 5 bits at a time. The potential drawback is that |
| 112 | * a table of 2**5 intermediate results is computed. |
| 113 | */ |
| 114 | #define FIVEARY_CUTOFF 8 |
| 115 | |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 116 | #define SIGCHECK(PyTryBlock) \ |
| 117 | do { \ |
| 118 | if (PyErr_CheckSignals()) PyTryBlock \ |
| 119 | } while(0) |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 120 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 121 | /* Normalize (remove leading zeros from) an int object. |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 122 | Doesn't attempt to free the storage--in most cases, due to the nature |
| 123 | of the algorithms used, this could save at most be one word anyway. */ |
| 124 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 125 | static PyLongObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 126 | long_normalize(PyLongObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 127 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 128 | Py_ssize_t j = Py_ABS(Py_SIZE(v)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 129 | Py_ssize_t i = j; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 130 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 131 | while (i > 0 && v->ob_digit[i-1] == 0) |
| 132 | --i; |
| 133 | if (i != j) |
| 134 | Py_SIZE(v) = (Py_SIZE(v) < 0) ? -(i) : i; |
| 135 | return v; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 138 | /* _PyLong_FromNbInt: Convert the given object to a PyLongObject |
| 139 | using the nb_int slot, if available. Raise TypeError if either the |
| 140 | nb_int slot is not available or the result of the call to nb_int |
| 141 | returns something not of type int. |
| 142 | */ |
| 143 | PyLongObject * |
| 144 | _PyLong_FromNbInt(PyObject *integral) |
| 145 | { |
| 146 | PyNumberMethods *nb; |
| 147 | PyObject *result; |
| 148 | |
| 149 | /* Fast path for the case that we already have an int. */ |
| 150 | if (PyLong_CheckExact(integral)) { |
| 151 | Py_INCREF(integral); |
| 152 | return (PyLongObject *)integral; |
| 153 | } |
| 154 | |
| 155 | nb = Py_TYPE(integral)->tp_as_number; |
| 156 | if (nb == NULL || nb->nb_int == NULL) { |
| 157 | PyErr_Format(PyExc_TypeError, |
| 158 | "an integer is required (got type %.200s)", |
| 159 | Py_TYPE(integral)->tp_name); |
| 160 | return NULL; |
| 161 | } |
| 162 | |
| 163 | /* Convert using the nb_int slot, which should return something |
| 164 | of exact type int. */ |
| 165 | result = nb->nb_int(integral); |
| 166 | if (!result || PyLong_CheckExact(result)) |
| 167 | return (PyLongObject *)result; |
| 168 | if (!PyLong_Check(result)) { |
| 169 | PyErr_Format(PyExc_TypeError, |
| 170 | "__int__ returned non-int (type %.200s)", |
| 171 | result->ob_type->tp_name); |
| 172 | Py_DECREF(result); |
| 173 | return NULL; |
| 174 | } |
| 175 | /* Issue #17576: warn if 'result' not of exact type int. */ |
| 176 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 177 | "__int__ returned non-int (type %.200s). " |
| 178 | "The ability to return an instance of a strict subclass of int " |
| 179 | "is deprecated, and may be removed in a future version of Python.", |
| 180 | result->ob_type->tp_name)) { |
| 181 | Py_DECREF(result); |
| 182 | return NULL; |
| 183 | } |
| 184 | return (PyLongObject *)result; |
| 185 | } |
| 186 | |
| 187 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 188 | /* Allocate a new int object with size digits. |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 189 | Return NULL and set exception if we run out of memory. */ |
| 190 | |
Mark Dickinson | 5a74bf6 | 2009-02-15 11:04:38 +0000 | [diff] [blame] | 191 | #define MAX_LONG_DIGITS \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 192 | ((PY_SSIZE_T_MAX - offsetof(PyLongObject, ob_digit))/sizeof(digit)) |
Mark Dickinson | 5a74bf6 | 2009-02-15 11:04:38 +0000 | [diff] [blame] | 193 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 194 | PyLongObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 195 | _PyLong_New(Py_ssize_t size) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 196 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 197 | PyLongObject *result; |
| 198 | /* Number of bytes needed is: offsetof(PyLongObject, ob_digit) + |
| 199 | sizeof(digit)*size. Previous incarnations of this code used |
| 200 | sizeof(PyVarObject) instead of the offsetof, but this risks being |
| 201 | incorrect in the presence of padding between the PyVarObject header |
| 202 | and the digits. */ |
| 203 | if (size > (Py_ssize_t)MAX_LONG_DIGITS) { |
| 204 | PyErr_SetString(PyExc_OverflowError, |
| 205 | "too many digits in integer"); |
| 206 | return NULL; |
| 207 | } |
| 208 | result = PyObject_MALLOC(offsetof(PyLongObject, ob_digit) + |
| 209 | size*sizeof(digit)); |
| 210 | if (!result) { |
| 211 | PyErr_NoMemory(); |
| 212 | return NULL; |
| 213 | } |
| 214 | return (PyLongObject*)PyObject_INIT_VAR(result, &PyLong_Type, size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 217 | PyObject * |
| 218 | _PyLong_Copy(PyLongObject *src) |
| 219 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 220 | PyLongObject *result; |
| 221 | Py_ssize_t i; |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 222 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 223 | assert(src != NULL); |
| 224 | i = Py_SIZE(src); |
| 225 | if (i < 0) |
| 226 | i = -(i); |
| 227 | if (i < 2) { |
Mark Dickinson | bcc17ee | 2012-04-20 21:42:49 +0100 | [diff] [blame] | 228 | sdigit ival = MEDIUM_VALUE(src); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 229 | CHECK_SMALL_INT(ival); |
| 230 | } |
| 231 | result = _PyLong_New(i); |
| 232 | if (result != NULL) { |
| 233 | Py_SIZE(result) = Py_SIZE(src); |
| 234 | while (--i >= 0) |
| 235 | result->ob_digit[i] = src->ob_digit[i]; |
| 236 | } |
| 237 | return (PyObject *)result; |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 240 | /* Create a new int object from a C long int */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 241 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 242 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 243 | PyLong_FromLong(long ival) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 244 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 245 | PyLongObject *v; |
| 246 | unsigned long abs_ival; |
| 247 | unsigned long t; /* unsigned so >> doesn't propagate sign bit */ |
| 248 | int ndigits = 0; |
Mark Dickinson | 36820dd | 2016-09-10 20:17:36 +0100 | [diff] [blame] | 249 | int sign; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 250 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 251 | CHECK_SMALL_INT(ival); |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 252 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 253 | if (ival < 0) { |
| 254 | /* negate: can't write this as abs_ival = -ival since that |
| 255 | invokes undefined behaviour when ival is LONG_MIN */ |
| 256 | abs_ival = 0U-(unsigned long)ival; |
| 257 | sign = -1; |
| 258 | } |
| 259 | else { |
| 260 | abs_ival = (unsigned long)ival; |
Mark Dickinson | 36820dd | 2016-09-10 20:17:36 +0100 | [diff] [blame] | 261 | sign = ival == 0 ? 0 : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 262 | } |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 263 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 264 | /* Fast path for single-digit ints */ |
| 265 | if (!(abs_ival >> PyLong_SHIFT)) { |
| 266 | v = _PyLong_New(1); |
| 267 | if (v) { |
| 268 | Py_SIZE(v) = sign; |
| 269 | v->ob_digit[0] = Py_SAFE_DOWNCAST( |
| 270 | abs_ival, unsigned long, digit); |
| 271 | } |
| 272 | return (PyObject*)v; |
| 273 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 274 | |
Mark Dickinson | 249b898 | 2009-04-27 19:41:00 +0000 | [diff] [blame] | 275 | #if PyLong_SHIFT==15 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 276 | /* 2 digits */ |
| 277 | if (!(abs_ival >> 2*PyLong_SHIFT)) { |
| 278 | v = _PyLong_New(2); |
| 279 | if (v) { |
| 280 | Py_SIZE(v) = 2*sign; |
| 281 | v->ob_digit[0] = Py_SAFE_DOWNCAST( |
| 282 | abs_ival & PyLong_MASK, unsigned long, digit); |
| 283 | v->ob_digit[1] = Py_SAFE_DOWNCAST( |
| 284 | abs_ival >> PyLong_SHIFT, unsigned long, digit); |
| 285 | } |
| 286 | return (PyObject*)v; |
| 287 | } |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 288 | #endif |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 290 | /* Larger numbers: loop to determine number of digits */ |
| 291 | t = abs_ival; |
| 292 | while (t) { |
| 293 | ++ndigits; |
| 294 | t >>= PyLong_SHIFT; |
| 295 | } |
| 296 | v = _PyLong_New(ndigits); |
| 297 | if (v != NULL) { |
| 298 | digit *p = v->ob_digit; |
| 299 | Py_SIZE(v) = ndigits*sign; |
| 300 | t = abs_ival; |
| 301 | while (t) { |
| 302 | *p++ = Py_SAFE_DOWNCAST( |
| 303 | t & PyLong_MASK, unsigned long, digit); |
| 304 | t >>= PyLong_SHIFT; |
| 305 | } |
| 306 | } |
| 307 | return (PyObject *)v; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 310 | /* Create a new int object from a C unsigned long int */ |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 311 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 312 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 313 | PyLong_FromUnsignedLong(unsigned long ival) |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 314 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 315 | PyLongObject *v; |
| 316 | unsigned long t; |
| 317 | int ndigits = 0; |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 318 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 319 | if (ival < PyLong_BASE) |
| 320 | return PyLong_FromLong(ival); |
| 321 | /* Count the number of Python digits. */ |
orenmn | 86aa269 | 2017-03-06 10:42:47 +0200 | [diff] [blame] | 322 | t = ival; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 323 | while (t) { |
| 324 | ++ndigits; |
| 325 | t >>= PyLong_SHIFT; |
| 326 | } |
| 327 | v = _PyLong_New(ndigits); |
| 328 | if (v != NULL) { |
| 329 | digit *p = v->ob_digit; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 330 | while (ival) { |
| 331 | *p++ = (digit)(ival & PyLong_MASK); |
| 332 | ival >>= PyLong_SHIFT; |
| 333 | } |
| 334 | } |
| 335 | return (PyObject *)v; |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 338 | /* Create a new int object from a C double */ |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 339 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 340 | PyObject * |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 341 | PyLong_FromDouble(double dval) |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 342 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 343 | PyLongObject *v; |
| 344 | double frac; |
| 345 | int i, ndig, expo, neg; |
| 346 | neg = 0; |
| 347 | if (Py_IS_INFINITY(dval)) { |
| 348 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 349 | "cannot convert float infinity to integer"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 350 | return NULL; |
| 351 | } |
| 352 | if (Py_IS_NAN(dval)) { |
| 353 | PyErr_SetString(PyExc_ValueError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 354 | "cannot convert float NaN to integer"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 355 | return NULL; |
| 356 | } |
| 357 | if (dval < 0.0) { |
| 358 | neg = 1; |
| 359 | dval = -dval; |
| 360 | } |
| 361 | frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */ |
| 362 | if (expo <= 0) |
| 363 | return PyLong_FromLong(0L); |
| 364 | ndig = (expo-1) / PyLong_SHIFT + 1; /* Number of 'digits' in result */ |
| 365 | v = _PyLong_New(ndig); |
| 366 | if (v == NULL) |
| 367 | return NULL; |
| 368 | frac = ldexp(frac, (expo-1) % PyLong_SHIFT + 1); |
| 369 | for (i = ndig; --i >= 0; ) { |
| 370 | digit bits = (digit)frac; |
| 371 | v->ob_digit[i] = bits; |
| 372 | frac = frac - (double)bits; |
| 373 | frac = ldexp(frac, PyLong_SHIFT); |
| 374 | } |
| 375 | if (neg) |
| 376 | Py_SIZE(v) = -(Py_SIZE(v)); |
| 377 | return (PyObject *)v; |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 380 | /* Checking for overflow in PyLong_AsLong is a PITA since C doesn't define |
| 381 | * anything about what happens when a signed integer operation overflows, |
| 382 | * and some compilers think they're doing you a favor by being "clever" |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 383 | * then. The bit pattern for the largest positive signed long is |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 384 | * (unsigned long)LONG_MAX, and for the smallest negative signed long |
| 385 | * it is abs(LONG_MIN), which we could write -(unsigned long)LONG_MIN. |
| 386 | * However, some other compilers warn about applying unary minus to an |
| 387 | * unsigned operand. Hence the weird "0-". |
| 388 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 389 | #define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN) |
| 390 | #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] | 391 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 392 | /* 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] | 393 | method. |
| 394 | |
| 395 | On overflow, return -1 and set *overflow to 1 or -1 depending on the sign of |
| 396 | the result. Otherwise *overflow is 0. |
| 397 | |
| 398 | For other errors (e.g., TypeError), return -1 and set an error condition. |
| 399 | In this case *overflow will be 0. |
| 400 | */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 401 | |
| 402 | long |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 403 | PyLong_AsLongAndOverflow(PyObject *vv, int *overflow) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 404 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 405 | /* This version by Tim Peters */ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 406 | PyLongObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 407 | unsigned long x, prev; |
| 408 | long res; |
| 409 | Py_ssize_t i; |
| 410 | int sign; |
| 411 | int do_decref = 0; /* if nb_int was called */ |
Guido van Rossum | f753181 | 1998-05-26 14:33:37 +0000 | [diff] [blame] | 412 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 413 | *overflow = 0; |
| 414 | if (vv == NULL) { |
| 415 | PyErr_BadInternalCall(); |
| 416 | return -1; |
| 417 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 418 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 419 | if (PyLong_Check(vv)) { |
| 420 | v = (PyLongObject *)vv; |
| 421 | } |
| 422 | else { |
| 423 | v = _PyLong_FromNbInt(vv); |
| 424 | if (v == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 425 | return -1; |
| 426 | do_decref = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 427 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 428 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 429 | res = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 430 | i = Py_SIZE(v); |
Guido van Rossum | f753181 | 1998-05-26 14:33:37 +0000 | [diff] [blame] | 431 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 432 | switch (i) { |
| 433 | case -1: |
| 434 | res = -(sdigit)v->ob_digit[0]; |
| 435 | break; |
| 436 | case 0: |
| 437 | res = 0; |
| 438 | break; |
| 439 | case 1: |
| 440 | res = v->ob_digit[0]; |
| 441 | break; |
| 442 | default: |
| 443 | sign = 1; |
| 444 | x = 0; |
| 445 | if (i < 0) { |
| 446 | sign = -1; |
| 447 | i = -(i); |
| 448 | } |
| 449 | while (--i >= 0) { |
| 450 | prev = x; |
| 451 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 452 | if ((x >> PyLong_SHIFT) != prev) { |
| 453 | *overflow = sign; |
| 454 | goto exit; |
| 455 | } |
| 456 | } |
| 457 | /* Haven't lost any bits, but casting to long requires extra |
| 458 | * care (see comment above). |
| 459 | */ |
| 460 | if (x <= (unsigned long)LONG_MAX) { |
| 461 | res = (long)x * sign; |
| 462 | } |
| 463 | else if (sign < 0 && x == PY_ABS_LONG_MIN) { |
| 464 | res = LONG_MIN; |
| 465 | } |
| 466 | else { |
| 467 | *overflow = sign; |
| 468 | /* res is already set to -1 */ |
| 469 | } |
| 470 | } |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 471 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 472 | if (do_decref) { |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 473 | Py_DECREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 474 | } |
| 475 | return res; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 478 | /* 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] | 479 | method. Return -1 and set an error if overflow occurs. */ |
| 480 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 481 | long |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 482 | PyLong_AsLong(PyObject *obj) |
| 483 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 484 | int overflow; |
| 485 | long result = PyLong_AsLongAndOverflow(obj, &overflow); |
| 486 | if (overflow) { |
| 487 | /* XXX: could be cute and give a different |
| 488 | message for overflow == -1 */ |
| 489 | PyErr_SetString(PyExc_OverflowError, |
| 490 | "Python int too large to convert to C long"); |
| 491 | } |
| 492 | return result; |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 495 | /* 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] | 496 | method. Return -1 and set an error if overflow occurs. */ |
| 497 | |
| 498 | int |
| 499 | _PyLong_AsInt(PyObject *obj) |
| 500 | { |
| 501 | int overflow; |
| 502 | long result = PyLong_AsLongAndOverflow(obj, &overflow); |
| 503 | if (overflow || result > INT_MAX || result < INT_MIN) { |
| 504 | /* XXX: could be cute and give a different |
| 505 | message for overflow == -1 */ |
| 506 | PyErr_SetString(PyExc_OverflowError, |
| 507 | "Python int too large to convert to C int"); |
| 508 | return -1; |
| 509 | } |
| 510 | return (int)result; |
| 511 | } |
| 512 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 513 | /* Get a Py_ssize_t from an int object. |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 514 | Returns -1 and sets an error condition if overflow occurs. */ |
| 515 | |
| 516 | Py_ssize_t |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 517 | PyLong_AsSsize_t(PyObject *vv) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 518 | PyLongObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 519 | size_t x, prev; |
| 520 | Py_ssize_t i; |
| 521 | int sign; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 522 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 523 | if (vv == NULL) { |
| 524 | PyErr_BadInternalCall(); |
| 525 | return -1; |
| 526 | } |
| 527 | if (!PyLong_Check(vv)) { |
| 528 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 529 | return -1; |
| 530 | } |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 531 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 532 | v = (PyLongObject *)vv; |
| 533 | i = Py_SIZE(v); |
| 534 | switch (i) { |
| 535 | case -1: return -(sdigit)v->ob_digit[0]; |
| 536 | case 0: return 0; |
| 537 | case 1: return v->ob_digit[0]; |
| 538 | } |
| 539 | sign = 1; |
| 540 | x = 0; |
| 541 | if (i < 0) { |
| 542 | sign = -1; |
| 543 | i = -(i); |
| 544 | } |
| 545 | while (--i >= 0) { |
| 546 | prev = x; |
| 547 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 548 | if ((x >> PyLong_SHIFT) != prev) |
| 549 | goto overflow; |
| 550 | } |
| 551 | /* Haven't lost any bits, but casting to a signed type requires |
| 552 | * extra care (see comment above). |
| 553 | */ |
| 554 | if (x <= (size_t)PY_SSIZE_T_MAX) { |
| 555 | return (Py_ssize_t)x * sign; |
| 556 | } |
| 557 | else if (sign < 0 && x == PY_ABS_SSIZE_T_MIN) { |
| 558 | return PY_SSIZE_T_MIN; |
| 559 | } |
| 560 | /* else overflow */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 561 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 562 | overflow: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 563 | PyErr_SetString(PyExc_OverflowError, |
| 564 | "Python int too large to convert to C ssize_t"); |
| 565 | return -1; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 568 | /* Get a C unsigned long int from an int object. |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 569 | Returns -1 and sets an error condition if overflow occurs. */ |
| 570 | |
| 571 | unsigned long |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 572 | PyLong_AsUnsignedLong(PyObject *vv) |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 573 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 574 | PyLongObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 575 | unsigned long x, prev; |
| 576 | Py_ssize_t i; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 577 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 578 | if (vv == NULL) { |
| 579 | PyErr_BadInternalCall(); |
| 580 | return (unsigned long)-1; |
| 581 | } |
| 582 | if (!PyLong_Check(vv)) { |
| 583 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 584 | return (unsigned long)-1; |
| 585 | } |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 586 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 587 | v = (PyLongObject *)vv; |
| 588 | i = Py_SIZE(v); |
| 589 | x = 0; |
| 590 | if (i < 0) { |
| 591 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 592 | "can't convert negative value to unsigned int"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 593 | return (unsigned long) -1; |
| 594 | } |
| 595 | switch (i) { |
| 596 | case 0: return 0; |
| 597 | case 1: return v->ob_digit[0]; |
| 598 | } |
| 599 | while (--i >= 0) { |
| 600 | prev = x; |
| 601 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 602 | if ((x >> PyLong_SHIFT) != prev) { |
| 603 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 02515f7 | 2013-08-03 12:08:22 +0100 | [diff] [blame] | 604 | "Python int too large to convert " |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 605 | "to C unsigned long"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 606 | return (unsigned long) -1; |
| 607 | } |
| 608 | } |
| 609 | return x; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 612 | /* 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] | 613 | an error condition if overflow occurs. */ |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 614 | |
| 615 | size_t |
| 616 | PyLong_AsSize_t(PyObject *vv) |
| 617 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 618 | PyLongObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 619 | size_t x, prev; |
| 620 | Py_ssize_t i; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 621 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 622 | if (vv == NULL) { |
| 623 | PyErr_BadInternalCall(); |
| 624 | return (size_t) -1; |
| 625 | } |
| 626 | if (!PyLong_Check(vv)) { |
| 627 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 628 | return (size_t)-1; |
| 629 | } |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 630 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 631 | v = (PyLongObject *)vv; |
| 632 | i = Py_SIZE(v); |
| 633 | x = 0; |
| 634 | if (i < 0) { |
| 635 | PyErr_SetString(PyExc_OverflowError, |
| 636 | "can't convert negative value to size_t"); |
| 637 | return (size_t) -1; |
| 638 | } |
| 639 | switch (i) { |
| 640 | case 0: return 0; |
| 641 | case 1: return v->ob_digit[0]; |
| 642 | } |
| 643 | while (--i >= 0) { |
| 644 | prev = x; |
| 645 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 646 | if ((x >> PyLong_SHIFT) != prev) { |
| 647 | PyErr_SetString(PyExc_OverflowError, |
| 648 | "Python int too large to convert to C size_t"); |
Stefan Krah | b77c6c6 | 2011-09-12 16:22:47 +0200 | [diff] [blame] | 649 | return (size_t) -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | return x; |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 655 | /* 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] | 656 | Returns -1 and sets an error condition if an error occurs. */ |
| 657 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 658 | static unsigned long |
| 659 | _PyLong_AsUnsignedLongMask(PyObject *vv) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 660 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 661 | PyLongObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 662 | unsigned long x; |
| 663 | Py_ssize_t i; |
| 664 | int sign; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 665 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 666 | if (vv == NULL || !PyLong_Check(vv)) { |
| 667 | PyErr_BadInternalCall(); |
| 668 | return (unsigned long) -1; |
| 669 | } |
| 670 | v = (PyLongObject *)vv; |
| 671 | i = Py_SIZE(v); |
| 672 | switch (i) { |
| 673 | case 0: return 0; |
| 674 | case 1: return v->ob_digit[0]; |
| 675 | } |
| 676 | sign = 1; |
| 677 | x = 0; |
| 678 | if (i < 0) { |
| 679 | sign = -1; |
| 680 | i = -i; |
| 681 | } |
| 682 | while (--i >= 0) { |
| 683 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 684 | } |
| 685 | return x * sign; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 688 | unsigned long |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 689 | PyLong_AsUnsignedLongMask(PyObject *op) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 690 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 691 | PyLongObject *lo; |
| 692 | unsigned long val; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 693 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 694 | if (op == NULL) { |
| 695 | PyErr_BadInternalCall(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 696 | return (unsigned long)-1; |
| 697 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 698 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 699 | if (PyLong_Check(op)) { |
| 700 | return _PyLong_AsUnsignedLongMask(op); |
| 701 | } |
| 702 | |
| 703 | lo = _PyLong_FromNbInt(op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 704 | if (lo == NULL) |
| 705 | return (unsigned long)-1; |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 706 | |
| 707 | val = _PyLong_AsUnsignedLongMask((PyObject *)lo); |
| 708 | Py_DECREF(lo); |
| 709 | return val; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 710 | } |
| 711 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 712 | int |
| 713 | _PyLong_Sign(PyObject *vv) |
| 714 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 715 | PyLongObject *v = (PyLongObject *)vv; |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 716 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 717 | assert(v != NULL); |
| 718 | assert(PyLong_Check(v)); |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 719 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 720 | return Py_SIZE(v) == 0 ? 0 : (Py_SIZE(v) < 0 ? -1 : 1); |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Serhiy Storchaka | b2e64f9 | 2016-11-08 20:34:22 +0200 | [diff] [blame] | 723 | /* bits_in_digit(d) returns the unique integer k such that 2**(k-1) <= d < |
| 724 | 2**k if d is nonzero, else 0. */ |
| 725 | |
| 726 | static const unsigned char BitLengthTable[32] = { |
| 727 | 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, |
| 728 | 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 |
| 729 | }; |
| 730 | |
| 731 | static int |
| 732 | bits_in_digit(digit d) |
| 733 | { |
| 734 | int d_bits = 0; |
| 735 | while (d >= 32) { |
| 736 | d_bits += 6; |
| 737 | d >>= 6; |
| 738 | } |
| 739 | d_bits += (int)BitLengthTable[d]; |
| 740 | return d_bits; |
| 741 | } |
| 742 | |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 743 | size_t |
| 744 | _PyLong_NumBits(PyObject *vv) |
| 745 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 746 | PyLongObject *v = (PyLongObject *)vv; |
| 747 | size_t result = 0; |
| 748 | Py_ssize_t ndigits; |
Serhiy Storchaka | b2e64f9 | 2016-11-08 20:34:22 +0200 | [diff] [blame] | 749 | int msd_bits; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 750 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 751 | assert(v != NULL); |
| 752 | assert(PyLong_Check(v)); |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 753 | ndigits = Py_ABS(Py_SIZE(v)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 754 | assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0); |
| 755 | if (ndigits > 0) { |
| 756 | digit msd = v->ob_digit[ndigits - 1]; |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 757 | if ((size_t)(ndigits - 1) > SIZE_MAX / (size_t)PyLong_SHIFT) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 758 | goto Overflow; |
Mark Dickinson | fc9adb6 | 2012-10-06 18:50:02 +0100 | [diff] [blame] | 759 | result = (size_t)(ndigits - 1) * (size_t)PyLong_SHIFT; |
Serhiy Storchaka | b2e64f9 | 2016-11-08 20:34:22 +0200 | [diff] [blame] | 760 | msd_bits = bits_in_digit(msd); |
| 761 | if (SIZE_MAX - msd_bits < result) |
| 762 | goto Overflow; |
| 763 | result += msd_bits; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 764 | } |
| 765 | return result; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 766 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 767 | Overflow: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 768 | PyErr_SetString(PyExc_OverflowError, "int has too many bits " |
| 769 | "to express in a platform size_t"); |
| 770 | return (size_t)-1; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 773 | PyObject * |
| 774 | _PyLong_FromByteArray(const unsigned char* bytes, size_t n, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 775 | int little_endian, int is_signed) |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 776 | { |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 777 | const unsigned char* pstartbyte; /* LSB of bytes */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 778 | int incr; /* direction to move pstartbyte */ |
| 779 | const unsigned char* pendbyte; /* MSB of bytes */ |
| 780 | size_t numsignificantbytes; /* number of bytes that matter */ |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 781 | Py_ssize_t ndigits; /* number of Python int digits */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 782 | PyLongObject* v; /* result */ |
| 783 | Py_ssize_t idigit = 0; /* next free index in v->ob_digit */ |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 784 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 785 | if (n == 0) |
| 786 | return PyLong_FromLong(0L); |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 787 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 788 | if (little_endian) { |
| 789 | pstartbyte = bytes; |
| 790 | pendbyte = bytes + n - 1; |
| 791 | incr = 1; |
| 792 | } |
| 793 | else { |
| 794 | pstartbyte = bytes + n - 1; |
| 795 | pendbyte = bytes; |
| 796 | incr = -1; |
| 797 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 798 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 799 | if (is_signed) |
| 800 | is_signed = *pendbyte >= 0x80; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 801 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 802 | /* Compute numsignificantbytes. This consists of finding the most |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 803 | significant byte. Leading 0 bytes are insignificant if the number |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 804 | is positive, and leading 0xff bytes if negative. */ |
| 805 | { |
| 806 | size_t i; |
| 807 | const unsigned char* p = pendbyte; |
| 808 | const int pincr = -incr; /* search MSB to LSB */ |
Martin Panter | eb99570 | 2016-07-28 01:11:04 +0000 | [diff] [blame] | 809 | const unsigned char insignificant = is_signed ? 0xff : 0x00; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 810 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 811 | for (i = 0; i < n; ++i, p += pincr) { |
Martin Panter | eb99570 | 2016-07-28 01:11:04 +0000 | [diff] [blame] | 812 | if (*p != insignificant) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 813 | break; |
| 814 | } |
| 815 | numsignificantbytes = n - i; |
| 816 | /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so |
| 817 | actually has 2 significant bytes. OTOH, 0xff0001 == |
| 818 | -0x00ffff, so we wouldn't *need* to bump it there; but we |
| 819 | do for 0xffff = -0x0001. To be safe without bothering to |
| 820 | check every case, bump it regardless. */ |
| 821 | if (is_signed && numsignificantbytes < n) |
| 822 | ++numsignificantbytes; |
| 823 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 824 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 825 | /* How many Python int digits do we need? We have |
| 826 | 8*numsignificantbytes bits, and each Python int digit has |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 827 | PyLong_SHIFT bits, so it's the ceiling of the quotient. */ |
| 828 | /* catch overflow before it happens */ |
| 829 | if (numsignificantbytes > (PY_SSIZE_T_MAX - PyLong_SHIFT) / 8) { |
| 830 | PyErr_SetString(PyExc_OverflowError, |
| 831 | "byte array too long to convert to int"); |
| 832 | return NULL; |
| 833 | } |
| 834 | ndigits = (numsignificantbytes * 8 + PyLong_SHIFT - 1) / PyLong_SHIFT; |
| 835 | v = _PyLong_New(ndigits); |
| 836 | if (v == NULL) |
| 837 | return NULL; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 838 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 839 | /* Copy the bits over. The tricky parts are computing 2's-comp on |
| 840 | the fly for signed numbers, and dealing with the mismatch between |
| 841 | 8-bit bytes and (probably) 15-bit Python digits.*/ |
| 842 | { |
| 843 | size_t i; |
| 844 | twodigits carry = 1; /* for 2's-comp calculation */ |
| 845 | twodigits accum = 0; /* sliding register */ |
| 846 | unsigned int accumbits = 0; /* number of bits in accum */ |
| 847 | const unsigned char* p = pstartbyte; |
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 | for (i = 0; i < numsignificantbytes; ++i, p += incr) { |
| 850 | twodigits thisbyte = *p; |
| 851 | /* Compute correction for 2's comp, if needed. */ |
| 852 | if (is_signed) { |
| 853 | thisbyte = (0xff ^ thisbyte) + carry; |
| 854 | carry = thisbyte >> 8; |
| 855 | thisbyte &= 0xff; |
| 856 | } |
| 857 | /* Because we're going LSB to MSB, thisbyte is |
| 858 | more significant than what's already in accum, |
| 859 | so needs to be prepended to accum. */ |
orenmn | 86aa269 | 2017-03-06 10:42:47 +0200 | [diff] [blame] | 860 | accum |= thisbyte << accumbits; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 861 | accumbits += 8; |
| 862 | if (accumbits >= PyLong_SHIFT) { |
| 863 | /* There's enough to fill a Python digit. */ |
| 864 | assert(idigit < ndigits); |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 865 | v->ob_digit[idigit] = (digit)(accum & PyLong_MASK); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 866 | ++idigit; |
| 867 | accum >>= PyLong_SHIFT; |
| 868 | accumbits -= PyLong_SHIFT; |
| 869 | assert(accumbits < PyLong_SHIFT); |
| 870 | } |
| 871 | } |
| 872 | assert(accumbits < PyLong_SHIFT); |
| 873 | if (accumbits) { |
| 874 | assert(idigit < ndigits); |
| 875 | v->ob_digit[idigit] = (digit)accum; |
| 876 | ++idigit; |
| 877 | } |
| 878 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 879 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 880 | Py_SIZE(v) = is_signed ? -idigit : idigit; |
| 881 | return (PyObject *)long_normalize(v); |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | int |
| 885 | _PyLong_AsByteArray(PyLongObject* v, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 886 | unsigned char* bytes, size_t n, |
| 887 | int little_endian, int is_signed) |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 888 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 889 | Py_ssize_t i; /* index into v->ob_digit */ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 890 | Py_ssize_t ndigits; /* |v->ob_size| */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 891 | twodigits accum; /* sliding register */ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 892 | unsigned int accumbits; /* # bits in accum */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 893 | int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */ |
| 894 | digit carry; /* for computing 2's-comp */ |
| 895 | size_t j; /* # bytes filled */ |
| 896 | unsigned char* p; /* pointer to next byte in bytes */ |
| 897 | int pincr; /* direction to move p */ |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 898 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 899 | assert(v != NULL && PyLong_Check(v)); |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 900 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 901 | if (Py_SIZE(v) < 0) { |
| 902 | ndigits = -(Py_SIZE(v)); |
| 903 | if (!is_signed) { |
| 904 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 905 | "can't convert negative int to unsigned"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 906 | return -1; |
| 907 | } |
| 908 | do_twos_comp = 1; |
| 909 | } |
| 910 | else { |
| 911 | ndigits = Py_SIZE(v); |
| 912 | do_twos_comp = 0; |
| 913 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 914 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 915 | if (little_endian) { |
| 916 | p = bytes; |
| 917 | pincr = 1; |
| 918 | } |
| 919 | else { |
| 920 | p = bytes + n - 1; |
| 921 | pincr = -1; |
| 922 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 923 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 924 | /* Copy over all the Python digits. |
| 925 | It's crucial that every Python digit except for the MSD contribute |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 926 | 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] | 927 | normalized. */ |
| 928 | assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0); |
| 929 | j = 0; |
| 930 | accum = 0; |
| 931 | accumbits = 0; |
| 932 | carry = do_twos_comp ? 1 : 0; |
| 933 | for (i = 0; i < ndigits; ++i) { |
| 934 | digit thisdigit = v->ob_digit[i]; |
| 935 | if (do_twos_comp) { |
| 936 | thisdigit = (thisdigit ^ PyLong_MASK) + carry; |
| 937 | carry = thisdigit >> PyLong_SHIFT; |
| 938 | thisdigit &= PyLong_MASK; |
| 939 | } |
| 940 | /* Because we're going LSB to MSB, thisdigit is more |
| 941 | significant than what's already in accum, so needs to be |
| 942 | prepended to accum. */ |
| 943 | accum |= (twodigits)thisdigit << accumbits; |
Tim Peters | 8bc84b4 | 2001-06-12 19:17:03 +0000 | [diff] [blame] | 944 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 945 | /* The most-significant digit may be (probably is) at least |
| 946 | partly empty. */ |
| 947 | if (i == ndigits - 1) { |
| 948 | /* Count # of sign bits -- they needn't be stored, |
| 949 | * although for signed conversion we need later to |
| 950 | * make sure at least one sign bit gets stored. */ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 951 | digit s = do_twos_comp ? thisdigit ^ PyLong_MASK : thisdigit; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 952 | while (s != 0) { |
| 953 | s >>= 1; |
| 954 | accumbits++; |
| 955 | } |
| 956 | } |
| 957 | else |
| 958 | accumbits += PyLong_SHIFT; |
Tim Peters | 8bc84b4 | 2001-06-12 19:17:03 +0000 | [diff] [blame] | 959 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 960 | /* Store as many bytes as possible. */ |
| 961 | while (accumbits >= 8) { |
| 962 | if (j >= n) |
| 963 | goto Overflow; |
| 964 | ++j; |
| 965 | *p = (unsigned char)(accum & 0xff); |
| 966 | p += pincr; |
| 967 | accumbits -= 8; |
| 968 | accum >>= 8; |
| 969 | } |
| 970 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 971 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 972 | /* Store the straggler (if any). */ |
| 973 | assert(accumbits < 8); |
| 974 | assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */ |
| 975 | if (accumbits > 0) { |
| 976 | if (j >= n) |
| 977 | goto Overflow; |
| 978 | ++j; |
| 979 | if (do_twos_comp) { |
| 980 | /* Fill leading bits of the byte with sign bits |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 981 | (appropriately pretending that the int had an |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 982 | infinite supply of sign bits). */ |
| 983 | accum |= (~(twodigits)0) << accumbits; |
| 984 | } |
| 985 | *p = (unsigned char)(accum & 0xff); |
| 986 | p += pincr; |
| 987 | } |
| 988 | else if (j == n && n > 0 && is_signed) { |
| 989 | /* The main loop filled the byte array exactly, so the code |
| 990 | just above didn't get to ensure there's a sign bit, and the |
| 991 | loop below wouldn't add one either. Make sure a sign bit |
| 992 | exists. */ |
| 993 | unsigned char msb = *(p - pincr); |
| 994 | int sign_bit_set = msb >= 0x80; |
| 995 | assert(accumbits == 0); |
| 996 | if (sign_bit_set == do_twos_comp) |
| 997 | return 0; |
| 998 | else |
| 999 | goto Overflow; |
| 1000 | } |
Tim Peters | 05607ad | 2001-06-13 21:01:27 +0000 | [diff] [blame] | 1001 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1002 | /* Fill remaining bytes with copies of the sign bit. */ |
| 1003 | { |
| 1004 | unsigned char signbyte = do_twos_comp ? 0xffU : 0U; |
| 1005 | for ( ; j < n; ++j, p += pincr) |
| 1006 | *p = signbyte; |
| 1007 | } |
Tim Peters | 05607ad | 2001-06-13 21:01:27 +0000 | [diff] [blame] | 1008 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1009 | return 0; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 1010 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 1011 | Overflow: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1012 | PyErr_SetString(PyExc_OverflowError, "int too big to convert"); |
| 1013 | return -1; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1014 | |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1017 | /* Create a new int object from a C pointer */ |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1018 | |
| 1019 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1020 | PyLong_FromVoidPtr(void *p) |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1021 | { |
Mark Dickinson | 9104479 | 2012-10-18 19:21:43 +0100 | [diff] [blame] | 1022 | #if SIZEOF_VOID_P <= SIZEOF_LONG |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 1023 | return PyLong_FromUnsignedLong((unsigned long)(uintptr_t)p); |
Mark Dickinson | 9104479 | 2012-10-18 19:21:43 +0100 | [diff] [blame] | 1024 | #else |
| 1025 | |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1026 | #if SIZEOF_LONG_LONG < SIZEOF_VOID_P |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1027 | # error "PyLong_FromVoidPtr: sizeof(long long) < sizeof(void*)" |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1028 | #endif |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 1029 | return PyLong_FromUnsignedLongLong((unsigned long long)(uintptr_t)p); |
Mark Dickinson | 9104479 | 2012-10-18 19:21:43 +0100 | [diff] [blame] | 1030 | #endif /* SIZEOF_VOID_P <= SIZEOF_LONG */ |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1031 | |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1034 | /* Get a C pointer from an int object. */ |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1035 | |
| 1036 | void * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1037 | PyLong_AsVoidPtr(PyObject *vv) |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1038 | { |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1039 | #if SIZEOF_VOID_P <= SIZEOF_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1040 | long x; |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1041 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1042 | if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0) |
| 1043 | x = PyLong_AsLong(vv); |
| 1044 | else |
| 1045 | x = PyLong_AsUnsignedLong(vv); |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1046 | #else |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1047 | |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1048 | #if SIZEOF_LONG_LONG < SIZEOF_VOID_P |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1049 | # error "PyLong_AsVoidPtr: sizeof(long long) < sizeof(void*)" |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1050 | #endif |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1051 | long long x; |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1052 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1053 | if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0) |
| 1054 | x = PyLong_AsLongLong(vv); |
| 1055 | else |
| 1056 | x = PyLong_AsUnsignedLongLong(vv); |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1057 | |
| 1058 | #endif /* SIZEOF_VOID_P <= SIZEOF_LONG */ |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1059 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1060 | if (x == -1 && PyErr_Occurred()) |
| 1061 | return NULL; |
| 1062 | return (void *)x; |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1065 | /* Initial long long support by Chris Herborth (chrish@qnx.com), later |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1066 | * rewritten to use the newer PyLong_{As,From}ByteArray API. |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1067 | */ |
| 1068 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1069 | #define PY_ABS_LLONG_MIN (0-(unsigned long long)PY_LLONG_MIN) |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1070 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1071 | /* Create a new int object from a C long long int. */ |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1072 | |
| 1073 | PyObject * |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1074 | PyLong_FromLongLong(long long ival) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1075 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1076 | PyLongObject *v; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1077 | unsigned long long abs_ival; |
| 1078 | unsigned long long t; /* unsigned so >> doesn't propagate sign bit */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1079 | int ndigits = 0; |
| 1080 | int negative = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1081 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1082 | CHECK_SMALL_INT(ival); |
| 1083 | if (ival < 0) { |
| 1084 | /* avoid signed overflow on negation; see comments |
| 1085 | in PyLong_FromLong above. */ |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1086 | abs_ival = (unsigned long long)(-1-ival) + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1087 | negative = 1; |
| 1088 | } |
| 1089 | else { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1090 | abs_ival = (unsigned long long)ival; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1091 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1092 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1093 | /* Count the number of Python digits. |
| 1094 | We used to pick 5 ("big enough for anything"), but that's a |
| 1095 | waste of time and space given that 5*15 = 75 bits are rarely |
| 1096 | needed. */ |
| 1097 | t = abs_ival; |
| 1098 | while (t) { |
| 1099 | ++ndigits; |
| 1100 | t >>= PyLong_SHIFT; |
| 1101 | } |
| 1102 | v = _PyLong_New(ndigits); |
| 1103 | if (v != NULL) { |
| 1104 | digit *p = v->ob_digit; |
| 1105 | Py_SIZE(v) = negative ? -ndigits : ndigits; |
| 1106 | t = abs_ival; |
| 1107 | while (t) { |
| 1108 | *p++ = (digit)(t & PyLong_MASK); |
| 1109 | t >>= PyLong_SHIFT; |
| 1110 | } |
| 1111 | } |
| 1112 | return (PyObject *)v; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1115 | /* Create a new int object from a C unsigned long long int. */ |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1116 | |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1117 | PyObject * |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1118 | PyLong_FromUnsignedLongLong(unsigned long long ival) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1119 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1120 | PyLongObject *v; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1121 | unsigned long long t; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1122 | int ndigits = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1123 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1124 | if (ival < PyLong_BASE) |
| 1125 | return PyLong_FromLong((long)ival); |
| 1126 | /* Count the number of Python digits. */ |
orenmn | 86aa269 | 2017-03-06 10:42:47 +0200 | [diff] [blame] | 1127 | t = ival; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1128 | while (t) { |
| 1129 | ++ndigits; |
| 1130 | t >>= PyLong_SHIFT; |
| 1131 | } |
| 1132 | v = _PyLong_New(ndigits); |
| 1133 | if (v != NULL) { |
| 1134 | digit *p = v->ob_digit; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1135 | while (ival) { |
| 1136 | *p++ = (digit)(ival & PyLong_MASK); |
| 1137 | ival >>= PyLong_SHIFT; |
| 1138 | } |
| 1139 | } |
| 1140 | return (PyObject *)v; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1143 | /* 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] | 1144 | |
| 1145 | PyObject * |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1146 | PyLong_FromSsize_t(Py_ssize_t ival) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1147 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1148 | PyLongObject *v; |
| 1149 | size_t abs_ival; |
| 1150 | size_t t; /* unsigned so >> doesn't propagate sign bit */ |
| 1151 | int ndigits = 0; |
| 1152 | int negative = 0; |
Mark Dickinson | 7ab6be2 | 2008-04-15 21:42:42 +0000 | [diff] [blame] | 1153 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1154 | CHECK_SMALL_INT(ival); |
| 1155 | if (ival < 0) { |
| 1156 | /* avoid signed overflow when ival = SIZE_T_MIN */ |
| 1157 | abs_ival = (size_t)(-1-ival)+1; |
| 1158 | negative = 1; |
| 1159 | } |
| 1160 | else { |
| 1161 | abs_ival = (size_t)ival; |
| 1162 | } |
Mark Dickinson | 7ab6be2 | 2008-04-15 21:42:42 +0000 | [diff] [blame] | 1163 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1164 | /* Count the number of Python digits. */ |
| 1165 | t = abs_ival; |
| 1166 | while (t) { |
| 1167 | ++ndigits; |
| 1168 | t >>= PyLong_SHIFT; |
| 1169 | } |
| 1170 | v = _PyLong_New(ndigits); |
| 1171 | if (v != NULL) { |
| 1172 | digit *p = v->ob_digit; |
| 1173 | Py_SIZE(v) = negative ? -ndigits : ndigits; |
| 1174 | t = abs_ival; |
| 1175 | while (t) { |
| 1176 | *p++ = (digit)(t & PyLong_MASK); |
| 1177 | t >>= PyLong_SHIFT; |
| 1178 | } |
| 1179 | } |
| 1180 | return (PyObject *)v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1183 | /* Create a new int object from a C size_t. */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1184 | |
| 1185 | PyObject * |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1186 | PyLong_FromSize_t(size_t ival) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1187 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1188 | PyLongObject *v; |
| 1189 | size_t t; |
| 1190 | int ndigits = 0; |
Mark Dickinson | 7ab6be2 | 2008-04-15 21:42:42 +0000 | [diff] [blame] | 1191 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1192 | if (ival < PyLong_BASE) |
| 1193 | return PyLong_FromLong((long)ival); |
| 1194 | /* Count the number of Python digits. */ |
| 1195 | t = ival; |
| 1196 | while (t) { |
| 1197 | ++ndigits; |
| 1198 | t >>= PyLong_SHIFT; |
| 1199 | } |
| 1200 | v = _PyLong_New(ndigits); |
| 1201 | if (v != NULL) { |
| 1202 | digit *p = v->ob_digit; |
| 1203 | Py_SIZE(v) = ndigits; |
| 1204 | while (ival) { |
| 1205 | *p++ = (digit)(ival & PyLong_MASK); |
| 1206 | ival >>= PyLong_SHIFT; |
| 1207 | } |
| 1208 | } |
| 1209 | return (PyObject *)v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1212 | /* 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] | 1213 | __int__ method. Return -1 and set an error if overflow occurs. */ |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1214 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1215 | long long |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1216 | PyLong_AsLongLong(PyObject *vv) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1217 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1218 | PyLongObject *v; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1219 | long long bytes; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1220 | int res; |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1221 | int do_decref = 0; /* if nb_int was called */ |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1222 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1223 | if (vv == NULL) { |
| 1224 | PyErr_BadInternalCall(); |
| 1225 | return -1; |
| 1226 | } |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1227 | |
| 1228 | if (PyLong_Check(vv)) { |
| 1229 | v = (PyLongObject *)vv; |
| 1230 | } |
| 1231 | else { |
| 1232 | v = _PyLong_FromNbInt(vv); |
| 1233 | if (v == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1234 | return -1; |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1235 | do_decref = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1236 | } |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1237 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1238 | res = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1239 | switch(Py_SIZE(v)) { |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1240 | case -1: |
| 1241 | bytes = -(sdigit)v->ob_digit[0]; |
| 1242 | break; |
| 1243 | case 0: |
| 1244 | bytes = 0; |
| 1245 | break; |
| 1246 | case 1: |
| 1247 | bytes = v->ob_digit[0]; |
| 1248 | break; |
| 1249 | default: |
| 1250 | res = _PyLong_AsByteArray((PyLongObject *)v, (unsigned char *)&bytes, |
Serhiy Storchaka | c4f3212 | 2013-12-11 21:26:36 +0200 | [diff] [blame] | 1251 | SIZEOF_LONG_LONG, PY_LITTLE_ENDIAN, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1252 | } |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1253 | if (do_decref) { |
| 1254 | Py_DECREF(v); |
| 1255 | } |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1256 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1257 | /* Plan 9 can't handle long long in ? : expressions */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1258 | if (res < 0) |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1259 | return (long long)-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1260 | else |
| 1261 | return bytes; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1264 | /* Get a C unsigned long long int from an int object. |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1265 | Return -1 and set an error if overflow occurs. */ |
| 1266 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1267 | unsigned long long |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1268 | PyLong_AsUnsignedLongLong(PyObject *vv) |
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 | PyLongObject *v; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1271 | unsigned long long bytes; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1272 | int res; |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1273 | |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 1274 | if (vv == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1275 | PyErr_BadInternalCall(); |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1276 | return (unsigned long long)-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1277 | } |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 1278 | if (!PyLong_Check(vv)) { |
| 1279 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1280 | return (unsigned long long)-1; |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 1281 | } |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1282 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1283 | v = (PyLongObject*)vv; |
| 1284 | switch(Py_SIZE(v)) { |
| 1285 | case 0: return 0; |
| 1286 | case 1: return v->ob_digit[0]; |
| 1287 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1288 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 1289 | res = _PyLong_AsByteArray((PyLongObject *)vv, (unsigned char *)&bytes, |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 1290 | SIZEOF_LONG_LONG, PY_LITTLE_ENDIAN, 0); |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1291 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1292 | /* Plan 9 can't handle long long in ? : expressions */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1293 | if (res < 0) |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1294 | return (unsigned long long)res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1295 | else |
| 1296 | return bytes; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1297 | } |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1298 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1299 | /* 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] | 1300 | Returns -1 and sets an error condition if an error occurs. */ |
| 1301 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1302 | static unsigned long long |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1303 | _PyLong_AsUnsignedLongLongMask(PyObject *vv) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1304 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1305 | PyLongObject *v; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1306 | unsigned long long x; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1307 | Py_ssize_t i; |
| 1308 | int sign; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1309 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1310 | if (vv == NULL || !PyLong_Check(vv)) { |
| 1311 | PyErr_BadInternalCall(); |
| 1312 | return (unsigned long) -1; |
| 1313 | } |
| 1314 | v = (PyLongObject *)vv; |
| 1315 | switch(Py_SIZE(v)) { |
| 1316 | case 0: return 0; |
| 1317 | case 1: return v->ob_digit[0]; |
| 1318 | } |
| 1319 | i = Py_SIZE(v); |
| 1320 | sign = 1; |
| 1321 | x = 0; |
| 1322 | if (i < 0) { |
| 1323 | sign = -1; |
| 1324 | i = -i; |
| 1325 | } |
| 1326 | while (--i >= 0) { |
| 1327 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 1328 | } |
| 1329 | return x * sign; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1330 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1331 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1332 | unsigned long long |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1333 | PyLong_AsUnsignedLongLongMask(PyObject *op) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1334 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1335 | PyLongObject *lo; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1336 | unsigned long long val; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1337 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1338 | if (op == NULL) { |
| 1339 | PyErr_BadInternalCall(); |
| 1340 | return (unsigned long)-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1341 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1342 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1343 | if (PyLong_Check(op)) { |
| 1344 | return _PyLong_AsUnsignedLongLongMask(op); |
| 1345 | } |
| 1346 | |
| 1347 | lo = _PyLong_FromNbInt(op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1348 | if (lo == NULL) |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1349 | return (unsigned long long)-1; |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1350 | |
| 1351 | val = _PyLong_AsUnsignedLongLongMask((PyObject *)lo); |
| 1352 | Py_DECREF(lo); |
| 1353 | return val; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1354 | } |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1355 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1356 | /* 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] | 1357 | __int__ method. |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1358 | |
Mark Dickinson | 8d48b43 | 2011-10-23 20:47:14 +0100 | [diff] [blame] | 1359 | On overflow, return -1 and set *overflow to 1 or -1 depending on the sign of |
| 1360 | the result. Otherwise *overflow is 0. |
| 1361 | |
| 1362 | For other errors (e.g., TypeError), return -1 and set an error condition. |
| 1363 | In this case *overflow will be 0. |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1364 | */ |
| 1365 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1366 | long long |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1367 | PyLong_AsLongLongAndOverflow(PyObject *vv, int *overflow) |
| 1368 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1369 | /* This version by Tim Peters */ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1370 | PyLongObject *v; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1371 | unsigned long long x, prev; |
| 1372 | long long res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1373 | Py_ssize_t i; |
| 1374 | int sign; |
| 1375 | int do_decref = 0; /* if nb_int was called */ |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1376 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1377 | *overflow = 0; |
| 1378 | if (vv == NULL) { |
| 1379 | PyErr_BadInternalCall(); |
| 1380 | return -1; |
| 1381 | } |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1382 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1383 | if (PyLong_Check(vv)) { |
| 1384 | v = (PyLongObject *)vv; |
| 1385 | } |
| 1386 | else { |
| 1387 | v = _PyLong_FromNbInt(vv); |
| 1388 | if (v == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1389 | return -1; |
| 1390 | do_decref = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1391 | } |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1392 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1393 | res = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1394 | i = Py_SIZE(v); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1395 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1396 | switch (i) { |
| 1397 | case -1: |
| 1398 | res = -(sdigit)v->ob_digit[0]; |
| 1399 | break; |
| 1400 | case 0: |
| 1401 | res = 0; |
| 1402 | break; |
| 1403 | case 1: |
| 1404 | res = v->ob_digit[0]; |
| 1405 | break; |
| 1406 | default: |
| 1407 | sign = 1; |
| 1408 | x = 0; |
| 1409 | if (i < 0) { |
| 1410 | sign = -1; |
| 1411 | i = -(i); |
| 1412 | } |
| 1413 | while (--i >= 0) { |
| 1414 | prev = x; |
| 1415 | x = (x << PyLong_SHIFT) + v->ob_digit[i]; |
| 1416 | if ((x >> PyLong_SHIFT) != prev) { |
| 1417 | *overflow = sign; |
| 1418 | goto exit; |
| 1419 | } |
| 1420 | } |
| 1421 | /* Haven't lost any bits, but casting to long requires extra |
| 1422 | * care (see comment above). |
| 1423 | */ |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1424 | if (x <= (unsigned long long)PY_LLONG_MAX) { |
| 1425 | res = (long long)x * sign; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1426 | } |
| 1427 | else if (sign < 0 && x == PY_ABS_LLONG_MIN) { |
| 1428 | res = PY_LLONG_MIN; |
| 1429 | } |
| 1430 | else { |
| 1431 | *overflow = sign; |
| 1432 | /* res is already set to -1 */ |
| 1433 | } |
| 1434 | } |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 1435 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1436 | if (do_decref) { |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1437 | Py_DECREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1438 | } |
| 1439 | return res; |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 1442 | #define CHECK_BINOP(v,w) \ |
| 1443 | do { \ |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1444 | if (!PyLong_Check(v) || !PyLong_Check(w)) \ |
| 1445 | Py_RETURN_NOTIMPLEMENTED; \ |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 1446 | } while(0) |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 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; |
Mark Dickinson | 4e1de16 | 2016-08-29 17:26:43 +0100 | [diff] [blame] | 1594 | int d; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1595 | enum PyUnicode_Kind kind; |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1596 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1597 | a = (PyLongObject *)aa; |
| 1598 | if (a == NULL || !PyLong_Check(a)) { |
| 1599 | PyErr_BadInternalCall(); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1600 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1601 | } |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 1602 | size_a = Py_ABS(Py_SIZE(a)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1603 | negative = Py_SIZE(a) < 0; |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1604 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1605 | /* quick and dirty upper bound for the number of digits |
| 1606 | required to express a in base _PyLong_DECIMAL_BASE: |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1607 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1608 | #digits = 1 + floor(log2(a) / log2(_PyLong_DECIMAL_BASE)) |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1609 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1610 | But log2(a) < size_a * PyLong_SHIFT, and |
| 1611 | log2(_PyLong_DECIMAL_BASE) = log2(10) * _PyLong_DECIMAL_SHIFT |
Mark Dickinson | 4e1de16 | 2016-08-29 17:26:43 +0100 | [diff] [blame] | 1612 | > 3.3 * _PyLong_DECIMAL_SHIFT |
| 1613 | |
| 1614 | size_a * PyLong_SHIFT / (3.3 * _PyLong_DECIMAL_SHIFT) = |
| 1615 | size_a + size_a / d < size_a + size_a / floor(d), |
| 1616 | where d = (3.3 * _PyLong_DECIMAL_SHIFT) / |
| 1617 | (PyLong_SHIFT - 3.3 * _PyLong_DECIMAL_SHIFT) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1618 | */ |
Mark Dickinson | 4e1de16 | 2016-08-29 17:26:43 +0100 | [diff] [blame] | 1619 | d = (33 * _PyLong_DECIMAL_SHIFT) / |
| 1620 | (10 * PyLong_SHIFT - 33 * _PyLong_DECIMAL_SHIFT); |
| 1621 | assert(size_a < PY_SSIZE_T_MAX/2); |
| 1622 | size = 1 + size_a + size_a / d; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1623 | scratch = _PyLong_New(size); |
| 1624 | if (scratch == NULL) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1625 | return -1; |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1626 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1627 | /* convert array of base _PyLong_BASE digits in pin to an array of |
| 1628 | base _PyLong_DECIMAL_BASE digits in pout, following Knuth (TAOCP, |
| 1629 | Volume 2 (3rd edn), section 4.4, Method 1b). */ |
| 1630 | pin = a->ob_digit; |
| 1631 | pout = scratch->ob_digit; |
| 1632 | size = 0; |
| 1633 | for (i = size_a; --i >= 0; ) { |
| 1634 | digit hi = pin[i]; |
| 1635 | for (j = 0; j < size; j++) { |
| 1636 | twodigits z = (twodigits)pout[j] << PyLong_SHIFT | hi; |
| 1637 | hi = (digit)(z / _PyLong_DECIMAL_BASE); |
| 1638 | pout[j] = (digit)(z - (twodigits)hi * |
| 1639 | _PyLong_DECIMAL_BASE); |
| 1640 | } |
| 1641 | while (hi) { |
| 1642 | pout[size++] = hi % _PyLong_DECIMAL_BASE; |
| 1643 | hi /= _PyLong_DECIMAL_BASE; |
| 1644 | } |
| 1645 | /* check for keyboard interrupt */ |
| 1646 | SIGCHECK({ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 1647 | Py_DECREF(scratch); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1648 | return -1; |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 1649 | }); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1650 | } |
| 1651 | /* pout should have at least one digit, so that the case when a = 0 |
| 1652 | works correctly */ |
| 1653 | if (size == 0) |
| 1654 | pout[size++] = 0; |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1655 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1656 | /* calculate exact length of output string, and allocate */ |
| 1657 | strlen = negative + 1 + (size - 1) * _PyLong_DECIMAL_SHIFT; |
| 1658 | tenpow = 10; |
| 1659 | rem = pout[size-1]; |
| 1660 | while (rem >= tenpow) { |
| 1661 | tenpow *= 10; |
| 1662 | strlen++; |
| 1663 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1664 | if (writer) { |
Christian Heimes | 110ac16 | 2012-09-10 02:51:27 +0200 | [diff] [blame] | 1665 | if (_PyUnicodeWriter_Prepare(writer, strlen, '9') == -1) { |
| 1666 | Py_DECREF(scratch); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1667 | return -1; |
Christian Heimes | 110ac16 | 2012-09-10 02:51:27 +0200 | [diff] [blame] | 1668 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1669 | kind = writer->kind; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1670 | } |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1671 | else if (bytes_writer) { |
| 1672 | *bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, strlen); |
| 1673 | if (*bytes_str == NULL) { |
| 1674 | Py_DECREF(scratch); |
| 1675 | return -1; |
| 1676 | } |
| 1677 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1678 | else { |
| 1679 | str = PyUnicode_New(strlen, '9'); |
| 1680 | if (str == NULL) { |
| 1681 | Py_DECREF(scratch); |
| 1682 | return -1; |
| 1683 | } |
| 1684 | kind = PyUnicode_KIND(str); |
| 1685 | } |
| 1686 | |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1687 | #define WRITE_DIGITS(p) \ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1688 | do { \ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1689 | /* pout[0] through pout[size-2] contribute exactly \ |
| 1690 | _PyLong_DECIMAL_SHIFT digits each */ \ |
| 1691 | for (i=0; i < size - 1; i++) { \ |
| 1692 | rem = pout[i]; \ |
| 1693 | for (j = 0; j < _PyLong_DECIMAL_SHIFT; j++) { \ |
| 1694 | *--p = '0' + rem % 10; \ |
| 1695 | rem /= 10; \ |
| 1696 | } \ |
| 1697 | } \ |
| 1698 | /* pout[size-1]: always produce at least one decimal digit */ \ |
| 1699 | rem = pout[i]; \ |
| 1700 | do { \ |
| 1701 | *--p = '0' + rem % 10; \ |
| 1702 | rem /= 10; \ |
| 1703 | } while (rem != 0); \ |
| 1704 | \ |
| 1705 | /* and sign */ \ |
| 1706 | if (negative) \ |
| 1707 | *--p = '-'; \ |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1708 | } while (0) |
| 1709 | |
| 1710 | #define WRITE_UNICODE_DIGITS(TYPE) \ |
| 1711 | do { \ |
| 1712 | if (writer) \ |
| 1713 | p = (TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos + strlen; \ |
| 1714 | else \ |
| 1715 | p = (TYPE*)PyUnicode_DATA(str) + strlen; \ |
| 1716 | \ |
| 1717 | WRITE_DIGITS(p); \ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1718 | \ |
| 1719 | /* check we've counted correctly */ \ |
| 1720 | if (writer) \ |
| 1721 | assert(p == ((TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos)); \ |
| 1722 | else \ |
| 1723 | assert(p == (TYPE*)PyUnicode_DATA(str)); \ |
| 1724 | } while (0) |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1725 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1726 | /* fill the string right-to-left */ |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1727 | if (bytes_writer) { |
| 1728 | char *p = *bytes_str + strlen; |
| 1729 | WRITE_DIGITS(p); |
| 1730 | assert(p == *bytes_str); |
| 1731 | } |
| 1732 | else if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1733 | Py_UCS1 *p; |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1734 | WRITE_UNICODE_DIGITS(Py_UCS1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1735 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1736 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 1737 | Py_UCS2 *p; |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1738 | WRITE_UNICODE_DIGITS(Py_UCS2); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1739 | } |
| 1740 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1741 | Py_UCS4 *p; |
Victor Stinner | e577ab3 | 2012-05-29 18:51:10 +0200 | [diff] [blame] | 1742 | assert (kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1743 | WRITE_UNICODE_DIGITS(Py_UCS4); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1744 | } |
| 1745 | #undef WRITE_DIGITS |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1746 | #undef WRITE_UNICODE_DIGITS |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1747 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1748 | Py_DECREF(scratch); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1749 | if (writer) { |
| 1750 | writer->pos += strlen; |
| 1751 | } |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1752 | else if (bytes_writer) { |
| 1753 | (*bytes_str) += strlen; |
| 1754 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1755 | else { |
| 1756 | assert(_PyUnicode_CheckConsistency(str, 1)); |
| 1757 | *p_output = (PyObject *)str; |
| 1758 | } |
| 1759 | return 0; |
| 1760 | } |
| 1761 | |
| 1762 | static PyObject * |
| 1763 | long_to_decimal_string(PyObject *aa) |
| 1764 | { |
| 1765 | PyObject *v; |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1766 | if (long_to_decimal_string_internal(aa, &v, NULL, NULL, NULL) == -1) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1767 | return NULL; |
| 1768 | return v; |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1771 | /* Convert an int object to a string, using a given conversion base, |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1772 | which should be one of 2, 8 or 16. Return a string object. |
| 1773 | If base is 2, 8 or 16, add the proper prefix '0b', '0o' or '0x' |
| 1774 | if alternate is nonzero. */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1775 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1776 | static int |
| 1777 | long_format_binary(PyObject *aa, int base, int alternate, |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1778 | PyObject **p_output, _PyUnicodeWriter *writer, |
| 1779 | _PyBytesWriter *bytes_writer, char **bytes_str) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1780 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1781 | PyLongObject *a = (PyLongObject *)aa; |
Victor Stinner | 1285e5c | 2015-10-14 12:10:20 +0200 | [diff] [blame] | 1782 | PyObject *v = NULL; |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1783 | Py_ssize_t sz; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1784 | Py_ssize_t size_a; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1785 | enum PyUnicode_Kind kind; |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1786 | int negative; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1787 | int bits; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1788 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1789 | assert(base == 2 || base == 8 || base == 16); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1790 | if (a == NULL || !PyLong_Check(a)) { |
| 1791 | PyErr_BadInternalCall(); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1792 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1793 | } |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 1794 | size_a = Py_ABS(Py_SIZE(a)); |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1795 | negative = Py_SIZE(a) < 0; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1796 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1797 | /* Compute a rough upper bound for the length of the string */ |
| 1798 | switch (base) { |
| 1799 | case 16: |
| 1800 | bits = 4; |
| 1801 | break; |
| 1802 | case 8: |
| 1803 | bits = 3; |
| 1804 | break; |
| 1805 | case 2: |
| 1806 | bits = 1; |
| 1807 | break; |
| 1808 | default: |
| 1809 | assert(0); /* shouldn't ever get here */ |
| 1810 | bits = 0; /* to silence gcc warning */ |
| 1811 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1812 | |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1813 | /* Compute exact length 'sz' of output string. */ |
| 1814 | if (size_a == 0) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1815 | sz = 1; |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1816 | } |
| 1817 | else { |
| 1818 | Py_ssize_t size_a_in_bits; |
| 1819 | /* Ensure overflow doesn't occur during computation of sz. */ |
| 1820 | if (size_a > (PY_SSIZE_T_MAX - 3) / PyLong_SHIFT) { |
| 1821 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 02515f7 | 2013-08-03 12:08:22 +0100 | [diff] [blame] | 1822 | "int too large to format"); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1823 | return -1; |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1824 | } |
| 1825 | size_a_in_bits = (size_a - 1) * PyLong_SHIFT + |
| 1826 | bits_in_digit(a->ob_digit[size_a - 1]); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1827 | /* Allow 1 character for a '-' sign. */ |
| 1828 | sz = negative + (size_a_in_bits + (bits - 1)) / bits; |
| 1829 | } |
| 1830 | if (alternate) { |
| 1831 | /* 2 characters for prefix */ |
| 1832 | sz += 2; |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1833 | } |
| 1834 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1835 | if (writer) { |
| 1836 | if (_PyUnicodeWriter_Prepare(writer, sz, 'x') == -1) |
| 1837 | return -1; |
| 1838 | kind = writer->kind; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1839 | } |
Victor Stinner | 199c9a6 | 2015-10-14 09:47:23 +0200 | [diff] [blame] | 1840 | else if (bytes_writer) { |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1841 | *bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, sz); |
| 1842 | if (*bytes_str == NULL) |
| 1843 | return -1; |
| 1844 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1845 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1846 | v = PyUnicode_New(sz, 'x'); |
| 1847 | if (v == NULL) |
| 1848 | return -1; |
| 1849 | kind = PyUnicode_KIND(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1850 | } |
Mark Dickinson | 8accd6b | 2009-09-17 19:39:12 +0000 | [diff] [blame] | 1851 | |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1852 | #define WRITE_DIGITS(p) \ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1853 | do { \ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1854 | if (size_a == 0) { \ |
| 1855 | *--p = '0'; \ |
| 1856 | } \ |
| 1857 | else { \ |
| 1858 | /* JRH: special case for power-of-2 bases */ \ |
| 1859 | twodigits accum = 0; \ |
| 1860 | int accumbits = 0; /* # of bits in accum */ \ |
| 1861 | Py_ssize_t i; \ |
| 1862 | for (i = 0; i < size_a; ++i) { \ |
| 1863 | accum |= (twodigits)a->ob_digit[i] << accumbits; \ |
| 1864 | accumbits += PyLong_SHIFT; \ |
| 1865 | assert(accumbits >= bits); \ |
| 1866 | do { \ |
| 1867 | char cdigit; \ |
| 1868 | cdigit = (char)(accum & (base - 1)); \ |
| 1869 | cdigit += (cdigit < 10) ? '0' : 'a'-10; \ |
| 1870 | *--p = cdigit; \ |
| 1871 | accumbits -= bits; \ |
| 1872 | accum >>= bits; \ |
| 1873 | } while (i < size_a-1 ? accumbits >= bits : accum > 0); \ |
| 1874 | } \ |
| 1875 | } \ |
| 1876 | \ |
| 1877 | if (alternate) { \ |
| 1878 | if (base == 16) \ |
| 1879 | *--p = 'x'; \ |
| 1880 | else if (base == 8) \ |
| 1881 | *--p = 'o'; \ |
| 1882 | else /* (base == 2) */ \ |
| 1883 | *--p = 'b'; \ |
| 1884 | *--p = '0'; \ |
| 1885 | } \ |
| 1886 | if (negative) \ |
| 1887 | *--p = '-'; \ |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1888 | } while (0) |
| 1889 | |
| 1890 | #define WRITE_UNICODE_DIGITS(TYPE) \ |
| 1891 | do { \ |
| 1892 | if (writer) \ |
| 1893 | p = (TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos + sz; \ |
| 1894 | else \ |
| 1895 | p = (TYPE*)PyUnicode_DATA(v) + sz; \ |
| 1896 | \ |
| 1897 | WRITE_DIGITS(p); \ |
| 1898 | \ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1899 | if (writer) \ |
| 1900 | assert(p == ((TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos)); \ |
| 1901 | else \ |
| 1902 | assert(p == (TYPE*)PyUnicode_DATA(v)); \ |
| 1903 | } while (0) |
| 1904 | |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1905 | if (bytes_writer) { |
| 1906 | char *p = *bytes_str + sz; |
| 1907 | WRITE_DIGITS(p); |
| 1908 | assert(p == *bytes_str); |
| 1909 | } |
| 1910 | else if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1911 | Py_UCS1 *p; |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1912 | WRITE_UNICODE_DIGITS(Py_UCS1); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1913 | } |
| 1914 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 1915 | Py_UCS2 *p; |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1916 | WRITE_UNICODE_DIGITS(Py_UCS2); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1917 | } |
| 1918 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1919 | Py_UCS4 *p; |
Victor Stinner | e577ab3 | 2012-05-29 18:51:10 +0200 | [diff] [blame] | 1920 | assert (kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1921 | WRITE_UNICODE_DIGITS(Py_UCS4); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1922 | } |
| 1923 | #undef WRITE_DIGITS |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1924 | #undef WRITE_UNICODE_DIGITS |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1925 | |
| 1926 | if (writer) { |
| 1927 | writer->pos += sz; |
| 1928 | } |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1929 | else if (bytes_writer) { |
| 1930 | (*bytes_str) += sz; |
| 1931 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1932 | else { |
| 1933 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 1934 | *p_output = v; |
| 1935 | } |
| 1936 | return 0; |
| 1937 | } |
| 1938 | |
| 1939 | PyObject * |
| 1940 | _PyLong_Format(PyObject *obj, int base) |
| 1941 | { |
| 1942 | PyObject *str; |
| 1943 | int err; |
| 1944 | if (base == 10) |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1945 | err = long_to_decimal_string_internal(obj, &str, NULL, NULL, NULL); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1946 | else |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1947 | err = long_format_binary(obj, base, 1, &str, NULL, NULL, NULL); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1948 | if (err == -1) |
| 1949 | return NULL; |
| 1950 | return str; |
| 1951 | } |
| 1952 | |
| 1953 | int |
| 1954 | _PyLong_FormatWriter(_PyUnicodeWriter *writer, |
| 1955 | PyObject *obj, |
| 1956 | int base, int alternate) |
| 1957 | { |
| 1958 | if (base == 10) |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1959 | return long_to_decimal_string_internal(obj, NULL, writer, |
| 1960 | NULL, NULL); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1961 | else |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1962 | return long_format_binary(obj, base, alternate, NULL, writer, |
| 1963 | NULL, NULL); |
| 1964 | } |
| 1965 | |
| 1966 | char* |
| 1967 | _PyLong_FormatBytesWriter(_PyBytesWriter *writer, char *str, |
| 1968 | PyObject *obj, |
| 1969 | int base, int alternate) |
| 1970 | { |
| 1971 | char *str2; |
| 1972 | int res; |
| 1973 | str2 = str; |
| 1974 | if (base == 10) |
| 1975 | res = long_to_decimal_string_internal(obj, NULL, NULL, |
| 1976 | writer, &str2); |
| 1977 | else |
| 1978 | res = long_format_binary(obj, base, alternate, NULL, NULL, |
| 1979 | writer, &str2); |
| 1980 | if (res < 0) |
| 1981 | return NULL; |
| 1982 | assert(str2 != NULL); |
| 1983 | return str2; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1984 | } |
| 1985 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1986 | /* Table of digit values for 8-bit string -> integer conversion. |
| 1987 | * '0' maps to 0, ..., '9' maps to 9. |
| 1988 | * 'a' and 'A' map to 10, ..., 'z' and 'Z' map to 35. |
| 1989 | * All other indices map to 37. |
| 1990 | * 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] | 1991 | * base B digit iff _PyLong_DigitValue[Py_CHARPyLong_MASK(c)] < B. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1992 | */ |
Raymond Hettinger | 3563153 | 2009-01-09 03:58:09 +0000 | [diff] [blame] | 1993 | unsigned char _PyLong_DigitValue[256] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1994 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 1995 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 1996 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 1997 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 37, 37, 37, 37, 37, 37, |
| 1998 | 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
| 1999 | 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37, |
| 2000 | 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
| 2001 | 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 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, |
| 2007 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2008 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2009 | 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] | 2010 | }; |
| 2011 | |
| 2012 | /* *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] | 2013 | * 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] | 2014 | * non-digit (which may be *str!). A normalized int is returned. |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 2015 | * The point to this routine is that it takes time linear in the number of |
| 2016 | * string characters. |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2017 | * |
| 2018 | * Return values: |
| 2019 | * -1 on syntax error (exception needs to be set, *res is untouched) |
| 2020 | * 0 else (exception may be set, in that case *res is set to NULL) |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 2021 | */ |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2022 | static int |
| 2023 | long_from_binary_base(const char **str, int base, PyLongObject **res) |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 2024 | { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 2025 | const char *p = *str; |
| 2026 | const char *start = p; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2027 | char prev = 0; |
| 2028 | int digits = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2029 | int bits_per_char; |
| 2030 | Py_ssize_t n; |
| 2031 | PyLongObject *z; |
| 2032 | twodigits accum; |
| 2033 | int bits_in_accum; |
| 2034 | digit *pdigit; |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 2035 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2036 | assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0); |
| 2037 | n = base; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2038 | for (bits_per_char = -1; n; ++bits_per_char) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2039 | n >>= 1; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2040 | } |
| 2041 | /* count digits and set p to end-of-string */ |
| 2042 | while (_PyLong_DigitValue[Py_CHARMASK(*p)] < base || *p == '_') { |
| 2043 | if (*p == '_') { |
| 2044 | if (prev == '_') { |
| 2045 | *str = p - 1; |
| 2046 | return -1; |
| 2047 | } |
| 2048 | } else { |
| 2049 | ++digits; |
| 2050 | } |
| 2051 | prev = *p; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2052 | ++p; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2053 | } |
| 2054 | if (prev == '_') { |
| 2055 | /* Trailing underscore not allowed. */ |
| 2056 | *str = p - 1; |
| 2057 | return -1; |
| 2058 | } |
| 2059 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2060 | *str = p; |
| 2061 | /* n <- # of Python digits needed, = ceiling(n/PyLong_SHIFT). */ |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2062 | n = digits * bits_per_char + PyLong_SHIFT - 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2063 | if (n / bits_per_char < p - start) { |
| 2064 | PyErr_SetString(PyExc_ValueError, |
| 2065 | "int string too large to convert"); |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2066 | *res = NULL; |
| 2067 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2068 | } |
| 2069 | n = n / PyLong_SHIFT; |
| 2070 | z = _PyLong_New(n); |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2071 | if (z == NULL) { |
| 2072 | *res = NULL; |
| 2073 | return 0; |
| 2074 | } |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2075 | /* Read string from right, and fill in int from left; i.e., |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2076 | * from least to most significant in both. |
| 2077 | */ |
| 2078 | accum = 0; |
| 2079 | bits_in_accum = 0; |
| 2080 | pdigit = z->ob_digit; |
| 2081 | while (--p >= start) { |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2082 | int k; |
| 2083 | if (*p == '_') { |
| 2084 | continue; |
| 2085 | } |
| 2086 | k = (int)_PyLong_DigitValue[Py_CHARMASK(*p)]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2087 | assert(k >= 0 && k < base); |
| 2088 | accum |= (twodigits)k << bits_in_accum; |
| 2089 | bits_in_accum += bits_per_char; |
| 2090 | if (bits_in_accum >= PyLong_SHIFT) { |
| 2091 | *pdigit++ = (digit)(accum & PyLong_MASK); |
| 2092 | assert(pdigit - z->ob_digit <= n); |
| 2093 | accum >>= PyLong_SHIFT; |
| 2094 | bits_in_accum -= PyLong_SHIFT; |
| 2095 | assert(bits_in_accum < PyLong_SHIFT); |
| 2096 | } |
| 2097 | } |
| 2098 | if (bits_in_accum) { |
| 2099 | assert(bits_in_accum <= PyLong_SHIFT); |
| 2100 | *pdigit++ = (digit)accum; |
| 2101 | assert(pdigit - z->ob_digit <= n); |
| 2102 | } |
| 2103 | while (pdigit - z->ob_digit < n) |
| 2104 | *pdigit++ = 0; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2105 | *res = long_normalize(z); |
| 2106 | return 0; |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 2107 | } |
| 2108 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2109 | /* Parses an int from a bytestring. Leading and trailing whitespace will be |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2110 | * ignored. |
| 2111 | * |
| 2112 | * If successful, a PyLong object will be returned and 'pend' will be pointing |
| 2113 | * to the first unused byte unless it's NULL. |
| 2114 | * |
| 2115 | * If unsuccessful, NULL will be returned. |
| 2116 | */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2117 | PyObject * |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 2118 | PyLong_FromString(const char *str, char **pend, int base) |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 2119 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2120 | int sign = 1, error_if_nonzero = 0; |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 2121 | const char *start, *orig_str = str; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2122 | PyLongObject *z = NULL; |
| 2123 | PyObject *strobj; |
| 2124 | Py_ssize_t slen; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2125 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2126 | if ((base != 0 && base < 2) || base > 36) { |
| 2127 | PyErr_SetString(PyExc_ValueError, |
| 2128 | "int() arg 2 must be >= 2 and <= 36"); |
| 2129 | return NULL; |
| 2130 | } |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2131 | while (*str != '\0' && Py_ISSPACE(Py_CHARMASK(*str))) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2132 | str++; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2133 | } |
| 2134 | if (*str == '+') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2135 | ++str; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2136 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2137 | else if (*str == '-') { |
| 2138 | ++str; |
| 2139 | sign = -1; |
| 2140 | } |
| 2141 | if (base == 0) { |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2142 | if (str[0] != '0') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2143 | base = 10; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2144 | } |
| 2145 | else if (str[1] == 'x' || str[1] == 'X') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2146 | base = 16; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2147 | } |
| 2148 | else if (str[1] == 'o' || str[1] == 'O') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2149 | base = 8; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2150 | } |
| 2151 | else if (str[1] == 'b' || str[1] == 'B') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2152 | base = 2; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2153 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2154 | else { |
| 2155 | /* "old" (C-style) octal literal, now invalid. |
| 2156 | it might still be zero though */ |
| 2157 | error_if_nonzero = 1; |
| 2158 | base = 10; |
| 2159 | } |
| 2160 | } |
| 2161 | if (str[0] == '0' && |
| 2162 | ((base == 16 && (str[1] == 'x' || str[1] == 'X')) || |
| 2163 | (base == 8 && (str[1] == 'o' || str[1] == 'O')) || |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2164 | (base == 2 && (str[1] == 'b' || str[1] == 'B')))) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2165 | str += 2; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2166 | /* One underscore allowed here. */ |
| 2167 | if (*str == '_') { |
| 2168 | ++str; |
| 2169 | } |
| 2170 | } |
| 2171 | if (str[0] == '_') { |
| 2172 | /* May not start with underscores. */ |
| 2173 | goto onError; |
| 2174 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2175 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2176 | start = str; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2177 | if ((base & (base - 1)) == 0) { |
| 2178 | int res = long_from_binary_base(&str, base, &z); |
| 2179 | if (res < 0) { |
| 2180 | /* Syntax error. */ |
| 2181 | goto onError; |
| 2182 | } |
| 2183 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2184 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2185 | /*** |
| 2186 | Binary bases can be converted in time linear in the number of digits, because |
| 2187 | Python's representation base is binary. Other bases (including decimal!) use |
| 2188 | the simple quadratic-time algorithm below, complicated by some speed tricks. |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2189 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2190 | First some math: the largest integer that can be expressed in N base-B digits |
| 2191 | is B**N-1. Consequently, if we have an N-digit input in base B, the worst- |
| 2192 | case number of Python digits needed to hold it is the smallest integer n s.t. |
| 2193 | |
| 2194 | BASE**n-1 >= B**N-1 [or, adding 1 to both sides] |
| 2195 | BASE**n >= B**N [taking logs to base BASE] |
| 2196 | n >= log(B**N)/log(BASE) = N * log(B)/log(BASE) |
| 2197 | |
| 2198 | 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] | 2199 | 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] | 2200 | and the result is computed into it. |
| 2201 | |
| 2202 | The input string is actually treated as being in base base**i (i.e., i digits |
| 2203 | are processed at a time), where two more static arrays hold: |
| 2204 | |
| 2205 | convwidth_base[base] = the largest integer i such that base**i <= BASE |
| 2206 | convmultmax_base[base] = base ** convwidth_base[base] |
| 2207 | |
| 2208 | The first of these is the largest i such that i consecutive input digits |
| 2209 | must fit in a single Python digit. The second is effectively the input |
| 2210 | base we're really using. |
| 2211 | |
| 2212 | Viewing the input as a sequence <c0, c1, ..., c_n-1> of digits in base |
| 2213 | convmultmax_base[base], the result is "simply" |
| 2214 | |
| 2215 | (((c0*B + c1)*B + c2)*B + c3)*B + ... ))) + c_n-1 |
| 2216 | |
| 2217 | where B = convmultmax_base[base]. |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2218 | |
| 2219 | Error analysis: as above, the number of Python digits `n` needed is worst- |
| 2220 | case |
| 2221 | |
| 2222 | n >= N * log(B)/log(BASE) |
| 2223 | |
| 2224 | where `N` is the number of input digits in base `B`. This is computed via |
| 2225 | |
| 2226 | size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1; |
| 2227 | |
| 2228 | below. Two numeric concerns are how much space this can waste, and whether |
| 2229 | the computed result can be too small. To be concrete, assume BASE = 2**15, |
| 2230 | which is the default (and it's unlikely anyone changes that). |
| 2231 | |
| 2232 | Waste isn't a problem: provided the first input digit isn't 0, the difference |
| 2233 | between the worst-case input with N digits and the smallest input with N |
| 2234 | digits is about a factor of B, but B is small compared to BASE so at most |
| 2235 | one allocated Python digit can remain unused on that count. If |
| 2236 | N*log(B)/log(BASE) is mathematically an exact integer, then truncating that |
| 2237 | and adding 1 returns a result 1 larger than necessary. However, that can't |
| 2238 | happen: whenever B is a power of 2, long_from_binary_base() is called |
| 2239 | instead, and it's impossible for B**i to be an integer power of 2**15 when |
| 2240 | B is not a power of 2 (i.e., it's impossible for N*log(B)/log(BASE) to be |
| 2241 | an exact integer when B is not a power of 2, since B**i has a prime factor |
| 2242 | other than 2 in that case, but (2**15)**j's only prime factor is 2). |
| 2243 | |
| 2244 | The computed result can be too small if the true value of N*log(B)/log(BASE) |
| 2245 | is a little bit larger than an exact integer, but due to roundoff errors (in |
| 2246 | computing log(B), log(BASE), their quotient, and/or multiplying that by N) |
| 2247 | yields a numeric result a little less than that integer. Unfortunately, "how |
| 2248 | close can a transcendental function get to an integer over some range?" |
| 2249 | questions are generally theoretically intractable. Computer analysis via |
| 2250 | continued fractions is practical: expand log(B)/log(BASE) via continued |
| 2251 | fractions, giving a sequence i/j of "the best" rational approximations. Then |
| 2252 | j*log(B)/log(BASE) is approximately equal to (the integer) i. This shows that |
| 2253 | we can get very close to being in trouble, but very rarely. For example, |
| 2254 | 76573 is a denominator in one of the continued-fraction approximations to |
| 2255 | log(10)/log(2**15), and indeed: |
| 2256 | |
| 2257 | >>> log(10)/log(2**15)*76573 |
| 2258 | 16958.000000654003 |
| 2259 | |
| 2260 | is very close to an integer. If we were working with IEEE single-precision, |
| 2261 | rounding errors could kill us. Finding worst cases in IEEE double-precision |
| 2262 | requires better-than-double-precision log() functions, and Tim didn't bother. |
| 2263 | 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] | 2264 | 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] | 2265 | This should happen extremely rarely, and in fact I don't have a test case |
| 2266 | that triggers it(!). Instead the code was tested by artificially allocating |
| 2267 | just 1 digit at the start, so that the copying code was exercised for every |
| 2268 | digit beyond the first. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2269 | ***/ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2270 | twodigits c; /* current input character */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2271 | Py_ssize_t size_z; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2272 | int digits = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2273 | int i; |
| 2274 | int convwidth; |
| 2275 | twodigits convmultmax, convmult; |
| 2276 | digit *pz, *pzstop; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2277 | const char *scan, *lastdigit; |
| 2278 | char prev = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2279 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2280 | static double log_base_BASE[37] = {0.0e0,}; |
| 2281 | static int convwidth_base[37] = {0,}; |
| 2282 | static twodigits convmultmax_base[37] = {0,}; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2283 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2284 | if (log_base_BASE[base] == 0.0) { |
| 2285 | twodigits convmax = base; |
| 2286 | int i = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2287 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2288 | log_base_BASE[base] = (log((double)base) / |
| 2289 | log((double)PyLong_BASE)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2290 | for (;;) { |
| 2291 | twodigits next = convmax * base; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2292 | if (next > PyLong_BASE) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2293 | break; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2294 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2295 | convmax = next; |
| 2296 | ++i; |
| 2297 | } |
| 2298 | convmultmax_base[base] = convmax; |
| 2299 | assert(i > 0); |
| 2300 | convwidth_base[base] = i; |
| 2301 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2303 | /* Find length of the string of numeric characters. */ |
| 2304 | scan = str; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2305 | lastdigit = str; |
| 2306 | |
| 2307 | while (_PyLong_DigitValue[Py_CHARMASK(*scan)] < base || *scan == '_') { |
| 2308 | if (*scan == '_') { |
| 2309 | if (prev == '_') { |
| 2310 | /* Only one underscore allowed. */ |
| 2311 | str = lastdigit + 1; |
| 2312 | goto onError; |
| 2313 | } |
| 2314 | } |
| 2315 | else { |
| 2316 | ++digits; |
| 2317 | lastdigit = scan; |
| 2318 | } |
| 2319 | prev = *scan; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2320 | ++scan; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2321 | } |
| 2322 | if (prev == '_') { |
| 2323 | /* Trailing underscore not allowed. */ |
| 2324 | /* Set error pointer to first underscore. */ |
| 2325 | str = lastdigit + 1; |
| 2326 | goto onError; |
| 2327 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2328 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2329 | /* Create an int object that can contain the largest possible |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2330 | * integer with this base and length. Note that there's no |
| 2331 | * need to initialize z->ob_digit -- no slot is read up before |
| 2332 | * being stored into. |
| 2333 | */ |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2334 | size_z = (Py_ssize_t)(digits * log_base_BASE[base]) + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2335 | /* Uncomment next line to test exceedingly rare copy code */ |
| 2336 | /* size_z = 1; */ |
| 2337 | assert(size_z > 0); |
| 2338 | z = _PyLong_New(size_z); |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2339 | if (z == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2340 | return NULL; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2341 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2342 | Py_SIZE(z) = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2343 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2344 | /* `convwidth` consecutive input digits are treated as a single |
| 2345 | * digit in base `convmultmax`. |
| 2346 | */ |
| 2347 | convwidth = convwidth_base[base]; |
| 2348 | convmultmax = convmultmax_base[base]; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2349 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2350 | /* Work ;-) */ |
| 2351 | while (str < scan) { |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2352 | if (*str == '_') { |
| 2353 | str++; |
| 2354 | continue; |
| 2355 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2356 | /* grab up to convwidth digits from the input string */ |
| 2357 | c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)]; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2358 | for (i = 1; i < convwidth && str != scan; ++str) { |
| 2359 | if (*str == '_') { |
| 2360 | continue; |
| 2361 | } |
| 2362 | i++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2363 | c = (twodigits)(c * base + |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2364 | (int)_PyLong_DigitValue[Py_CHARMASK(*str)]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2365 | assert(c < PyLong_BASE); |
| 2366 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2367 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2368 | convmult = convmultmax; |
| 2369 | /* Calculate the shift only if we couldn't get |
| 2370 | * convwidth digits. |
| 2371 | */ |
| 2372 | if (i != convwidth) { |
| 2373 | convmult = base; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2374 | for ( ; i > 1; --i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2375 | convmult *= base; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2376 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2377 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2378 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2379 | /* Multiply z by convmult, and add c. */ |
| 2380 | pz = z->ob_digit; |
| 2381 | pzstop = pz + Py_SIZE(z); |
| 2382 | for (; pz < pzstop; ++pz) { |
| 2383 | c += (twodigits)*pz * convmult; |
| 2384 | *pz = (digit)(c & PyLong_MASK); |
| 2385 | c >>= PyLong_SHIFT; |
| 2386 | } |
| 2387 | /* carry off the current end? */ |
| 2388 | if (c) { |
| 2389 | assert(c < PyLong_BASE); |
| 2390 | if (Py_SIZE(z) < size_z) { |
| 2391 | *pz = (digit)c; |
| 2392 | ++Py_SIZE(z); |
| 2393 | } |
| 2394 | else { |
| 2395 | PyLongObject *tmp; |
| 2396 | /* Extremely rare. Get more space. */ |
| 2397 | assert(Py_SIZE(z) == size_z); |
| 2398 | tmp = _PyLong_New(size_z + 1); |
| 2399 | if (tmp == NULL) { |
| 2400 | Py_DECREF(z); |
| 2401 | return NULL; |
| 2402 | } |
| 2403 | memcpy(tmp->ob_digit, |
| 2404 | z->ob_digit, |
| 2405 | sizeof(digit) * size_z); |
| 2406 | Py_DECREF(z); |
| 2407 | z = tmp; |
| 2408 | z->ob_digit[size_z] = (digit)c; |
| 2409 | ++size_z; |
| 2410 | } |
| 2411 | } |
| 2412 | } |
| 2413 | } |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2414 | if (z == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2415 | return NULL; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2416 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2417 | if (error_if_nonzero) { |
| 2418 | /* reset the base to 0, else the exception message |
| 2419 | doesn't make too much sense */ |
| 2420 | base = 0; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2421 | if (Py_SIZE(z) != 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2422 | goto onError; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2423 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2424 | /* there might still be other problems, therefore base |
| 2425 | remains zero here for the same reason */ |
| 2426 | } |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2427 | if (str == start) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2428 | goto onError; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2429 | } |
| 2430 | if (sign < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2431 | Py_SIZE(z) = -(Py_SIZE(z)); |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2432 | } |
| 2433 | while (*str && Py_ISSPACE(Py_CHARMASK(*str))) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2434 | str++; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2435 | } |
| 2436 | if (*str != '\0') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2437 | goto onError; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2438 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2439 | long_normalize(z); |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2440 | z = maybe_small_long(z); |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2441 | if (z == NULL) { |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2442 | return NULL; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2443 | } |
| 2444 | if (pend != NULL) { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 2445 | *pend = (char *)str; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2446 | } |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2447 | return (PyObject *) z; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2448 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2449 | onError: |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2450 | if (pend != NULL) { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 2451 | *pend = (char *)str; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2452 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2453 | Py_XDECREF(z); |
| 2454 | slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200; |
| 2455 | strobj = PyUnicode_FromStringAndSize(orig_str, slen); |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2456 | if (strobj == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2457 | return NULL; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2458 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2459 | PyErr_Format(PyExc_ValueError, |
Serhiy Storchaka | 579ddc2 | 2013-08-03 21:14:05 +0300 | [diff] [blame] | 2460 | "invalid literal for int() with base %d: %.200R", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2461 | base, strobj); |
| 2462 | Py_DECREF(strobj); |
| 2463 | return NULL; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2464 | } |
| 2465 | |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2466 | /* Since PyLong_FromString doesn't have a length parameter, |
| 2467 | * check here for possible NULs in the string. |
| 2468 | * |
| 2469 | * Reports an invalid literal as a bytes object. |
| 2470 | */ |
| 2471 | PyObject * |
| 2472 | _PyLong_FromBytes(const char *s, Py_ssize_t len, int base) |
| 2473 | { |
| 2474 | PyObject *result, *strobj; |
| 2475 | char *end = NULL; |
| 2476 | |
Serhiy Storchaka | 20b39b2 | 2014-09-28 11:27:24 +0300 | [diff] [blame] | 2477 | result = PyLong_FromString(s, &end, base); |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2478 | if (end == NULL || (result != NULL && end == s + len)) |
| 2479 | return result; |
| 2480 | Py_XDECREF(result); |
| 2481 | strobj = PyBytes_FromStringAndSize(s, Py_MIN(len, 200)); |
| 2482 | if (strobj != NULL) { |
| 2483 | PyErr_Format(PyExc_ValueError, |
Serhiy Storchaka | 579ddc2 | 2013-08-03 21:14:05 +0300 | [diff] [blame] | 2484 | "invalid literal for int() with base %d: %.200R", |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2485 | base, strobj); |
| 2486 | Py_DECREF(strobj); |
| 2487 | } |
| 2488 | return NULL; |
| 2489 | } |
| 2490 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2491 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2492 | PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2493 | { |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 2494 | PyObject *v, *unicode = PyUnicode_FromWideChar(u, length); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2495 | if (unicode == NULL) |
| 2496 | return NULL; |
| 2497 | v = PyLong_FromUnicodeObject(unicode, base); |
| 2498 | Py_DECREF(unicode); |
| 2499 | return v; |
| 2500 | } |
| 2501 | |
| 2502 | PyObject * |
| 2503 | PyLong_FromUnicodeObject(PyObject *u, int base) |
| 2504 | { |
Serhiy Storchaka | 579ddc2 | 2013-08-03 21:14:05 +0300 | [diff] [blame] | 2505 | PyObject *result, *asciidig; |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 2506 | const char *buffer; |
| 2507 | char *end = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2508 | Py_ssize_t buflen; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2509 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2510 | asciidig = _PyUnicode_TransformDecimalAndSpaceToASCII(u); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 2511 | if (asciidig == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2512 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2513 | buffer = PyUnicode_AsUTF8AndSize(asciidig, &buflen); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 2514 | if (buffer == NULL) { |
| 2515 | Py_DECREF(asciidig); |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2516 | if (!PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
| 2517 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2518 | } |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2519 | else { |
| 2520 | result = PyLong_FromString(buffer, &end, base); |
| 2521 | if (end == NULL || (result != NULL && end == buffer + buflen)) { |
| 2522 | Py_DECREF(asciidig); |
| 2523 | return result; |
| 2524 | } |
| 2525 | Py_DECREF(asciidig); |
| 2526 | Py_XDECREF(result); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 2527 | } |
Serhiy Storchaka | 579ddc2 | 2013-08-03 21:14:05 +0300 | [diff] [blame] | 2528 | PyErr_Format(PyExc_ValueError, |
| 2529 | "invalid literal for int() with base %d: %.200R", |
| 2530 | base, u); |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2531 | return NULL; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2534 | /* forward */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2535 | static PyLongObject *x_divrem |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2536 | (PyLongObject *, PyLongObject *, PyLongObject **); |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 2537 | static PyObject *long_long(PyObject *v); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2538 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2539 | /* Int division with remainder, top-level routine */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2540 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2541 | static int |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2542 | long_divrem(PyLongObject *a, PyLongObject *b, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2543 | PyLongObject **pdiv, PyLongObject **prem) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2544 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 2545 | 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] | 2546 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2547 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2548 | if (size_b == 0) { |
| 2549 | PyErr_SetString(PyExc_ZeroDivisionError, |
| 2550 | "integer division or modulo by zero"); |
| 2551 | return -1; |
| 2552 | } |
| 2553 | if (size_a < size_b || |
| 2554 | (size_a == size_b && |
| 2555 | a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) { |
| 2556 | /* |a| < |b|. */ |
Mark Dickinson | b820d7f | 2016-08-22 12:24:46 +0100 | [diff] [blame] | 2557 | *prem = (PyLongObject *)long_long((PyObject *)a); |
| 2558 | if (*prem == NULL) { |
Mark Dickinson | b820d7f | 2016-08-22 12:24:46 +0100 | [diff] [blame] | 2559 | return -1; |
| 2560 | } |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 2561 | Py_INCREF(_PyLong_Zero); |
| 2562 | *pdiv = (PyLongObject*)_PyLong_Zero; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2563 | return 0; |
| 2564 | } |
| 2565 | if (size_b == 1) { |
| 2566 | digit rem = 0; |
| 2567 | z = divrem1(a, b->ob_digit[0], &rem); |
| 2568 | if (z == NULL) |
| 2569 | return -1; |
| 2570 | *prem = (PyLongObject *) PyLong_FromLong((long)rem); |
| 2571 | if (*prem == NULL) { |
| 2572 | Py_DECREF(z); |
| 2573 | return -1; |
| 2574 | } |
| 2575 | } |
| 2576 | else { |
| 2577 | z = x_divrem(a, b, prem); |
| 2578 | if (z == NULL) |
| 2579 | return -1; |
| 2580 | } |
| 2581 | /* Set the signs. |
| 2582 | The quotient z has the sign of a*b; |
| 2583 | the remainder r has the sign of a, |
| 2584 | so a = b*z + r. */ |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 2585 | if ((Py_SIZE(a) < 0) != (Py_SIZE(b) < 0)) { |
| 2586 | _PyLong_Negate(&z); |
| 2587 | if (z == NULL) { |
| 2588 | Py_CLEAR(*prem); |
| 2589 | return -1; |
| 2590 | } |
| 2591 | } |
| 2592 | if (Py_SIZE(a) < 0 && Py_SIZE(*prem) != 0) { |
| 2593 | _PyLong_Negate(prem); |
| 2594 | if (*prem == NULL) { |
| 2595 | Py_DECREF(z); |
| 2596 | Py_CLEAR(*prem); |
| 2597 | return -1; |
| 2598 | } |
| 2599 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2600 | *pdiv = maybe_small_long(z); |
| 2601 | return 0; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2602 | } |
| 2603 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2604 | /* Unsigned int division with remainder -- the algorithm. The arguments v1 |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 2605 | 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] | 2606 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2607 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2608 | x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2609 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2610 | PyLongObject *v, *w, *a; |
| 2611 | Py_ssize_t i, k, size_v, size_w; |
| 2612 | int d; |
| 2613 | digit wm1, wm2, carry, q, r, vtop, *v0, *vk, *w0, *ak; |
| 2614 | twodigits vv; |
| 2615 | sdigit zhi; |
| 2616 | stwodigits z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2617 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2618 | /* We follow Knuth [The Art of Computer Programming, Vol. 2 (3rd |
| 2619 | edn.), section 4.3.1, Algorithm D], except that we don't explicitly |
| 2620 | handle the special case when the initial estimate q for a quotient |
| 2621 | digit is >= PyLong_BASE: the max value for q is PyLong_BASE+1, and |
| 2622 | that won't overflow a digit. */ |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 2623 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2624 | /* allocate space; w will also be used to hold the final remainder */ |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 2625 | size_v = Py_ABS(Py_SIZE(v1)); |
| 2626 | size_w = Py_ABS(Py_SIZE(w1)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2627 | assert(size_v >= size_w && size_w >= 2); /* Assert checks by div() */ |
| 2628 | v = _PyLong_New(size_v+1); |
| 2629 | if (v == NULL) { |
| 2630 | *prem = NULL; |
| 2631 | return NULL; |
| 2632 | } |
| 2633 | w = _PyLong_New(size_w); |
| 2634 | if (w == NULL) { |
| 2635 | Py_DECREF(v); |
| 2636 | *prem = NULL; |
| 2637 | return NULL; |
| 2638 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2639 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2640 | /* normalize: shift w1 left so that its top digit is >= PyLong_BASE/2. |
| 2641 | shift v1 left by the same amount. Results go into w and v. */ |
| 2642 | d = PyLong_SHIFT - bits_in_digit(w1->ob_digit[size_w-1]); |
| 2643 | carry = v_lshift(w->ob_digit, w1->ob_digit, size_w, d); |
| 2644 | assert(carry == 0); |
| 2645 | carry = v_lshift(v->ob_digit, v1->ob_digit, size_v, d); |
| 2646 | if (carry != 0 || v->ob_digit[size_v-1] >= w->ob_digit[size_w-1]) { |
| 2647 | v->ob_digit[size_v] = carry; |
| 2648 | size_v++; |
| 2649 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2650 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2651 | /* Now v->ob_digit[size_v-1] < w->ob_digit[size_w-1], so quotient has |
| 2652 | at most (and usually exactly) k = size_v - size_w digits. */ |
| 2653 | k = size_v - size_w; |
| 2654 | assert(k >= 0); |
| 2655 | a = _PyLong_New(k); |
| 2656 | if (a == NULL) { |
| 2657 | Py_DECREF(w); |
| 2658 | Py_DECREF(v); |
| 2659 | *prem = NULL; |
| 2660 | return NULL; |
| 2661 | } |
| 2662 | v0 = v->ob_digit; |
| 2663 | w0 = w->ob_digit; |
| 2664 | wm1 = w0[size_w-1]; |
| 2665 | wm2 = w0[size_w-2]; |
| 2666 | for (vk = v0+k, ak = a->ob_digit + k; vk-- > v0;) { |
| 2667 | /* inner loop: divide vk[0:size_w+1] by w0[0:size_w], giving |
| 2668 | single-digit quotient q, remainder in vk[0:size_w]. */ |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2669 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2670 | SIGCHECK({ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2671 | Py_DECREF(a); |
| 2672 | Py_DECREF(w); |
| 2673 | Py_DECREF(v); |
| 2674 | *prem = NULL; |
| 2675 | return NULL; |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 2676 | }); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2677 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2678 | /* estimate quotient digit q; may overestimate by 1 (rare) */ |
| 2679 | vtop = vk[size_w]; |
| 2680 | assert(vtop <= wm1); |
| 2681 | vv = ((twodigits)vtop << PyLong_SHIFT) | vk[size_w-1]; |
| 2682 | q = (digit)(vv / wm1); |
| 2683 | r = (digit)(vv - (twodigits)wm1 * q); /* r = vv % wm1 */ |
| 2684 | while ((twodigits)wm2 * q > (((twodigits)r << PyLong_SHIFT) |
| 2685 | | vk[size_w-2])) { |
| 2686 | --q; |
| 2687 | r += wm1; |
| 2688 | if (r >= PyLong_BASE) |
| 2689 | break; |
| 2690 | } |
| 2691 | assert(q <= PyLong_BASE); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2692 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2693 | /* subtract q*w0[0:size_w] from vk[0:size_w+1] */ |
| 2694 | zhi = 0; |
| 2695 | for (i = 0; i < size_w; ++i) { |
| 2696 | /* invariants: -PyLong_BASE <= -q <= zhi <= 0; |
| 2697 | -PyLong_BASE * q <= z < PyLong_BASE */ |
| 2698 | z = (sdigit)vk[i] + zhi - |
| 2699 | (stwodigits)q * (stwodigits)w0[i]; |
| 2700 | vk[i] = (digit)z & PyLong_MASK; |
| 2701 | zhi = (sdigit)Py_ARITHMETIC_RIGHT_SHIFT(stwodigits, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2702 | z, PyLong_SHIFT); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2703 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2704 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2705 | /* add w back if q was too large (this branch taken rarely) */ |
| 2706 | assert((sdigit)vtop + zhi == -1 || (sdigit)vtop + zhi == 0); |
| 2707 | if ((sdigit)vtop + zhi < 0) { |
| 2708 | carry = 0; |
| 2709 | for (i = 0; i < size_w; ++i) { |
| 2710 | carry += vk[i] + w0[i]; |
| 2711 | vk[i] = carry & PyLong_MASK; |
| 2712 | carry >>= PyLong_SHIFT; |
| 2713 | } |
| 2714 | --q; |
| 2715 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2716 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2717 | /* store quotient digit */ |
| 2718 | assert(q < PyLong_BASE); |
| 2719 | *--ak = q; |
| 2720 | } |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 2721 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2722 | /* unshift remainder; we reuse w to store the result */ |
| 2723 | carry = v_rshift(w0, v0, size_w, d); |
| 2724 | assert(carry==0); |
| 2725 | Py_DECREF(v); |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 2726 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2727 | *prem = long_normalize(w); |
| 2728 | return long_normalize(a); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2729 | } |
| 2730 | |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2731 | /* For a nonzero PyLong a, express a in the form x * 2**e, with 0.5 <= |
| 2732 | abs(x) < 1.0 and e >= 0; return x and put e in *e. Here x is |
| 2733 | rounded to DBL_MANT_DIG significant bits using round-half-to-even. |
| 2734 | If a == 0, return 0.0 and set *e = 0. If the resulting exponent |
| 2735 | e is larger than PY_SSIZE_T_MAX, raise OverflowError and return |
| 2736 | -1.0. */ |
| 2737 | |
| 2738 | /* attempt to define 2.0**DBL_MANT_DIG as a compile-time constant */ |
| 2739 | #if DBL_MANT_DIG == 53 |
| 2740 | #define EXP2_DBL_MANT_DIG 9007199254740992.0 |
| 2741 | #else |
| 2742 | #define EXP2_DBL_MANT_DIG (ldexp(1.0, DBL_MANT_DIG)) |
| 2743 | #endif |
| 2744 | |
| 2745 | double |
| 2746 | _PyLong_Frexp(PyLongObject *a, Py_ssize_t *e) |
| 2747 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2748 | Py_ssize_t a_size, a_bits, shift_digits, shift_bits, x_size; |
| 2749 | /* See below for why x_digits is always large enough. */ |
| 2750 | digit rem, x_digits[2 + (DBL_MANT_DIG + 1) / PyLong_SHIFT]; |
| 2751 | double dx; |
| 2752 | /* Correction term for round-half-to-even rounding. For a digit x, |
| 2753 | "x + half_even_correction[x & 7]" gives x rounded to the nearest |
| 2754 | multiple of 4, rounding ties to a multiple of 8. */ |
| 2755 | 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] | 2756 | |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 2757 | a_size = Py_ABS(Py_SIZE(a)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2758 | if (a_size == 0) { |
| 2759 | /* Special case for 0: significand 0.0, exponent 0. */ |
| 2760 | *e = 0; |
| 2761 | return 0.0; |
| 2762 | } |
| 2763 | a_bits = bits_in_digit(a->ob_digit[a_size-1]); |
| 2764 | /* The following is an overflow-free version of the check |
| 2765 | "if ((a_size - 1) * PyLong_SHIFT + a_bits > PY_SSIZE_T_MAX) ..." */ |
| 2766 | if (a_size >= (PY_SSIZE_T_MAX - 1) / PyLong_SHIFT + 1 && |
| 2767 | (a_size > (PY_SSIZE_T_MAX - 1) / PyLong_SHIFT + 1 || |
| 2768 | a_bits > (PY_SSIZE_T_MAX - 1) % PyLong_SHIFT + 1)) |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2769 | goto overflow; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2770 | a_bits = (a_size - 1) * PyLong_SHIFT + a_bits; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2771 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2772 | /* Shift the first DBL_MANT_DIG + 2 bits of a into x_digits[0:x_size] |
| 2773 | (shifting left if a_bits <= DBL_MANT_DIG + 2). |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2774 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2775 | Number of digits needed for result: write // for floor division. |
| 2776 | Then if shifting left, we end up using |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2777 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2778 | 1 + a_size + (DBL_MANT_DIG + 2 - a_bits) // PyLong_SHIFT |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2779 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2780 | digits. If shifting right, we use |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2781 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2782 | a_size - (a_bits - DBL_MANT_DIG - 2) // PyLong_SHIFT |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2783 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2784 | digits. Using a_size = 1 + (a_bits - 1) // PyLong_SHIFT along with |
| 2785 | the inequalities |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2786 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2787 | m // PyLong_SHIFT + n // PyLong_SHIFT <= (m + n) // PyLong_SHIFT |
| 2788 | m // PyLong_SHIFT - n // PyLong_SHIFT <= |
| 2789 | 1 + (m - n - 1) // PyLong_SHIFT, |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2790 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2791 | 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] | 2792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2793 | x_size <= 2 + (DBL_MANT_DIG + 1) // PyLong_SHIFT |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2794 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2795 | in both cases. |
| 2796 | */ |
| 2797 | if (a_bits <= DBL_MANT_DIG + 2) { |
| 2798 | shift_digits = (DBL_MANT_DIG + 2 - a_bits) / PyLong_SHIFT; |
| 2799 | shift_bits = (DBL_MANT_DIG + 2 - a_bits) % PyLong_SHIFT; |
| 2800 | x_size = 0; |
| 2801 | while (x_size < shift_digits) |
| 2802 | x_digits[x_size++] = 0; |
| 2803 | rem = v_lshift(x_digits + x_size, a->ob_digit, a_size, |
| 2804 | (int)shift_bits); |
| 2805 | x_size += a_size; |
| 2806 | x_digits[x_size++] = rem; |
| 2807 | } |
| 2808 | else { |
| 2809 | shift_digits = (a_bits - DBL_MANT_DIG - 2) / PyLong_SHIFT; |
| 2810 | shift_bits = (a_bits - DBL_MANT_DIG - 2) % PyLong_SHIFT; |
| 2811 | rem = v_rshift(x_digits, a->ob_digit + shift_digits, |
| 2812 | a_size - shift_digits, (int)shift_bits); |
| 2813 | x_size = a_size - shift_digits; |
| 2814 | /* For correct rounding below, we need the least significant |
| 2815 | bit of x to be 'sticky' for this shift: if any of the bits |
| 2816 | shifted out was nonzero, we set the least significant bit |
| 2817 | of x. */ |
| 2818 | if (rem) |
| 2819 | x_digits[0] |= 1; |
| 2820 | else |
| 2821 | while (shift_digits > 0) |
| 2822 | if (a->ob_digit[--shift_digits]) { |
| 2823 | x_digits[0] |= 1; |
| 2824 | break; |
| 2825 | } |
| 2826 | } |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2827 | 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] | 2828 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2829 | /* Round, and convert to double. */ |
| 2830 | x_digits[0] += half_even_correction[x_digits[0] & 7]; |
| 2831 | dx = x_digits[--x_size]; |
| 2832 | while (x_size > 0) |
| 2833 | dx = dx * PyLong_BASE + x_digits[--x_size]; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2834 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2835 | /* Rescale; make correction if result is 1.0. */ |
| 2836 | dx /= 4.0 * EXP2_DBL_MANT_DIG; |
| 2837 | if (dx == 1.0) { |
| 2838 | if (a_bits == PY_SSIZE_T_MAX) |
| 2839 | goto overflow; |
| 2840 | dx = 0.5; |
| 2841 | a_bits += 1; |
| 2842 | } |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2843 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2844 | *e = a_bits; |
| 2845 | return Py_SIZE(a) < 0 ? -dx : dx; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2846 | |
| 2847 | overflow: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2848 | /* exponent > PY_SSIZE_T_MAX */ |
| 2849 | PyErr_SetString(PyExc_OverflowError, |
| 2850 | "huge integer: number of bits overflows a Py_ssize_t"); |
| 2851 | *e = 0; |
| 2852 | return -1.0; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2853 | } |
| 2854 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2855 | /* 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] | 2856 | using the round-half-to-even rule in the case of a tie. */ |
| 2857 | |
| 2858 | double |
| 2859 | PyLong_AsDouble(PyObject *v) |
| 2860 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2861 | Py_ssize_t exponent; |
| 2862 | double x; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2863 | |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 2864 | if (v == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2865 | PyErr_BadInternalCall(); |
| 2866 | return -1.0; |
| 2867 | } |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 2868 | if (!PyLong_Check(v)) { |
| 2869 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 2870 | return -1.0; |
| 2871 | } |
Yury Selivanov | 186c30b | 2016-02-05 19:40:01 -0500 | [diff] [blame] | 2872 | if (Py_ABS(Py_SIZE(v)) <= 1) { |
Yury Selivanov | a0fcaca | 2016-02-06 12:21:33 -0500 | [diff] [blame] | 2873 | /* Fast path; single digit long (31 bits) will cast safely |
Mark Dickinson | 1dc3c89 | 2016-08-21 10:33:36 +0100 | [diff] [blame] | 2874 | to double. This improves performance of FP/long operations |
| 2875 | by 20%. |
Yury Selivanov | 186c30b | 2016-02-05 19:40:01 -0500 | [diff] [blame] | 2876 | */ |
| 2877 | return (double)MEDIUM_VALUE((PyLongObject *)v); |
| 2878 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2879 | x = _PyLong_Frexp((PyLongObject *)v, &exponent); |
| 2880 | if ((x == -1.0 && PyErr_Occurred()) || exponent > DBL_MAX_EXP) { |
| 2881 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 02515f7 | 2013-08-03 12:08:22 +0100 | [diff] [blame] | 2882 | "int too large to convert to float"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2883 | return -1.0; |
| 2884 | } |
| 2885 | return ldexp(x, (int)exponent); |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2886 | } |
| 2887 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2888 | /* Methods */ |
| 2889 | |
| 2890 | static void |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2891 | long_dealloc(PyObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2892 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2893 | Py_TYPE(v)->tp_free(v); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2894 | } |
| 2895 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2896 | static int |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2897 | long_compare(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2898 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2899 | Py_ssize_t sign; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2900 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2901 | if (Py_SIZE(a) != Py_SIZE(b)) { |
| 2902 | sign = Py_SIZE(a) - Py_SIZE(b); |
| 2903 | } |
| 2904 | else { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 2905 | Py_ssize_t i = Py_ABS(Py_SIZE(a)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2906 | while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) |
| 2907 | ; |
| 2908 | if (i < 0) |
| 2909 | sign = 0; |
| 2910 | else { |
| 2911 | sign = (sdigit)a->ob_digit[i] - (sdigit)b->ob_digit[i]; |
| 2912 | if (Py_SIZE(a) < 0) |
| 2913 | sign = -sign; |
| 2914 | } |
| 2915 | } |
| 2916 | return sign < 0 ? -1 : sign > 0 ? 1 : 0; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2917 | } |
| 2918 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 2919 | #define TEST_COND(cond) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2920 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 2921 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2922 | static PyObject * |
| 2923 | long_richcompare(PyObject *self, PyObject *other, int op) |
| 2924 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2925 | int result; |
| 2926 | PyObject *v; |
| 2927 | CHECK_BINOP(self, other); |
| 2928 | if (self == other) |
| 2929 | result = 0; |
| 2930 | else |
| 2931 | result = long_compare((PyLongObject*)self, (PyLongObject*)other); |
| 2932 | /* Convert the return value to a Boolean */ |
| 2933 | switch (op) { |
| 2934 | case Py_EQ: |
| 2935 | v = TEST_COND(result == 0); |
| 2936 | break; |
| 2937 | case Py_NE: |
| 2938 | v = TEST_COND(result != 0); |
| 2939 | break; |
| 2940 | case Py_LE: |
| 2941 | v = TEST_COND(result <= 0); |
| 2942 | break; |
| 2943 | case Py_GE: |
| 2944 | v = TEST_COND(result >= 0); |
| 2945 | break; |
| 2946 | case Py_LT: |
| 2947 | v = TEST_COND(result == -1); |
| 2948 | break; |
| 2949 | case Py_GT: |
| 2950 | v = TEST_COND(result == 1); |
| 2951 | break; |
| 2952 | default: |
| 2953 | PyErr_BadArgument(); |
| 2954 | return NULL; |
| 2955 | } |
| 2956 | Py_INCREF(v); |
| 2957 | return v; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2958 | } |
| 2959 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2960 | static Py_hash_t |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2961 | long_hash(PyLongObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2962 | { |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 2963 | Py_uhash_t x; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2964 | Py_ssize_t i; |
| 2965 | int sign; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2966 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2967 | i = Py_SIZE(v); |
| 2968 | switch(i) { |
| 2969 | case -1: return v->ob_digit[0]==1 ? -2 : -(sdigit)v->ob_digit[0]; |
| 2970 | case 0: return 0; |
| 2971 | case 1: return v->ob_digit[0]; |
| 2972 | } |
| 2973 | sign = 1; |
| 2974 | x = 0; |
| 2975 | if (i < 0) { |
| 2976 | sign = -1; |
| 2977 | i = -(i); |
| 2978 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2979 | while (--i >= 0) { |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 2980 | /* Here x is a quantity in the range [0, _PyHASH_MODULUS); we |
| 2981 | want to compute x * 2**PyLong_SHIFT + v->ob_digit[i] modulo |
| 2982 | _PyHASH_MODULUS. |
| 2983 | |
| 2984 | The computation of x * 2**PyLong_SHIFT % _PyHASH_MODULUS |
| 2985 | amounts to a rotation of the bits of x. To see this, write |
| 2986 | |
| 2987 | x * 2**PyLong_SHIFT = y * 2**_PyHASH_BITS + z |
| 2988 | |
| 2989 | where y = x >> (_PyHASH_BITS - PyLong_SHIFT) gives the top |
| 2990 | PyLong_SHIFT bits of x (those that are shifted out of the |
| 2991 | original _PyHASH_BITS bits, and z = (x << PyLong_SHIFT) & |
| 2992 | _PyHASH_MODULUS gives the bottom _PyHASH_BITS - PyLong_SHIFT |
| 2993 | bits of x, shifted up. Then since 2**_PyHASH_BITS is |
| 2994 | congruent to 1 modulo _PyHASH_MODULUS, y*2**_PyHASH_BITS is |
| 2995 | congruent to y modulo _PyHASH_MODULUS. So |
| 2996 | |
| 2997 | x * 2**PyLong_SHIFT = y + z (mod _PyHASH_MODULUS). |
| 2998 | |
| 2999 | The right-hand side is just the result of rotating the |
| 3000 | _PyHASH_BITS bits of x left by PyLong_SHIFT places; since |
| 3001 | not all _PyHASH_BITS bits of x are 1s, the same is true |
| 3002 | after rotation, so 0 <= y+z < _PyHASH_MODULUS and y + z is |
| 3003 | the reduction of x*2**PyLong_SHIFT modulo |
| 3004 | _PyHASH_MODULUS. */ |
| 3005 | x = ((x << PyLong_SHIFT) & _PyHASH_MODULUS) | |
| 3006 | (x >> (_PyHASH_BITS - PyLong_SHIFT)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3007 | x += v->ob_digit[i]; |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 3008 | if (x >= _PyHASH_MODULUS) |
| 3009 | x -= _PyHASH_MODULUS; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3010 | } |
| 3011 | x = x * sign; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 3012 | if (x == (Py_uhash_t)-1) |
| 3013 | x = (Py_uhash_t)-2; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3014 | return (Py_hash_t)x; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 3015 | } |
| 3016 | |
| 3017 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 3018 | /* Add the absolute values of two integers. */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3019 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3020 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3021 | x_add(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3022 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3023 | 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] | 3024 | PyLongObject *z; |
| 3025 | Py_ssize_t i; |
| 3026 | digit carry = 0; |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 3027 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3028 | /* Ensure a is the larger of the two: */ |
| 3029 | if (size_a < size_b) { |
| 3030 | { PyLongObject *temp = a; a = b; b = temp; } |
| 3031 | { Py_ssize_t size_temp = size_a; |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3032 | size_a = size_b; |
| 3033 | size_b = size_temp; } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3034 | } |
| 3035 | z = _PyLong_New(size_a+1); |
| 3036 | if (z == NULL) |
| 3037 | return NULL; |
| 3038 | for (i = 0; i < size_b; ++i) { |
| 3039 | carry += a->ob_digit[i] + b->ob_digit[i]; |
| 3040 | z->ob_digit[i] = carry & PyLong_MASK; |
| 3041 | carry >>= PyLong_SHIFT; |
| 3042 | } |
| 3043 | for (; i < size_a; ++i) { |
| 3044 | carry += a->ob_digit[i]; |
| 3045 | z->ob_digit[i] = carry & PyLong_MASK; |
| 3046 | carry >>= PyLong_SHIFT; |
| 3047 | } |
| 3048 | z->ob_digit[i] = carry; |
| 3049 | return long_normalize(z); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3050 | } |
| 3051 | |
| 3052 | /* Subtract the absolute values of two integers. */ |
| 3053 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3054 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3055 | x_sub(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3056 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3057 | 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] | 3058 | PyLongObject *z; |
| 3059 | Py_ssize_t i; |
| 3060 | int sign = 1; |
| 3061 | digit borrow = 0; |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 3062 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3063 | /* Ensure a is the larger of the two: */ |
| 3064 | if (size_a < size_b) { |
| 3065 | sign = -1; |
| 3066 | { PyLongObject *temp = a; a = b; b = temp; } |
| 3067 | { Py_ssize_t size_temp = size_a; |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3068 | size_a = size_b; |
| 3069 | size_b = size_temp; } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3070 | } |
| 3071 | else if (size_a == size_b) { |
| 3072 | /* Find highest digit where a and b differ: */ |
| 3073 | i = size_a; |
| 3074 | while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) |
| 3075 | ; |
| 3076 | if (i < 0) |
| 3077 | return (PyLongObject *)PyLong_FromLong(0); |
| 3078 | if (a->ob_digit[i] < b->ob_digit[i]) { |
| 3079 | sign = -1; |
| 3080 | { PyLongObject *temp = a; a = b; b = temp; } |
| 3081 | } |
| 3082 | size_a = size_b = i+1; |
| 3083 | } |
| 3084 | z = _PyLong_New(size_a); |
| 3085 | if (z == NULL) |
| 3086 | return NULL; |
| 3087 | for (i = 0; i < size_b; ++i) { |
| 3088 | /* The following assumes unsigned arithmetic |
| 3089 | works module 2**N for some N>PyLong_SHIFT. */ |
| 3090 | borrow = a->ob_digit[i] - b->ob_digit[i] - borrow; |
| 3091 | z->ob_digit[i] = borrow & PyLong_MASK; |
| 3092 | borrow >>= PyLong_SHIFT; |
| 3093 | borrow &= 1; /* Keep only one sign bit */ |
| 3094 | } |
| 3095 | for (; i < size_a; ++i) { |
| 3096 | borrow = a->ob_digit[i] - borrow; |
| 3097 | z->ob_digit[i] = borrow & PyLong_MASK; |
| 3098 | borrow >>= PyLong_SHIFT; |
| 3099 | borrow &= 1; /* Keep only one sign bit */ |
| 3100 | } |
| 3101 | assert(borrow == 0); |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 3102 | if (sign < 0) { |
Victor Stinner | 8bcf312 | 2016-08-17 19:48:33 +0200 | [diff] [blame] | 3103 | Py_SIZE(z) = -Py_SIZE(z); |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 3104 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3105 | return long_normalize(z); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3106 | } |
| 3107 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3108 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3109 | long_add(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3110 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3111 | PyLongObject *z; |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 3112 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3113 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3114 | |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3115 | if (Py_ABS(Py_SIZE(a)) <= 1 && Py_ABS(Py_SIZE(b)) <= 1) { |
Mark Dickinson | c1c4a64 | 2016-09-17 20:01:56 +0100 | [diff] [blame] | 3116 | return PyLong_FromLong(MEDIUM_VALUE(a) + MEDIUM_VALUE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3117 | } |
| 3118 | if (Py_SIZE(a) < 0) { |
| 3119 | if (Py_SIZE(b) < 0) { |
| 3120 | z = x_add(a, b); |
Serhiy Storchaka | e63e5d6 | 2016-06-04 00:06:45 +0300 | [diff] [blame] | 3121 | if (z != NULL) { |
| 3122 | /* x_add received at least one multiple-digit int, |
| 3123 | and thus z must be a multiple-digit int. |
| 3124 | That also means z is not an element of |
| 3125 | small_ints, so negating it in-place is safe. */ |
| 3126 | assert(Py_REFCNT(z) == 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3127 | Py_SIZE(z) = -(Py_SIZE(z)); |
Serhiy Storchaka | e63e5d6 | 2016-06-04 00:06:45 +0300 | [diff] [blame] | 3128 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3129 | } |
| 3130 | else |
| 3131 | z = x_sub(b, a); |
| 3132 | } |
| 3133 | else { |
| 3134 | if (Py_SIZE(b) < 0) |
| 3135 | z = x_sub(a, b); |
| 3136 | else |
| 3137 | z = x_add(a, b); |
| 3138 | } |
| 3139 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3140 | } |
| 3141 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3142 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3143 | long_sub(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3144 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3145 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3146 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3147 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3148 | |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3149 | if (Py_ABS(Py_SIZE(a)) <= 1 && Py_ABS(Py_SIZE(b)) <= 1) { |
Mark Dickinson | c1c4a64 | 2016-09-17 20:01:56 +0100 | [diff] [blame] | 3150 | return PyLong_FromLong(MEDIUM_VALUE(a) - MEDIUM_VALUE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3151 | } |
| 3152 | if (Py_SIZE(a) < 0) { |
| 3153 | if (Py_SIZE(b) < 0) |
| 3154 | z = x_sub(a, b); |
| 3155 | else |
| 3156 | z = x_add(a, b); |
Serhiy Storchaka | e63e5d6 | 2016-06-04 00:06:45 +0300 | [diff] [blame] | 3157 | if (z != NULL) { |
| 3158 | assert(Py_SIZE(z) == 0 || Py_REFCNT(z) == 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3159 | Py_SIZE(z) = -(Py_SIZE(z)); |
Serhiy Storchaka | e63e5d6 | 2016-06-04 00:06:45 +0300 | [diff] [blame] | 3160 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3161 | } |
| 3162 | else { |
| 3163 | if (Py_SIZE(b) < 0) |
| 3164 | z = x_add(a, b); |
| 3165 | else |
| 3166 | z = x_sub(a, b); |
| 3167 | } |
| 3168 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3169 | } |
| 3170 | |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3171 | /* Grade school multiplication, ignoring the signs. |
| 3172 | * Returns the absolute value of the product, or NULL if error. |
| 3173 | */ |
| 3174 | static PyLongObject * |
| 3175 | x_mul(PyLongObject *a, PyLongObject *b) |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3176 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3177 | PyLongObject *z; |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3178 | Py_ssize_t size_a = Py_ABS(Py_SIZE(a)); |
| 3179 | Py_ssize_t size_b = Py_ABS(Py_SIZE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3180 | Py_ssize_t i; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3181 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3182 | z = _PyLong_New(size_a + size_b); |
| 3183 | if (z == NULL) |
| 3184 | return NULL; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3185 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3186 | memset(z->ob_digit, 0, Py_SIZE(z) * sizeof(digit)); |
| 3187 | if (a == b) { |
| 3188 | /* Efficient squaring per HAC, Algorithm 14.16: |
| 3189 | * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf |
| 3190 | * Gives slightly less than a 2x speedup when a == b, |
| 3191 | * via exploiting that each entry in the multiplication |
| 3192 | * pyramid appears twice (except for the size_a squares). |
| 3193 | */ |
| 3194 | for (i = 0; i < size_a; ++i) { |
| 3195 | twodigits carry; |
| 3196 | twodigits f = a->ob_digit[i]; |
| 3197 | digit *pz = z->ob_digit + (i << 1); |
| 3198 | digit *pa = a->ob_digit + i + 1; |
| 3199 | digit *paend = a->ob_digit + size_a; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3200 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3201 | SIGCHECK({ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3202 | Py_DECREF(z); |
| 3203 | return NULL; |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 3204 | }); |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 3205 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3206 | carry = *pz + f * f; |
| 3207 | *pz++ = (digit)(carry & PyLong_MASK); |
| 3208 | carry >>= PyLong_SHIFT; |
| 3209 | assert(carry <= PyLong_MASK); |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 3210 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3211 | /* Now f is added in twice in each column of the |
| 3212 | * pyramid it appears. Same as adding f<<1 once. |
| 3213 | */ |
| 3214 | f <<= 1; |
| 3215 | while (pa < paend) { |
| 3216 | carry += *pz + *pa++ * f; |
| 3217 | *pz++ = (digit)(carry & PyLong_MASK); |
| 3218 | carry >>= PyLong_SHIFT; |
| 3219 | assert(carry <= (PyLong_MASK << 1)); |
| 3220 | } |
| 3221 | if (carry) { |
| 3222 | carry += *pz; |
| 3223 | *pz++ = (digit)(carry & PyLong_MASK); |
| 3224 | carry >>= PyLong_SHIFT; |
| 3225 | } |
| 3226 | if (carry) |
| 3227 | *pz += (digit)(carry & PyLong_MASK); |
| 3228 | assert((carry >> PyLong_SHIFT) == 0); |
| 3229 | } |
| 3230 | } |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 3231 | else { /* a is not the same as b -- gradeschool int mult */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3232 | for (i = 0; i < size_a; ++i) { |
| 3233 | twodigits carry = 0; |
| 3234 | twodigits f = a->ob_digit[i]; |
| 3235 | digit *pz = z->ob_digit + i; |
| 3236 | digit *pb = b->ob_digit; |
| 3237 | digit *pbend = b->ob_digit + size_b; |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 3238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3239 | SIGCHECK({ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3240 | Py_DECREF(z); |
| 3241 | return NULL; |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 3242 | }); |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 3243 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3244 | while (pb < pbend) { |
| 3245 | carry += *pz + *pb++ * f; |
| 3246 | *pz++ = (digit)(carry & PyLong_MASK); |
| 3247 | carry >>= PyLong_SHIFT; |
| 3248 | assert(carry <= PyLong_MASK); |
| 3249 | } |
| 3250 | if (carry) |
| 3251 | *pz += (digit)(carry & PyLong_MASK); |
| 3252 | assert((carry >> PyLong_SHIFT) == 0); |
| 3253 | } |
| 3254 | } |
| 3255 | return long_normalize(z); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3256 | } |
| 3257 | |
| 3258 | /* A helper for Karatsuba multiplication (k_mul). |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 3259 | Takes an int "n" and an integer "size" representing the place to |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3260 | split, and sets low and high such that abs(n) == (high << size) + low, |
| 3261 | viewing the shift as being by digits. The sign bit is ignored, and |
| 3262 | the return values are >= 0. |
| 3263 | Returns 0 on success, -1 on failure. |
| 3264 | */ |
| 3265 | static int |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3266 | kmul_split(PyLongObject *n, |
| 3267 | Py_ssize_t size, |
| 3268 | PyLongObject **high, |
| 3269 | PyLongObject **low) |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3270 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3271 | PyLongObject *hi, *lo; |
| 3272 | Py_ssize_t size_lo, size_hi; |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3273 | const Py_ssize_t size_n = Py_ABS(Py_SIZE(n)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3274 | |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 3275 | size_lo = Py_MIN(size_n, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3276 | size_hi = size_n - size_lo; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3277 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3278 | if ((hi = _PyLong_New(size_hi)) == NULL) |
| 3279 | return -1; |
| 3280 | if ((lo = _PyLong_New(size_lo)) == NULL) { |
| 3281 | Py_DECREF(hi); |
| 3282 | return -1; |
| 3283 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3285 | memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit)); |
| 3286 | 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] | 3287 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3288 | *high = long_normalize(hi); |
| 3289 | *low = long_normalize(lo); |
| 3290 | return 0; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3291 | } |
| 3292 | |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3293 | static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b); |
| 3294 | |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3295 | /* Karatsuba multiplication. Ignores the input signs, and returns the |
| 3296 | * absolute value of the product (or NULL if error). |
| 3297 | * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295). |
| 3298 | */ |
| 3299 | static PyLongObject * |
| 3300 | k_mul(PyLongObject *a, PyLongObject *b) |
| 3301 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3302 | Py_ssize_t asize = Py_ABS(Py_SIZE(a)); |
| 3303 | Py_ssize_t bsize = Py_ABS(Py_SIZE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3304 | PyLongObject *ah = NULL; |
| 3305 | PyLongObject *al = NULL; |
| 3306 | PyLongObject *bh = NULL; |
| 3307 | PyLongObject *bl = NULL; |
| 3308 | PyLongObject *ret = NULL; |
| 3309 | PyLongObject *t1, *t2, *t3; |
| 3310 | Py_ssize_t shift; /* the number of digits we split off */ |
| 3311 | Py_ssize_t i; |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 3312 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3313 | /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl |
| 3314 | * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl |
| 3315 | * Then the original product is |
| 3316 | * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl |
| 3317 | * By picking X to be a power of 2, "*X" is just shifting, and it's |
| 3318 | * been reduced to 3 multiplies on numbers half the size. |
| 3319 | */ |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3320 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3321 | /* We want to split based on the larger number; fiddle so that b |
| 3322 | * is largest. |
| 3323 | */ |
| 3324 | if (asize > bsize) { |
| 3325 | t1 = a; |
| 3326 | a = b; |
| 3327 | b = t1; |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 3328 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3329 | i = asize; |
| 3330 | asize = bsize; |
| 3331 | bsize = i; |
| 3332 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3333 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3334 | /* Use gradeschool math when either number is too small. */ |
| 3335 | i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF; |
| 3336 | if (asize <= i) { |
| 3337 | if (asize == 0) |
| 3338 | return (PyLongObject *)PyLong_FromLong(0); |
| 3339 | else |
| 3340 | return x_mul(a, b); |
| 3341 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3342 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3343 | /* If a is small compared to b, splitting on b gives a degenerate |
| 3344 | * case with ah==0, and Karatsuba may be (even much) less efficient |
| 3345 | * than "grade school" then. However, we can still win, by viewing |
| 3346 | * b as a string of "big digits", each of width a->ob_size. That |
| 3347 | * leads to a sequence of balanced calls to k_mul. |
| 3348 | */ |
| 3349 | if (2 * asize <= bsize) |
| 3350 | return k_lopsided_mul(a, b); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3351 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3352 | /* Split a & b into hi & lo pieces. */ |
| 3353 | shift = bsize >> 1; |
| 3354 | if (kmul_split(a, shift, &ah, &al) < 0) goto fail; |
| 3355 | assert(Py_SIZE(ah) > 0); /* the split isn't degenerate */ |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3356 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3357 | if (a == b) { |
| 3358 | bh = ah; |
| 3359 | bl = al; |
| 3360 | Py_INCREF(bh); |
| 3361 | Py_INCREF(bl); |
| 3362 | } |
| 3363 | else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3364 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3365 | /* The plan: |
| 3366 | * 1. Allocate result space (asize + bsize digits: that's always |
| 3367 | * enough). |
| 3368 | * 2. Compute ah*bh, and copy into result at 2*shift. |
| 3369 | * 3. Compute al*bl, and copy into result at 0. Note that this |
| 3370 | * can't overlap with #2. |
| 3371 | * 4. Subtract al*bl from the result, starting at shift. This may |
| 3372 | * underflow (borrow out of the high digit), but we don't care: |
| 3373 | * we're effectively doing unsigned arithmetic mod |
| 3374 | * BASE**(sizea + sizeb), and so long as the *final* result fits, |
| 3375 | * borrows and carries out of the high digit can be ignored. |
| 3376 | * 5. Subtract ah*bh from the result, starting at shift. |
| 3377 | * 6. Compute (ah+al)*(bh+bl), and add it into the result starting |
| 3378 | * at shift. |
| 3379 | */ |
Tim Peters | d64c1de | 2002-08-12 17:36:03 +0000 | [diff] [blame] | 3380 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3381 | /* 1. Allocate result space. */ |
| 3382 | ret = _PyLong_New(asize + bsize); |
| 3383 | if (ret == NULL) goto fail; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3384 | #ifdef Py_DEBUG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3385 | /* Fill with trash, to catch reference to uninitialized digits. */ |
| 3386 | memset(ret->ob_digit, 0xDF, Py_SIZE(ret) * sizeof(digit)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3387 | #endif |
Tim Peters | 44121a6 | 2002-08-12 06:17:58 +0000 | [diff] [blame] | 3388 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3389 | /* 2. t1 <- ah*bh, and copy into high digits of result. */ |
| 3390 | if ((t1 = k_mul(ah, bh)) == NULL) goto fail; |
| 3391 | assert(Py_SIZE(t1) >= 0); |
| 3392 | assert(2*shift + Py_SIZE(t1) <= Py_SIZE(ret)); |
| 3393 | memcpy(ret->ob_digit + 2*shift, t1->ob_digit, |
| 3394 | Py_SIZE(t1) * sizeof(digit)); |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 3395 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3396 | /* Zero-out the digits higher than the ah*bh copy. */ |
| 3397 | i = Py_SIZE(ret) - 2*shift - Py_SIZE(t1); |
| 3398 | if (i) |
| 3399 | memset(ret->ob_digit + 2*shift + Py_SIZE(t1), 0, |
| 3400 | i * sizeof(digit)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3401 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3402 | /* 3. t2 <- al*bl, and copy into the low digits. */ |
| 3403 | if ((t2 = k_mul(al, bl)) == NULL) { |
| 3404 | Py_DECREF(t1); |
| 3405 | goto fail; |
| 3406 | } |
| 3407 | assert(Py_SIZE(t2) >= 0); |
| 3408 | assert(Py_SIZE(t2) <= 2*shift); /* no overlap with high digits */ |
| 3409 | memcpy(ret->ob_digit, t2->ob_digit, Py_SIZE(t2) * sizeof(digit)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3410 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3411 | /* Zero out remaining digits. */ |
| 3412 | i = 2*shift - Py_SIZE(t2); /* number of uninitialized digits */ |
| 3413 | if (i) |
| 3414 | memset(ret->ob_digit + Py_SIZE(t2), 0, i * sizeof(digit)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3415 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3416 | /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first |
| 3417 | * because it's fresher in cache. |
| 3418 | */ |
| 3419 | i = Py_SIZE(ret) - shift; /* # digits after shift */ |
| 3420 | (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, Py_SIZE(t2)); |
| 3421 | Py_DECREF(t2); |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 3422 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3423 | (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, Py_SIZE(t1)); |
| 3424 | Py_DECREF(t1); |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 3425 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3426 | /* 6. t3 <- (ah+al)(bh+bl), and add into result. */ |
| 3427 | if ((t1 = x_add(ah, al)) == NULL) goto fail; |
| 3428 | Py_DECREF(ah); |
| 3429 | Py_DECREF(al); |
| 3430 | ah = al = NULL; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3431 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3432 | if (a == b) { |
| 3433 | t2 = t1; |
| 3434 | Py_INCREF(t2); |
| 3435 | } |
| 3436 | else if ((t2 = x_add(bh, bl)) == NULL) { |
| 3437 | Py_DECREF(t1); |
| 3438 | goto fail; |
| 3439 | } |
| 3440 | Py_DECREF(bh); |
| 3441 | Py_DECREF(bl); |
| 3442 | bh = bl = NULL; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3443 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3444 | t3 = k_mul(t1, t2); |
| 3445 | Py_DECREF(t1); |
| 3446 | Py_DECREF(t2); |
| 3447 | if (t3 == NULL) goto fail; |
| 3448 | assert(Py_SIZE(t3) >= 0); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3449 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3450 | /* Add t3. It's not obvious why we can't run out of room here. |
| 3451 | * See the (*) comment after this function. |
| 3452 | */ |
| 3453 | (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, Py_SIZE(t3)); |
| 3454 | Py_DECREF(t3); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3455 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3456 | return long_normalize(ret); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3457 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3458 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3459 | Py_XDECREF(ret); |
| 3460 | Py_XDECREF(ah); |
| 3461 | Py_XDECREF(al); |
| 3462 | Py_XDECREF(bh); |
| 3463 | Py_XDECREF(bl); |
| 3464 | return NULL; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3465 | } |
| 3466 | |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3467 | /* (*) Why adding t3 can't "run out of room" above. |
| 3468 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3469 | Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts |
| 3470 | to start with: |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3471 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3472 | 1. For any integer i, i = c(i/2) + f(i/2). In particular, |
| 3473 | bsize = c(bsize/2) + f(bsize/2). |
| 3474 | 2. shift = f(bsize/2) |
| 3475 | 3. asize <= bsize |
| 3476 | 4. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this |
| 3477 | routine, so asize > bsize/2 >= f(bsize/2) in this routine. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3478 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3479 | We allocated asize + bsize result digits, and add t3 into them at an offset |
| 3480 | of shift. This leaves asize+bsize-shift allocated digit positions for t3 |
| 3481 | to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) = |
| 3482 | asize + c(bsize/2) available digit positions. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3483 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3484 | bh has c(bsize/2) digits, and bl at most f(size/2) digits. So bh+hl has |
| 3485 | at most c(bsize/2) digits + 1 bit. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3486 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3487 | If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2) |
| 3488 | digits, and al has at most f(bsize/2) digits in any case. So ah+al has at |
| 3489 | most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3490 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3491 | The product (ah+al)*(bh+bl) therefore has at most |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3492 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3493 | 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] | 3494 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3495 | and we have asize + c(bsize/2) available digit positions. We need to show |
| 3496 | this is always enough. An instance of c(bsize/2) cancels out in both, so |
| 3497 | the question reduces to whether asize digits is enough to hold |
| 3498 | (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits. If asize < bsize, |
| 3499 | then we're asking whether asize digits >= f(bsize/2) digits + 2 bits. By #4, |
| 3500 | 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] | 3501 | 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] | 3502 | 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] | 3503 | c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits |
| 3504 | is enough to hold 2 bits. This is so if bsize >= 2, which holds because |
| 3505 | bsize >= KARATSUBA_CUTOFF >= 2. |
Tim Peters | 8e966ee | 2002-08-14 16:36:23 +0000 | [diff] [blame] | 3506 | |
Tim Peters | 48d52c0 | 2002-08-14 17:07:32 +0000 | [diff] [blame] | 3507 | Note that since there's always enough room for (ah+al)*(bh+bl), and that's |
| 3508 | clearly >= each of ah*bh and al*bl, there's always enough room to subtract |
| 3509 | ah*bh and al*bl too. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3510 | */ |
| 3511 | |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3512 | /* b has at least twice the digits of a, and a is big enough that Karatsuba |
| 3513 | * would pay off *if* the inputs had balanced sizes. View b as a sequence |
| 3514 | * of slices, each with a->ob_size digits, and multiply the slices by a, |
| 3515 | * one at a time. This gives k_mul balanced inputs to work with, and is |
| 3516 | * also cache-friendly (we compute one double-width slice of the result |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 3517 | * at a time, then move on, never backtracking except for the helpful |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3518 | * single-width slice overlap between successive partial sums). |
| 3519 | */ |
| 3520 | static PyLongObject * |
| 3521 | k_lopsided_mul(PyLongObject *a, PyLongObject *b) |
| 3522 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3523 | const Py_ssize_t asize = Py_ABS(Py_SIZE(a)); |
| 3524 | Py_ssize_t bsize = Py_ABS(Py_SIZE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3525 | Py_ssize_t nbdone; /* # of b digits already multiplied */ |
| 3526 | PyLongObject *ret; |
| 3527 | PyLongObject *bslice = NULL; |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3528 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3529 | assert(asize > KARATSUBA_CUTOFF); |
| 3530 | assert(2 * asize <= bsize); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3531 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3532 | /* Allocate result space, and zero it out. */ |
| 3533 | ret = _PyLong_New(asize + bsize); |
| 3534 | if (ret == NULL) |
| 3535 | return NULL; |
| 3536 | memset(ret->ob_digit, 0, Py_SIZE(ret) * sizeof(digit)); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3537 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3538 | /* Successive slices of b are copied into bslice. */ |
| 3539 | bslice = _PyLong_New(asize); |
| 3540 | if (bslice == NULL) |
| 3541 | goto fail; |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3542 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3543 | nbdone = 0; |
| 3544 | while (bsize > 0) { |
| 3545 | PyLongObject *product; |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 3546 | const Py_ssize_t nbtouse = Py_MIN(bsize, asize); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3547 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3548 | /* Multiply the next slice of b by a. */ |
| 3549 | memcpy(bslice->ob_digit, b->ob_digit + nbdone, |
| 3550 | nbtouse * sizeof(digit)); |
| 3551 | Py_SIZE(bslice) = nbtouse; |
| 3552 | product = k_mul(a, bslice); |
| 3553 | if (product == NULL) |
| 3554 | goto fail; |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3555 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3556 | /* Add into result. */ |
| 3557 | (void)v_iadd(ret->ob_digit + nbdone, Py_SIZE(ret) - nbdone, |
| 3558 | product->ob_digit, Py_SIZE(product)); |
| 3559 | Py_DECREF(product); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3560 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3561 | bsize -= nbtouse; |
| 3562 | nbdone += nbtouse; |
| 3563 | } |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3564 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3565 | Py_DECREF(bslice); |
| 3566 | return long_normalize(ret); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3567 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3568 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3569 | Py_DECREF(ret); |
| 3570 | Py_XDECREF(bslice); |
| 3571 | return NULL; |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3572 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3573 | |
| 3574 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3575 | long_mul(PyLongObject *a, PyLongObject *b) |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3576 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3577 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3578 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3579 | CHECK_BINOP(a, b); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3580 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3581 | /* fast path for single-digit multiplication */ |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3582 | 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] | 3583 | stwodigits v = (stwodigits)(MEDIUM_VALUE(a)) * MEDIUM_VALUE(b); |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 3584 | return PyLong_FromLongLong((long long)v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3585 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3586 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3587 | z = k_mul(a, b); |
| 3588 | /* Negate if exactly one of the inputs is negative. */ |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 3589 | if (((Py_SIZE(a) ^ Py_SIZE(b)) < 0) && z) { |
| 3590 | _PyLong_Negate(&z); |
| 3591 | if (z == NULL) |
| 3592 | return NULL; |
| 3593 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3594 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3595 | } |
| 3596 | |
Yury Selivanov | e0b2309 | 2016-02-11 10:26:27 -0500 | [diff] [blame] | 3597 | /* Fast modulo division for single-digit longs. */ |
| 3598 | static PyObject * |
| 3599 | fast_mod(PyLongObject *a, PyLongObject *b) |
| 3600 | { |
| 3601 | sdigit left = a->ob_digit[0]; |
| 3602 | sdigit right = b->ob_digit[0]; |
| 3603 | sdigit mod; |
| 3604 | |
| 3605 | assert(Py_ABS(Py_SIZE(a)) == 1); |
| 3606 | assert(Py_ABS(Py_SIZE(b)) == 1); |
| 3607 | |
| 3608 | if (Py_SIZE(a) == Py_SIZE(b)) { |
| 3609 | /* 'a' and 'b' have the same sign. */ |
| 3610 | mod = left % right; |
| 3611 | } |
| 3612 | else { |
| 3613 | /* Either 'a' or 'b' is negative. */ |
| 3614 | mod = right - 1 - (left - 1) % right; |
| 3615 | } |
| 3616 | |
Victor Stinner | f963c13 | 2016-03-23 18:36:54 +0100 | [diff] [blame] | 3617 | return PyLong_FromLong(mod * (sdigit)Py_SIZE(b)); |
Yury Selivanov | e0b2309 | 2016-02-11 10:26:27 -0500 | [diff] [blame] | 3618 | } |
| 3619 | |
| 3620 | /* Fast floor division for single-digit longs. */ |
| 3621 | static PyObject * |
| 3622 | fast_floor_div(PyLongObject *a, PyLongObject *b) |
| 3623 | { |
| 3624 | sdigit left = a->ob_digit[0]; |
| 3625 | sdigit right = b->ob_digit[0]; |
| 3626 | sdigit div; |
| 3627 | |
| 3628 | assert(Py_ABS(Py_SIZE(a)) == 1); |
| 3629 | assert(Py_ABS(Py_SIZE(b)) == 1); |
| 3630 | |
| 3631 | if (Py_SIZE(a) == Py_SIZE(b)) { |
| 3632 | /* 'a' and 'b' have the same sign. */ |
| 3633 | div = left / right; |
| 3634 | } |
| 3635 | else { |
| 3636 | /* Either 'a' or 'b' is negative. */ |
| 3637 | div = -1 - (left - 1) / right; |
| 3638 | } |
| 3639 | |
| 3640 | return PyLong_FromLong(div); |
| 3641 | } |
| 3642 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3643 | /* The / and % operators are now defined in terms of divmod(). |
| 3644 | The expression a mod b has the value a - b*floor(a/b). |
| 3645 | The long_divrem function gives the remainder after division of |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3646 | |a| by |b|, with the sign of a. This is also expressed |
| 3647 | as a - b*trunc(a/b), if trunc truncates towards zero. |
| 3648 | Some examples: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3649 | a b a rem b a mod b |
| 3650 | 13 10 3 3 |
| 3651 | -13 10 -3 7 |
| 3652 | 13 -10 3 -7 |
| 3653 | -13 -10 -3 -3 |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3654 | 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] | 3655 | have different signs. We then subtract one from the 'div' |
| 3656 | part of the outcome to keep the invariant intact. */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3657 | |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3658 | /* Compute |
| 3659 | * *pdiv, *pmod = divmod(v, w) |
| 3660 | * NULL can be passed for pdiv or pmod, in which case that part of |
| 3661 | * the result is simply thrown away. The caller owns a reference to |
| 3662 | * each of these it requests (does not pass NULL for). |
| 3663 | */ |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3664 | static int |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3665 | l_divmod(PyLongObject *v, PyLongObject *w, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3666 | PyLongObject **pdiv, PyLongObject **pmod) |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3667 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3668 | PyLongObject *div, *mod; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3669 | |
Yury Selivanov | e0b2309 | 2016-02-11 10:26:27 -0500 | [diff] [blame] | 3670 | if (Py_ABS(Py_SIZE(v)) == 1 && Py_ABS(Py_SIZE(w)) == 1) { |
| 3671 | /* Fast path for single-digit longs */ |
| 3672 | div = NULL; |
| 3673 | if (pdiv != NULL) { |
| 3674 | div = (PyLongObject *)fast_floor_div(v, w); |
| 3675 | if (div == NULL) { |
| 3676 | return -1; |
| 3677 | } |
| 3678 | } |
| 3679 | if (pmod != NULL) { |
| 3680 | mod = (PyLongObject *)fast_mod(v, w); |
| 3681 | if (mod == NULL) { |
| 3682 | Py_XDECREF(div); |
| 3683 | return -1; |
| 3684 | } |
| 3685 | *pmod = mod; |
| 3686 | } |
| 3687 | if (pdiv != NULL) { |
| 3688 | /* We only want to set `*pdiv` when `*pmod` is |
| 3689 | set successfully. */ |
| 3690 | *pdiv = div; |
| 3691 | } |
| 3692 | return 0; |
| 3693 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3694 | if (long_divrem(v, w, &div, &mod) < 0) |
| 3695 | return -1; |
| 3696 | if ((Py_SIZE(mod) < 0 && Py_SIZE(w) > 0) || |
| 3697 | (Py_SIZE(mod) > 0 && Py_SIZE(w) < 0)) { |
| 3698 | PyLongObject *temp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3699 | temp = (PyLongObject *) long_add(mod, w); |
| 3700 | Py_DECREF(mod); |
| 3701 | mod = temp; |
| 3702 | if (mod == NULL) { |
| 3703 | Py_DECREF(div); |
| 3704 | return -1; |
| 3705 | } |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 3706 | temp = (PyLongObject *) long_sub(div, (PyLongObject *)_PyLong_One); |
| 3707 | if (temp == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3708 | Py_DECREF(mod); |
| 3709 | Py_DECREF(div); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3710 | return -1; |
| 3711 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3712 | Py_DECREF(div); |
| 3713 | div = temp; |
| 3714 | } |
| 3715 | if (pdiv != NULL) |
| 3716 | *pdiv = div; |
| 3717 | else |
| 3718 | Py_DECREF(div); |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3719 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3720 | if (pmod != NULL) |
| 3721 | *pmod = mod; |
| 3722 | else |
| 3723 | Py_DECREF(mod); |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3724 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3725 | return 0; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3726 | } |
| 3727 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3728 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3729 | long_div(PyObject *a, PyObject *b) |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3730 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3731 | PyLongObject *div; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3732 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3733 | CHECK_BINOP(a, b); |
Yury Selivanov | e0b2309 | 2016-02-11 10:26:27 -0500 | [diff] [blame] | 3734 | |
| 3735 | if (Py_ABS(Py_SIZE(a)) == 1 && Py_ABS(Py_SIZE(b)) == 1) { |
| 3736 | return fast_floor_div((PyLongObject*)a, (PyLongObject*)b); |
| 3737 | } |
| 3738 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3739 | if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, NULL) < 0) |
| 3740 | div = NULL; |
| 3741 | return (PyObject *)div; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3742 | } |
| 3743 | |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3744 | /* PyLong/PyLong -> float, with correctly rounded result. */ |
| 3745 | |
| 3746 | #define MANT_DIG_DIGITS (DBL_MANT_DIG / PyLong_SHIFT) |
| 3747 | #define MANT_DIG_BITS (DBL_MANT_DIG % PyLong_SHIFT) |
| 3748 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3749 | static PyObject * |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3750 | long_true_divide(PyObject *v, PyObject *w) |
Tim Peters | 20dab9f | 2001-09-04 05:31:47 +0000 | [diff] [blame] | 3751 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3752 | PyLongObject *a, *b, *x; |
| 3753 | Py_ssize_t a_size, b_size, shift, extra_bits, diff, x_size, x_bits; |
| 3754 | digit mask, low; |
| 3755 | int inexact, negate, a_is_small, b_is_small; |
| 3756 | double dx, result; |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 3757 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3758 | CHECK_BINOP(v, w); |
| 3759 | a = (PyLongObject *)v; |
| 3760 | b = (PyLongObject *)w; |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 3761 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3762 | /* |
| 3763 | Method in a nutshell: |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3764 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3765 | 0. reduce to case a, b > 0; filter out obvious underflow/overflow |
| 3766 | 1. choose a suitable integer 'shift' |
| 3767 | 2. use integer arithmetic to compute x = floor(2**-shift*a/b) |
| 3768 | 3. adjust x for correct rounding |
| 3769 | 4. convert x to a double dx with the same value |
| 3770 | 5. return ldexp(dx, shift). |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3771 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3772 | In more detail: |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3773 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3774 | 0. For any a, a/0 raises ZeroDivisionError; for nonzero b, 0/b |
| 3775 | returns either 0.0 or -0.0, depending on the sign of b. For a and |
| 3776 | b both nonzero, ignore signs of a and b, and add the sign back in |
| 3777 | at the end. Now write a_bits and b_bits for the bit lengths of a |
| 3778 | and b respectively (that is, a_bits = 1 + floor(log_2(a)); likewise |
| 3779 | for b). Then |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3780 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3781 | 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] | 3782 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3783 | So if a_bits - b_bits > DBL_MAX_EXP then a/b > 2**DBL_MAX_EXP and |
| 3784 | so overflows. Similarly, if a_bits - b_bits < DBL_MIN_EXP - |
| 3785 | DBL_MANT_DIG - 1 then a/b underflows to 0. With these cases out of |
| 3786 | the way, we can assume that |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3787 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3788 | 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] | 3789 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3790 | 1. The integer 'shift' is chosen so that x has the right number of |
| 3791 | bits for a double, plus two or three extra bits that will be used |
| 3792 | in the rounding decisions. Writing a_bits and b_bits for the |
| 3793 | number of significant bits in a and b respectively, a |
| 3794 | straightforward formula for shift is: |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3795 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3796 | shift = a_bits - b_bits - DBL_MANT_DIG - 2 |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3797 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3798 | This is fine in the usual case, but if a/b is smaller than the |
| 3799 | smallest normal float then it can lead to double rounding on an |
| 3800 | IEEE 754 platform, giving incorrectly rounded results. So we |
| 3801 | adjust the formula slightly. The actual formula used is: |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3802 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3803 | 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] | 3804 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3805 | 2. The quantity x is computed by first shifting a (left -shift bits |
| 3806 | if shift <= 0, right shift bits if shift > 0) and then dividing by |
| 3807 | b. For both the shift and the division, we keep track of whether |
| 3808 | the result is inexact, in a flag 'inexact'; this information is |
| 3809 | needed at the rounding stage. |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3810 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3811 | With the choice of shift above, together with our assumption that |
| 3812 | a_bits - b_bits >= DBL_MIN_EXP - DBL_MANT_DIG - 1, it follows |
| 3813 | that x >= 1. |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3814 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3815 | 3. Now x * 2**shift <= a/b < (x+1) * 2**shift. We want to replace |
| 3816 | this with an exactly representable float of the form |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3817 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3818 | round(x/2**extra_bits) * 2**(extra_bits+shift). |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3819 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3820 | For float representability, we need x/2**extra_bits < |
| 3821 | 2**DBL_MANT_DIG and extra_bits + shift >= DBL_MIN_EXP - |
| 3822 | DBL_MANT_DIG. This translates to the condition: |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3823 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3824 | extra_bits >= MAX(x_bits, DBL_MIN_EXP - shift) - DBL_MANT_DIG |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3825 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3826 | To round, we just modify the bottom digit of x in-place; this can |
| 3827 | end up giving a digit with value > PyLONG_MASK, but that's not a |
| 3828 | problem since digits can hold values up to 2*PyLONG_MASK+1. |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3829 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3830 | With the original choices for shift above, extra_bits will always |
| 3831 | be 2 or 3. Then rounding under the round-half-to-even rule, we |
| 3832 | round up iff the most significant of the extra bits is 1, and |
| 3833 | either: (a) the computation of x in step 2 had an inexact result, |
| 3834 | or (b) at least one other of the extra bits is 1, or (c) the least |
| 3835 | significant bit of x (above those to be rounded) is 1. |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3836 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3837 | 4. Conversion to a double is straightforward; all floating-point |
| 3838 | operations involved in the conversion are exact, so there's no |
| 3839 | danger of rounding errors. |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3840 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3841 | 5. Use ldexp(x, shift) to compute x*2**shift, the final result. |
| 3842 | The result will always be exactly representable as a double, except |
| 3843 | in the case that it overflows. To avoid dependence on the exact |
| 3844 | behaviour of ldexp on overflow, we check for overflow before |
| 3845 | applying ldexp. The result of ldexp is adjusted for sign before |
| 3846 | returning. |
| 3847 | */ |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3848 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3849 | /* Reduce to case where a and b are both positive. */ |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3850 | a_size = Py_ABS(Py_SIZE(a)); |
| 3851 | b_size = Py_ABS(Py_SIZE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3852 | negate = (Py_SIZE(a) < 0) ^ (Py_SIZE(b) < 0); |
| 3853 | if (b_size == 0) { |
| 3854 | PyErr_SetString(PyExc_ZeroDivisionError, |
| 3855 | "division by zero"); |
| 3856 | goto error; |
| 3857 | } |
| 3858 | if (a_size == 0) |
| 3859 | goto underflow_or_zero; |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3860 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3861 | /* Fast path for a and b small (exactly representable in a double). |
| 3862 | Relies on floating-point division being correctly rounded; results |
| 3863 | may be subject to double rounding on x86 machines that operate with |
| 3864 | the x87 FPU set to 64-bit precision. */ |
| 3865 | a_is_small = a_size <= MANT_DIG_DIGITS || |
| 3866 | (a_size == MANT_DIG_DIGITS+1 && |
| 3867 | a->ob_digit[MANT_DIG_DIGITS] >> MANT_DIG_BITS == 0); |
| 3868 | b_is_small = b_size <= MANT_DIG_DIGITS || |
| 3869 | (b_size == MANT_DIG_DIGITS+1 && |
| 3870 | b->ob_digit[MANT_DIG_DIGITS] >> MANT_DIG_BITS == 0); |
| 3871 | if (a_is_small && b_is_small) { |
| 3872 | double da, db; |
| 3873 | da = a->ob_digit[--a_size]; |
| 3874 | while (a_size > 0) |
| 3875 | da = da * PyLong_BASE + a->ob_digit[--a_size]; |
| 3876 | db = b->ob_digit[--b_size]; |
| 3877 | while (b_size > 0) |
| 3878 | db = db * PyLong_BASE + b->ob_digit[--b_size]; |
| 3879 | result = da / db; |
| 3880 | goto success; |
| 3881 | } |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 3882 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3883 | /* Catch obvious cases of underflow and overflow */ |
| 3884 | diff = a_size - b_size; |
| 3885 | if (diff > PY_SSIZE_T_MAX/PyLong_SHIFT - 1) |
| 3886 | /* Extreme overflow */ |
| 3887 | goto overflow; |
| 3888 | else if (diff < 1 - PY_SSIZE_T_MAX/PyLong_SHIFT) |
| 3889 | /* Extreme underflow */ |
| 3890 | goto underflow_or_zero; |
| 3891 | /* Next line is now safe from overflowing a Py_ssize_t */ |
| 3892 | diff = diff * PyLong_SHIFT + bits_in_digit(a->ob_digit[a_size - 1]) - |
| 3893 | bits_in_digit(b->ob_digit[b_size - 1]); |
| 3894 | /* Now diff = a_bits - b_bits. */ |
| 3895 | if (diff > DBL_MAX_EXP) |
| 3896 | goto overflow; |
| 3897 | else if (diff < DBL_MIN_EXP - DBL_MANT_DIG - 1) |
| 3898 | goto underflow_or_zero; |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 3899 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3900 | /* Choose value for shift; see comments for step 1 above. */ |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 3901 | shift = Py_MAX(diff, DBL_MIN_EXP) - DBL_MANT_DIG - 2; |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3902 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3903 | inexact = 0; |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3904 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3905 | /* x = abs(a * 2**-shift) */ |
| 3906 | if (shift <= 0) { |
| 3907 | Py_ssize_t i, shift_digits = -shift / PyLong_SHIFT; |
| 3908 | digit rem; |
| 3909 | /* x = a << -shift */ |
| 3910 | if (a_size >= PY_SSIZE_T_MAX - 1 - shift_digits) { |
| 3911 | /* In practice, it's probably impossible to end up |
| 3912 | here. Both a and b would have to be enormous, |
| 3913 | using close to SIZE_T_MAX bytes of memory each. */ |
| 3914 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3915 | "intermediate overflow during division"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3916 | goto error; |
| 3917 | } |
| 3918 | x = _PyLong_New(a_size + shift_digits + 1); |
| 3919 | if (x == NULL) |
| 3920 | goto error; |
| 3921 | for (i = 0; i < shift_digits; i++) |
| 3922 | x->ob_digit[i] = 0; |
| 3923 | rem = v_lshift(x->ob_digit + shift_digits, a->ob_digit, |
| 3924 | a_size, -shift % PyLong_SHIFT); |
| 3925 | x->ob_digit[a_size + shift_digits] = rem; |
| 3926 | } |
| 3927 | else { |
| 3928 | Py_ssize_t shift_digits = shift / PyLong_SHIFT; |
| 3929 | digit rem; |
| 3930 | /* x = a >> shift */ |
| 3931 | assert(a_size >= shift_digits); |
| 3932 | x = _PyLong_New(a_size - shift_digits); |
| 3933 | if (x == NULL) |
| 3934 | goto error; |
| 3935 | rem = v_rshift(x->ob_digit, a->ob_digit + shift_digits, |
| 3936 | a_size - shift_digits, shift % PyLong_SHIFT); |
| 3937 | /* set inexact if any of the bits shifted out is nonzero */ |
| 3938 | if (rem) |
| 3939 | inexact = 1; |
| 3940 | while (!inexact && shift_digits > 0) |
| 3941 | if (a->ob_digit[--shift_digits]) |
| 3942 | inexact = 1; |
| 3943 | } |
| 3944 | long_normalize(x); |
| 3945 | x_size = Py_SIZE(x); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3946 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3947 | /* x //= b. If the remainder is nonzero, set inexact. We own the only |
| 3948 | reference to x, so it's safe to modify it in-place. */ |
| 3949 | if (b_size == 1) { |
| 3950 | digit rem = inplace_divrem1(x->ob_digit, x->ob_digit, x_size, |
| 3951 | b->ob_digit[0]); |
| 3952 | long_normalize(x); |
| 3953 | if (rem) |
| 3954 | inexact = 1; |
| 3955 | } |
| 3956 | else { |
| 3957 | PyLongObject *div, *rem; |
| 3958 | div = x_divrem(x, b, &rem); |
| 3959 | Py_DECREF(x); |
| 3960 | x = div; |
| 3961 | if (x == NULL) |
| 3962 | goto error; |
| 3963 | if (Py_SIZE(rem)) |
| 3964 | inexact = 1; |
| 3965 | Py_DECREF(rem); |
| 3966 | } |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3967 | x_size = Py_ABS(Py_SIZE(x)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3968 | assert(x_size > 0); /* result of division is never zero */ |
| 3969 | 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] | 3970 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3971 | /* The number of extra bits that have to be rounded away. */ |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 3972 | 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] | 3973 | assert(extra_bits == 2 || extra_bits == 3); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3974 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3975 | /* Round by directly modifying the low digit of x. */ |
| 3976 | mask = (digit)1 << (extra_bits - 1); |
| 3977 | low = x->ob_digit[0] | inexact; |
Mark Dickinson | dc590a4 | 2016-08-21 10:23:23 +0100 | [diff] [blame] | 3978 | if ((low & mask) && (low & (3U*mask-1U))) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3979 | low += mask; |
Mark Dickinson | dc590a4 | 2016-08-21 10:23:23 +0100 | [diff] [blame] | 3980 | x->ob_digit[0] = low & ~(2U*mask-1U); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3981 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3982 | /* Convert x to a double dx; the conversion is exact. */ |
| 3983 | dx = x->ob_digit[--x_size]; |
| 3984 | while (x_size > 0) |
| 3985 | dx = dx * PyLong_BASE + x->ob_digit[--x_size]; |
| 3986 | Py_DECREF(x); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3987 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3988 | /* Check whether ldexp result will overflow a double. */ |
| 3989 | if (shift + x_bits >= DBL_MAX_EXP && |
| 3990 | (shift + x_bits > DBL_MAX_EXP || dx == ldexp(1.0, (int)x_bits))) |
| 3991 | goto overflow; |
| 3992 | result = ldexp(dx, (int)shift); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3993 | |
| 3994 | success: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3995 | return PyFloat_FromDouble(negate ? -result : result); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3996 | |
| 3997 | underflow_or_zero: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3998 | return PyFloat_FromDouble(negate ? -0.0 : 0.0); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3999 | |
| 4000 | overflow: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4001 | PyErr_SetString(PyExc_OverflowError, |
| 4002 | "integer division result too large for a float"); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 4003 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4004 | return NULL; |
Tim Peters | 20dab9f | 2001-09-04 05:31:47 +0000 | [diff] [blame] | 4005 | } |
| 4006 | |
| 4007 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 4008 | long_mod(PyObject *a, PyObject *b) |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 4009 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4010 | PyLongObject *mod; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 4011 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4012 | CHECK_BINOP(a, b); |
| 4013 | |
Yury Selivanov | e0b2309 | 2016-02-11 10:26:27 -0500 | [diff] [blame] | 4014 | if (Py_ABS(Py_SIZE(a)) == 1 && Py_ABS(Py_SIZE(b)) == 1) { |
| 4015 | return fast_mod((PyLongObject*)a, (PyLongObject*)b); |
| 4016 | } |
| 4017 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4018 | if (l_divmod((PyLongObject*)a, (PyLongObject*)b, NULL, &mod) < 0) |
| 4019 | mod = NULL; |
| 4020 | return (PyObject *)mod; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 4021 | } |
| 4022 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4023 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 4024 | long_divmod(PyObject *a, PyObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4025 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4026 | PyLongObject *div, *mod; |
| 4027 | PyObject *z; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 4028 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4029 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 4030 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4031 | if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, &mod) < 0) { |
| 4032 | return NULL; |
| 4033 | } |
| 4034 | z = PyTuple_New(2); |
| 4035 | if (z != NULL) { |
| 4036 | PyTuple_SetItem(z, 0, (PyObject *) div); |
| 4037 | PyTuple_SetItem(z, 1, (PyObject *) mod); |
| 4038 | } |
| 4039 | else { |
| 4040 | Py_DECREF(div); |
| 4041 | Py_DECREF(mod); |
| 4042 | } |
| 4043 | return z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4044 | } |
| 4045 | |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4046 | /* pow(v, w, x) */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4047 | static PyObject * |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 4048 | long_pow(PyObject *v, PyObject *w, PyObject *x) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4049 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4050 | PyLongObject *a, *b, *c; /* a,b,c = v,w,x */ |
| 4051 | int negativeOutput = 0; /* if x<0 return negative output */ |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 4052 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4053 | PyLongObject *z = NULL; /* accumulated result */ |
| 4054 | Py_ssize_t i, j, k; /* counters */ |
| 4055 | PyLongObject *temp = NULL; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4056 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4057 | /* 5-ary values. If the exponent is large enough, table is |
| 4058 | * precomputed so that table[i] == a**i % c for i in range(32). |
| 4059 | */ |
| 4060 | PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 4061 | 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] | 4062 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4063 | /* a, b, c = v, w, x */ |
| 4064 | CHECK_BINOP(v, w); |
| 4065 | a = (PyLongObject*)v; Py_INCREF(a); |
| 4066 | b = (PyLongObject*)w; Py_INCREF(b); |
| 4067 | if (PyLong_Check(x)) { |
| 4068 | c = (PyLongObject *)x; |
| 4069 | Py_INCREF(x); |
| 4070 | } |
| 4071 | else if (x == Py_None) |
| 4072 | c = NULL; |
| 4073 | else { |
| 4074 | Py_DECREF(a); |
| 4075 | Py_DECREF(b); |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 4076 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4077 | } |
Tim Peters | 4c483c4 | 2001-09-05 06:24:58 +0000 | [diff] [blame] | 4078 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4079 | if (Py_SIZE(b) < 0) { /* if exponent is negative */ |
| 4080 | if (c) { |
Mark Dickinson | 0c346d8 | 2014-04-11 14:34:40 -0400 | [diff] [blame] | 4081 | PyErr_SetString(PyExc_ValueError, "pow() 2nd argument " |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4082 | "cannot be negative when 3rd argument specified"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4083 | goto Error; |
| 4084 | } |
| 4085 | else { |
| 4086 | /* else return a float. This works because we know |
| 4087 | that this calls float_pow() which converts its |
| 4088 | arguments to double. */ |
| 4089 | Py_DECREF(a); |
| 4090 | Py_DECREF(b); |
| 4091 | return PyFloat_Type.tp_as_number->nb_power(v, w, x); |
| 4092 | } |
| 4093 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4094 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4095 | if (c) { |
| 4096 | /* if modulus == 0: |
| 4097 | raise ValueError() */ |
| 4098 | if (Py_SIZE(c) == 0) { |
| 4099 | PyErr_SetString(PyExc_ValueError, |
| 4100 | "pow() 3rd argument cannot be 0"); |
| 4101 | goto Error; |
| 4102 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4103 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4104 | /* if modulus < 0: |
| 4105 | negativeOutput = True |
| 4106 | modulus = -modulus */ |
| 4107 | if (Py_SIZE(c) < 0) { |
| 4108 | negativeOutput = 1; |
| 4109 | temp = (PyLongObject *)_PyLong_Copy(c); |
| 4110 | if (temp == NULL) |
| 4111 | goto Error; |
| 4112 | Py_DECREF(c); |
| 4113 | c = temp; |
| 4114 | temp = NULL; |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 4115 | _PyLong_Negate(&c); |
| 4116 | if (c == NULL) |
| 4117 | goto Error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4118 | } |
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 | /* if modulus == 1: |
| 4121 | return 0 */ |
| 4122 | if ((Py_SIZE(c) == 1) && (c->ob_digit[0] == 1)) { |
| 4123 | z = (PyLongObject *)PyLong_FromLong(0L); |
| 4124 | goto Done; |
| 4125 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4126 | |
Tim Peters | 81a9315 | 2013-10-05 16:53:52 -0500 | [diff] [blame] | 4127 | /* Reduce base by modulus in some cases: |
| 4128 | 1. If base < 0. Forcing the base non-negative makes things easier. |
| 4129 | 2. If base is obviously larger than the modulus. The "small |
| 4130 | exponent" case later can multiply directly by base repeatedly, |
| 4131 | while the "large exponent" case multiplies directly by base 31 |
| 4132 | times. It can be unboundedly faster to multiply by |
| 4133 | base % modulus instead. |
| 4134 | We could _always_ do this reduction, but l_divmod() isn't cheap, |
| 4135 | so we only do it when it buys something. */ |
| 4136 | if (Py_SIZE(a) < 0 || Py_SIZE(a) > Py_SIZE(c)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4137 | if (l_divmod(a, c, NULL, &temp) < 0) |
| 4138 | goto Error; |
| 4139 | Py_DECREF(a); |
| 4140 | a = temp; |
| 4141 | temp = NULL; |
| 4142 | } |
| 4143 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4144 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4145 | /* At this point a, b, and c are guaranteed non-negative UNLESS |
| 4146 | c is NULL, in which case a may be negative. */ |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4147 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4148 | z = (PyLongObject *)PyLong_FromLong(1L); |
| 4149 | if (z == NULL) |
| 4150 | goto Error; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4151 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4152 | /* Perform a modular reduction, X = X % c, but leave X alone if c |
| 4153 | * is NULL. |
| 4154 | */ |
| 4155 | #define REDUCE(X) \ |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4156 | do { \ |
| 4157 | if (c != NULL) { \ |
| 4158 | if (l_divmod(X, c, NULL, &temp) < 0) \ |
| 4159 | goto Error; \ |
| 4160 | Py_XDECREF(X); \ |
| 4161 | X = temp; \ |
| 4162 | temp = NULL; \ |
| 4163 | } \ |
| 4164 | } while(0) |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4165 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4166 | /* Multiply two values, then reduce the result: |
| 4167 | result = X*Y % c. If c is NULL, skip the mod. */ |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4168 | #define MULT(X, Y, result) \ |
| 4169 | do { \ |
| 4170 | temp = (PyLongObject *)long_mul(X, Y); \ |
| 4171 | if (temp == NULL) \ |
| 4172 | goto Error; \ |
| 4173 | Py_XDECREF(result); \ |
| 4174 | result = temp; \ |
| 4175 | temp = NULL; \ |
| 4176 | REDUCE(result); \ |
| 4177 | } while(0) |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4178 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4179 | if (Py_SIZE(b) <= FIVEARY_CUTOFF) { |
| 4180 | /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */ |
| 4181 | /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */ |
| 4182 | for (i = Py_SIZE(b) - 1; i >= 0; --i) { |
| 4183 | digit bi = b->ob_digit[i]; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4184 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4185 | for (j = (digit)1 << (PyLong_SHIFT-1); j != 0; j >>= 1) { |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4186 | MULT(z, z, z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4187 | if (bi & j) |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4188 | MULT(z, a, z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4189 | } |
| 4190 | } |
| 4191 | } |
| 4192 | else { |
| 4193 | /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */ |
| 4194 | Py_INCREF(z); /* still holds 1L */ |
| 4195 | table[0] = z; |
| 4196 | for (i = 1; i < 32; ++i) |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4197 | MULT(table[i-1], a, table[i]); |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4198 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4199 | for (i = Py_SIZE(b) - 1; i >= 0; --i) { |
| 4200 | const digit bi = b->ob_digit[i]; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4201 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4202 | for (j = PyLong_SHIFT - 5; j >= 0; j -= 5) { |
| 4203 | const int index = (bi >> j) & 0x1f; |
| 4204 | for (k = 0; k < 5; ++k) |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4205 | MULT(z, z, z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4206 | if (index) |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4207 | MULT(z, table[index], z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4208 | } |
| 4209 | } |
| 4210 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4211 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4212 | if (negativeOutput && (Py_SIZE(z) != 0)) { |
| 4213 | temp = (PyLongObject *)long_sub(z, c); |
| 4214 | if (temp == NULL) |
| 4215 | goto Error; |
| 4216 | Py_DECREF(z); |
| 4217 | z = temp; |
| 4218 | temp = NULL; |
| 4219 | } |
| 4220 | goto Done; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4221 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4222 | Error: |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 4223 | Py_CLEAR(z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4224 | /* fall through */ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4225 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4226 | if (Py_SIZE(b) > FIVEARY_CUTOFF) { |
| 4227 | for (i = 0; i < 32; ++i) |
| 4228 | Py_XDECREF(table[i]); |
| 4229 | } |
| 4230 | Py_DECREF(a); |
| 4231 | Py_DECREF(b); |
| 4232 | Py_XDECREF(c); |
| 4233 | Py_XDECREF(temp); |
| 4234 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4235 | } |
| 4236 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4237 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 4238 | long_invert(PyLongObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4239 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4240 | /* Implement ~x as -(x+1) */ |
| 4241 | PyLongObject *x; |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 4242 | if (Py_ABS(Py_SIZE(v)) <=1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4243 | return PyLong_FromLong(-(MEDIUM_VALUE(v)+1)); |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 4244 | x = (PyLongObject *) long_add(v, (PyLongObject *)_PyLong_One); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4245 | if (x == NULL) |
| 4246 | return NULL; |
Mark Dickinson | 583c6e8 | 2016-08-29 16:40:29 +0100 | [diff] [blame] | 4247 | _PyLong_Negate(&x); |
| 4248 | /* No need for maybe_small_long here, since any small |
| 4249 | longs will have been caught in the Py_SIZE <= 1 fast path. */ |
| 4250 | return (PyObject *)x; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4251 | } |
| 4252 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4253 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 4254 | long_neg(PyLongObject *v) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4255 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4256 | PyLongObject *z; |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 4257 | if (Py_ABS(Py_SIZE(v)) <= 1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4258 | return PyLong_FromLong(-MEDIUM_VALUE(v)); |
| 4259 | z = (PyLongObject *)_PyLong_Copy(v); |
| 4260 | if (z != NULL) |
| 4261 | Py_SIZE(z) = -(Py_SIZE(v)); |
| 4262 | return (PyObject *)z; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4263 | } |
| 4264 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4265 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 4266 | long_abs(PyLongObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4267 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4268 | if (Py_SIZE(v) < 0) |
| 4269 | return long_neg(v); |
| 4270 | else |
| 4271 | return long_long((PyObject *)v); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4272 | } |
| 4273 | |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 4274 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 4275 | long_bool(PyLongObject *v) |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 4276 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4277 | return Py_SIZE(v) != 0; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4278 | } |
| 4279 | |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4280 | /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */ |
| 4281 | static int |
| 4282 | divmod_shift(PyLongObject *shiftby, Py_ssize_t *wordshift, digit *remshift) |
| 4283 | { |
| 4284 | assert(PyLong_Check((PyObject *)shiftby)); |
| 4285 | assert(Py_SIZE(shiftby) >= 0); |
| 4286 | Py_ssize_t lshiftby = PyLong_AsSsize_t((PyObject *)shiftby); |
| 4287 | if (lshiftby >= 0) { |
| 4288 | *wordshift = lshiftby / PyLong_SHIFT; |
| 4289 | *remshift = lshiftby % PyLong_SHIFT; |
| 4290 | return 0; |
| 4291 | } |
| 4292 | /* PyLong_Check(shiftby) is true and Py_SIZE(shiftby) >= 0, so it must |
| 4293 | be that PyLong_AsSsize_t raised an OverflowError. */ |
| 4294 | assert(PyErr_ExceptionMatches(PyExc_OverflowError)); |
| 4295 | PyErr_Clear(); |
| 4296 | PyLongObject *wordshift_obj = divrem1(shiftby, PyLong_SHIFT, remshift); |
| 4297 | if (wordshift_obj == NULL) { |
| 4298 | return -1; |
| 4299 | } |
| 4300 | *wordshift = PyLong_AsSsize_t((PyObject *)wordshift_obj); |
| 4301 | Py_DECREF(wordshift_obj); |
| 4302 | if (*wordshift >= 0 && *wordshift < PY_SSIZE_T_MAX / (Py_ssize_t)sizeof(digit)) { |
| 4303 | return 0; |
| 4304 | } |
| 4305 | PyErr_Clear(); |
| 4306 | /* Clip the value. With such large wordshift the right shift |
| 4307 | returns 0 and the left shift raises an error in _PyLong_New(). */ |
| 4308 | *wordshift = PY_SSIZE_T_MAX / sizeof(digit); |
| 4309 | *remshift = 0; |
| 4310 | return 0; |
| 4311 | } |
| 4312 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4313 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 4314 | long_rshift(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4315 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4316 | PyLongObject *z = NULL; |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4317 | Py_ssize_t newsize, wordshift, hishift, i, j; |
| 4318 | digit loshift, lomask, himask; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4319 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4320 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 4321 | |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4322 | if (Py_SIZE(b) < 0) { |
| 4323 | PyErr_SetString(PyExc_ValueError, |
| 4324 | "negative shift count"); |
| 4325 | return NULL; |
| 4326 | } |
| 4327 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4328 | if (Py_SIZE(a) < 0) { |
| 4329 | /* Right shifting negative numbers is harder */ |
| 4330 | PyLongObject *a1, *a2; |
| 4331 | a1 = (PyLongObject *) long_invert(a); |
| 4332 | if (a1 == NULL) |
Mark Dickinson | 92ca535 | 2016-09-17 17:50:50 +0100 | [diff] [blame] | 4333 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4334 | a2 = (PyLongObject *) long_rshift(a1, b); |
| 4335 | Py_DECREF(a1); |
| 4336 | if (a2 == NULL) |
Mark Dickinson | 92ca535 | 2016-09-17 17:50:50 +0100 | [diff] [blame] | 4337 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4338 | z = (PyLongObject *) long_invert(a2); |
| 4339 | Py_DECREF(a2); |
| 4340 | } |
| 4341 | else { |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4342 | if (divmod_shift(b, &wordshift, &loshift) < 0) |
Mark Dickinson | 92ca535 | 2016-09-17 17:50:50 +0100 | [diff] [blame] | 4343 | return NULL; |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4344 | newsize = Py_SIZE(a) - wordshift; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4345 | if (newsize <= 0) |
| 4346 | return PyLong_FromLong(0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4347 | hishift = PyLong_SHIFT - loshift; |
| 4348 | lomask = ((digit)1 << hishift) - 1; |
| 4349 | himask = PyLong_MASK ^ lomask; |
| 4350 | z = _PyLong_New(newsize); |
| 4351 | if (z == NULL) |
Mark Dickinson | 92ca535 | 2016-09-17 17:50:50 +0100 | [diff] [blame] | 4352 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4353 | for (i = 0, j = wordshift; i < newsize; i++, j++) { |
| 4354 | z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask; |
| 4355 | if (i+1 < newsize) |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4356 | z->ob_digit[i] |= (a->ob_digit[j+1] << hishift) & himask; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4357 | } |
Mark Dickinson | 92ca535 | 2016-09-17 17:50:50 +0100 | [diff] [blame] | 4358 | z = maybe_small_long(long_normalize(z)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4359 | } |
Mark Dickinson | 92ca535 | 2016-09-17 17:50:50 +0100 | [diff] [blame] | 4360 | return (PyObject *)z; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4361 | } |
| 4362 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4363 | static PyObject * |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 4364 | long_lshift(PyObject *v, PyObject *w) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4365 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4366 | /* This version due to Tim Peters */ |
| 4367 | PyLongObject *a = (PyLongObject*)v; |
| 4368 | PyLongObject *b = (PyLongObject*)w; |
| 4369 | PyLongObject *z = NULL; |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4370 | Py_ssize_t oldsize, newsize, wordshift, i, j; |
| 4371 | digit remshift; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4372 | twodigits accum; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4373 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4374 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 4375 | |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4376 | if (Py_SIZE(b) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4377 | PyErr_SetString(PyExc_ValueError, "negative shift count"); |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 4378 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4379 | } |
Mark Dickinson | 82a9527 | 2016-08-29 19:27:06 +0100 | [diff] [blame] | 4380 | if (Py_SIZE(a) == 0) { |
| 4381 | return PyLong_FromLong(0); |
| 4382 | } |
| 4383 | |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4384 | if (divmod_shift(b, &wordshift, &remshift) < 0) |
| 4385 | return NULL; |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 4386 | oldsize = Py_ABS(Py_SIZE(a)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4387 | newsize = oldsize + wordshift; |
| 4388 | if (remshift) |
| 4389 | ++newsize; |
| 4390 | z = _PyLong_New(newsize); |
| 4391 | if (z == NULL) |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 4392 | return NULL; |
| 4393 | if (Py_SIZE(a) < 0) { |
| 4394 | assert(Py_REFCNT(z) == 1); |
| 4395 | Py_SIZE(z) = -Py_SIZE(z); |
| 4396 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4397 | for (i = 0; i < wordshift; i++) |
| 4398 | z->ob_digit[i] = 0; |
| 4399 | accum = 0; |
| 4400 | for (i = wordshift, j = 0; j < oldsize; i++, j++) { |
| 4401 | accum |= (twodigits)a->ob_digit[j] << remshift; |
| 4402 | z->ob_digit[i] = (digit)(accum & PyLong_MASK); |
| 4403 | accum >>= PyLong_SHIFT; |
| 4404 | } |
| 4405 | if (remshift) |
| 4406 | z->ob_digit[newsize-1] = (digit)accum; |
| 4407 | else |
| 4408 | assert(!accum); |
| 4409 | z = long_normalize(z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4410 | return (PyObject *) maybe_small_long(z); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4411 | } |
| 4412 | |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4413 | /* Compute two's complement of digit vector a[0:m], writing result to |
| 4414 | z[0:m]. The digit vector a need not be normalized, but should not |
| 4415 | be entirely zero. a and z may point to the same digit vector. */ |
| 4416 | |
| 4417 | static void |
| 4418 | v_complement(digit *z, digit *a, Py_ssize_t m) |
| 4419 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4420 | Py_ssize_t i; |
| 4421 | digit carry = 1; |
| 4422 | for (i = 0; i < m; ++i) { |
| 4423 | carry += a[i] ^ PyLong_MASK; |
| 4424 | z[i] = carry & PyLong_MASK; |
| 4425 | carry >>= PyLong_SHIFT; |
| 4426 | } |
| 4427 | assert(carry == 0); |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4428 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 4429 | |
| 4430 | /* Bitwise and/xor/or operations */ |
| 4431 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4432 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 4433 | long_bitwise(PyLongObject *a, |
Benjamin Peterson | 513762f | 2012-12-26 16:43:33 -0600 | [diff] [blame] | 4434 | char op, /* '&', '|', '^' */ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4435 | PyLongObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4436 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4437 | int nega, negb, negz; |
| 4438 | Py_ssize_t size_a, size_b, size_z, i; |
| 4439 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4440 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4441 | /* Bitwise operations for negative numbers operate as though |
| 4442 | on a two's complement representation. So convert arguments |
| 4443 | from sign-magnitude to two's complement, and convert the |
| 4444 | result back to sign-magnitude at the end. */ |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4445 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4446 | /* If a is negative, replace it by its two's complement. */ |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 4447 | size_a = Py_ABS(Py_SIZE(a)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4448 | nega = Py_SIZE(a) < 0; |
| 4449 | if (nega) { |
| 4450 | z = _PyLong_New(size_a); |
| 4451 | if (z == NULL) |
| 4452 | return NULL; |
| 4453 | v_complement(z->ob_digit, a->ob_digit, size_a); |
| 4454 | a = z; |
| 4455 | } |
| 4456 | else |
| 4457 | /* Keep reference count consistent. */ |
| 4458 | Py_INCREF(a); |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4459 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4460 | /* Same for b. */ |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 4461 | size_b = Py_ABS(Py_SIZE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4462 | negb = Py_SIZE(b) < 0; |
| 4463 | if (negb) { |
| 4464 | z = _PyLong_New(size_b); |
| 4465 | if (z == NULL) { |
| 4466 | Py_DECREF(a); |
| 4467 | return NULL; |
| 4468 | } |
| 4469 | v_complement(z->ob_digit, b->ob_digit, size_b); |
| 4470 | b = z; |
| 4471 | } |
| 4472 | else |
| 4473 | Py_INCREF(b); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4474 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4475 | /* Swap a and b if necessary to ensure size_a >= size_b. */ |
| 4476 | if (size_a < size_b) { |
| 4477 | z = a; a = b; b = z; |
| 4478 | size_z = size_a; size_a = size_b; size_b = size_z; |
| 4479 | negz = nega; nega = negb; negb = negz; |
| 4480 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4481 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4482 | /* JRH: The original logic here was to allocate the result value (z) |
| 4483 | as the longer of the two operands. However, there are some cases |
| 4484 | where the result is guaranteed to be shorter than that: AND of two |
| 4485 | positives, OR of two negatives: use the shorter number. AND with |
| 4486 | mixed signs: use the positive number. OR with mixed signs: use the |
| 4487 | negative number. |
| 4488 | */ |
| 4489 | switch (op) { |
| 4490 | case '^': |
| 4491 | negz = nega ^ negb; |
| 4492 | size_z = size_a; |
| 4493 | break; |
| 4494 | case '&': |
| 4495 | negz = nega & negb; |
| 4496 | size_z = negb ? size_a : size_b; |
| 4497 | break; |
| 4498 | case '|': |
| 4499 | negz = nega | negb; |
| 4500 | size_z = negb ? size_b : size_a; |
| 4501 | break; |
| 4502 | default: |
| 4503 | PyErr_BadArgument(); |
| 4504 | return NULL; |
| 4505 | } |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 4506 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4507 | /* We allow an extra digit if z is negative, to make sure that |
| 4508 | the final two's complement of z doesn't overflow. */ |
| 4509 | z = _PyLong_New(size_z + negz); |
| 4510 | if (z == NULL) { |
| 4511 | Py_DECREF(a); |
| 4512 | Py_DECREF(b); |
| 4513 | return NULL; |
| 4514 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4515 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4516 | /* Compute digits for overlap of a and b. */ |
| 4517 | switch(op) { |
| 4518 | case '&': |
| 4519 | for (i = 0; i < size_b; ++i) |
| 4520 | z->ob_digit[i] = a->ob_digit[i] & b->ob_digit[i]; |
| 4521 | break; |
| 4522 | case '|': |
| 4523 | for (i = 0; i < size_b; ++i) |
| 4524 | z->ob_digit[i] = a->ob_digit[i] | b->ob_digit[i]; |
| 4525 | break; |
| 4526 | case '^': |
| 4527 | for (i = 0; i < size_b; ++i) |
| 4528 | z->ob_digit[i] = a->ob_digit[i] ^ b->ob_digit[i]; |
| 4529 | break; |
| 4530 | default: |
| 4531 | PyErr_BadArgument(); |
| 4532 | return NULL; |
| 4533 | } |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4534 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4535 | /* Copy any remaining digits of a, inverting if necessary. */ |
| 4536 | if (op == '^' && negb) |
| 4537 | for (; i < size_z; ++i) |
| 4538 | z->ob_digit[i] = a->ob_digit[i] ^ PyLong_MASK; |
| 4539 | else if (i < size_z) |
| 4540 | memcpy(&z->ob_digit[i], &a->ob_digit[i], |
| 4541 | (size_z-i)*sizeof(digit)); |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4542 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4543 | /* Complement result if negative. */ |
| 4544 | if (negz) { |
| 4545 | Py_SIZE(z) = -(Py_SIZE(z)); |
| 4546 | z->ob_digit[size_z] = PyLong_MASK; |
| 4547 | v_complement(z->ob_digit, z->ob_digit, size_z+1); |
| 4548 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4549 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4550 | Py_DECREF(a); |
| 4551 | Py_DECREF(b); |
| 4552 | return (PyObject *)maybe_small_long(long_normalize(z)); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4553 | } |
| 4554 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4555 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 4556 | long_and(PyObject *a, PyObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4557 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4558 | PyObject *c; |
| 4559 | CHECK_BINOP(a, b); |
| 4560 | c = long_bitwise((PyLongObject*)a, '&', (PyLongObject*)b); |
| 4561 | return c; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4562 | } |
| 4563 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4564 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 4565 | long_xor(PyObject *a, PyObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4566 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4567 | PyObject *c; |
| 4568 | CHECK_BINOP(a, b); |
| 4569 | c = long_bitwise((PyLongObject*)a, '^', (PyLongObject*)b); |
| 4570 | return c; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4571 | } |
| 4572 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4573 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 4574 | long_or(PyObject *a, PyObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4575 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4576 | PyObject *c; |
| 4577 | CHECK_BINOP(a, b); |
| 4578 | c = long_bitwise((PyLongObject*)a, '|', (PyLongObject*)b); |
| 4579 | return c; |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 4580 | } |
| 4581 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4582 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 4583 | long_long(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 4584 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4585 | if (PyLong_CheckExact(v)) |
| 4586 | Py_INCREF(v); |
| 4587 | else |
| 4588 | v = _PyLong_Copy((PyLongObject *)v); |
| 4589 | return v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 4590 | } |
| 4591 | |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 4592 | PyObject * |
| 4593 | _PyLong_GCD(PyObject *aarg, PyObject *barg) |
| 4594 | { |
| 4595 | PyLongObject *a, *b, *c = NULL, *d = NULL, *r; |
| 4596 | stwodigits x, y, q, s, t, c_carry, d_carry; |
| 4597 | stwodigits A, B, C, D, T; |
| 4598 | int nbits, k; |
| 4599 | Py_ssize_t size_a, size_b, alloc_a, alloc_b; |
| 4600 | digit *a_digit, *b_digit, *c_digit, *d_digit, *a_end, *b_end; |
| 4601 | |
| 4602 | a = (PyLongObject *)aarg; |
| 4603 | b = (PyLongObject *)barg; |
| 4604 | size_a = Py_SIZE(a); |
| 4605 | size_b = Py_SIZE(b); |
| 4606 | if (-2 <= size_a && size_a <= 2 && -2 <= size_b && size_b <= 2) { |
| 4607 | Py_INCREF(a); |
| 4608 | Py_INCREF(b); |
| 4609 | goto simple; |
| 4610 | } |
| 4611 | |
| 4612 | /* Initial reduction: make sure that 0 <= b <= a. */ |
| 4613 | a = (PyLongObject *)long_abs(a); |
| 4614 | if (a == NULL) |
| 4615 | return NULL; |
| 4616 | b = (PyLongObject *)long_abs(b); |
| 4617 | if (b == NULL) { |
| 4618 | Py_DECREF(a); |
| 4619 | return NULL; |
| 4620 | } |
| 4621 | if (long_compare(a, b) < 0) { |
| 4622 | r = a; |
| 4623 | a = b; |
| 4624 | b = r; |
| 4625 | } |
| 4626 | /* We now own references to a and b */ |
| 4627 | |
| 4628 | alloc_a = Py_SIZE(a); |
| 4629 | alloc_b = Py_SIZE(b); |
| 4630 | /* reduce until a fits into 2 digits */ |
| 4631 | while ((size_a = Py_SIZE(a)) > 2) { |
| 4632 | nbits = bits_in_digit(a->ob_digit[size_a-1]); |
| 4633 | /* extract top 2*PyLong_SHIFT bits of a into x, along with |
| 4634 | corresponding bits of b into y */ |
| 4635 | size_b = Py_SIZE(b); |
| 4636 | assert(size_b <= size_a); |
| 4637 | if (size_b == 0) { |
| 4638 | if (size_a < alloc_a) { |
| 4639 | r = (PyLongObject *)_PyLong_Copy(a); |
| 4640 | Py_DECREF(a); |
| 4641 | } |
| 4642 | else |
| 4643 | r = a; |
| 4644 | Py_DECREF(b); |
| 4645 | Py_XDECREF(c); |
| 4646 | Py_XDECREF(d); |
| 4647 | return (PyObject *)r; |
| 4648 | } |
| 4649 | x = (((twodigits)a->ob_digit[size_a-1] << (2*PyLong_SHIFT-nbits)) | |
| 4650 | ((twodigits)a->ob_digit[size_a-2] << (PyLong_SHIFT-nbits)) | |
| 4651 | (a->ob_digit[size_a-3] >> nbits)); |
| 4652 | |
| 4653 | y = ((size_b >= size_a - 2 ? b->ob_digit[size_a-3] >> nbits : 0) | |
| 4654 | (size_b >= size_a - 1 ? (twodigits)b->ob_digit[size_a-2] << (PyLong_SHIFT-nbits) : 0) | |
| 4655 | (size_b >= size_a ? (twodigits)b->ob_digit[size_a-1] << (2*PyLong_SHIFT-nbits) : 0)); |
| 4656 | |
| 4657 | /* inner loop of Lehmer's algorithm; A, B, C, D never grow |
| 4658 | larger than PyLong_MASK during the algorithm. */ |
| 4659 | A = 1; B = 0; C = 0; D = 1; |
| 4660 | for (k=0;; k++) { |
| 4661 | if (y-C == 0) |
| 4662 | break; |
| 4663 | q = (x+(A-1))/(y-C); |
| 4664 | s = B+q*D; |
| 4665 | t = x-q*y; |
| 4666 | if (s > t) |
| 4667 | break; |
| 4668 | x = y; y = t; |
| 4669 | t = A+q*C; A = D; B = C; C = s; D = t; |
| 4670 | } |
| 4671 | |
| 4672 | if (k == 0) { |
| 4673 | /* no progress; do a Euclidean step */ |
| 4674 | if (l_divmod(a, b, NULL, &r) < 0) |
| 4675 | goto error; |
| 4676 | Py_DECREF(a); |
| 4677 | a = b; |
| 4678 | b = r; |
| 4679 | alloc_a = alloc_b; |
| 4680 | alloc_b = Py_SIZE(b); |
| 4681 | continue; |
| 4682 | } |
| 4683 | |
| 4684 | /* |
| 4685 | a, b = A*b-B*a, D*a-C*b if k is odd |
| 4686 | a, b = A*a-B*b, D*b-C*a if k is even |
| 4687 | */ |
| 4688 | if (k&1) { |
| 4689 | T = -A; A = -B; B = T; |
| 4690 | T = -C; C = -D; D = T; |
| 4691 | } |
| 4692 | if (c != NULL) |
| 4693 | Py_SIZE(c) = size_a; |
| 4694 | else if (Py_REFCNT(a) == 1) { |
| 4695 | Py_INCREF(a); |
| 4696 | c = a; |
| 4697 | } |
| 4698 | else { |
| 4699 | alloc_a = size_a; |
| 4700 | c = _PyLong_New(size_a); |
| 4701 | if (c == NULL) |
| 4702 | goto error; |
| 4703 | } |
| 4704 | |
| 4705 | if (d != NULL) |
| 4706 | Py_SIZE(d) = size_a; |
| 4707 | else if (Py_REFCNT(b) == 1 && size_a <= alloc_b) { |
| 4708 | Py_INCREF(b); |
| 4709 | d = b; |
| 4710 | Py_SIZE(d) = size_a; |
| 4711 | } |
| 4712 | else { |
| 4713 | alloc_b = size_a; |
| 4714 | d = _PyLong_New(size_a); |
| 4715 | if (d == NULL) |
| 4716 | goto error; |
| 4717 | } |
| 4718 | a_end = a->ob_digit + size_a; |
| 4719 | b_end = b->ob_digit + size_b; |
| 4720 | |
| 4721 | /* compute new a and new b in parallel */ |
| 4722 | a_digit = a->ob_digit; |
| 4723 | b_digit = b->ob_digit; |
| 4724 | c_digit = c->ob_digit; |
| 4725 | d_digit = d->ob_digit; |
| 4726 | c_carry = 0; |
| 4727 | d_carry = 0; |
| 4728 | while (b_digit < b_end) { |
| 4729 | c_carry += (A * *a_digit) - (B * *b_digit); |
| 4730 | d_carry += (D * *b_digit++) - (C * *a_digit++); |
| 4731 | *c_digit++ = (digit)(c_carry & PyLong_MASK); |
| 4732 | *d_digit++ = (digit)(d_carry & PyLong_MASK); |
| 4733 | c_carry >>= PyLong_SHIFT; |
| 4734 | d_carry >>= PyLong_SHIFT; |
| 4735 | } |
| 4736 | while (a_digit < a_end) { |
| 4737 | c_carry += A * *a_digit; |
| 4738 | d_carry -= C * *a_digit++; |
| 4739 | *c_digit++ = (digit)(c_carry & PyLong_MASK); |
| 4740 | *d_digit++ = (digit)(d_carry & PyLong_MASK); |
| 4741 | c_carry >>= PyLong_SHIFT; |
| 4742 | d_carry >>= PyLong_SHIFT; |
| 4743 | } |
| 4744 | assert(c_carry == 0); |
| 4745 | assert(d_carry == 0); |
| 4746 | |
| 4747 | Py_INCREF(c); |
| 4748 | Py_INCREF(d); |
| 4749 | Py_DECREF(a); |
| 4750 | Py_DECREF(b); |
| 4751 | a = long_normalize(c); |
| 4752 | b = long_normalize(d); |
| 4753 | } |
| 4754 | Py_XDECREF(c); |
| 4755 | Py_XDECREF(d); |
| 4756 | |
| 4757 | simple: |
| 4758 | assert(Py_REFCNT(a) > 0); |
| 4759 | assert(Py_REFCNT(b) > 0); |
Victor Stinner | 5783fd2 | 2015-09-19 13:39:03 +0200 | [diff] [blame] | 4760 | /* Issue #24999: use two shifts instead of ">> 2*PyLong_SHIFT" to avoid |
| 4761 | undefined behaviour when LONG_MAX type is smaller than 60 bits */ |
| 4762 | #if LONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 4763 | /* a fits into a long, so b must too */ |
| 4764 | x = PyLong_AsLong((PyObject *)a); |
| 4765 | y = PyLong_AsLong((PyObject *)b); |
Benjamin Peterson | d953f8e | 2016-09-06 10:51:19 -0700 | [diff] [blame] | 4766 | #elif PY_LLONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 4767 | x = PyLong_AsLongLong((PyObject *)a); |
| 4768 | y = PyLong_AsLongLong((PyObject *)b); |
| 4769 | #else |
| 4770 | # error "_PyLong_GCD" |
| 4771 | #endif |
| 4772 | x = Py_ABS(x); |
| 4773 | y = Py_ABS(y); |
| 4774 | Py_DECREF(a); |
| 4775 | Py_DECREF(b); |
| 4776 | |
| 4777 | /* usual Euclidean algorithm for longs */ |
| 4778 | while (y != 0) { |
| 4779 | t = y; |
| 4780 | y = x % y; |
| 4781 | x = t; |
| 4782 | } |
Victor Stinner | 5783fd2 | 2015-09-19 13:39:03 +0200 | [diff] [blame] | 4783 | #if LONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 4784 | return PyLong_FromLong(x); |
Benjamin Peterson | d953f8e | 2016-09-06 10:51:19 -0700 | [diff] [blame] | 4785 | #elif PY_LLONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 4786 | return PyLong_FromLongLong(x); |
| 4787 | #else |
| 4788 | # error "_PyLong_GCD" |
| 4789 | #endif |
| 4790 | |
| 4791 | error: |
| 4792 | Py_DECREF(a); |
| 4793 | Py_DECREF(b); |
| 4794 | Py_XDECREF(c); |
| 4795 | Py_XDECREF(d); |
| 4796 | return NULL; |
| 4797 | } |
| 4798 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4799 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 4800 | long_float(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 4801 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4802 | double result; |
| 4803 | result = PyLong_AsDouble(v); |
| 4804 | if (result == -1.0 && PyErr_Occurred()) |
| 4805 | return NULL; |
| 4806 | return PyFloat_FromDouble(result); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 4807 | } |
| 4808 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4809 | static PyObject * |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 4810 | long_subtype_new(PyTypeObject *type, PyObject *x, PyObject *obase); |
| 4811 | |
| 4812 | /*[clinic input] |
| 4813 | @classmethod |
| 4814 | int.__new__ as long_new |
| 4815 | x: object(c_default="NULL") = 0 |
| 4816 | / |
| 4817 | base as obase: object(c_default="NULL") = 10 |
| 4818 | [clinic start generated code]*/ |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 4819 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4820 | static PyObject * |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 4821 | long_new_impl(PyTypeObject *type, PyObject *x, PyObject *obase) |
| 4822 | /*[clinic end generated code: output=e47cfe777ab0f24c input=81c98f418af9eb6f]*/ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4823 | { |
Gregory P. Smith | a689e52 | 2012-12-25 22:38:32 -0800 | [diff] [blame] | 4824 | Py_ssize_t base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4825 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4826 | if (type != &PyLong_Type) |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 4827 | return long_subtype_new(type, x, obase); /* Wimp out */ |
Serhiy Storchaka | 0b386d5 | 2012-12-28 09:42:11 +0200 | [diff] [blame] | 4828 | if (x == NULL) { |
| 4829 | if (obase != NULL) { |
| 4830 | PyErr_SetString(PyExc_TypeError, |
| 4831 | "int() missing string argument"); |
| 4832 | return NULL; |
| 4833 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4834 | return PyLong_FromLong(0L); |
Serhiy Storchaka | 0b386d5 | 2012-12-28 09:42:11 +0200 | [diff] [blame] | 4835 | } |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 4836 | if (obase == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4837 | return PyNumber_Long(x); |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 4838 | |
Gregory P. Smith | a689e52 | 2012-12-25 22:38:32 -0800 | [diff] [blame] | 4839 | base = PyNumber_AsSsize_t(obase, NULL); |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 4840 | if (base == -1 && PyErr_Occurred()) |
| 4841 | return NULL; |
Gregory P. Smith | a689e52 | 2012-12-25 22:38:32 -0800 | [diff] [blame] | 4842 | if ((base != 0 && base < 2) || base > 36) { |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 4843 | PyErr_SetString(PyExc_ValueError, |
Serhiy Storchaka | 0b386d5 | 2012-12-28 09:42:11 +0200 | [diff] [blame] | 4844 | "int() base must be >= 2 and <= 36"); |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 4845 | return NULL; |
| 4846 | } |
| 4847 | |
| 4848 | if (PyUnicode_Check(x)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4849 | return PyLong_FromUnicodeObject(x, (int)base); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4850 | else if (PyByteArray_Check(x) || PyBytes_Check(x)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4851 | char *string; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4852 | if (PyByteArray_Check(x)) |
| 4853 | string = PyByteArray_AS_STRING(x); |
| 4854 | else |
| 4855 | string = PyBytes_AS_STRING(x); |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 4856 | return _PyLong_FromBytes(string, Py_SIZE(x), (int)base); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4857 | } |
| 4858 | else { |
| 4859 | PyErr_SetString(PyExc_TypeError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4860 | "int() can't convert non-string with explicit base"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4861 | return NULL; |
| 4862 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4863 | } |
| 4864 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4865 | /* Wimpy, slow approach to tp_new calls for subtypes of int: |
| 4866 | first create a regular int from whatever arguments we got, |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 4867 | then allocate a subtype instance and initialize it from |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4868 | the regular int. The regular int is then thrown away. |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 4869 | */ |
| 4870 | static PyObject * |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 4871 | long_subtype_new(PyTypeObject *type, PyObject *x, PyObject *obase) |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 4872 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4873 | PyLongObject *tmp, *newobj; |
| 4874 | Py_ssize_t i, n; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 4875 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4876 | assert(PyType_IsSubtype(type, &PyLong_Type)); |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 4877 | tmp = (PyLongObject *)long_new_impl(&PyLong_Type, x, obase); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4878 | if (tmp == NULL) |
| 4879 | return NULL; |
Serhiy Storchaka | 1509580 | 2015-11-25 15:47:01 +0200 | [diff] [blame] | 4880 | assert(PyLong_Check(tmp)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4881 | n = Py_SIZE(tmp); |
| 4882 | if (n < 0) |
| 4883 | n = -n; |
| 4884 | newobj = (PyLongObject *)type->tp_alloc(type, n); |
| 4885 | if (newobj == NULL) { |
| 4886 | Py_DECREF(tmp); |
| 4887 | return NULL; |
| 4888 | } |
| 4889 | assert(PyLong_Check(newobj)); |
| 4890 | Py_SIZE(newobj) = Py_SIZE(tmp); |
| 4891 | for (i = 0; i < n; i++) |
| 4892 | newobj->ob_digit[i] = tmp->ob_digit[i]; |
| 4893 | Py_DECREF(tmp); |
| 4894 | return (PyObject *)newobj; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 4895 | } |
| 4896 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 4897 | /*[clinic input] |
| 4898 | int.__getnewargs__ |
| 4899 | [clinic start generated code]*/ |
| 4900 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 4901 | static PyObject * |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 4902 | int___getnewargs___impl(PyObject *self) |
| 4903 | /*[clinic end generated code: output=839a49de3f00b61b input=5904770ab1fb8c75]*/ |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 4904 | { |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 4905 | return Py_BuildValue("(N)", _PyLong_Copy((PyLongObject *)self)); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 4906 | } |
| 4907 | |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 4908 | static PyObject * |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 4909 | long_get0(PyLongObject *v, void *context) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4910 | return PyLong_FromLong(0L); |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 4911 | } |
| 4912 | |
| 4913 | static PyObject * |
| 4914 | long_get1(PyLongObject *v, void *context) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4915 | return PyLong_FromLong(1L); |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 4916 | } |
| 4917 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 4918 | /*[clinic input] |
| 4919 | int.__format__ |
| 4920 | |
| 4921 | format_spec: unicode |
| 4922 | / |
| 4923 | [clinic start generated code]*/ |
| 4924 | |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 4925 | static PyObject * |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 4926 | int___format___impl(PyObject *self, PyObject *format_spec) |
| 4927 | /*[clinic end generated code: output=b4929dee9ae18689 input=e31944a9b3e428b7]*/ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 4928 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 4929 | _PyUnicodeWriter writer; |
| 4930 | int ret; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 4931 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4932 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 4933 | ret = _PyLong_FormatAdvancedWriter( |
| 4934 | &writer, |
| 4935 | self, |
| 4936 | format_spec, 0, PyUnicode_GET_LENGTH(format_spec)); |
| 4937 | if (ret == -1) { |
| 4938 | _PyUnicodeWriter_Dealloc(&writer); |
| 4939 | return NULL; |
| 4940 | } |
| 4941 | return _PyUnicodeWriter_Finish(&writer); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 4942 | } |
| 4943 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 4944 | /* Return a pair (q, r) such that a = b * q + r, and |
| 4945 | abs(r) <= abs(b)/2, with equality possible only if q is even. |
| 4946 | In other words, q == a / b, rounded to the nearest integer using |
| 4947 | round-half-to-even. */ |
| 4948 | |
| 4949 | PyObject * |
Mark Dickinson | fa68a61 | 2010-06-07 18:47:09 +0000 | [diff] [blame] | 4950 | _PyLong_DivmodNear(PyObject *a, PyObject *b) |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 4951 | { |
| 4952 | PyLongObject *quo = NULL, *rem = NULL; |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 4953 | PyObject *twice_rem, *result, *temp; |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 4954 | int cmp, quo_is_odd, quo_is_neg; |
| 4955 | |
| 4956 | /* Equivalent Python code: |
| 4957 | |
| 4958 | def divmod_near(a, b): |
| 4959 | q, r = divmod(a, b) |
| 4960 | # round up if either r / b > 0.5, or r / b == 0.5 and q is odd. |
| 4961 | # The expression r / b > 0.5 is equivalent to 2 * r > b if b is |
| 4962 | # positive, 2 * r < b if b negative. |
| 4963 | greater_than_half = 2*r > b if b > 0 else 2*r < b |
| 4964 | exactly_half = 2*r == b |
| 4965 | if greater_than_half or exactly_half and q % 2 == 1: |
| 4966 | q += 1 |
| 4967 | r -= b |
| 4968 | return q, r |
| 4969 | |
| 4970 | */ |
| 4971 | if (!PyLong_Check(a) || !PyLong_Check(b)) { |
| 4972 | PyErr_SetString(PyExc_TypeError, |
| 4973 | "non-integer arguments in division"); |
| 4974 | return NULL; |
| 4975 | } |
| 4976 | |
| 4977 | /* Do a and b have different signs? If so, quotient is negative. */ |
| 4978 | quo_is_neg = (Py_SIZE(a) < 0) != (Py_SIZE(b) < 0); |
| 4979 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 4980 | if (long_divrem((PyLongObject*)a, (PyLongObject*)b, &quo, &rem) < 0) |
| 4981 | goto error; |
| 4982 | |
| 4983 | /* compare twice the remainder with the divisor, to see |
| 4984 | if we need to adjust the quotient and remainder */ |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 4985 | twice_rem = long_lshift((PyObject *)rem, _PyLong_One); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 4986 | if (twice_rem == NULL) |
| 4987 | goto error; |
| 4988 | if (quo_is_neg) { |
| 4989 | temp = long_neg((PyLongObject*)twice_rem); |
| 4990 | Py_DECREF(twice_rem); |
| 4991 | twice_rem = temp; |
| 4992 | if (twice_rem == NULL) |
| 4993 | goto error; |
| 4994 | } |
| 4995 | cmp = long_compare((PyLongObject *)twice_rem, (PyLongObject *)b); |
| 4996 | Py_DECREF(twice_rem); |
| 4997 | |
| 4998 | quo_is_odd = Py_SIZE(quo) != 0 && ((quo->ob_digit[0] & 1) != 0); |
| 4999 | if ((Py_SIZE(b) < 0 ? cmp < 0 : cmp > 0) || (cmp == 0 && quo_is_odd)) { |
| 5000 | /* fix up quotient */ |
| 5001 | if (quo_is_neg) |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 5002 | temp = long_sub(quo, (PyLongObject *)_PyLong_One); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5003 | else |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 5004 | temp = long_add(quo, (PyLongObject *)_PyLong_One); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5005 | Py_DECREF(quo); |
| 5006 | quo = (PyLongObject *)temp; |
| 5007 | if (quo == NULL) |
| 5008 | goto error; |
| 5009 | /* and remainder */ |
| 5010 | if (quo_is_neg) |
| 5011 | temp = long_add(rem, (PyLongObject *)b); |
| 5012 | else |
| 5013 | temp = long_sub(rem, (PyLongObject *)b); |
| 5014 | Py_DECREF(rem); |
| 5015 | rem = (PyLongObject *)temp; |
| 5016 | if (rem == NULL) |
| 5017 | goto error; |
| 5018 | } |
| 5019 | |
| 5020 | result = PyTuple_New(2); |
| 5021 | if (result == NULL) |
| 5022 | goto error; |
| 5023 | |
| 5024 | /* PyTuple_SET_ITEM steals references */ |
| 5025 | PyTuple_SET_ITEM(result, 0, (PyObject *)quo); |
| 5026 | PyTuple_SET_ITEM(result, 1, (PyObject *)rem); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5027 | return result; |
| 5028 | |
| 5029 | error: |
| 5030 | Py_XDECREF(quo); |
| 5031 | Py_XDECREF(rem); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5032 | return NULL; |
| 5033 | } |
| 5034 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 5035 | static PyObject * |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 5036 | long_round(PyObject *self, PyObject *args) |
| 5037 | { |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5038 | PyObject *o_ndigits=NULL, *temp, *result, *ndigits; |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 5039 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5040 | /* To round an integer m to the nearest 10**n (n positive), we make use of |
| 5041 | * the divmod_near operation, defined by: |
| 5042 | * |
| 5043 | * divmod_near(a, b) = (q, r) |
| 5044 | * |
| 5045 | * where q is the nearest integer to the quotient a / b (the |
| 5046 | * nearest even integer in the case of a tie) and r == a - q * b. |
| 5047 | * Hence q * b = a - r is the nearest multiple of b to a, |
| 5048 | * preferring even multiples in the case of a tie. |
| 5049 | * |
| 5050 | * So the nearest multiple of 10**n to m is: |
| 5051 | * |
| 5052 | * m - divmod_near(m, 10**n)[1]. |
| 5053 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5054 | if (!PyArg_ParseTuple(args, "|O", &o_ndigits)) |
| 5055 | return NULL; |
| 5056 | if (o_ndigits == NULL) |
| 5057 | return long_long(self); |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 5058 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5059 | ndigits = PyNumber_Index(o_ndigits); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5060 | if (ndigits == NULL) |
| 5061 | return NULL; |
Mark Dickinson | 1124e71 | 2009-01-28 21:25:58 +0000 | [diff] [blame] | 5062 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5063 | /* if ndigits >= 0 then no rounding is necessary; return self unchanged */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5064 | if (Py_SIZE(ndigits) >= 0) { |
| 5065 | Py_DECREF(ndigits); |
| 5066 | return long_long(self); |
| 5067 | } |
Mark Dickinson | 1124e71 | 2009-01-28 21:25:58 +0000 | [diff] [blame] | 5068 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5069 | /* result = self - divmod_near(self, 10 ** -ndigits)[1] */ |
| 5070 | temp = long_neg((PyLongObject*)ndigits); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5071 | Py_DECREF(ndigits); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5072 | ndigits = temp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5073 | if (ndigits == NULL) |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5074 | return NULL; |
Mark Dickinson | 1124e71 | 2009-01-28 21:25:58 +0000 | [diff] [blame] | 5075 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5076 | result = PyLong_FromLong(10L); |
| 5077 | if (result == NULL) { |
| 5078 | Py_DECREF(ndigits); |
| 5079 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5080 | } |
Mark Dickinson | 1124e71 | 2009-01-28 21:25:58 +0000 | [diff] [blame] | 5081 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5082 | temp = long_pow(result, ndigits, Py_None); |
| 5083 | Py_DECREF(ndigits); |
| 5084 | Py_DECREF(result); |
| 5085 | result = temp; |
| 5086 | if (result == NULL) |
| 5087 | return NULL; |
| 5088 | |
Mark Dickinson | fa68a61 | 2010-06-07 18:47:09 +0000 | [diff] [blame] | 5089 | temp = _PyLong_DivmodNear(self, result); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5090 | Py_DECREF(result); |
| 5091 | result = temp; |
| 5092 | if (result == NULL) |
| 5093 | return NULL; |
| 5094 | |
| 5095 | temp = long_sub((PyLongObject *)self, |
| 5096 | (PyLongObject *)PyTuple_GET_ITEM(result, 1)); |
| 5097 | Py_DECREF(result); |
| 5098 | result = temp; |
| 5099 | |
| 5100 | return result; |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 5101 | } |
| 5102 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5103 | /*[clinic input] |
| 5104 | int.__sizeof__ -> Py_ssize_t |
| 5105 | |
| 5106 | Returns size in memory, in bytes. |
| 5107 | [clinic start generated code]*/ |
| 5108 | |
| 5109 | static Py_ssize_t |
| 5110 | int___sizeof___impl(PyObject *self) |
| 5111 | /*[clinic end generated code: output=3303f008eaa6a0a5 input=9b51620c76fc4507]*/ |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 5112 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5113 | Py_ssize_t res; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 5114 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5115 | res = offsetof(PyLongObject, ob_digit) + Py_ABS(Py_SIZE(self))*sizeof(digit); |
| 5116 | return res; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 5117 | } |
| 5118 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5119 | /*[clinic input] |
| 5120 | int.bit_length |
| 5121 | |
| 5122 | Number of bits necessary to represent self in binary. |
| 5123 | |
| 5124 | >>> bin(37) |
| 5125 | '0b100101' |
| 5126 | >>> (37).bit_length() |
| 5127 | 6 |
| 5128 | [clinic start generated code]*/ |
| 5129 | |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5130 | static PyObject * |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5131 | int_bit_length_impl(PyObject *self) |
| 5132 | /*[clinic end generated code: output=fc1977c9353d6a59 input=e4eb7a587e849a32]*/ |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5133 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5134 | PyLongObject *result, *x, *y; |
Serhiy Storchaka | b2e64f9 | 2016-11-08 20:34:22 +0200 | [diff] [blame] | 5135 | Py_ssize_t ndigits; |
| 5136 | int msd_bits; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5137 | digit msd; |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5138 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5139 | assert(self != NULL); |
| 5140 | assert(PyLong_Check(self)); |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5141 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5142 | ndigits = Py_ABS(Py_SIZE(self)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5143 | if (ndigits == 0) |
| 5144 | return PyLong_FromLong(0); |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5145 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5146 | msd = ((PyLongObject *)self)->ob_digit[ndigits-1]; |
Serhiy Storchaka | b2e64f9 | 2016-11-08 20:34:22 +0200 | [diff] [blame] | 5147 | msd_bits = bits_in_digit(msd); |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5148 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5149 | if (ndigits <= PY_SSIZE_T_MAX/PyLong_SHIFT) |
| 5150 | return PyLong_FromSsize_t((ndigits-1)*PyLong_SHIFT + msd_bits); |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5151 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5152 | /* expression above may overflow; use Python integers instead */ |
| 5153 | result = (PyLongObject *)PyLong_FromSsize_t(ndigits - 1); |
| 5154 | if (result == NULL) |
| 5155 | return NULL; |
| 5156 | x = (PyLongObject *)PyLong_FromLong(PyLong_SHIFT); |
| 5157 | if (x == NULL) |
| 5158 | goto error; |
| 5159 | y = (PyLongObject *)long_mul(result, x); |
| 5160 | Py_DECREF(x); |
| 5161 | if (y == NULL) |
| 5162 | goto error; |
| 5163 | Py_DECREF(result); |
| 5164 | result = y; |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5165 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5166 | x = (PyLongObject *)PyLong_FromLong((long)msd_bits); |
| 5167 | if (x == NULL) |
| 5168 | goto error; |
| 5169 | y = (PyLongObject *)long_add(result, x); |
| 5170 | Py_DECREF(x); |
| 5171 | if (y == NULL) |
| 5172 | goto error; |
| 5173 | Py_DECREF(result); |
| 5174 | result = y; |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5175 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5176 | return (PyObject *)result; |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5177 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 5178 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5179 | Py_DECREF(result); |
| 5180 | return NULL; |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5181 | } |
| 5182 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 5183 | #if 0 |
| 5184 | static PyObject * |
| 5185 | long_is_finite(PyObject *v) |
| 5186 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5187 | Py_RETURN_TRUE; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 5188 | } |
| 5189 | #endif |
| 5190 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5191 | /*[clinic input] |
| 5192 | int.to_bytes |
| 5193 | |
| 5194 | length: Py_ssize_t |
| 5195 | Length of bytes object to use. An OverflowError is raised if the |
| 5196 | integer is not representable with the given number of bytes. |
| 5197 | byteorder: unicode |
| 5198 | The byte order used to represent the integer. If byteorder is 'big', |
| 5199 | the most significant byte is at the beginning of the byte array. If |
| 5200 | byteorder is 'little', the most significant byte is at the end of the |
| 5201 | byte array. To request the native byte order of the host system, use |
| 5202 | `sys.byteorder' as the byte order value. |
| 5203 | * |
| 5204 | signed as is_signed: bool = False |
| 5205 | Determines whether two's complement is used to represent the integer. |
| 5206 | If signed is False and a negative integer is given, an OverflowError |
| 5207 | is raised. |
| 5208 | |
| 5209 | Return an array of bytes representing an integer. |
| 5210 | [clinic start generated code]*/ |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5211 | |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5212 | static PyObject * |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5213 | int_to_bytes_impl(PyObject *self, Py_ssize_t length, PyObject *byteorder, |
| 5214 | int is_signed) |
| 5215 | /*[clinic end generated code: output=89c801df114050a3 input=ddac63f4c7bf414c]*/ |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5216 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5217 | int little_endian; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5218 | PyObject *bytes; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5219 | |
Serhiy Storchaka | 196a7bc | 2017-02-02 16:54:45 +0200 | [diff] [blame] | 5220 | if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_little)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5221 | little_endian = 1; |
Serhiy Storchaka | 196a7bc | 2017-02-02 16:54:45 +0200 | [diff] [blame] | 5222 | else if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_big)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5223 | little_endian = 0; |
| 5224 | else { |
| 5225 | PyErr_SetString(PyExc_ValueError, |
| 5226 | "byteorder must be either 'little' or 'big'"); |
| 5227 | return NULL; |
| 5228 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5229 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5230 | if (length < 0) { |
| 5231 | PyErr_SetString(PyExc_ValueError, |
| 5232 | "length argument must be non-negative"); |
| 5233 | return NULL; |
| 5234 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5235 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5236 | bytes = PyBytes_FromStringAndSize(NULL, length); |
| 5237 | if (bytes == NULL) |
| 5238 | return NULL; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5239 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5240 | if (_PyLong_AsByteArray((PyLongObject *)self, |
| 5241 | (unsigned char *)PyBytes_AS_STRING(bytes), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5242 | length, little_endian, is_signed) < 0) { |
| 5243 | Py_DECREF(bytes); |
| 5244 | return NULL; |
| 5245 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5246 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5247 | return bytes; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5248 | } |
| 5249 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5250 | /*[clinic input] |
| 5251 | @classmethod |
| 5252 | int.from_bytes |
| 5253 | |
| 5254 | bytes as bytes_obj: object |
| 5255 | Holds the array of bytes to convert. The argument must either |
| 5256 | support the buffer protocol or be an iterable object producing bytes. |
| 5257 | Bytes and bytearray are examples of built-in objects that support the |
| 5258 | buffer protocol. |
| 5259 | byteorder: unicode |
| 5260 | The byte order used to represent the integer. If byteorder is 'big', |
| 5261 | the most significant byte is at the beginning of the byte array. If |
| 5262 | byteorder is 'little', the most significant byte is at the end of the |
| 5263 | byte array. To request the native byte order of the host system, use |
| 5264 | `sys.byteorder' as the byte order value. |
| 5265 | * |
| 5266 | signed as is_signed: bool = False |
| 5267 | Indicates whether two's complement is used to represent the integer. |
| 5268 | |
| 5269 | Return the integer represented by the given array of bytes. |
| 5270 | [clinic start generated code]*/ |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5271 | |
| 5272 | static PyObject * |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5273 | int_from_bytes_impl(PyTypeObject *type, PyObject *bytes_obj, |
| 5274 | PyObject *byteorder, int is_signed) |
| 5275 | /*[clinic end generated code: output=efc5d68e31f9314f input=cdf98332b6a821b0]*/ |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5276 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5277 | int little_endian; |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5278 | PyObject *long_obj, *bytes; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5279 | |
Serhiy Storchaka | 196a7bc | 2017-02-02 16:54:45 +0200 | [diff] [blame] | 5280 | if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_little)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5281 | little_endian = 1; |
Serhiy Storchaka | 196a7bc | 2017-02-02 16:54:45 +0200 | [diff] [blame] | 5282 | else if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_big)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5283 | little_endian = 0; |
| 5284 | else { |
| 5285 | PyErr_SetString(PyExc_ValueError, |
| 5286 | "byteorder must be either 'little' or 'big'"); |
| 5287 | return NULL; |
| 5288 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5289 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5290 | bytes = PyObject_Bytes(bytes_obj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5291 | if (bytes == NULL) |
| 5292 | return NULL; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5293 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5294 | long_obj = _PyLong_FromByteArray( |
| 5295 | (unsigned char *)PyBytes_AS_STRING(bytes), Py_SIZE(bytes), |
| 5296 | little_endian, is_signed); |
| 5297 | Py_DECREF(bytes); |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5298 | |
Serhiy Storchaka | ea36c94 | 2016-05-12 10:37:58 +0300 | [diff] [blame] | 5299 | if (type != &PyLong_Type) { |
Victor Stinner | de4ae3d | 2016-12-04 22:59:09 +0100 | [diff] [blame] | 5300 | Py_SETREF(long_obj, PyObject_CallFunctionObjArgs((PyObject *)type, |
| 5301 | long_obj, NULL)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5302 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5303 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5304 | return long_obj; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5305 | } |
| 5306 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 5307 | static PyMethodDef long_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5308 | {"conjugate", (PyCFunction)long_long, METH_NOARGS, |
| 5309 | "Returns self, the complex conjugate of any int."}, |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5310 | INT_BIT_LENGTH_METHODDEF |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 5311 | #if 0 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5312 | {"is_finite", (PyCFunction)long_is_finite, METH_NOARGS, |
| 5313 | "Returns always True."}, |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 5314 | #endif |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5315 | INT_TO_BYTES_METHODDEF |
| 5316 | INT_FROM_BYTES_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5317 | {"__trunc__", (PyCFunction)long_long, METH_NOARGS, |
| 5318 | "Truncating an Integral returns itself."}, |
| 5319 | {"__floor__", (PyCFunction)long_long, METH_NOARGS, |
| 5320 | "Flooring an Integral returns itself."}, |
| 5321 | {"__ceil__", (PyCFunction)long_long, METH_NOARGS, |
| 5322 | "Ceiling of an Integral returns itself."}, |
| 5323 | {"__round__", (PyCFunction)long_round, METH_VARARGS, |
| 5324 | "Rounding an Integral returns itself.\n" |
| 5325 | "Rounding with an ndigits argument also returns an integer."}, |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5326 | INT___GETNEWARGS___METHODDEF |
| 5327 | INT___FORMAT___METHODDEF |
| 5328 | INT___SIZEOF___METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5329 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 5330 | }; |
| 5331 | |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5332 | static PyGetSetDef long_getset[] = { |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 5333 | {"real", |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5334 | (getter)long_long, (setter)NULL, |
| 5335 | "the real part of a complex number", |
| 5336 | NULL}, |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 5337 | {"imag", |
| 5338 | (getter)long_get0, (setter)NULL, |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5339 | "the imaginary part of a complex number", |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 5340 | NULL}, |
| 5341 | {"numerator", |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5342 | (getter)long_long, (setter)NULL, |
| 5343 | "the numerator of a rational number in lowest terms", |
| 5344 | NULL}, |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 5345 | {"denominator", |
| 5346 | (getter)long_get1, (setter)NULL, |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5347 | "the denominator of a rational number in lowest terms", |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 5348 | NULL}, |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5349 | {NULL} /* Sentinel */ |
| 5350 | }; |
| 5351 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5352 | PyDoc_STRVAR(long_doc, |
svelankar | 390a096 | 2017-03-08 19:29:01 -0500 | [diff] [blame] | 5353 | "int([x]) -> integer\n\ |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 5354 | int(x, base=10) -> integer\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5355 | \n\ |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 5356 | Convert a number or string to an integer, or return 0 if no arguments\n\ |
| 5357 | are given. If x is a number, return x.__int__(). For floating point\n\ |
| 5358 | numbers, this truncates towards zero.\n\ |
| 5359 | \n\ |
| 5360 | If x is not a number or if base is given, then x must be a string,\n\ |
| 5361 | bytes, or bytearray instance representing an integer literal in the\n\ |
| 5362 | given base. The literal can be preceded by '+' or '-' and be surrounded\n\ |
| 5363 | by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.\n\ |
| 5364 | Base 0 means to interpret the base from the string as an integer literal.\n\ |
| 5365 | >>> int('0b100', base=0)\n\ |
| 5366 | 4"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5367 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 5368 | static PyNumberMethods long_as_number = { |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 5369 | (binaryfunc)long_add, /*nb_add*/ |
| 5370 | (binaryfunc)long_sub, /*nb_subtract*/ |
| 5371 | (binaryfunc)long_mul, /*nb_multiply*/ |
| 5372 | long_mod, /*nb_remainder*/ |
| 5373 | long_divmod, /*nb_divmod*/ |
| 5374 | long_pow, /*nb_power*/ |
| 5375 | (unaryfunc)long_neg, /*nb_negative*/ |
| 5376 | (unaryfunc)long_long, /*tp_positive*/ |
| 5377 | (unaryfunc)long_abs, /*tp_absolute*/ |
| 5378 | (inquiry)long_bool, /*tp_bool*/ |
| 5379 | (unaryfunc)long_invert, /*nb_invert*/ |
| 5380 | long_lshift, /*nb_lshift*/ |
| 5381 | (binaryfunc)long_rshift, /*nb_rshift*/ |
| 5382 | long_and, /*nb_and*/ |
| 5383 | long_xor, /*nb_xor*/ |
| 5384 | long_or, /*nb_or*/ |
| 5385 | long_long, /*nb_int*/ |
| 5386 | 0, /*nb_reserved*/ |
| 5387 | long_float, /*nb_float*/ |
| 5388 | 0, /* nb_inplace_add */ |
| 5389 | 0, /* nb_inplace_subtract */ |
| 5390 | 0, /* nb_inplace_multiply */ |
| 5391 | 0, /* nb_inplace_remainder */ |
| 5392 | 0, /* nb_inplace_power */ |
| 5393 | 0, /* nb_inplace_lshift */ |
| 5394 | 0, /* nb_inplace_rshift */ |
| 5395 | 0, /* nb_inplace_and */ |
| 5396 | 0, /* nb_inplace_xor */ |
| 5397 | 0, /* nb_inplace_or */ |
| 5398 | long_div, /* nb_floor_divide */ |
| 5399 | long_true_divide, /* nb_true_divide */ |
| 5400 | 0, /* nb_inplace_floor_divide */ |
| 5401 | 0, /* nb_inplace_true_divide */ |
| 5402 | long_long, /* nb_index */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 5403 | }; |
| 5404 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 5405 | PyTypeObject PyLong_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5406 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 5407 | "int", /* tp_name */ |
| 5408 | offsetof(PyLongObject, ob_digit), /* tp_basicsize */ |
| 5409 | sizeof(digit), /* tp_itemsize */ |
| 5410 | long_dealloc, /* tp_dealloc */ |
| 5411 | 0, /* tp_print */ |
| 5412 | 0, /* tp_getattr */ |
| 5413 | 0, /* tp_setattr */ |
| 5414 | 0, /* tp_reserved */ |
| 5415 | long_to_decimal_string, /* tp_repr */ |
| 5416 | &long_as_number, /* tp_as_number */ |
| 5417 | 0, /* tp_as_sequence */ |
| 5418 | 0, /* tp_as_mapping */ |
| 5419 | (hashfunc)long_hash, /* tp_hash */ |
| 5420 | 0, /* tp_call */ |
| 5421 | long_to_decimal_string, /* tp_str */ |
| 5422 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 5423 | 0, /* tp_setattro */ |
| 5424 | 0, /* tp_as_buffer */ |
| 5425 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 5426 | Py_TPFLAGS_LONG_SUBCLASS, /* tp_flags */ |
| 5427 | long_doc, /* tp_doc */ |
| 5428 | 0, /* tp_traverse */ |
| 5429 | 0, /* tp_clear */ |
| 5430 | long_richcompare, /* tp_richcompare */ |
| 5431 | 0, /* tp_weaklistoffset */ |
| 5432 | 0, /* tp_iter */ |
| 5433 | 0, /* tp_iternext */ |
| 5434 | long_methods, /* tp_methods */ |
| 5435 | 0, /* tp_members */ |
| 5436 | long_getset, /* tp_getset */ |
| 5437 | 0, /* tp_base */ |
| 5438 | 0, /* tp_dict */ |
| 5439 | 0, /* tp_descr_get */ |
| 5440 | 0, /* tp_descr_set */ |
| 5441 | 0, /* tp_dictoffset */ |
| 5442 | 0, /* tp_init */ |
| 5443 | 0, /* tp_alloc */ |
| 5444 | long_new, /* tp_new */ |
| 5445 | PyObject_Del, /* tp_free */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 5446 | }; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5447 | |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 5448 | static PyTypeObject Int_InfoType; |
| 5449 | |
| 5450 | PyDoc_STRVAR(int_info__doc__, |
| 5451 | "sys.int_info\n\ |
| 5452 | \n\ |
| 5453 | A struct sequence that holds information about Python's\n\ |
| 5454 | internal representation of integers. The attributes are read only."); |
| 5455 | |
| 5456 | static PyStructSequence_Field int_info_fields[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5457 | {"bits_per_digit", "size of a digit in bits"}, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 5458 | {"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] | 5459 | {NULL, NULL} |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 5460 | }; |
| 5461 | |
| 5462 | static PyStructSequence_Desc int_info_desc = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5463 | "sys.int_info", /* name */ |
| 5464 | int_info__doc__, /* doc */ |
| 5465 | int_info_fields, /* fields */ |
| 5466 | 2 /* number of fields */ |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 5467 | }; |
| 5468 | |
| 5469 | PyObject * |
| 5470 | PyLong_GetInfo(void) |
| 5471 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5472 | PyObject* int_info; |
| 5473 | int field = 0; |
| 5474 | int_info = PyStructSequence_New(&Int_InfoType); |
| 5475 | if (int_info == NULL) |
| 5476 | return NULL; |
| 5477 | PyStructSequence_SET_ITEM(int_info, field++, |
| 5478 | PyLong_FromLong(PyLong_SHIFT)); |
| 5479 | PyStructSequence_SET_ITEM(int_info, field++, |
| 5480 | PyLong_FromLong(sizeof(digit))); |
| 5481 | if (PyErr_Occurred()) { |
| 5482 | Py_CLEAR(int_info); |
| 5483 | return NULL; |
| 5484 | } |
| 5485 | return int_info; |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 5486 | } |
| 5487 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5488 | int |
| 5489 | _PyLong_Init(void) |
| 5490 | { |
| 5491 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5492 | int ival, size; |
| 5493 | PyLongObject *v = small_ints; |
Christian Heimes | dfc12ed | 2008-01-31 15:16:38 +0000 | [diff] [blame] | 5494 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5495 | for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++, v++) { |
| 5496 | size = (ival < 0) ? -1 : ((ival == 0) ? 0 : 1); |
| 5497 | if (Py_TYPE(v) == &PyLong_Type) { |
| 5498 | /* The element is already initialized, most likely |
| 5499 | * the Python interpreter was initialized before. |
| 5500 | */ |
| 5501 | Py_ssize_t refcnt; |
| 5502 | PyObject* op = (PyObject*)v; |
Christian Heimes | dfc12ed | 2008-01-31 15:16:38 +0000 | [diff] [blame] | 5503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5504 | refcnt = Py_REFCNT(op) < 0 ? 0 : Py_REFCNT(op); |
| 5505 | _Py_NewReference(op); |
| 5506 | /* _Py_NewReference sets the ref count to 1 but |
| 5507 | * the ref count might be larger. Set the refcnt |
| 5508 | * to the original refcnt + 1 */ |
| 5509 | Py_REFCNT(op) = refcnt + 1; |
| 5510 | assert(Py_SIZE(op) == size); |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 5511 | assert(v->ob_digit[0] == (digit)abs(ival)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5512 | } |
| 5513 | else { |
Christian Heimes | d3afe78 | 2013-12-04 09:27:47 +0100 | [diff] [blame] | 5514 | (void)PyObject_INIT(v, &PyLong_Type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5515 | } |
| 5516 | Py_SIZE(v) = size; |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 5517 | v->ob_digit[0] = (digit)abs(ival); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5518 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5519 | #endif |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 5520 | _PyLong_Zero = PyLong_FromLong(0); |
| 5521 | if (_PyLong_Zero == NULL) |
| 5522 | return 0; |
| 5523 | _PyLong_One = PyLong_FromLong(1); |
| 5524 | if (_PyLong_One == NULL) |
| 5525 | return 0; |
| 5526 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5527 | /* initialize int_info */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 5528 | if (Int_InfoType.tp_name == NULL) { |
| 5529 | if (PyStructSequence_InitType2(&Int_InfoType, &int_info_desc) < 0) |
| 5530 | return 0; |
| 5531 | } |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 5532 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5533 | return 1; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5534 | } |
| 5535 | |
| 5536 | void |
| 5537 | PyLong_Fini(void) |
| 5538 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5539 | /* Integers are currently statically allocated. Py_DECREF is not |
| 5540 | needed, but Python must forget about the reference or multiple |
| 5541 | reinitializations will fail. */ |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 5542 | Py_CLEAR(_PyLong_One); |
| 5543 | Py_CLEAR(_PyLong_Zero); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5544 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5545 | int i; |
| 5546 | PyLongObject *v = small_ints; |
| 5547 | for (i = 0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++, v++) { |
| 5548 | _Py_DEC_REFTOTAL; |
| 5549 | _Py_ForgetReference((PyObject*)v); |
| 5550 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5551 | #endif |
| 5552 | } |