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" |
Victor Stinner | 630c8df | 2019-12-17 13:02:18 +0100 | [diff] [blame] | 6 | #include "pycore_pystate.h" /* _Py_IsMainInterpreter() */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 7 | #include "longintrepr.h" |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 8 | |
Mark Dickinson | c630039 | 2009-04-20 21:38:00 +0000 | [diff] [blame] | 9 | #include <float.h> |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 10 | #include <ctype.h> |
Mark Dickinson | 5a74bf6 | 2009-02-15 11:04:38 +0000 | [diff] [blame] | 11 | #include <stddef.h> |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 12 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 13 | #include "clinic/longobject.c.h" |
| 14 | /*[clinic input] |
| 15 | class int "PyObject *" "&PyLong_Type" |
| 16 | [clinic start generated code]*/ |
| 17 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=ec0275e3422a36e3]*/ |
| 18 | |
Victor Stinner | 630c8df | 2019-12-17 13:02:18 +0100 | [diff] [blame] | 19 | #define NSMALLPOSINTS _PY_NSMALLPOSINTS |
| 20 | #define NSMALLNEGINTS _PY_NSMALLNEGINTS |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 21 | |
Serhiy Storchaka | 196a7bc | 2017-02-02 16:54:45 +0200 | [diff] [blame] | 22 | _Py_IDENTIFIER(little); |
| 23 | _Py_IDENTIFIER(big); |
| 24 | |
Mark Dickinson | e441674 | 2009-02-15 15:14:57 +0000 | [diff] [blame] | 25 | /* convert a PyLong of size 1, 0 or -1 to an sdigit */ |
Victor Stinner | 08a80b1 | 2013-07-17 22:33:42 +0200 | [diff] [blame] | 26 | #define MEDIUM_VALUE(x) (assert(-1 <= Py_SIZE(x) && Py_SIZE(x) <= 1), \ |
| 27 | Py_SIZE(x) < 0 ? -(sdigit)(x)->ob_digit[0] : \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 28 | (Py_SIZE(x) == 0 ? (sdigit)0 : \ |
| 29 | (sdigit)(x)->ob_digit[0])) |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 30 | |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 31 | PyObject *_PyLong_Zero = NULL; |
| 32 | PyObject *_PyLong_One = NULL; |
| 33 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 34 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
animalize | 6b51998 | 2019-09-06 14:00:56 +0800 | [diff] [blame] | 35 | #define IS_SMALL_INT(ival) (-NSMALLNEGINTS <= (ival) && (ival) < NSMALLPOSINTS) |
Sergey Fedoseev | c6734ee | 2019-09-12 19:41:14 +0500 | [diff] [blame] | 36 | #define IS_SMALL_UINT(ival) ((ival) < NSMALLPOSINTS) |
Greg Price | 5e63ab0 | 2019-08-24 10:19:37 -0700 | [diff] [blame] | 37 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 38 | #ifdef COUNT_ALLOCS |
Pablo Galindo | 49c75a8 | 2018-10-28 15:02:17 +0000 | [diff] [blame] | 39 | Py_ssize_t _Py_quick_int_allocs, _Py_quick_neg_int_allocs; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 40 | #endif |
| 41 | |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 42 | static PyObject * |
Mark Dickinson | e441674 | 2009-02-15 15:14:57 +0000 | [diff] [blame] | 43 | get_small_int(sdigit ival) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 44 | { |
animalize | 6b51998 | 2019-09-06 14:00:56 +0800 | [diff] [blame] | 45 | assert(IS_SMALL_INT(ival)); |
Victor Stinner | 630c8df | 2019-12-17 13:02:18 +0100 | [diff] [blame] | 46 | PyThreadState *tstate = _PyThreadState_GET(); |
| 47 | PyObject *v = (PyObject*)tstate->interp->small_ints[ival + NSMALLNEGINTS]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 48 | Py_INCREF(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 49 | #ifdef COUNT_ALLOCS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 50 | if (ival >= 0) |
Pablo Galindo | 49c75a8 | 2018-10-28 15:02:17 +0000 | [diff] [blame] | 51 | _Py_quick_int_allocs++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 52 | else |
Pablo Galindo | 49c75a8 | 2018-10-28 15:02:17 +0000 | [diff] [blame] | 53 | _Py_quick_neg_int_allocs++; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 54 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 55 | return v; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 56 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 57 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 58 | static PyLongObject * |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 59 | maybe_small_long(PyLongObject *v) |
| 60 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 61 | if (v && Py_ABS(Py_SIZE(v)) <= 1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 62 | sdigit ival = MEDIUM_VALUE(v); |
animalize | 6b51998 | 2019-09-06 14:00:56 +0800 | [diff] [blame] | 63 | if (IS_SMALL_INT(ival)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 64 | Py_DECREF(v); |
| 65 | return (PyLongObject *)get_small_int(ival); |
| 66 | } |
| 67 | } |
| 68 | return v; |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 69 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 70 | #else |
animalize | 6b51998 | 2019-09-06 14:00:56 +0800 | [diff] [blame] | 71 | #define IS_SMALL_INT(ival) 0 |
Sergey Fedoseev | c6734ee | 2019-09-12 19:41:14 +0500 | [diff] [blame] | 72 | #define IS_SMALL_UINT(ival) 0 |
animalize | 6b51998 | 2019-09-06 14:00:56 +0800 | [diff] [blame] | 73 | #define get_small_int(ival) (Py_UNREACHABLE(), NULL) |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 74 | #define maybe_small_long(val) (val) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 75 | #endif |
| 76 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 77 | /* If a freshly-allocated int is already shared, it must |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 78 | be a small integer, so negating it must go to PyLong_FromLong */ |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 79 | Py_LOCAL_INLINE(void) |
| 80 | _PyLong_Negate(PyLongObject **x_p) |
| 81 | { |
| 82 | PyLongObject *x; |
| 83 | |
| 84 | x = (PyLongObject *)*x_p; |
| 85 | if (Py_REFCNT(x) == 1) { |
| 86 | Py_SIZE(x) = -Py_SIZE(x); |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | *x_p = (PyLongObject *)PyLong_FromLong(-MEDIUM_VALUE(x)); |
| 91 | Py_DECREF(x); |
| 92 | } |
| 93 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 94 | /* For int multiplication, use the O(N**2) school algorithm unless |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 95 | * both operands contain more than KARATSUBA_CUTOFF digits (this |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 96 | * being an internal Python int digit, in base BASE). |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 97 | */ |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 98 | #define KARATSUBA_CUTOFF 70 |
| 99 | #define KARATSUBA_SQUARE_CUTOFF (2 * KARATSUBA_CUTOFF) |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 100 | |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 101 | /* For exponentiation, use the binary left-to-right algorithm |
| 102 | * unless the exponent contains more than FIVEARY_CUTOFF digits. |
| 103 | * In that case, do 5 bits at a time. The potential drawback is that |
| 104 | * a table of 2**5 intermediate results is computed. |
| 105 | */ |
| 106 | #define FIVEARY_CUTOFF 8 |
| 107 | |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 108 | #define SIGCHECK(PyTryBlock) \ |
| 109 | do { \ |
| 110 | if (PyErr_CheckSignals()) PyTryBlock \ |
| 111 | } while(0) |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 112 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 113 | /* Normalize (remove leading zeros from) an int object. |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 114 | Doesn't attempt to free the storage--in most cases, due to the nature |
| 115 | of the algorithms used, this could save at most be one word anyway. */ |
| 116 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 117 | static PyLongObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 118 | long_normalize(PyLongObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 119 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 120 | Py_ssize_t j = Py_ABS(Py_SIZE(v)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 121 | Py_ssize_t i = j; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 123 | while (i > 0 && v->ob_digit[i-1] == 0) |
| 124 | --i; |
| 125 | if (i != j) |
| 126 | Py_SIZE(v) = (Py_SIZE(v) < 0) ? -(i) : i; |
| 127 | return v; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 130 | /* _PyLong_FromNbInt: Convert the given object to a PyLongObject |
| 131 | using the nb_int slot, if available. Raise TypeError if either the |
| 132 | nb_int slot is not available or the result of the call to nb_int |
| 133 | returns something not of type int. |
| 134 | */ |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 135 | PyObject * |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 136 | _PyLong_FromNbInt(PyObject *integral) |
| 137 | { |
| 138 | PyNumberMethods *nb; |
| 139 | PyObject *result; |
| 140 | |
| 141 | /* Fast path for the case that we already have an int. */ |
| 142 | if (PyLong_CheckExact(integral)) { |
| 143 | Py_INCREF(integral); |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 144 | return integral; |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | nb = Py_TYPE(integral)->tp_as_number; |
| 148 | if (nb == NULL || nb->nb_int == NULL) { |
| 149 | PyErr_Format(PyExc_TypeError, |
| 150 | "an integer is required (got type %.200s)", |
| 151 | Py_TYPE(integral)->tp_name); |
| 152 | return NULL; |
| 153 | } |
| 154 | |
| 155 | /* Convert using the nb_int slot, which should return something |
| 156 | of exact type int. */ |
| 157 | result = nb->nb_int(integral); |
| 158 | if (!result || PyLong_CheckExact(result)) |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 159 | return result; |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 160 | if (!PyLong_Check(result)) { |
| 161 | PyErr_Format(PyExc_TypeError, |
| 162 | "__int__ returned non-int (type %.200s)", |
| 163 | result->ob_type->tp_name); |
| 164 | Py_DECREF(result); |
| 165 | return NULL; |
| 166 | } |
| 167 | /* Issue #17576: warn if 'result' not of exact type int. */ |
| 168 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 169 | "__int__ returned non-int (type %.200s). " |
| 170 | "The ability to return an instance of a strict subclass of int " |
| 171 | "is deprecated, and may be removed in a future version of Python.", |
| 172 | result->ob_type->tp_name)) { |
| 173 | Py_DECREF(result); |
| 174 | return NULL; |
| 175 | } |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 176 | return result; |
| 177 | } |
| 178 | |
| 179 | /* Convert the given object to a PyLongObject using the nb_index or |
| 180 | nb_int slots, if available (the latter is deprecated). |
| 181 | Raise TypeError if either nb_index and nb_int slots are not |
| 182 | available or the result of the call to nb_index or nb_int |
| 183 | returns something not of type int. |
| 184 | Should be replaced with PyNumber_Index after the end of the |
| 185 | deprecation period. |
| 186 | */ |
| 187 | PyObject * |
| 188 | _PyLong_FromNbIndexOrNbInt(PyObject *integral) |
| 189 | { |
| 190 | PyNumberMethods *nb; |
| 191 | PyObject *result; |
| 192 | |
| 193 | /* Fast path for the case that we already have an int. */ |
| 194 | if (PyLong_CheckExact(integral)) { |
| 195 | Py_INCREF(integral); |
| 196 | return integral; |
| 197 | } |
| 198 | |
| 199 | nb = Py_TYPE(integral)->tp_as_number; |
| 200 | if (nb == NULL || (nb->nb_index == NULL && nb->nb_int == NULL)) { |
| 201 | PyErr_Format(PyExc_TypeError, |
| 202 | "an integer is required (got type %.200s)", |
| 203 | Py_TYPE(integral)->tp_name); |
| 204 | return NULL; |
| 205 | } |
| 206 | |
| 207 | if (nb->nb_index) { |
| 208 | /* Convert using the nb_index slot, which should return something |
| 209 | of exact type int. */ |
| 210 | result = nb->nb_index(integral); |
| 211 | if (!result || PyLong_CheckExact(result)) |
| 212 | return result; |
| 213 | if (!PyLong_Check(result)) { |
| 214 | PyErr_Format(PyExc_TypeError, |
| 215 | "__index__ returned non-int (type %.200s)", |
| 216 | result->ob_type->tp_name); |
| 217 | Py_DECREF(result); |
| 218 | return NULL; |
| 219 | } |
| 220 | /* Issue #17576: warn if 'result' not of exact type int. */ |
| 221 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 222 | "__index__ returned non-int (type %.200s). " |
| 223 | "The ability to return an instance of a strict subclass of int " |
| 224 | "is deprecated, and may be removed in a future version of Python.", |
| 225 | result->ob_type->tp_name)) |
| 226 | { |
| 227 | Py_DECREF(result); |
| 228 | return NULL; |
| 229 | } |
| 230 | return result; |
| 231 | } |
| 232 | |
| 233 | result = _PyLong_FromNbInt(integral); |
| 234 | if (result && PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 235 | "an integer is required (got type %.200s). " |
| 236 | "Implicit conversion to integers using __int__ is deprecated, " |
| 237 | "and may be removed in a future version of Python.", |
| 238 | Py_TYPE(integral)->tp_name)) |
| 239 | { |
| 240 | Py_DECREF(result); |
| 241 | return NULL; |
| 242 | } |
| 243 | return result; |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 247 | /* Allocate a new int object with size digits. |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 248 | Return NULL and set exception if we run out of memory. */ |
| 249 | |
Mark Dickinson | 5a74bf6 | 2009-02-15 11:04:38 +0000 | [diff] [blame] | 250 | #define MAX_LONG_DIGITS \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 251 | ((PY_SSIZE_T_MAX - offsetof(PyLongObject, ob_digit))/sizeof(digit)) |
Mark Dickinson | 5a74bf6 | 2009-02-15 11:04:38 +0000 | [diff] [blame] | 252 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 253 | PyLongObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 254 | _PyLong_New(Py_ssize_t size) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 255 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 256 | PyLongObject *result; |
| 257 | /* Number of bytes needed is: offsetof(PyLongObject, ob_digit) + |
| 258 | sizeof(digit)*size. Previous incarnations of this code used |
| 259 | sizeof(PyVarObject) instead of the offsetof, but this risks being |
| 260 | incorrect in the presence of padding between the PyVarObject header |
| 261 | and the digits. */ |
| 262 | if (size > (Py_ssize_t)MAX_LONG_DIGITS) { |
| 263 | PyErr_SetString(PyExc_OverflowError, |
| 264 | "too many digits in integer"); |
| 265 | return NULL; |
| 266 | } |
| 267 | result = PyObject_MALLOC(offsetof(PyLongObject, ob_digit) + |
| 268 | size*sizeof(digit)); |
| 269 | if (!result) { |
| 270 | PyErr_NoMemory(); |
| 271 | return NULL; |
| 272 | } |
Victor Stinner | b509d52 | 2018-11-23 14:27:38 +0100 | [diff] [blame] | 273 | return (PyLongObject*)PyObject_INIT_VAR(result, &PyLong_Type, size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 276 | PyObject * |
| 277 | _PyLong_Copy(PyLongObject *src) |
| 278 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 279 | PyLongObject *result; |
| 280 | Py_ssize_t i; |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 281 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 282 | assert(src != NULL); |
| 283 | i = Py_SIZE(src); |
| 284 | if (i < 0) |
| 285 | i = -(i); |
| 286 | if (i < 2) { |
Mark Dickinson | bcc17ee | 2012-04-20 21:42:49 +0100 | [diff] [blame] | 287 | sdigit ival = MEDIUM_VALUE(src); |
animalize | 6b51998 | 2019-09-06 14:00:56 +0800 | [diff] [blame] | 288 | if (IS_SMALL_INT(ival)) { |
Greg Price | 5e63ab0 | 2019-08-24 10:19:37 -0700 | [diff] [blame] | 289 | return get_small_int(ival); |
| 290 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 291 | } |
| 292 | result = _PyLong_New(i); |
| 293 | if (result != NULL) { |
| 294 | Py_SIZE(result) = Py_SIZE(src); |
| 295 | while (--i >= 0) |
| 296 | result->ob_digit[i] = src->ob_digit[i]; |
| 297 | } |
| 298 | return (PyObject *)result; |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 301 | /* Create a new int object from a C long int */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 302 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 303 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 304 | PyLong_FromLong(long ival) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 305 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 306 | PyLongObject *v; |
| 307 | unsigned long abs_ival; |
| 308 | unsigned long t; /* unsigned so >> doesn't propagate sign bit */ |
| 309 | int ndigits = 0; |
Mark Dickinson | 36820dd | 2016-09-10 20:17:36 +0100 | [diff] [blame] | 310 | int sign; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 311 | |
animalize | 6b51998 | 2019-09-06 14:00:56 +0800 | [diff] [blame] | 312 | if (IS_SMALL_INT(ival)) { |
Greg Price | 5e63ab0 | 2019-08-24 10:19:37 -0700 | [diff] [blame] | 313 | return get_small_int((sdigit)ival); |
| 314 | } |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 315 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 316 | if (ival < 0) { |
| 317 | /* negate: can't write this as abs_ival = -ival since that |
| 318 | invokes undefined behaviour when ival is LONG_MIN */ |
| 319 | abs_ival = 0U-(unsigned long)ival; |
| 320 | sign = -1; |
| 321 | } |
| 322 | else { |
| 323 | abs_ival = (unsigned long)ival; |
Mark Dickinson | 36820dd | 2016-09-10 20:17:36 +0100 | [diff] [blame] | 324 | sign = ival == 0 ? 0 : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 325 | } |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 326 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 327 | /* Fast path for single-digit ints */ |
| 328 | if (!(abs_ival >> PyLong_SHIFT)) { |
| 329 | v = _PyLong_New(1); |
| 330 | if (v) { |
| 331 | Py_SIZE(v) = sign; |
| 332 | v->ob_digit[0] = Py_SAFE_DOWNCAST( |
| 333 | abs_ival, unsigned long, digit); |
| 334 | } |
| 335 | return (PyObject*)v; |
| 336 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 337 | |
Mark Dickinson | 249b898 | 2009-04-27 19:41:00 +0000 | [diff] [blame] | 338 | #if PyLong_SHIFT==15 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 339 | /* 2 digits */ |
| 340 | if (!(abs_ival >> 2*PyLong_SHIFT)) { |
| 341 | v = _PyLong_New(2); |
| 342 | if (v) { |
| 343 | Py_SIZE(v) = 2*sign; |
| 344 | v->ob_digit[0] = Py_SAFE_DOWNCAST( |
| 345 | abs_ival & PyLong_MASK, unsigned long, digit); |
| 346 | v->ob_digit[1] = Py_SAFE_DOWNCAST( |
| 347 | abs_ival >> PyLong_SHIFT, unsigned long, digit); |
| 348 | } |
| 349 | return (PyObject*)v; |
| 350 | } |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 351 | #endif |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 352 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 353 | /* Larger numbers: loop to determine number of digits */ |
| 354 | t = abs_ival; |
| 355 | while (t) { |
| 356 | ++ndigits; |
| 357 | t >>= PyLong_SHIFT; |
| 358 | } |
| 359 | v = _PyLong_New(ndigits); |
| 360 | if (v != NULL) { |
| 361 | digit *p = v->ob_digit; |
| 362 | Py_SIZE(v) = ndigits*sign; |
| 363 | t = abs_ival; |
| 364 | while (t) { |
| 365 | *p++ = Py_SAFE_DOWNCAST( |
| 366 | t & PyLong_MASK, unsigned long, digit); |
| 367 | t >>= PyLong_SHIFT; |
| 368 | } |
| 369 | } |
| 370 | return (PyObject *)v; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Sergey Fedoseev | c6734ee | 2019-09-12 19:41:14 +0500 | [diff] [blame] | 373 | #define PYLONG_FROM_UINT(INT_TYPE, ival) \ |
| 374 | do { \ |
| 375 | if (IS_SMALL_UINT(ival)) { \ |
Victor Stinner | 6314abc | 2019-10-01 13:29:53 +0200 | [diff] [blame] | 376 | return get_small_int((sdigit)(ival)); \ |
Sergey Fedoseev | c6734ee | 2019-09-12 19:41:14 +0500 | [diff] [blame] | 377 | } \ |
| 378 | /* Count the number of Python digits. */ \ |
| 379 | Py_ssize_t ndigits = 0; \ |
| 380 | INT_TYPE t = (ival); \ |
| 381 | while (t) { \ |
| 382 | ++ndigits; \ |
| 383 | t >>= PyLong_SHIFT; \ |
| 384 | } \ |
| 385 | PyLongObject *v = _PyLong_New(ndigits); \ |
| 386 | if (v == NULL) { \ |
| 387 | return NULL; \ |
| 388 | } \ |
| 389 | digit *p = v->ob_digit; \ |
| 390 | while ((ival)) { \ |
| 391 | *p++ = (digit)((ival) & PyLong_MASK); \ |
| 392 | (ival) >>= PyLong_SHIFT; \ |
| 393 | } \ |
| 394 | return (PyObject *)v; \ |
| 395 | } while(0) |
| 396 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 397 | /* Create a new int object from a C unsigned long int */ |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 398 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 399 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 400 | PyLong_FromUnsignedLong(unsigned long ival) |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 401 | { |
Sergey Fedoseev | c6734ee | 2019-09-12 19:41:14 +0500 | [diff] [blame] | 402 | PYLONG_FROM_UINT(unsigned long, ival); |
| 403 | } |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 404 | |
Sergey Fedoseev | c6734ee | 2019-09-12 19:41:14 +0500 | [diff] [blame] | 405 | /* Create a new int object from a C unsigned long long int. */ |
| 406 | |
| 407 | PyObject * |
| 408 | PyLong_FromUnsignedLongLong(unsigned long long ival) |
| 409 | { |
| 410 | PYLONG_FROM_UINT(unsigned long long, ival); |
| 411 | } |
| 412 | |
| 413 | /* Create a new int object from a C size_t. */ |
| 414 | |
| 415 | PyObject * |
| 416 | PyLong_FromSize_t(size_t ival) |
| 417 | { |
| 418 | PYLONG_FROM_UINT(size_t, ival); |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 421 | /* Create a new int object from a C double */ |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 422 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 423 | PyObject * |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 424 | PyLong_FromDouble(double dval) |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 425 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 426 | PyLongObject *v; |
| 427 | double frac; |
| 428 | int i, ndig, expo, neg; |
| 429 | neg = 0; |
| 430 | if (Py_IS_INFINITY(dval)) { |
| 431 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 432 | "cannot convert float infinity to integer"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 433 | return NULL; |
| 434 | } |
| 435 | if (Py_IS_NAN(dval)) { |
| 436 | PyErr_SetString(PyExc_ValueError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 437 | "cannot convert float NaN to integer"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 438 | return NULL; |
| 439 | } |
| 440 | if (dval < 0.0) { |
| 441 | neg = 1; |
| 442 | dval = -dval; |
| 443 | } |
| 444 | frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */ |
| 445 | if (expo <= 0) |
| 446 | return PyLong_FromLong(0L); |
| 447 | ndig = (expo-1) / PyLong_SHIFT + 1; /* Number of 'digits' in result */ |
| 448 | v = _PyLong_New(ndig); |
| 449 | if (v == NULL) |
| 450 | return NULL; |
| 451 | frac = ldexp(frac, (expo-1) % PyLong_SHIFT + 1); |
| 452 | for (i = ndig; --i >= 0; ) { |
| 453 | digit bits = (digit)frac; |
| 454 | v->ob_digit[i] = bits; |
| 455 | frac = frac - (double)bits; |
| 456 | frac = ldexp(frac, PyLong_SHIFT); |
| 457 | } |
| 458 | if (neg) |
| 459 | Py_SIZE(v) = -(Py_SIZE(v)); |
| 460 | return (PyObject *)v; |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 463 | /* Checking for overflow in PyLong_AsLong is a PITA since C doesn't define |
| 464 | * anything about what happens when a signed integer operation overflows, |
| 465 | * 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] | 466 | * then. The bit pattern for the largest positive signed long is |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 467 | * (unsigned long)LONG_MAX, and for the smallest negative signed long |
| 468 | * it is abs(LONG_MIN), which we could write -(unsigned long)LONG_MIN. |
| 469 | * However, some other compilers warn about applying unary minus to an |
| 470 | * unsigned operand. Hence the weird "0-". |
| 471 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 472 | #define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN) |
| 473 | #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] | 474 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 475 | /* 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] | 476 | method. |
| 477 | |
| 478 | On overflow, return -1 and set *overflow to 1 or -1 depending on the sign of |
| 479 | the result. Otherwise *overflow is 0. |
| 480 | |
| 481 | For other errors (e.g., TypeError), return -1 and set an error condition. |
| 482 | In this case *overflow will be 0. |
| 483 | */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 484 | |
| 485 | long |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 486 | PyLong_AsLongAndOverflow(PyObject *vv, int *overflow) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 487 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 488 | /* This version by Tim Peters */ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 489 | PyLongObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 490 | unsigned long x, prev; |
| 491 | long res; |
| 492 | Py_ssize_t i; |
| 493 | int sign; |
| 494 | int do_decref = 0; /* if nb_int was called */ |
Guido van Rossum | f753181 | 1998-05-26 14:33:37 +0000 | [diff] [blame] | 495 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 496 | *overflow = 0; |
| 497 | if (vv == NULL) { |
| 498 | PyErr_BadInternalCall(); |
| 499 | return -1; |
| 500 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 501 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 502 | if (PyLong_Check(vv)) { |
| 503 | v = (PyLongObject *)vv; |
| 504 | } |
| 505 | else { |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 506 | v = (PyLongObject *)_PyLong_FromNbIndexOrNbInt(vv); |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 507 | if (v == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 508 | return -1; |
| 509 | do_decref = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 510 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 511 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 512 | res = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 513 | i = Py_SIZE(v); |
Guido van Rossum | f753181 | 1998-05-26 14:33:37 +0000 | [diff] [blame] | 514 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 515 | switch (i) { |
| 516 | case -1: |
| 517 | res = -(sdigit)v->ob_digit[0]; |
| 518 | break; |
| 519 | case 0: |
| 520 | res = 0; |
| 521 | break; |
| 522 | case 1: |
| 523 | res = v->ob_digit[0]; |
| 524 | break; |
| 525 | default: |
| 526 | sign = 1; |
| 527 | x = 0; |
| 528 | if (i < 0) { |
| 529 | sign = -1; |
| 530 | i = -(i); |
| 531 | } |
| 532 | while (--i >= 0) { |
| 533 | prev = x; |
| 534 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 535 | if ((x >> PyLong_SHIFT) != prev) { |
| 536 | *overflow = sign; |
| 537 | goto exit; |
| 538 | } |
| 539 | } |
| 540 | /* Haven't lost any bits, but casting to long requires extra |
| 541 | * care (see comment above). |
| 542 | */ |
| 543 | if (x <= (unsigned long)LONG_MAX) { |
| 544 | res = (long)x * sign; |
| 545 | } |
| 546 | else if (sign < 0 && x == PY_ABS_LONG_MIN) { |
| 547 | res = LONG_MIN; |
| 548 | } |
| 549 | else { |
| 550 | *overflow = sign; |
| 551 | /* res is already set to -1 */ |
| 552 | } |
| 553 | } |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 554 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 555 | if (do_decref) { |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 556 | Py_DECREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 557 | } |
| 558 | return res; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 561 | /* 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] | 562 | method. Return -1 and set an error if overflow occurs. */ |
| 563 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 564 | long |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 565 | PyLong_AsLong(PyObject *obj) |
| 566 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 567 | int overflow; |
| 568 | long result = PyLong_AsLongAndOverflow(obj, &overflow); |
| 569 | if (overflow) { |
| 570 | /* XXX: could be cute and give a different |
| 571 | message for overflow == -1 */ |
| 572 | PyErr_SetString(PyExc_OverflowError, |
| 573 | "Python int too large to convert to C long"); |
| 574 | } |
| 575 | return result; |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 578 | /* 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] | 579 | method. Return -1 and set an error if overflow occurs. */ |
| 580 | |
| 581 | int |
| 582 | _PyLong_AsInt(PyObject *obj) |
| 583 | { |
| 584 | int overflow; |
| 585 | long result = PyLong_AsLongAndOverflow(obj, &overflow); |
| 586 | if (overflow || result > INT_MAX || result < INT_MIN) { |
| 587 | /* XXX: could be cute and give a different |
| 588 | message for overflow == -1 */ |
| 589 | PyErr_SetString(PyExc_OverflowError, |
| 590 | "Python int too large to convert to C int"); |
| 591 | return -1; |
| 592 | } |
| 593 | return (int)result; |
| 594 | } |
| 595 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 596 | /* Get a Py_ssize_t from an int object. |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 597 | Returns -1 and sets an error condition if overflow occurs. */ |
| 598 | |
| 599 | Py_ssize_t |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 600 | PyLong_AsSsize_t(PyObject *vv) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 601 | PyLongObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 602 | size_t x, prev; |
| 603 | Py_ssize_t i; |
| 604 | int sign; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 605 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 606 | if (vv == NULL) { |
| 607 | PyErr_BadInternalCall(); |
| 608 | return -1; |
| 609 | } |
| 610 | if (!PyLong_Check(vv)) { |
| 611 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 612 | return -1; |
| 613 | } |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 614 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 615 | v = (PyLongObject *)vv; |
| 616 | i = Py_SIZE(v); |
| 617 | switch (i) { |
| 618 | case -1: return -(sdigit)v->ob_digit[0]; |
| 619 | case 0: return 0; |
| 620 | case 1: return v->ob_digit[0]; |
| 621 | } |
| 622 | sign = 1; |
| 623 | x = 0; |
| 624 | if (i < 0) { |
| 625 | sign = -1; |
| 626 | i = -(i); |
| 627 | } |
| 628 | while (--i >= 0) { |
| 629 | prev = x; |
| 630 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 631 | if ((x >> PyLong_SHIFT) != prev) |
| 632 | goto overflow; |
| 633 | } |
| 634 | /* Haven't lost any bits, but casting to a signed type requires |
| 635 | * extra care (see comment above). |
| 636 | */ |
| 637 | if (x <= (size_t)PY_SSIZE_T_MAX) { |
| 638 | return (Py_ssize_t)x * sign; |
| 639 | } |
| 640 | else if (sign < 0 && x == PY_ABS_SSIZE_T_MIN) { |
| 641 | return PY_SSIZE_T_MIN; |
| 642 | } |
| 643 | /* else overflow */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 644 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 645 | overflow: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 646 | PyErr_SetString(PyExc_OverflowError, |
| 647 | "Python int too large to convert to C ssize_t"); |
| 648 | return -1; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 651 | /* Get a C unsigned long int from an int object. |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 652 | Returns -1 and sets an error condition if overflow occurs. */ |
| 653 | |
| 654 | unsigned long |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 655 | PyLong_AsUnsignedLong(PyObject *vv) |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 656 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 657 | PyLongObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 658 | unsigned long x, prev; |
| 659 | Py_ssize_t i; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 660 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 661 | if (vv == NULL) { |
| 662 | PyErr_BadInternalCall(); |
| 663 | return (unsigned long)-1; |
| 664 | } |
| 665 | if (!PyLong_Check(vv)) { |
| 666 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 667 | return (unsigned long)-1; |
| 668 | } |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 669 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 670 | v = (PyLongObject *)vv; |
| 671 | i = Py_SIZE(v); |
| 672 | x = 0; |
| 673 | if (i < 0) { |
| 674 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 675 | "can't convert negative value to unsigned int"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 676 | return (unsigned long) -1; |
| 677 | } |
| 678 | switch (i) { |
| 679 | case 0: return 0; |
| 680 | case 1: return v->ob_digit[0]; |
| 681 | } |
| 682 | while (--i >= 0) { |
| 683 | prev = x; |
| 684 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 685 | if ((x >> PyLong_SHIFT) != prev) { |
| 686 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 02515f7 | 2013-08-03 12:08:22 +0100 | [diff] [blame] | 687 | "Python int too large to convert " |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 688 | "to C unsigned long"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 689 | return (unsigned long) -1; |
| 690 | } |
| 691 | } |
| 692 | return x; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 695 | /* 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] | 696 | an error condition if overflow occurs. */ |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 697 | |
| 698 | size_t |
| 699 | PyLong_AsSize_t(PyObject *vv) |
| 700 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 701 | PyLongObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 702 | size_t x, prev; |
| 703 | Py_ssize_t i; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 704 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 705 | if (vv == NULL) { |
| 706 | PyErr_BadInternalCall(); |
| 707 | return (size_t) -1; |
| 708 | } |
| 709 | if (!PyLong_Check(vv)) { |
| 710 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 711 | return (size_t)-1; |
| 712 | } |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 713 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 714 | v = (PyLongObject *)vv; |
| 715 | i = Py_SIZE(v); |
| 716 | x = 0; |
| 717 | if (i < 0) { |
| 718 | PyErr_SetString(PyExc_OverflowError, |
| 719 | "can't convert negative value to size_t"); |
| 720 | return (size_t) -1; |
| 721 | } |
| 722 | switch (i) { |
| 723 | case 0: return 0; |
| 724 | case 1: return v->ob_digit[0]; |
| 725 | } |
| 726 | while (--i >= 0) { |
| 727 | prev = x; |
| 728 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 729 | if ((x >> PyLong_SHIFT) != prev) { |
| 730 | PyErr_SetString(PyExc_OverflowError, |
| 731 | "Python int too large to convert to C size_t"); |
Stefan Krah | b77c6c6 | 2011-09-12 16:22:47 +0200 | [diff] [blame] | 732 | return (size_t) -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 733 | } |
| 734 | } |
| 735 | return x; |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 736 | } |
| 737 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 738 | /* 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] | 739 | Returns -1 and sets an error condition if an error occurs. */ |
| 740 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 741 | static unsigned long |
| 742 | _PyLong_AsUnsignedLongMask(PyObject *vv) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 743 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 744 | PyLongObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 745 | unsigned long x; |
| 746 | Py_ssize_t i; |
| 747 | int sign; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 748 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 749 | if (vv == NULL || !PyLong_Check(vv)) { |
| 750 | PyErr_BadInternalCall(); |
| 751 | return (unsigned long) -1; |
| 752 | } |
| 753 | v = (PyLongObject *)vv; |
| 754 | i = Py_SIZE(v); |
| 755 | switch (i) { |
| 756 | case 0: return 0; |
| 757 | case 1: return v->ob_digit[0]; |
| 758 | } |
| 759 | sign = 1; |
| 760 | x = 0; |
| 761 | if (i < 0) { |
| 762 | sign = -1; |
| 763 | i = -i; |
| 764 | } |
| 765 | while (--i >= 0) { |
| 766 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 767 | } |
| 768 | return x * sign; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 769 | } |
| 770 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 771 | unsigned long |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 772 | PyLong_AsUnsignedLongMask(PyObject *op) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 773 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 774 | PyLongObject *lo; |
| 775 | unsigned long val; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 776 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 777 | if (op == NULL) { |
| 778 | PyErr_BadInternalCall(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 779 | return (unsigned long)-1; |
| 780 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 781 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 782 | if (PyLong_Check(op)) { |
| 783 | return _PyLong_AsUnsignedLongMask(op); |
| 784 | } |
| 785 | |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 786 | lo = (PyLongObject *)_PyLong_FromNbIndexOrNbInt(op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 787 | if (lo == NULL) |
| 788 | return (unsigned long)-1; |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 789 | |
| 790 | val = _PyLong_AsUnsignedLongMask((PyObject *)lo); |
| 791 | Py_DECREF(lo); |
| 792 | return val; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 793 | } |
| 794 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 795 | int |
| 796 | _PyLong_Sign(PyObject *vv) |
| 797 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 798 | PyLongObject *v = (PyLongObject *)vv; |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 799 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 800 | assert(v != NULL); |
| 801 | assert(PyLong_Check(v)); |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 802 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 803 | return Py_SIZE(v) == 0 ? 0 : (Py_SIZE(v) < 0 ? -1 : 1); |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 806 | size_t |
| 807 | _PyLong_NumBits(PyObject *vv) |
| 808 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 809 | PyLongObject *v = (PyLongObject *)vv; |
| 810 | size_t result = 0; |
| 811 | Py_ssize_t ndigits; |
Serhiy Storchaka | b2e64f9 | 2016-11-08 20:34:22 +0200 | [diff] [blame] | 812 | int msd_bits; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 813 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 814 | assert(v != NULL); |
| 815 | assert(PyLong_Check(v)); |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 816 | ndigits = Py_ABS(Py_SIZE(v)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 817 | assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0); |
| 818 | if (ndigits > 0) { |
| 819 | digit msd = v->ob_digit[ndigits - 1]; |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 820 | if ((size_t)(ndigits - 1) > SIZE_MAX / (size_t)PyLong_SHIFT) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 821 | goto Overflow; |
Mark Dickinson | fc9adb6 | 2012-10-06 18:50:02 +0100 | [diff] [blame] | 822 | result = (size_t)(ndigits - 1) * (size_t)PyLong_SHIFT; |
Niklas Fiekas | c5b7900 | 2020-01-16 15:09:19 +0100 | [diff] [blame^] | 823 | msd_bits = _Py_bit_length(msd); |
Serhiy Storchaka | b2e64f9 | 2016-11-08 20:34:22 +0200 | [diff] [blame] | 824 | if (SIZE_MAX - msd_bits < result) |
| 825 | goto Overflow; |
| 826 | result += msd_bits; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 827 | } |
| 828 | return result; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 829 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 830 | Overflow: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 831 | PyErr_SetString(PyExc_OverflowError, "int has too many bits " |
| 832 | "to express in a platform size_t"); |
| 833 | return (size_t)-1; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 834 | } |
| 835 | |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 836 | PyObject * |
| 837 | _PyLong_FromByteArray(const unsigned char* bytes, size_t n, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 838 | int little_endian, int is_signed) |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 839 | { |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 840 | const unsigned char* pstartbyte; /* LSB of bytes */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 841 | int incr; /* direction to move pstartbyte */ |
| 842 | const unsigned char* pendbyte; /* MSB of bytes */ |
| 843 | size_t numsignificantbytes; /* number of bytes that matter */ |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 844 | Py_ssize_t ndigits; /* number of Python int digits */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 845 | PyLongObject* v; /* result */ |
| 846 | Py_ssize_t idigit = 0; /* next free index in v->ob_digit */ |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 847 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 848 | if (n == 0) |
| 849 | return PyLong_FromLong(0L); |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 850 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 851 | if (little_endian) { |
| 852 | pstartbyte = bytes; |
| 853 | pendbyte = bytes + n - 1; |
| 854 | incr = 1; |
| 855 | } |
| 856 | else { |
| 857 | pstartbyte = bytes + n - 1; |
| 858 | pendbyte = bytes; |
| 859 | incr = -1; |
| 860 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 861 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 862 | if (is_signed) |
| 863 | is_signed = *pendbyte >= 0x80; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 864 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 865 | /* Compute numsignificantbytes. This consists of finding the most |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 866 | significant byte. Leading 0 bytes are insignificant if the number |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 867 | is positive, and leading 0xff bytes if negative. */ |
| 868 | { |
| 869 | size_t i; |
| 870 | const unsigned char* p = pendbyte; |
| 871 | const int pincr = -incr; /* search MSB to LSB */ |
Martin Panter | eb99570 | 2016-07-28 01:11:04 +0000 | [diff] [blame] | 872 | const unsigned char insignificant = is_signed ? 0xff : 0x00; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 873 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 874 | for (i = 0; i < n; ++i, p += pincr) { |
Martin Panter | eb99570 | 2016-07-28 01:11:04 +0000 | [diff] [blame] | 875 | if (*p != insignificant) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 876 | break; |
| 877 | } |
| 878 | numsignificantbytes = n - i; |
| 879 | /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so |
| 880 | actually has 2 significant bytes. OTOH, 0xff0001 == |
| 881 | -0x00ffff, so we wouldn't *need* to bump it there; but we |
| 882 | do for 0xffff = -0x0001. To be safe without bothering to |
| 883 | check every case, bump it regardless. */ |
| 884 | if (is_signed && numsignificantbytes < n) |
| 885 | ++numsignificantbytes; |
| 886 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 887 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 888 | /* How many Python int digits do we need? We have |
| 889 | 8*numsignificantbytes bits, and each Python int digit has |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 890 | PyLong_SHIFT bits, so it's the ceiling of the quotient. */ |
| 891 | /* catch overflow before it happens */ |
| 892 | if (numsignificantbytes > (PY_SSIZE_T_MAX - PyLong_SHIFT) / 8) { |
| 893 | PyErr_SetString(PyExc_OverflowError, |
| 894 | "byte array too long to convert to int"); |
| 895 | return NULL; |
| 896 | } |
| 897 | ndigits = (numsignificantbytes * 8 + PyLong_SHIFT - 1) / PyLong_SHIFT; |
| 898 | v = _PyLong_New(ndigits); |
| 899 | if (v == NULL) |
| 900 | return NULL; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 901 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 902 | /* Copy the bits over. The tricky parts are computing 2's-comp on |
| 903 | the fly for signed numbers, and dealing with the mismatch between |
| 904 | 8-bit bytes and (probably) 15-bit Python digits.*/ |
| 905 | { |
| 906 | size_t i; |
| 907 | twodigits carry = 1; /* for 2's-comp calculation */ |
| 908 | twodigits accum = 0; /* sliding register */ |
| 909 | unsigned int accumbits = 0; /* number of bits in accum */ |
| 910 | const unsigned char* p = pstartbyte; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 911 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 912 | for (i = 0; i < numsignificantbytes; ++i, p += incr) { |
| 913 | twodigits thisbyte = *p; |
| 914 | /* Compute correction for 2's comp, if needed. */ |
| 915 | if (is_signed) { |
| 916 | thisbyte = (0xff ^ thisbyte) + carry; |
| 917 | carry = thisbyte >> 8; |
| 918 | thisbyte &= 0xff; |
| 919 | } |
| 920 | /* Because we're going LSB to MSB, thisbyte is |
| 921 | more significant than what's already in accum, |
| 922 | so needs to be prepended to accum. */ |
orenmn | 86aa269 | 2017-03-06 10:42:47 +0200 | [diff] [blame] | 923 | accum |= thisbyte << accumbits; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 924 | accumbits += 8; |
| 925 | if (accumbits >= PyLong_SHIFT) { |
| 926 | /* There's enough to fill a Python digit. */ |
| 927 | assert(idigit < ndigits); |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 928 | v->ob_digit[idigit] = (digit)(accum & PyLong_MASK); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 929 | ++idigit; |
| 930 | accum >>= PyLong_SHIFT; |
| 931 | accumbits -= PyLong_SHIFT; |
| 932 | assert(accumbits < PyLong_SHIFT); |
| 933 | } |
| 934 | } |
| 935 | assert(accumbits < PyLong_SHIFT); |
| 936 | if (accumbits) { |
| 937 | assert(idigit < ndigits); |
| 938 | v->ob_digit[idigit] = (digit)accum; |
| 939 | ++idigit; |
| 940 | } |
| 941 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 942 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 943 | Py_SIZE(v) = is_signed ? -idigit : idigit; |
| 944 | return (PyObject *)long_normalize(v); |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | int |
| 948 | _PyLong_AsByteArray(PyLongObject* v, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 949 | unsigned char* bytes, size_t n, |
| 950 | int little_endian, int is_signed) |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 951 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 952 | Py_ssize_t i; /* index into v->ob_digit */ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 953 | Py_ssize_t ndigits; /* |v->ob_size| */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 954 | twodigits accum; /* sliding register */ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 955 | unsigned int accumbits; /* # bits in accum */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 956 | int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */ |
| 957 | digit carry; /* for computing 2's-comp */ |
| 958 | size_t j; /* # bytes filled */ |
| 959 | unsigned char* p; /* pointer to next byte in bytes */ |
| 960 | int pincr; /* direction to move p */ |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 961 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 962 | assert(v != NULL && PyLong_Check(v)); |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 963 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 964 | if (Py_SIZE(v) < 0) { |
| 965 | ndigits = -(Py_SIZE(v)); |
| 966 | if (!is_signed) { |
| 967 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 968 | "can't convert negative int to unsigned"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 969 | return -1; |
| 970 | } |
| 971 | do_twos_comp = 1; |
| 972 | } |
| 973 | else { |
| 974 | ndigits = Py_SIZE(v); |
| 975 | do_twos_comp = 0; |
| 976 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 977 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 978 | if (little_endian) { |
| 979 | p = bytes; |
| 980 | pincr = 1; |
| 981 | } |
| 982 | else { |
| 983 | p = bytes + n - 1; |
| 984 | pincr = -1; |
| 985 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 986 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 987 | /* Copy over all the Python digits. |
| 988 | It's crucial that every Python digit except for the MSD contribute |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 989 | 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] | 990 | normalized. */ |
| 991 | assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0); |
| 992 | j = 0; |
| 993 | accum = 0; |
| 994 | accumbits = 0; |
| 995 | carry = do_twos_comp ? 1 : 0; |
| 996 | for (i = 0; i < ndigits; ++i) { |
| 997 | digit thisdigit = v->ob_digit[i]; |
| 998 | if (do_twos_comp) { |
| 999 | thisdigit = (thisdigit ^ PyLong_MASK) + carry; |
| 1000 | carry = thisdigit >> PyLong_SHIFT; |
| 1001 | thisdigit &= PyLong_MASK; |
| 1002 | } |
| 1003 | /* Because we're going LSB to MSB, thisdigit is more |
| 1004 | significant than what's already in accum, so needs to be |
| 1005 | prepended to accum. */ |
| 1006 | accum |= (twodigits)thisdigit << accumbits; |
Tim Peters | 8bc84b4 | 2001-06-12 19:17:03 +0000 | [diff] [blame] | 1007 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1008 | /* The most-significant digit may be (probably is) at least |
| 1009 | partly empty. */ |
| 1010 | if (i == ndigits - 1) { |
| 1011 | /* Count # of sign bits -- they needn't be stored, |
| 1012 | * although for signed conversion we need later to |
| 1013 | * make sure at least one sign bit gets stored. */ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 1014 | digit s = do_twos_comp ? thisdigit ^ PyLong_MASK : thisdigit; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1015 | while (s != 0) { |
| 1016 | s >>= 1; |
| 1017 | accumbits++; |
| 1018 | } |
| 1019 | } |
| 1020 | else |
| 1021 | accumbits += PyLong_SHIFT; |
Tim Peters | 8bc84b4 | 2001-06-12 19:17:03 +0000 | [diff] [blame] | 1022 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1023 | /* Store as many bytes as possible. */ |
| 1024 | while (accumbits >= 8) { |
| 1025 | if (j >= n) |
| 1026 | goto Overflow; |
| 1027 | ++j; |
| 1028 | *p = (unsigned char)(accum & 0xff); |
| 1029 | p += pincr; |
| 1030 | accumbits -= 8; |
| 1031 | accum >>= 8; |
| 1032 | } |
| 1033 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 1034 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1035 | /* Store the straggler (if any). */ |
| 1036 | assert(accumbits < 8); |
| 1037 | assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */ |
| 1038 | if (accumbits > 0) { |
| 1039 | if (j >= n) |
| 1040 | goto Overflow; |
| 1041 | ++j; |
| 1042 | if (do_twos_comp) { |
| 1043 | /* Fill leading bits of the byte with sign bits |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1044 | (appropriately pretending that the int had an |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1045 | infinite supply of sign bits). */ |
| 1046 | accum |= (~(twodigits)0) << accumbits; |
| 1047 | } |
| 1048 | *p = (unsigned char)(accum & 0xff); |
| 1049 | p += pincr; |
| 1050 | } |
| 1051 | else if (j == n && n > 0 && is_signed) { |
| 1052 | /* The main loop filled the byte array exactly, so the code |
| 1053 | just above didn't get to ensure there's a sign bit, and the |
| 1054 | loop below wouldn't add one either. Make sure a sign bit |
| 1055 | exists. */ |
| 1056 | unsigned char msb = *(p - pincr); |
| 1057 | int sign_bit_set = msb >= 0x80; |
| 1058 | assert(accumbits == 0); |
| 1059 | if (sign_bit_set == do_twos_comp) |
| 1060 | return 0; |
| 1061 | else |
| 1062 | goto Overflow; |
| 1063 | } |
Tim Peters | 05607ad | 2001-06-13 21:01:27 +0000 | [diff] [blame] | 1064 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1065 | /* Fill remaining bytes with copies of the sign bit. */ |
| 1066 | { |
| 1067 | unsigned char signbyte = do_twos_comp ? 0xffU : 0U; |
| 1068 | for ( ; j < n; ++j, p += pincr) |
| 1069 | *p = signbyte; |
| 1070 | } |
Tim Peters | 05607ad | 2001-06-13 21:01:27 +0000 | [diff] [blame] | 1071 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1072 | return 0; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 1073 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 1074 | Overflow: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1075 | PyErr_SetString(PyExc_OverflowError, "int too big to convert"); |
| 1076 | return -1; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1077 | |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1080 | /* Create a new int object from a C pointer */ |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1081 | |
| 1082 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1083 | PyLong_FromVoidPtr(void *p) |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1084 | { |
Mark Dickinson | 9104479 | 2012-10-18 19:21:43 +0100 | [diff] [blame] | 1085 | #if SIZEOF_VOID_P <= SIZEOF_LONG |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 1086 | return PyLong_FromUnsignedLong((unsigned long)(uintptr_t)p); |
Mark Dickinson | 9104479 | 2012-10-18 19:21:43 +0100 | [diff] [blame] | 1087 | #else |
| 1088 | |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1089 | #if SIZEOF_LONG_LONG < SIZEOF_VOID_P |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1090 | # error "PyLong_FromVoidPtr: sizeof(long long) < sizeof(void*)" |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1091 | #endif |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 1092 | return PyLong_FromUnsignedLongLong((unsigned long long)(uintptr_t)p); |
Mark Dickinson | 9104479 | 2012-10-18 19:21:43 +0100 | [diff] [blame] | 1093 | #endif /* SIZEOF_VOID_P <= SIZEOF_LONG */ |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1094 | |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1097 | /* Get a C pointer from an int object. */ |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1098 | |
| 1099 | void * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1100 | PyLong_AsVoidPtr(PyObject *vv) |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1101 | { |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1102 | #if SIZEOF_VOID_P <= SIZEOF_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1103 | long x; |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1104 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1105 | if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0) |
| 1106 | x = PyLong_AsLong(vv); |
| 1107 | else |
| 1108 | x = PyLong_AsUnsignedLong(vv); |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1109 | #else |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1110 | |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1111 | #if SIZEOF_LONG_LONG < SIZEOF_VOID_P |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1112 | # error "PyLong_AsVoidPtr: sizeof(long long) < sizeof(void*)" |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1113 | #endif |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1114 | long long x; |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1115 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1116 | if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0) |
| 1117 | x = PyLong_AsLongLong(vv); |
| 1118 | else |
| 1119 | x = PyLong_AsUnsignedLongLong(vv); |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1120 | |
| 1121 | #endif /* SIZEOF_VOID_P <= SIZEOF_LONG */ |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1123 | if (x == -1 && PyErr_Occurred()) |
| 1124 | return NULL; |
| 1125 | return (void *)x; |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1128 | /* Initial long long support by Chris Herborth (chrish@qnx.com), later |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1129 | * rewritten to use the newer PyLong_{As,From}ByteArray API. |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1130 | */ |
| 1131 | |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 1132 | #define PY_ABS_LLONG_MIN (0-(unsigned long long)LLONG_MIN) |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1133 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1134 | /* Create a new int object from a C long long int. */ |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1135 | |
| 1136 | PyObject * |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1137 | PyLong_FromLongLong(long long ival) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1138 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1139 | PyLongObject *v; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1140 | unsigned long long abs_ival; |
| 1141 | unsigned long long t; /* unsigned so >> doesn't propagate sign bit */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1142 | int ndigits = 0; |
| 1143 | int negative = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1144 | |
animalize | 6b51998 | 2019-09-06 14:00:56 +0800 | [diff] [blame] | 1145 | if (IS_SMALL_INT(ival)) { |
Greg Price | 5e63ab0 | 2019-08-24 10:19:37 -0700 | [diff] [blame] | 1146 | return get_small_int((sdigit)ival); |
| 1147 | } |
| 1148 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1149 | if (ival < 0) { |
| 1150 | /* avoid signed overflow on negation; see comments |
| 1151 | in PyLong_FromLong above. */ |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1152 | abs_ival = (unsigned long long)(-1-ival) + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1153 | negative = 1; |
| 1154 | } |
| 1155 | else { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1156 | abs_ival = (unsigned long long)ival; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1157 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1158 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1159 | /* Count the number of Python digits. |
| 1160 | We used to pick 5 ("big enough for anything"), but that's a |
| 1161 | waste of time and space given that 5*15 = 75 bits are rarely |
| 1162 | needed. */ |
| 1163 | t = abs_ival; |
| 1164 | while (t) { |
| 1165 | ++ndigits; |
| 1166 | t >>= PyLong_SHIFT; |
| 1167 | } |
| 1168 | v = _PyLong_New(ndigits); |
| 1169 | if (v != NULL) { |
| 1170 | digit *p = v->ob_digit; |
| 1171 | Py_SIZE(v) = negative ? -ndigits : ndigits; |
| 1172 | t = abs_ival; |
| 1173 | while (t) { |
| 1174 | *p++ = (digit)(t & PyLong_MASK); |
| 1175 | t >>= PyLong_SHIFT; |
| 1176 | } |
| 1177 | } |
| 1178 | return (PyObject *)v; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1179 | } |
| 1180 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1181 | /* 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] | 1182 | |
| 1183 | PyObject * |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1184 | PyLong_FromSsize_t(Py_ssize_t ival) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1185 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1186 | PyLongObject *v; |
| 1187 | size_t abs_ival; |
| 1188 | size_t t; /* unsigned so >> doesn't propagate sign bit */ |
| 1189 | int ndigits = 0; |
| 1190 | int negative = 0; |
Mark Dickinson | 7ab6be2 | 2008-04-15 21:42:42 +0000 | [diff] [blame] | 1191 | |
animalize | 6b51998 | 2019-09-06 14:00:56 +0800 | [diff] [blame] | 1192 | if (IS_SMALL_INT(ival)) { |
Greg Price | 5e63ab0 | 2019-08-24 10:19:37 -0700 | [diff] [blame] | 1193 | return get_small_int((sdigit)ival); |
| 1194 | } |
| 1195 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1196 | if (ival < 0) { |
| 1197 | /* avoid signed overflow when ival = SIZE_T_MIN */ |
| 1198 | abs_ival = (size_t)(-1-ival)+1; |
| 1199 | negative = 1; |
| 1200 | } |
| 1201 | else { |
| 1202 | abs_ival = (size_t)ival; |
| 1203 | } |
Mark Dickinson | 7ab6be2 | 2008-04-15 21:42:42 +0000 | [diff] [blame] | 1204 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1205 | /* Count the number of Python digits. */ |
| 1206 | t = abs_ival; |
| 1207 | while (t) { |
| 1208 | ++ndigits; |
| 1209 | t >>= PyLong_SHIFT; |
| 1210 | } |
| 1211 | v = _PyLong_New(ndigits); |
| 1212 | if (v != NULL) { |
| 1213 | digit *p = v->ob_digit; |
| 1214 | Py_SIZE(v) = negative ? -ndigits : ndigits; |
| 1215 | t = abs_ival; |
| 1216 | while (t) { |
| 1217 | *p++ = (digit)(t & PyLong_MASK); |
| 1218 | t >>= PyLong_SHIFT; |
| 1219 | } |
| 1220 | } |
| 1221 | return (PyObject *)v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1224 | /* 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] | 1225 | __int__ method. Return -1 and set an error if overflow occurs. */ |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1226 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1227 | long long |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1228 | PyLong_AsLongLong(PyObject *vv) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1229 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1230 | PyLongObject *v; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1231 | long long bytes; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1232 | int res; |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1233 | int do_decref = 0; /* if nb_int was called */ |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1234 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1235 | if (vv == NULL) { |
| 1236 | PyErr_BadInternalCall(); |
| 1237 | return -1; |
| 1238 | } |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1239 | |
| 1240 | if (PyLong_Check(vv)) { |
| 1241 | v = (PyLongObject *)vv; |
| 1242 | } |
| 1243 | else { |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 1244 | v = (PyLongObject *)_PyLong_FromNbIndexOrNbInt(vv); |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1245 | if (v == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1246 | return -1; |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1247 | do_decref = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1248 | } |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1249 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1250 | res = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1251 | switch(Py_SIZE(v)) { |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1252 | case -1: |
| 1253 | bytes = -(sdigit)v->ob_digit[0]; |
| 1254 | break; |
| 1255 | case 0: |
| 1256 | bytes = 0; |
| 1257 | break; |
| 1258 | case 1: |
| 1259 | bytes = v->ob_digit[0]; |
| 1260 | break; |
| 1261 | default: |
| 1262 | res = _PyLong_AsByteArray((PyLongObject *)v, (unsigned char *)&bytes, |
Serhiy Storchaka | c4f3212 | 2013-12-11 21:26:36 +0200 | [diff] [blame] | 1263 | SIZEOF_LONG_LONG, PY_LITTLE_ENDIAN, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1264 | } |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1265 | if (do_decref) { |
| 1266 | Py_DECREF(v); |
| 1267 | } |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1268 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1269 | /* Plan 9 can't handle long long in ? : expressions */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1270 | if (res < 0) |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1271 | return (long long)-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1272 | else |
| 1273 | return bytes; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1274 | } |
| 1275 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1276 | /* Get a C unsigned long long int from an int object. |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1277 | Return -1 and set an error if overflow occurs. */ |
| 1278 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1279 | unsigned long long |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1280 | PyLong_AsUnsignedLongLong(PyObject *vv) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1281 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1282 | PyLongObject *v; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1283 | unsigned long long bytes; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1284 | int res; |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1285 | |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 1286 | if (vv == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1287 | PyErr_BadInternalCall(); |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1288 | return (unsigned long long)-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1289 | } |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 1290 | if (!PyLong_Check(vv)) { |
| 1291 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1292 | return (unsigned long long)-1; |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 1293 | } |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1294 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1295 | v = (PyLongObject*)vv; |
| 1296 | switch(Py_SIZE(v)) { |
| 1297 | case 0: return 0; |
| 1298 | case 1: return v->ob_digit[0]; |
| 1299 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1300 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 1301 | res = _PyLong_AsByteArray((PyLongObject *)vv, (unsigned char *)&bytes, |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 1302 | SIZEOF_LONG_LONG, PY_LITTLE_ENDIAN, 0); |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1303 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1304 | /* Plan 9 can't handle long long in ? : expressions */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1305 | if (res < 0) |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1306 | return (unsigned long long)res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1307 | else |
| 1308 | return bytes; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1309 | } |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1310 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1311 | /* 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] | 1312 | Returns -1 and sets an error condition if an error occurs. */ |
| 1313 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1314 | static unsigned long long |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1315 | _PyLong_AsUnsignedLongLongMask(PyObject *vv) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1316 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1317 | PyLongObject *v; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1318 | unsigned long long x; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1319 | Py_ssize_t i; |
| 1320 | int sign; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1321 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1322 | if (vv == NULL || !PyLong_Check(vv)) { |
| 1323 | PyErr_BadInternalCall(); |
Zackery Spytz | dc24765 | 2019-06-06 14:39:23 -0600 | [diff] [blame] | 1324 | return (unsigned long long) -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1325 | } |
| 1326 | v = (PyLongObject *)vv; |
| 1327 | switch(Py_SIZE(v)) { |
| 1328 | case 0: return 0; |
| 1329 | case 1: return v->ob_digit[0]; |
| 1330 | } |
| 1331 | i = Py_SIZE(v); |
| 1332 | sign = 1; |
| 1333 | x = 0; |
| 1334 | if (i < 0) { |
| 1335 | sign = -1; |
| 1336 | i = -i; |
| 1337 | } |
| 1338 | while (--i >= 0) { |
| 1339 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 1340 | } |
| 1341 | return x * sign; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1342 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1343 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1344 | unsigned long long |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1345 | PyLong_AsUnsignedLongLongMask(PyObject *op) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1346 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1347 | PyLongObject *lo; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1348 | unsigned long long val; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1349 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1350 | if (op == NULL) { |
| 1351 | PyErr_BadInternalCall(); |
Zackery Spytz | dc24765 | 2019-06-06 14:39:23 -0600 | [diff] [blame] | 1352 | return (unsigned long long)-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1353 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1354 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1355 | if (PyLong_Check(op)) { |
| 1356 | return _PyLong_AsUnsignedLongLongMask(op); |
| 1357 | } |
| 1358 | |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 1359 | lo = (PyLongObject *)_PyLong_FromNbIndexOrNbInt(op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1360 | if (lo == NULL) |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1361 | return (unsigned long long)-1; |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1362 | |
| 1363 | val = _PyLong_AsUnsignedLongLongMask((PyObject *)lo); |
| 1364 | Py_DECREF(lo); |
| 1365 | return val; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1366 | } |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1367 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1368 | /* 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] | 1369 | __int__ method. |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1370 | |
Mark Dickinson | 8d48b43 | 2011-10-23 20:47:14 +0100 | [diff] [blame] | 1371 | On overflow, return -1 and set *overflow to 1 or -1 depending on the sign of |
| 1372 | the result. Otherwise *overflow is 0. |
| 1373 | |
| 1374 | For other errors (e.g., TypeError), return -1 and set an error condition. |
| 1375 | In this case *overflow will be 0. |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1376 | */ |
| 1377 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1378 | long long |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1379 | PyLong_AsLongLongAndOverflow(PyObject *vv, int *overflow) |
| 1380 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1381 | /* This version by Tim Peters */ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1382 | PyLongObject *v; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1383 | unsigned long long x, prev; |
| 1384 | long long res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1385 | Py_ssize_t i; |
| 1386 | int sign; |
| 1387 | int do_decref = 0; /* if nb_int was called */ |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1388 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1389 | *overflow = 0; |
| 1390 | if (vv == NULL) { |
| 1391 | PyErr_BadInternalCall(); |
| 1392 | return -1; |
| 1393 | } |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1394 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1395 | if (PyLong_Check(vv)) { |
| 1396 | v = (PyLongObject *)vv; |
| 1397 | } |
| 1398 | else { |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 1399 | v = (PyLongObject *)_PyLong_FromNbIndexOrNbInt(vv); |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1400 | if (v == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1401 | return -1; |
| 1402 | do_decref = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1403 | } |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1404 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1405 | res = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1406 | i = Py_SIZE(v); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1407 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1408 | switch (i) { |
| 1409 | case -1: |
| 1410 | res = -(sdigit)v->ob_digit[0]; |
| 1411 | break; |
| 1412 | case 0: |
| 1413 | res = 0; |
| 1414 | break; |
| 1415 | case 1: |
| 1416 | res = v->ob_digit[0]; |
| 1417 | break; |
| 1418 | default: |
| 1419 | sign = 1; |
| 1420 | x = 0; |
| 1421 | if (i < 0) { |
| 1422 | sign = -1; |
| 1423 | i = -(i); |
| 1424 | } |
| 1425 | while (--i >= 0) { |
| 1426 | prev = x; |
| 1427 | x = (x << PyLong_SHIFT) + v->ob_digit[i]; |
| 1428 | if ((x >> PyLong_SHIFT) != prev) { |
| 1429 | *overflow = sign; |
| 1430 | goto exit; |
| 1431 | } |
| 1432 | } |
| 1433 | /* Haven't lost any bits, but casting to long requires extra |
| 1434 | * care (see comment above). |
| 1435 | */ |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 1436 | if (x <= (unsigned long long)LLONG_MAX) { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1437 | res = (long long)x * sign; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1438 | } |
| 1439 | else if (sign < 0 && x == PY_ABS_LLONG_MIN) { |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 1440 | res = LLONG_MIN; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1441 | } |
| 1442 | else { |
| 1443 | *overflow = sign; |
| 1444 | /* res is already set to -1 */ |
| 1445 | } |
| 1446 | } |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 1447 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1448 | if (do_decref) { |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1449 | Py_DECREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1450 | } |
| 1451 | return res; |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
Serhiy Storchaka | 7cb7bcf | 2018-07-26 13:22:16 +0300 | [diff] [blame] | 1454 | int |
| 1455 | _PyLong_UnsignedShort_Converter(PyObject *obj, void *ptr) |
| 1456 | { |
| 1457 | unsigned long uval; |
| 1458 | |
| 1459 | if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) { |
| 1460 | PyErr_SetString(PyExc_ValueError, "value must be positive"); |
| 1461 | return 0; |
| 1462 | } |
| 1463 | uval = PyLong_AsUnsignedLong(obj); |
| 1464 | if (uval == (unsigned long)-1 && PyErr_Occurred()) |
| 1465 | return 0; |
| 1466 | if (uval > USHRT_MAX) { |
| 1467 | PyErr_SetString(PyExc_OverflowError, |
| 1468 | "Python int too large for C unsigned short"); |
| 1469 | return 0; |
| 1470 | } |
| 1471 | |
| 1472 | *(unsigned short *)ptr = Py_SAFE_DOWNCAST(uval, unsigned long, unsigned short); |
| 1473 | return 1; |
| 1474 | } |
| 1475 | |
| 1476 | int |
| 1477 | _PyLong_UnsignedInt_Converter(PyObject *obj, void *ptr) |
| 1478 | { |
| 1479 | unsigned long uval; |
| 1480 | |
| 1481 | if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) { |
| 1482 | PyErr_SetString(PyExc_ValueError, "value must be positive"); |
| 1483 | return 0; |
| 1484 | } |
| 1485 | uval = PyLong_AsUnsignedLong(obj); |
| 1486 | if (uval == (unsigned long)-1 && PyErr_Occurred()) |
| 1487 | return 0; |
| 1488 | if (uval > UINT_MAX) { |
| 1489 | PyErr_SetString(PyExc_OverflowError, |
| 1490 | "Python int too large for C unsigned int"); |
| 1491 | return 0; |
| 1492 | } |
| 1493 | |
| 1494 | *(unsigned int *)ptr = Py_SAFE_DOWNCAST(uval, unsigned long, unsigned int); |
| 1495 | return 1; |
| 1496 | } |
| 1497 | |
| 1498 | int |
| 1499 | _PyLong_UnsignedLong_Converter(PyObject *obj, void *ptr) |
| 1500 | { |
| 1501 | unsigned long uval; |
| 1502 | |
| 1503 | if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) { |
| 1504 | PyErr_SetString(PyExc_ValueError, "value must be positive"); |
| 1505 | return 0; |
| 1506 | } |
| 1507 | uval = PyLong_AsUnsignedLong(obj); |
| 1508 | if (uval == (unsigned long)-1 && PyErr_Occurred()) |
| 1509 | return 0; |
| 1510 | |
| 1511 | *(unsigned long *)ptr = uval; |
| 1512 | return 1; |
| 1513 | } |
| 1514 | |
| 1515 | int |
| 1516 | _PyLong_UnsignedLongLong_Converter(PyObject *obj, void *ptr) |
| 1517 | { |
| 1518 | unsigned long long uval; |
| 1519 | |
| 1520 | if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) { |
| 1521 | PyErr_SetString(PyExc_ValueError, "value must be positive"); |
| 1522 | return 0; |
| 1523 | } |
| 1524 | uval = PyLong_AsUnsignedLongLong(obj); |
| 1525 | if (uval == (unsigned long long)-1 && PyErr_Occurred()) |
| 1526 | return 0; |
| 1527 | |
| 1528 | *(unsigned long long *)ptr = uval; |
| 1529 | return 1; |
| 1530 | } |
| 1531 | |
| 1532 | int |
| 1533 | _PyLong_Size_t_Converter(PyObject *obj, void *ptr) |
| 1534 | { |
| 1535 | size_t uval; |
| 1536 | |
| 1537 | if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) { |
| 1538 | PyErr_SetString(PyExc_ValueError, "value must be positive"); |
| 1539 | return 0; |
| 1540 | } |
| 1541 | uval = PyLong_AsSize_t(obj); |
| 1542 | if (uval == (size_t)-1 && PyErr_Occurred()) |
| 1543 | return 0; |
| 1544 | |
| 1545 | *(size_t *)ptr = uval; |
| 1546 | return 1; |
| 1547 | } |
| 1548 | |
| 1549 | |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 1550 | #define CHECK_BINOP(v,w) \ |
| 1551 | do { \ |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1552 | if (!PyLong_Check(v) || !PyLong_Check(w)) \ |
| 1553 | Py_RETURN_NOTIMPLEMENTED; \ |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 1554 | } while(0) |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1555 | |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1556 | /* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n] |
| 1557 | * is modified in place, by adding y to it. Carries are propagated as far as |
| 1558 | * x[m-1], and the remaining carry (0 or 1) is returned. |
| 1559 | */ |
| 1560 | static digit |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1561 | 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] | 1562 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1563 | Py_ssize_t i; |
| 1564 | digit carry = 0; |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1565 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1566 | assert(m >= n); |
| 1567 | for (i = 0; i < n; ++i) { |
| 1568 | carry += x[i] + y[i]; |
| 1569 | x[i] = carry & PyLong_MASK; |
| 1570 | carry >>= PyLong_SHIFT; |
| 1571 | assert((carry & 1) == carry); |
| 1572 | } |
| 1573 | for (; carry && i < m; ++i) { |
| 1574 | carry += x[i]; |
| 1575 | x[i] = carry & PyLong_MASK; |
| 1576 | carry >>= PyLong_SHIFT; |
| 1577 | assert((carry & 1) == carry); |
| 1578 | } |
| 1579 | return carry; |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | /* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n] |
| 1583 | * is modified in place, by subtracting y from it. Borrows are propagated as |
| 1584 | * far as x[m-1], and the remaining borrow (0 or 1) is returned. |
| 1585 | */ |
| 1586 | static digit |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1587 | 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] | 1588 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1589 | Py_ssize_t i; |
| 1590 | digit borrow = 0; |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1591 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1592 | assert(m >= n); |
| 1593 | for (i = 0; i < n; ++i) { |
| 1594 | borrow = x[i] - y[i] - borrow; |
| 1595 | x[i] = borrow & PyLong_MASK; |
| 1596 | borrow >>= PyLong_SHIFT; |
| 1597 | borrow &= 1; /* keep only 1 sign bit */ |
| 1598 | } |
| 1599 | for (; borrow && i < m; ++i) { |
| 1600 | borrow = x[i] - borrow; |
| 1601 | x[i] = borrow & PyLong_MASK; |
| 1602 | borrow >>= PyLong_SHIFT; |
| 1603 | borrow &= 1; |
| 1604 | } |
| 1605 | return borrow; |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1606 | } |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1607 | |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 1608 | /* Shift digit vector a[0:m] d bits left, with 0 <= d < PyLong_SHIFT. Put |
| 1609 | * result in z[0:m], and return the d bits shifted out of the top. |
| 1610 | */ |
| 1611 | static digit |
| 1612 | 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] | 1613 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1614 | Py_ssize_t i; |
| 1615 | digit carry = 0; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1616 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1617 | assert(0 <= d && d < PyLong_SHIFT); |
| 1618 | for (i=0; i < m; i++) { |
| 1619 | twodigits acc = (twodigits)a[i] << d | carry; |
| 1620 | z[i] = (digit)acc & PyLong_MASK; |
| 1621 | carry = (digit)(acc >> PyLong_SHIFT); |
| 1622 | } |
| 1623 | return carry; |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | /* Shift digit vector a[0:m] d bits right, with 0 <= d < PyLong_SHIFT. Put |
| 1627 | * result in z[0:m], and return the d bits shifted out of the bottom. |
| 1628 | */ |
| 1629 | static digit |
| 1630 | v_rshift(digit *z, digit *a, Py_ssize_t m, int d) |
| 1631 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1632 | Py_ssize_t i; |
| 1633 | digit carry = 0; |
| 1634 | digit mask = ((digit)1 << d) - 1U; |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 1635 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1636 | assert(0 <= d && d < PyLong_SHIFT); |
| 1637 | for (i=m; i-- > 0;) { |
| 1638 | twodigits acc = (twodigits)carry << PyLong_SHIFT | a[i]; |
| 1639 | carry = (digit)acc & mask; |
| 1640 | z[i] = (digit)(acc >> d); |
| 1641 | } |
| 1642 | return carry; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1645 | /* Divide long pin, w/ size digits, by non-zero digit n, storing quotient |
| 1646 | in pout, and returning the remainder. pin and pout point at the LSD. |
| 1647 | 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] | 1648 | _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] | 1649 | immutable. */ |
| 1650 | |
| 1651 | static digit |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1652 | inplace_divrem1(digit *pout, digit *pin, Py_ssize_t size, digit n) |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1653 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1654 | twodigits rem = 0; |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1655 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1656 | assert(n > 0 && n <= PyLong_MASK); |
| 1657 | pin += size; |
| 1658 | pout += size; |
| 1659 | while (--size >= 0) { |
| 1660 | digit hi; |
| 1661 | rem = (rem << PyLong_SHIFT) | *--pin; |
| 1662 | *--pout = hi = (digit)(rem / n); |
| 1663 | rem -= (twodigits)hi * n; |
| 1664 | } |
| 1665 | return (digit)rem; |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1666 | } |
| 1667 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1668 | /* Divide an integer by a digit, returning both the quotient |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1669 | (as function result) and the remainder (through *prem). |
| 1670 | The sign of a is ignored; n should not be zero. */ |
| 1671 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1672 | static PyLongObject * |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1673 | divrem1(PyLongObject *a, digit n, digit *prem) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1674 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 1675 | const Py_ssize_t size = Py_ABS(Py_SIZE(a)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1676 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1677 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1678 | assert(n > 0 && n <= PyLong_MASK); |
| 1679 | z = _PyLong_New(size); |
| 1680 | if (z == NULL) |
| 1681 | return NULL; |
| 1682 | *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n); |
| 1683 | return long_normalize(z); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1686 | /* 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] | 1687 | string. (Return value is non-shared so that callers can modify the |
| 1688 | returned value if necessary.) */ |
| 1689 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1690 | static int |
| 1691 | long_to_decimal_string_internal(PyObject *aa, |
| 1692 | PyObject **p_output, |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1693 | _PyUnicodeWriter *writer, |
| 1694 | _PyBytesWriter *bytes_writer, |
| 1695 | char **bytes_str) |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1696 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1697 | PyLongObject *scratch, *a; |
Victor Stinner | 1285e5c | 2015-10-14 12:10:20 +0200 | [diff] [blame] | 1698 | PyObject *str = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1699 | Py_ssize_t size, strlen, size_a, i, j; |
| 1700 | digit *pout, *pin, rem, tenpow; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1701 | int negative; |
Mark Dickinson | 4e1de16 | 2016-08-29 17:26:43 +0100 | [diff] [blame] | 1702 | int d; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1703 | enum PyUnicode_Kind kind; |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1704 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1705 | a = (PyLongObject *)aa; |
| 1706 | if (a == NULL || !PyLong_Check(a)) { |
| 1707 | PyErr_BadInternalCall(); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1708 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1709 | } |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 1710 | size_a = Py_ABS(Py_SIZE(a)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1711 | negative = Py_SIZE(a) < 0; |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1712 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1713 | /* quick and dirty upper bound for the number of digits |
| 1714 | required to express a in base _PyLong_DECIMAL_BASE: |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1715 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1716 | #digits = 1 + floor(log2(a) / log2(_PyLong_DECIMAL_BASE)) |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1717 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1718 | But log2(a) < size_a * PyLong_SHIFT, and |
| 1719 | log2(_PyLong_DECIMAL_BASE) = log2(10) * _PyLong_DECIMAL_SHIFT |
Mark Dickinson | 4e1de16 | 2016-08-29 17:26:43 +0100 | [diff] [blame] | 1720 | > 3.3 * _PyLong_DECIMAL_SHIFT |
| 1721 | |
| 1722 | size_a * PyLong_SHIFT / (3.3 * _PyLong_DECIMAL_SHIFT) = |
| 1723 | size_a + size_a / d < size_a + size_a / floor(d), |
| 1724 | where d = (3.3 * _PyLong_DECIMAL_SHIFT) / |
| 1725 | (PyLong_SHIFT - 3.3 * _PyLong_DECIMAL_SHIFT) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1726 | */ |
Mark Dickinson | 4e1de16 | 2016-08-29 17:26:43 +0100 | [diff] [blame] | 1727 | d = (33 * _PyLong_DECIMAL_SHIFT) / |
| 1728 | (10 * PyLong_SHIFT - 33 * _PyLong_DECIMAL_SHIFT); |
| 1729 | assert(size_a < PY_SSIZE_T_MAX/2); |
| 1730 | size = 1 + size_a + size_a / d; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1731 | scratch = _PyLong_New(size); |
| 1732 | if (scratch == NULL) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1733 | return -1; |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1734 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1735 | /* convert array of base _PyLong_BASE digits in pin to an array of |
| 1736 | base _PyLong_DECIMAL_BASE digits in pout, following Knuth (TAOCP, |
| 1737 | Volume 2 (3rd edn), section 4.4, Method 1b). */ |
| 1738 | pin = a->ob_digit; |
| 1739 | pout = scratch->ob_digit; |
| 1740 | size = 0; |
| 1741 | for (i = size_a; --i >= 0; ) { |
| 1742 | digit hi = pin[i]; |
| 1743 | for (j = 0; j < size; j++) { |
| 1744 | twodigits z = (twodigits)pout[j] << PyLong_SHIFT | hi; |
| 1745 | hi = (digit)(z / _PyLong_DECIMAL_BASE); |
| 1746 | pout[j] = (digit)(z - (twodigits)hi * |
| 1747 | _PyLong_DECIMAL_BASE); |
| 1748 | } |
| 1749 | while (hi) { |
| 1750 | pout[size++] = hi % _PyLong_DECIMAL_BASE; |
| 1751 | hi /= _PyLong_DECIMAL_BASE; |
| 1752 | } |
| 1753 | /* check for keyboard interrupt */ |
| 1754 | SIGCHECK({ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 1755 | Py_DECREF(scratch); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1756 | return -1; |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 1757 | }); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1758 | } |
| 1759 | /* pout should have at least one digit, so that the case when a = 0 |
| 1760 | works correctly */ |
| 1761 | if (size == 0) |
| 1762 | pout[size++] = 0; |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1763 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1764 | /* calculate exact length of output string, and allocate */ |
| 1765 | strlen = negative + 1 + (size - 1) * _PyLong_DECIMAL_SHIFT; |
| 1766 | tenpow = 10; |
| 1767 | rem = pout[size-1]; |
| 1768 | while (rem >= tenpow) { |
| 1769 | tenpow *= 10; |
| 1770 | strlen++; |
| 1771 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1772 | if (writer) { |
Christian Heimes | 110ac16 | 2012-09-10 02:51:27 +0200 | [diff] [blame] | 1773 | if (_PyUnicodeWriter_Prepare(writer, strlen, '9') == -1) { |
| 1774 | Py_DECREF(scratch); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1775 | return -1; |
Christian Heimes | 110ac16 | 2012-09-10 02:51:27 +0200 | [diff] [blame] | 1776 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1777 | kind = writer->kind; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1778 | } |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1779 | else if (bytes_writer) { |
| 1780 | *bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, strlen); |
| 1781 | if (*bytes_str == NULL) { |
| 1782 | Py_DECREF(scratch); |
| 1783 | return -1; |
| 1784 | } |
| 1785 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1786 | else { |
| 1787 | str = PyUnicode_New(strlen, '9'); |
| 1788 | if (str == NULL) { |
| 1789 | Py_DECREF(scratch); |
| 1790 | return -1; |
| 1791 | } |
| 1792 | kind = PyUnicode_KIND(str); |
| 1793 | } |
| 1794 | |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1795 | #define WRITE_DIGITS(p) \ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1796 | do { \ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1797 | /* pout[0] through pout[size-2] contribute exactly \ |
| 1798 | _PyLong_DECIMAL_SHIFT digits each */ \ |
| 1799 | for (i=0; i < size - 1; i++) { \ |
| 1800 | rem = pout[i]; \ |
| 1801 | for (j = 0; j < _PyLong_DECIMAL_SHIFT; j++) { \ |
| 1802 | *--p = '0' + rem % 10; \ |
| 1803 | rem /= 10; \ |
| 1804 | } \ |
| 1805 | } \ |
| 1806 | /* pout[size-1]: always produce at least one decimal digit */ \ |
| 1807 | rem = pout[i]; \ |
| 1808 | do { \ |
| 1809 | *--p = '0' + rem % 10; \ |
| 1810 | rem /= 10; \ |
| 1811 | } while (rem != 0); \ |
| 1812 | \ |
| 1813 | /* and sign */ \ |
| 1814 | if (negative) \ |
| 1815 | *--p = '-'; \ |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1816 | } while (0) |
| 1817 | |
| 1818 | #define WRITE_UNICODE_DIGITS(TYPE) \ |
| 1819 | do { \ |
| 1820 | if (writer) \ |
| 1821 | p = (TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos + strlen; \ |
| 1822 | else \ |
| 1823 | p = (TYPE*)PyUnicode_DATA(str) + strlen; \ |
| 1824 | \ |
| 1825 | WRITE_DIGITS(p); \ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1826 | \ |
| 1827 | /* check we've counted correctly */ \ |
| 1828 | if (writer) \ |
| 1829 | assert(p == ((TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos)); \ |
| 1830 | else \ |
| 1831 | assert(p == (TYPE*)PyUnicode_DATA(str)); \ |
| 1832 | } while (0) |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1833 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1834 | /* fill the string right-to-left */ |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1835 | if (bytes_writer) { |
| 1836 | char *p = *bytes_str + strlen; |
| 1837 | WRITE_DIGITS(p); |
| 1838 | assert(p == *bytes_str); |
| 1839 | } |
| 1840 | else if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1841 | Py_UCS1 *p; |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1842 | WRITE_UNICODE_DIGITS(Py_UCS1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1843 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1844 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 1845 | Py_UCS2 *p; |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1846 | WRITE_UNICODE_DIGITS(Py_UCS2); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1847 | } |
| 1848 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1849 | Py_UCS4 *p; |
Victor Stinner | e577ab3 | 2012-05-29 18:51:10 +0200 | [diff] [blame] | 1850 | assert (kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1851 | WRITE_UNICODE_DIGITS(Py_UCS4); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1852 | } |
| 1853 | #undef WRITE_DIGITS |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1854 | #undef WRITE_UNICODE_DIGITS |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1855 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1856 | Py_DECREF(scratch); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1857 | if (writer) { |
| 1858 | writer->pos += strlen; |
| 1859 | } |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1860 | else if (bytes_writer) { |
| 1861 | (*bytes_str) += strlen; |
| 1862 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1863 | else { |
| 1864 | assert(_PyUnicode_CheckConsistency(str, 1)); |
| 1865 | *p_output = (PyObject *)str; |
| 1866 | } |
| 1867 | return 0; |
| 1868 | } |
| 1869 | |
| 1870 | static PyObject * |
| 1871 | long_to_decimal_string(PyObject *aa) |
| 1872 | { |
| 1873 | PyObject *v; |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1874 | if (long_to_decimal_string_internal(aa, &v, NULL, NULL, NULL) == -1) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1875 | return NULL; |
| 1876 | return v; |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1879 | /* Convert an int object to a string, using a given conversion base, |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1880 | which should be one of 2, 8 or 16. Return a string object. |
| 1881 | If base is 2, 8 or 16, add the proper prefix '0b', '0o' or '0x' |
| 1882 | if alternate is nonzero. */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1883 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1884 | static int |
| 1885 | long_format_binary(PyObject *aa, int base, int alternate, |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1886 | PyObject **p_output, _PyUnicodeWriter *writer, |
| 1887 | _PyBytesWriter *bytes_writer, char **bytes_str) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1888 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1889 | PyLongObject *a = (PyLongObject *)aa; |
Victor Stinner | 1285e5c | 2015-10-14 12:10:20 +0200 | [diff] [blame] | 1890 | PyObject *v = NULL; |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1891 | Py_ssize_t sz; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1892 | Py_ssize_t size_a; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1893 | enum PyUnicode_Kind kind; |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1894 | int negative; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1895 | int bits; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1896 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1897 | assert(base == 2 || base == 8 || base == 16); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1898 | if (a == NULL || !PyLong_Check(a)) { |
| 1899 | PyErr_BadInternalCall(); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1900 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1901 | } |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 1902 | size_a = Py_ABS(Py_SIZE(a)); |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1903 | negative = Py_SIZE(a) < 0; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1904 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1905 | /* Compute a rough upper bound for the length of the string */ |
| 1906 | switch (base) { |
| 1907 | case 16: |
| 1908 | bits = 4; |
| 1909 | break; |
| 1910 | case 8: |
| 1911 | bits = 3; |
| 1912 | break; |
| 1913 | case 2: |
| 1914 | bits = 1; |
| 1915 | break; |
| 1916 | default: |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 1917 | Py_UNREACHABLE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1918 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1919 | |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1920 | /* Compute exact length 'sz' of output string. */ |
| 1921 | if (size_a == 0) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1922 | sz = 1; |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1923 | } |
| 1924 | else { |
| 1925 | Py_ssize_t size_a_in_bits; |
| 1926 | /* Ensure overflow doesn't occur during computation of sz. */ |
| 1927 | if (size_a > (PY_SSIZE_T_MAX - 3) / PyLong_SHIFT) { |
| 1928 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 02515f7 | 2013-08-03 12:08:22 +0100 | [diff] [blame] | 1929 | "int too large to format"); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1930 | return -1; |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1931 | } |
| 1932 | size_a_in_bits = (size_a - 1) * PyLong_SHIFT + |
Niklas Fiekas | c5b7900 | 2020-01-16 15:09:19 +0100 | [diff] [blame^] | 1933 | _Py_bit_length(a->ob_digit[size_a - 1]); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1934 | /* Allow 1 character for a '-' sign. */ |
| 1935 | sz = negative + (size_a_in_bits + (bits - 1)) / bits; |
| 1936 | } |
| 1937 | if (alternate) { |
| 1938 | /* 2 characters for prefix */ |
| 1939 | sz += 2; |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1940 | } |
| 1941 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1942 | if (writer) { |
| 1943 | if (_PyUnicodeWriter_Prepare(writer, sz, 'x') == -1) |
| 1944 | return -1; |
| 1945 | kind = writer->kind; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1946 | } |
Victor Stinner | 199c9a6 | 2015-10-14 09:47:23 +0200 | [diff] [blame] | 1947 | else if (bytes_writer) { |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1948 | *bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, sz); |
| 1949 | if (*bytes_str == NULL) |
| 1950 | return -1; |
| 1951 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1952 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1953 | v = PyUnicode_New(sz, 'x'); |
| 1954 | if (v == NULL) |
| 1955 | return -1; |
| 1956 | kind = PyUnicode_KIND(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1957 | } |
Mark Dickinson | 8accd6b | 2009-09-17 19:39:12 +0000 | [diff] [blame] | 1958 | |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1959 | #define WRITE_DIGITS(p) \ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1960 | do { \ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1961 | if (size_a == 0) { \ |
| 1962 | *--p = '0'; \ |
| 1963 | } \ |
| 1964 | else { \ |
| 1965 | /* JRH: special case for power-of-2 bases */ \ |
| 1966 | twodigits accum = 0; \ |
| 1967 | int accumbits = 0; /* # of bits in accum */ \ |
| 1968 | Py_ssize_t i; \ |
| 1969 | for (i = 0; i < size_a; ++i) { \ |
| 1970 | accum |= (twodigits)a->ob_digit[i] << accumbits; \ |
| 1971 | accumbits += PyLong_SHIFT; \ |
| 1972 | assert(accumbits >= bits); \ |
| 1973 | do { \ |
| 1974 | char cdigit; \ |
| 1975 | cdigit = (char)(accum & (base - 1)); \ |
| 1976 | cdigit += (cdigit < 10) ? '0' : 'a'-10; \ |
| 1977 | *--p = cdigit; \ |
| 1978 | accumbits -= bits; \ |
| 1979 | accum >>= bits; \ |
| 1980 | } while (i < size_a-1 ? accumbits >= bits : accum > 0); \ |
| 1981 | } \ |
| 1982 | } \ |
| 1983 | \ |
| 1984 | if (alternate) { \ |
| 1985 | if (base == 16) \ |
| 1986 | *--p = 'x'; \ |
| 1987 | else if (base == 8) \ |
| 1988 | *--p = 'o'; \ |
| 1989 | else /* (base == 2) */ \ |
| 1990 | *--p = 'b'; \ |
| 1991 | *--p = '0'; \ |
| 1992 | } \ |
| 1993 | if (negative) \ |
| 1994 | *--p = '-'; \ |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1995 | } while (0) |
| 1996 | |
| 1997 | #define WRITE_UNICODE_DIGITS(TYPE) \ |
| 1998 | do { \ |
| 1999 | if (writer) \ |
| 2000 | p = (TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos + sz; \ |
| 2001 | else \ |
| 2002 | p = (TYPE*)PyUnicode_DATA(v) + sz; \ |
| 2003 | \ |
| 2004 | WRITE_DIGITS(p); \ |
| 2005 | \ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2006 | if (writer) \ |
| 2007 | assert(p == ((TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos)); \ |
| 2008 | else \ |
| 2009 | assert(p == (TYPE*)PyUnicode_DATA(v)); \ |
| 2010 | } while (0) |
| 2011 | |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 2012 | if (bytes_writer) { |
| 2013 | char *p = *bytes_str + sz; |
| 2014 | WRITE_DIGITS(p); |
| 2015 | assert(p == *bytes_str); |
| 2016 | } |
| 2017 | else if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2018 | Py_UCS1 *p; |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 2019 | WRITE_UNICODE_DIGITS(Py_UCS1); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2020 | } |
| 2021 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2022 | Py_UCS2 *p; |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 2023 | WRITE_UNICODE_DIGITS(Py_UCS2); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2024 | } |
| 2025 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2026 | Py_UCS4 *p; |
Victor Stinner | e577ab3 | 2012-05-29 18:51:10 +0200 | [diff] [blame] | 2027 | assert (kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 2028 | WRITE_UNICODE_DIGITS(Py_UCS4); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2029 | } |
| 2030 | #undef WRITE_DIGITS |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 2031 | #undef WRITE_UNICODE_DIGITS |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2032 | |
| 2033 | if (writer) { |
| 2034 | writer->pos += sz; |
| 2035 | } |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 2036 | else if (bytes_writer) { |
| 2037 | (*bytes_str) += sz; |
| 2038 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2039 | else { |
| 2040 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 2041 | *p_output = v; |
| 2042 | } |
| 2043 | return 0; |
| 2044 | } |
| 2045 | |
| 2046 | PyObject * |
| 2047 | _PyLong_Format(PyObject *obj, int base) |
| 2048 | { |
| 2049 | PyObject *str; |
| 2050 | int err; |
| 2051 | if (base == 10) |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 2052 | err = long_to_decimal_string_internal(obj, &str, NULL, NULL, NULL); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2053 | else |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 2054 | err = long_format_binary(obj, base, 1, &str, NULL, NULL, NULL); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2055 | if (err == -1) |
| 2056 | return NULL; |
| 2057 | return str; |
| 2058 | } |
| 2059 | |
| 2060 | int |
| 2061 | _PyLong_FormatWriter(_PyUnicodeWriter *writer, |
| 2062 | PyObject *obj, |
| 2063 | int base, int alternate) |
| 2064 | { |
| 2065 | if (base == 10) |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 2066 | return long_to_decimal_string_internal(obj, NULL, writer, |
| 2067 | NULL, NULL); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2068 | else |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 2069 | return long_format_binary(obj, base, alternate, NULL, writer, |
| 2070 | NULL, NULL); |
| 2071 | } |
| 2072 | |
| 2073 | char* |
| 2074 | _PyLong_FormatBytesWriter(_PyBytesWriter *writer, char *str, |
| 2075 | PyObject *obj, |
| 2076 | int base, int alternate) |
| 2077 | { |
| 2078 | char *str2; |
| 2079 | int res; |
| 2080 | str2 = str; |
| 2081 | if (base == 10) |
| 2082 | res = long_to_decimal_string_internal(obj, NULL, NULL, |
| 2083 | writer, &str2); |
| 2084 | else |
| 2085 | res = long_format_binary(obj, base, alternate, NULL, NULL, |
| 2086 | writer, &str2); |
| 2087 | if (res < 0) |
| 2088 | return NULL; |
| 2089 | assert(str2 != NULL); |
| 2090 | return str2; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2093 | /* Table of digit values for 8-bit string -> integer conversion. |
| 2094 | * '0' maps to 0, ..., '9' maps to 9. |
| 2095 | * 'a' and 'A' map to 10, ..., 'z' and 'Z' map to 35. |
| 2096 | * All other indices map to 37. |
| 2097 | * 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] | 2098 | * base B digit iff _PyLong_DigitValue[Py_CHARPyLong_MASK(c)] < B. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2099 | */ |
Raymond Hettinger | 3563153 | 2009-01-09 03:58:09 +0000 | [diff] [blame] | 2100 | unsigned char _PyLong_DigitValue[256] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2101 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2102 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2103 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2104 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 37, 37, 37, 37, 37, 37, |
| 2105 | 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
| 2106 | 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37, |
| 2107 | 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
| 2108 | 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37, |
| 2109 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2110 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2111 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2112 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2113 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2114 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2115 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2116 | 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] | 2117 | }; |
| 2118 | |
| 2119 | /* *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] | 2120 | * 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] | 2121 | * non-digit (which may be *str!). A normalized int is returned. |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 2122 | * The point to this routine is that it takes time linear in the number of |
| 2123 | * string characters. |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2124 | * |
| 2125 | * Return values: |
| 2126 | * -1 on syntax error (exception needs to be set, *res is untouched) |
| 2127 | * 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] | 2128 | */ |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2129 | static int |
| 2130 | long_from_binary_base(const char **str, int base, PyLongObject **res) |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 2131 | { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 2132 | const char *p = *str; |
| 2133 | const char *start = p; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2134 | char prev = 0; |
Serhiy Storchaka | 29ba688 | 2017-12-03 22:16:21 +0200 | [diff] [blame] | 2135 | Py_ssize_t digits = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2136 | int bits_per_char; |
| 2137 | Py_ssize_t n; |
| 2138 | PyLongObject *z; |
| 2139 | twodigits accum; |
| 2140 | int bits_in_accum; |
| 2141 | digit *pdigit; |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 2142 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2143 | assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0); |
| 2144 | n = base; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2145 | for (bits_per_char = -1; n; ++bits_per_char) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2146 | n >>= 1; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2147 | } |
| 2148 | /* count digits and set p to end-of-string */ |
| 2149 | while (_PyLong_DigitValue[Py_CHARMASK(*p)] < base || *p == '_') { |
| 2150 | if (*p == '_') { |
| 2151 | if (prev == '_') { |
| 2152 | *str = p - 1; |
| 2153 | return -1; |
| 2154 | } |
| 2155 | } else { |
| 2156 | ++digits; |
| 2157 | } |
| 2158 | prev = *p; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2159 | ++p; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2160 | } |
| 2161 | if (prev == '_') { |
| 2162 | /* Trailing underscore not allowed. */ |
| 2163 | *str = p - 1; |
| 2164 | return -1; |
| 2165 | } |
| 2166 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2167 | *str = p; |
Serhiy Storchaka | 85c0b89 | 2017-10-03 14:13:44 +0300 | [diff] [blame] | 2168 | /* n <- the number of Python digits needed, |
| 2169 | = ceiling((digits * bits_per_char) / PyLong_SHIFT). */ |
| 2170 | if (digits > (PY_SSIZE_T_MAX - (PyLong_SHIFT - 1)) / bits_per_char) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2171 | PyErr_SetString(PyExc_ValueError, |
| 2172 | "int string too large to convert"); |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2173 | *res = NULL; |
| 2174 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2175 | } |
Serhiy Storchaka | 85c0b89 | 2017-10-03 14:13:44 +0300 | [diff] [blame] | 2176 | n = (digits * bits_per_char + PyLong_SHIFT - 1) / PyLong_SHIFT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2177 | z = _PyLong_New(n); |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2178 | if (z == NULL) { |
| 2179 | *res = NULL; |
| 2180 | return 0; |
| 2181 | } |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2182 | /* Read string from right, and fill in int from left; i.e., |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2183 | * from least to most significant in both. |
| 2184 | */ |
| 2185 | accum = 0; |
| 2186 | bits_in_accum = 0; |
| 2187 | pdigit = z->ob_digit; |
| 2188 | while (--p >= start) { |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2189 | int k; |
| 2190 | if (*p == '_') { |
| 2191 | continue; |
| 2192 | } |
| 2193 | k = (int)_PyLong_DigitValue[Py_CHARMASK(*p)]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2194 | assert(k >= 0 && k < base); |
| 2195 | accum |= (twodigits)k << bits_in_accum; |
| 2196 | bits_in_accum += bits_per_char; |
| 2197 | if (bits_in_accum >= PyLong_SHIFT) { |
| 2198 | *pdigit++ = (digit)(accum & PyLong_MASK); |
| 2199 | assert(pdigit - z->ob_digit <= n); |
| 2200 | accum >>= PyLong_SHIFT; |
| 2201 | bits_in_accum -= PyLong_SHIFT; |
| 2202 | assert(bits_in_accum < PyLong_SHIFT); |
| 2203 | } |
| 2204 | } |
| 2205 | if (bits_in_accum) { |
| 2206 | assert(bits_in_accum <= PyLong_SHIFT); |
| 2207 | *pdigit++ = (digit)accum; |
| 2208 | assert(pdigit - z->ob_digit <= n); |
| 2209 | } |
| 2210 | while (pdigit - z->ob_digit < n) |
| 2211 | *pdigit++ = 0; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2212 | *res = long_normalize(z); |
| 2213 | return 0; |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 2214 | } |
| 2215 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2216 | /* Parses an int from a bytestring. Leading and trailing whitespace will be |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2217 | * ignored. |
| 2218 | * |
| 2219 | * If successful, a PyLong object will be returned and 'pend' will be pointing |
| 2220 | * to the first unused byte unless it's NULL. |
| 2221 | * |
| 2222 | * If unsuccessful, NULL will be returned. |
| 2223 | */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2224 | PyObject * |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 2225 | PyLong_FromString(const char *str, char **pend, int base) |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 2226 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2227 | int sign = 1, error_if_nonzero = 0; |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 2228 | const char *start, *orig_str = str; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2229 | PyLongObject *z = NULL; |
| 2230 | PyObject *strobj; |
| 2231 | Py_ssize_t slen; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2232 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2233 | if ((base != 0 && base < 2) || base > 36) { |
| 2234 | PyErr_SetString(PyExc_ValueError, |
| 2235 | "int() arg 2 must be >= 2 and <= 36"); |
| 2236 | return NULL; |
| 2237 | } |
Jordon Xu | 2ec7010 | 2019-09-11 00:04:08 +0800 | [diff] [blame] | 2238 | while (*str != '\0' && Py_ISSPACE(*str)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2239 | str++; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2240 | } |
| 2241 | if (*str == '+') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2242 | ++str; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2243 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2244 | else if (*str == '-') { |
| 2245 | ++str; |
| 2246 | sign = -1; |
| 2247 | } |
| 2248 | if (base == 0) { |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2249 | if (str[0] != '0') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2250 | base = 10; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2251 | } |
| 2252 | else if (str[1] == 'x' || str[1] == 'X') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2253 | base = 16; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2254 | } |
| 2255 | else if (str[1] == 'o' || str[1] == 'O') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2256 | base = 8; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2257 | } |
| 2258 | else if (str[1] == 'b' || str[1] == 'B') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2259 | base = 2; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2260 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2261 | else { |
| 2262 | /* "old" (C-style) octal literal, now invalid. |
| 2263 | it might still be zero though */ |
| 2264 | error_if_nonzero = 1; |
| 2265 | base = 10; |
| 2266 | } |
| 2267 | } |
| 2268 | if (str[0] == '0' && |
| 2269 | ((base == 16 && (str[1] == 'x' || str[1] == 'X')) || |
| 2270 | (base == 8 && (str[1] == 'o' || str[1] == 'O')) || |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2271 | (base == 2 && (str[1] == 'b' || str[1] == 'B')))) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2272 | str += 2; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2273 | /* One underscore allowed here. */ |
| 2274 | if (*str == '_') { |
| 2275 | ++str; |
| 2276 | } |
| 2277 | } |
| 2278 | if (str[0] == '_') { |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 2279 | /* May not start with underscores. */ |
| 2280 | goto onError; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2281 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2282 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2283 | start = str; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2284 | if ((base & (base - 1)) == 0) { |
| 2285 | int res = long_from_binary_base(&str, base, &z); |
| 2286 | if (res < 0) { |
| 2287 | /* Syntax error. */ |
| 2288 | goto onError; |
| 2289 | } |
| 2290 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2291 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2292 | /*** |
| 2293 | Binary bases can be converted in time linear in the number of digits, because |
| 2294 | Python's representation base is binary. Other bases (including decimal!) use |
| 2295 | the simple quadratic-time algorithm below, complicated by some speed tricks. |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2296 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2297 | First some math: the largest integer that can be expressed in N base-B digits |
| 2298 | is B**N-1. Consequently, if we have an N-digit input in base B, the worst- |
| 2299 | case number of Python digits needed to hold it is the smallest integer n s.t. |
| 2300 | |
| 2301 | BASE**n-1 >= B**N-1 [or, adding 1 to both sides] |
| 2302 | BASE**n >= B**N [taking logs to base BASE] |
| 2303 | n >= log(B**N)/log(BASE) = N * log(B)/log(BASE) |
| 2304 | |
| 2305 | 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] | 2306 | 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] | 2307 | and the result is computed into it. |
| 2308 | |
| 2309 | The input string is actually treated as being in base base**i (i.e., i digits |
| 2310 | are processed at a time), where two more static arrays hold: |
| 2311 | |
| 2312 | convwidth_base[base] = the largest integer i such that base**i <= BASE |
| 2313 | convmultmax_base[base] = base ** convwidth_base[base] |
| 2314 | |
| 2315 | The first of these is the largest i such that i consecutive input digits |
| 2316 | must fit in a single Python digit. The second is effectively the input |
| 2317 | base we're really using. |
| 2318 | |
| 2319 | Viewing the input as a sequence <c0, c1, ..., c_n-1> of digits in base |
| 2320 | convmultmax_base[base], the result is "simply" |
| 2321 | |
| 2322 | (((c0*B + c1)*B + c2)*B + c3)*B + ... ))) + c_n-1 |
| 2323 | |
| 2324 | where B = convmultmax_base[base]. |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2325 | |
| 2326 | Error analysis: as above, the number of Python digits `n` needed is worst- |
| 2327 | case |
| 2328 | |
| 2329 | n >= N * log(B)/log(BASE) |
| 2330 | |
| 2331 | where `N` is the number of input digits in base `B`. This is computed via |
| 2332 | |
| 2333 | size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1; |
| 2334 | |
| 2335 | below. Two numeric concerns are how much space this can waste, and whether |
| 2336 | the computed result can be too small. To be concrete, assume BASE = 2**15, |
| 2337 | which is the default (and it's unlikely anyone changes that). |
| 2338 | |
| 2339 | Waste isn't a problem: provided the first input digit isn't 0, the difference |
| 2340 | between the worst-case input with N digits and the smallest input with N |
| 2341 | digits is about a factor of B, but B is small compared to BASE so at most |
| 2342 | one allocated Python digit can remain unused on that count. If |
| 2343 | N*log(B)/log(BASE) is mathematically an exact integer, then truncating that |
| 2344 | and adding 1 returns a result 1 larger than necessary. However, that can't |
| 2345 | happen: whenever B is a power of 2, long_from_binary_base() is called |
| 2346 | instead, and it's impossible for B**i to be an integer power of 2**15 when |
| 2347 | B is not a power of 2 (i.e., it's impossible for N*log(B)/log(BASE) to be |
| 2348 | an exact integer when B is not a power of 2, since B**i has a prime factor |
| 2349 | other than 2 in that case, but (2**15)**j's only prime factor is 2). |
| 2350 | |
| 2351 | The computed result can be too small if the true value of N*log(B)/log(BASE) |
| 2352 | is a little bit larger than an exact integer, but due to roundoff errors (in |
| 2353 | computing log(B), log(BASE), their quotient, and/or multiplying that by N) |
| 2354 | yields a numeric result a little less than that integer. Unfortunately, "how |
| 2355 | close can a transcendental function get to an integer over some range?" |
| 2356 | questions are generally theoretically intractable. Computer analysis via |
| 2357 | continued fractions is practical: expand log(B)/log(BASE) via continued |
| 2358 | fractions, giving a sequence i/j of "the best" rational approximations. Then |
| 2359 | j*log(B)/log(BASE) is approximately equal to (the integer) i. This shows that |
| 2360 | we can get very close to being in trouble, but very rarely. For example, |
| 2361 | 76573 is a denominator in one of the continued-fraction approximations to |
| 2362 | log(10)/log(2**15), and indeed: |
| 2363 | |
| 2364 | >>> log(10)/log(2**15)*76573 |
| 2365 | 16958.000000654003 |
| 2366 | |
| 2367 | is very close to an integer. If we were working with IEEE single-precision, |
| 2368 | rounding errors could kill us. Finding worst cases in IEEE double-precision |
| 2369 | requires better-than-double-precision log() functions, and Tim didn't bother. |
| 2370 | 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] | 2371 | 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] | 2372 | This should happen extremely rarely, and in fact I don't have a test case |
| 2373 | that triggers it(!). Instead the code was tested by artificially allocating |
| 2374 | just 1 digit at the start, so that the copying code was exercised for every |
| 2375 | digit beyond the first. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2376 | ***/ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2377 | twodigits c; /* current input character */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2378 | Py_ssize_t size_z; |
Serhiy Storchaka | 29ba688 | 2017-12-03 22:16:21 +0200 | [diff] [blame] | 2379 | Py_ssize_t digits = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2380 | int i; |
| 2381 | int convwidth; |
| 2382 | twodigits convmultmax, convmult; |
| 2383 | digit *pz, *pzstop; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2384 | const char *scan, *lastdigit; |
| 2385 | char prev = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2386 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2387 | static double log_base_BASE[37] = {0.0e0,}; |
| 2388 | static int convwidth_base[37] = {0,}; |
| 2389 | static twodigits convmultmax_base[37] = {0,}; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2390 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2391 | if (log_base_BASE[base] == 0.0) { |
| 2392 | twodigits convmax = base; |
| 2393 | int i = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2394 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2395 | log_base_BASE[base] = (log((double)base) / |
| 2396 | log((double)PyLong_BASE)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2397 | for (;;) { |
| 2398 | twodigits next = convmax * base; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2399 | if (next > PyLong_BASE) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2400 | break; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2401 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2402 | convmax = next; |
| 2403 | ++i; |
| 2404 | } |
| 2405 | convmultmax_base[base] = convmax; |
| 2406 | assert(i > 0); |
| 2407 | convwidth_base[base] = i; |
| 2408 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2409 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2410 | /* Find length of the string of numeric characters. */ |
| 2411 | scan = str; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2412 | lastdigit = str; |
| 2413 | |
| 2414 | while (_PyLong_DigitValue[Py_CHARMASK(*scan)] < base || *scan == '_') { |
| 2415 | if (*scan == '_') { |
| 2416 | if (prev == '_') { |
| 2417 | /* Only one underscore allowed. */ |
| 2418 | str = lastdigit + 1; |
| 2419 | goto onError; |
| 2420 | } |
| 2421 | } |
| 2422 | else { |
| 2423 | ++digits; |
| 2424 | lastdigit = scan; |
| 2425 | } |
| 2426 | prev = *scan; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2427 | ++scan; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2428 | } |
| 2429 | if (prev == '_') { |
| 2430 | /* Trailing underscore not allowed. */ |
| 2431 | /* Set error pointer to first underscore. */ |
| 2432 | str = lastdigit + 1; |
| 2433 | goto onError; |
| 2434 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2435 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2436 | /* Create an int object that can contain the largest possible |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2437 | * integer with this base and length. Note that there's no |
| 2438 | * need to initialize z->ob_digit -- no slot is read up before |
| 2439 | * being stored into. |
| 2440 | */ |
Victor Stinner | dd431b3 | 2017-12-08 00:06:55 +0100 | [diff] [blame] | 2441 | double fsize_z = (double)digits * log_base_BASE[base] + 1.0; |
| 2442 | if (fsize_z > (double)MAX_LONG_DIGITS) { |
Serhiy Storchaka | 29ba688 | 2017-12-03 22:16:21 +0200 | [diff] [blame] | 2443 | /* The same exception as in _PyLong_New(). */ |
| 2444 | PyErr_SetString(PyExc_OverflowError, |
| 2445 | "too many digits in integer"); |
| 2446 | return NULL; |
| 2447 | } |
| 2448 | size_z = (Py_ssize_t)fsize_z; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2449 | /* Uncomment next line to test exceedingly rare copy code */ |
| 2450 | /* size_z = 1; */ |
| 2451 | assert(size_z > 0); |
| 2452 | z = _PyLong_New(size_z); |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2453 | if (z == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2454 | return NULL; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2455 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2456 | Py_SIZE(z) = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2457 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2458 | /* `convwidth` consecutive input digits are treated as a single |
| 2459 | * digit in base `convmultmax`. |
| 2460 | */ |
| 2461 | convwidth = convwidth_base[base]; |
| 2462 | convmultmax = convmultmax_base[base]; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2463 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2464 | /* Work ;-) */ |
| 2465 | while (str < scan) { |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2466 | if (*str == '_') { |
| 2467 | str++; |
| 2468 | continue; |
| 2469 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2470 | /* grab up to convwidth digits from the input string */ |
| 2471 | c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)]; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2472 | for (i = 1; i < convwidth && str != scan; ++str) { |
| 2473 | if (*str == '_') { |
| 2474 | continue; |
| 2475 | } |
| 2476 | i++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2477 | c = (twodigits)(c * base + |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2478 | (int)_PyLong_DigitValue[Py_CHARMASK(*str)]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2479 | assert(c < PyLong_BASE); |
| 2480 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2481 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2482 | convmult = convmultmax; |
| 2483 | /* Calculate the shift only if we couldn't get |
| 2484 | * convwidth digits. |
| 2485 | */ |
| 2486 | if (i != convwidth) { |
| 2487 | convmult = base; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2488 | for ( ; i > 1; --i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2489 | convmult *= base; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2490 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2491 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2492 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2493 | /* Multiply z by convmult, and add c. */ |
| 2494 | pz = z->ob_digit; |
| 2495 | pzstop = pz + Py_SIZE(z); |
| 2496 | for (; pz < pzstop; ++pz) { |
| 2497 | c += (twodigits)*pz * convmult; |
| 2498 | *pz = (digit)(c & PyLong_MASK); |
| 2499 | c >>= PyLong_SHIFT; |
| 2500 | } |
| 2501 | /* carry off the current end? */ |
| 2502 | if (c) { |
| 2503 | assert(c < PyLong_BASE); |
| 2504 | if (Py_SIZE(z) < size_z) { |
| 2505 | *pz = (digit)c; |
| 2506 | ++Py_SIZE(z); |
| 2507 | } |
| 2508 | else { |
| 2509 | PyLongObject *tmp; |
| 2510 | /* Extremely rare. Get more space. */ |
| 2511 | assert(Py_SIZE(z) == size_z); |
| 2512 | tmp = _PyLong_New(size_z + 1); |
| 2513 | if (tmp == NULL) { |
| 2514 | Py_DECREF(z); |
| 2515 | return NULL; |
| 2516 | } |
| 2517 | memcpy(tmp->ob_digit, |
| 2518 | z->ob_digit, |
| 2519 | sizeof(digit) * size_z); |
| 2520 | Py_DECREF(z); |
| 2521 | z = tmp; |
| 2522 | z->ob_digit[size_z] = (digit)c; |
| 2523 | ++size_z; |
| 2524 | } |
| 2525 | } |
| 2526 | } |
| 2527 | } |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2528 | if (z == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2529 | return NULL; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2530 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2531 | if (error_if_nonzero) { |
| 2532 | /* reset the base to 0, else the exception message |
| 2533 | doesn't make too much sense */ |
| 2534 | base = 0; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2535 | if (Py_SIZE(z) != 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2536 | goto onError; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2537 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2538 | /* there might still be other problems, therefore base |
| 2539 | remains zero here for the same reason */ |
| 2540 | } |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2541 | if (str == start) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2542 | goto onError; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2543 | } |
| 2544 | if (sign < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2545 | Py_SIZE(z) = -(Py_SIZE(z)); |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2546 | } |
Jordon Xu | 2ec7010 | 2019-09-11 00:04:08 +0800 | [diff] [blame] | 2547 | while (*str && Py_ISSPACE(*str)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2548 | str++; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2549 | } |
| 2550 | if (*str != '\0') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2551 | goto onError; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2552 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2553 | long_normalize(z); |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2554 | z = maybe_small_long(z); |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2555 | if (z == NULL) { |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2556 | return NULL; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2557 | } |
| 2558 | if (pend != NULL) { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 2559 | *pend = (char *)str; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2560 | } |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2561 | return (PyObject *) z; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2562 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2563 | onError: |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2564 | if (pend != NULL) { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 2565 | *pend = (char *)str; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2566 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2567 | Py_XDECREF(z); |
| 2568 | slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200; |
| 2569 | strobj = PyUnicode_FromStringAndSize(orig_str, slen); |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2570 | if (strobj == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2571 | return NULL; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2572 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2573 | PyErr_Format(PyExc_ValueError, |
Serhiy Storchaka | 579ddc2 | 2013-08-03 21:14:05 +0300 | [diff] [blame] | 2574 | "invalid literal for int() with base %d: %.200R", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2575 | base, strobj); |
| 2576 | Py_DECREF(strobj); |
| 2577 | return NULL; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2578 | } |
| 2579 | |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2580 | /* Since PyLong_FromString doesn't have a length parameter, |
| 2581 | * check here for possible NULs in the string. |
| 2582 | * |
| 2583 | * Reports an invalid literal as a bytes object. |
| 2584 | */ |
| 2585 | PyObject * |
| 2586 | _PyLong_FromBytes(const char *s, Py_ssize_t len, int base) |
| 2587 | { |
| 2588 | PyObject *result, *strobj; |
| 2589 | char *end = NULL; |
| 2590 | |
Serhiy Storchaka | 20b39b2 | 2014-09-28 11:27:24 +0300 | [diff] [blame] | 2591 | result = PyLong_FromString(s, &end, base); |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2592 | if (end == NULL || (result != NULL && end == s + len)) |
| 2593 | return result; |
| 2594 | Py_XDECREF(result); |
| 2595 | strobj = PyBytes_FromStringAndSize(s, Py_MIN(len, 200)); |
| 2596 | if (strobj != NULL) { |
| 2597 | PyErr_Format(PyExc_ValueError, |
Serhiy Storchaka | 579ddc2 | 2013-08-03 21:14:05 +0300 | [diff] [blame] | 2598 | "invalid literal for int() with base %d: %.200R", |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2599 | base, strobj); |
| 2600 | Py_DECREF(strobj); |
| 2601 | } |
| 2602 | return NULL; |
| 2603 | } |
| 2604 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2605 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2606 | PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2607 | { |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 2608 | PyObject *v, *unicode = PyUnicode_FromWideChar(u, length); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2609 | if (unicode == NULL) |
| 2610 | return NULL; |
| 2611 | v = PyLong_FromUnicodeObject(unicode, base); |
| 2612 | Py_DECREF(unicode); |
| 2613 | return v; |
| 2614 | } |
| 2615 | |
| 2616 | PyObject * |
| 2617 | PyLong_FromUnicodeObject(PyObject *u, int base) |
| 2618 | { |
Serhiy Storchaka | 579ddc2 | 2013-08-03 21:14:05 +0300 | [diff] [blame] | 2619 | PyObject *result, *asciidig; |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 2620 | const char *buffer; |
| 2621 | char *end = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2622 | Py_ssize_t buflen; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2623 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2624 | asciidig = _PyUnicode_TransformDecimalAndSpaceToASCII(u); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 2625 | if (asciidig == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2626 | return NULL; |
Serhiy Storchaka | 9b6c60c | 2017-11-13 21:23:48 +0200 | [diff] [blame] | 2627 | assert(PyUnicode_IS_ASCII(asciidig)); |
| 2628 | /* Simply get a pointer to existing ASCII characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2629 | buffer = PyUnicode_AsUTF8AndSize(asciidig, &buflen); |
Serhiy Storchaka | 9b6c60c | 2017-11-13 21:23:48 +0200 | [diff] [blame] | 2630 | assert(buffer != NULL); |
| 2631 | |
| 2632 | result = PyLong_FromString(buffer, &end, base); |
| 2633 | if (end == NULL || (result != NULL && end == buffer + buflen)) { |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 2634 | Py_DECREF(asciidig); |
Serhiy Storchaka | 9b6c60c | 2017-11-13 21:23:48 +0200 | [diff] [blame] | 2635 | return result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2636 | } |
Serhiy Storchaka | 9b6c60c | 2017-11-13 21:23:48 +0200 | [diff] [blame] | 2637 | Py_DECREF(asciidig); |
| 2638 | Py_XDECREF(result); |
Serhiy Storchaka | 579ddc2 | 2013-08-03 21:14:05 +0300 | [diff] [blame] | 2639 | PyErr_Format(PyExc_ValueError, |
| 2640 | "invalid literal for int() with base %d: %.200R", |
| 2641 | base, u); |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2642 | return NULL; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2643 | } |
| 2644 | |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2645 | /* forward */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2646 | static PyLongObject *x_divrem |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2647 | (PyLongObject *, PyLongObject *, PyLongObject **); |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 2648 | static PyObject *long_long(PyObject *v); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2649 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2650 | /* Int division with remainder, top-level routine */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2651 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2652 | static int |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2653 | long_divrem(PyLongObject *a, PyLongObject *b, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2654 | PyLongObject **pdiv, PyLongObject **prem) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2655 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 2656 | 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] | 2657 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2658 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2659 | if (size_b == 0) { |
| 2660 | PyErr_SetString(PyExc_ZeroDivisionError, |
| 2661 | "integer division or modulo by zero"); |
| 2662 | return -1; |
| 2663 | } |
| 2664 | if (size_a < size_b || |
| 2665 | (size_a == size_b && |
| 2666 | a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) { |
| 2667 | /* |a| < |b|. */ |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 2668 | *prem = (PyLongObject *)long_long((PyObject *)a); |
Mark Dickinson | b820d7f | 2016-08-22 12:24:46 +0100 | [diff] [blame] | 2669 | if (*prem == NULL) { |
Mark Dickinson | b820d7f | 2016-08-22 12:24:46 +0100 | [diff] [blame] | 2670 | return -1; |
| 2671 | } |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 2672 | Py_INCREF(_PyLong_Zero); |
| 2673 | *pdiv = (PyLongObject*)_PyLong_Zero; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2674 | return 0; |
| 2675 | } |
| 2676 | if (size_b == 1) { |
| 2677 | digit rem = 0; |
| 2678 | z = divrem1(a, b->ob_digit[0], &rem); |
| 2679 | if (z == NULL) |
| 2680 | return -1; |
| 2681 | *prem = (PyLongObject *) PyLong_FromLong((long)rem); |
| 2682 | if (*prem == NULL) { |
| 2683 | Py_DECREF(z); |
| 2684 | return -1; |
| 2685 | } |
| 2686 | } |
| 2687 | else { |
| 2688 | z = x_divrem(a, b, prem); |
| 2689 | if (z == NULL) |
| 2690 | return -1; |
| 2691 | } |
| 2692 | /* Set the signs. |
| 2693 | The quotient z has the sign of a*b; |
| 2694 | the remainder r has the sign of a, |
| 2695 | so a = b*z + r. */ |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 2696 | if ((Py_SIZE(a) < 0) != (Py_SIZE(b) < 0)) { |
| 2697 | _PyLong_Negate(&z); |
| 2698 | if (z == NULL) { |
| 2699 | Py_CLEAR(*prem); |
| 2700 | return -1; |
| 2701 | } |
| 2702 | } |
| 2703 | if (Py_SIZE(a) < 0 && Py_SIZE(*prem) != 0) { |
| 2704 | _PyLong_Negate(prem); |
| 2705 | if (*prem == NULL) { |
| 2706 | Py_DECREF(z); |
| 2707 | Py_CLEAR(*prem); |
| 2708 | return -1; |
| 2709 | } |
| 2710 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2711 | *pdiv = maybe_small_long(z); |
| 2712 | return 0; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2713 | } |
| 2714 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2715 | /* Unsigned int division with remainder -- the algorithm. The arguments v1 |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 2716 | 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] | 2717 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2718 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2719 | x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2720 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2721 | PyLongObject *v, *w, *a; |
| 2722 | Py_ssize_t i, k, size_v, size_w; |
| 2723 | int d; |
| 2724 | digit wm1, wm2, carry, q, r, vtop, *v0, *vk, *w0, *ak; |
| 2725 | twodigits vv; |
| 2726 | sdigit zhi; |
| 2727 | stwodigits z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2728 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2729 | /* We follow Knuth [The Art of Computer Programming, Vol. 2 (3rd |
| 2730 | edn.), section 4.3.1, Algorithm D], except that we don't explicitly |
| 2731 | handle the special case when the initial estimate q for a quotient |
| 2732 | digit is >= PyLong_BASE: the max value for q is PyLong_BASE+1, and |
| 2733 | that won't overflow a digit. */ |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 2734 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2735 | /* allocate space; w will also be used to hold the final remainder */ |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 2736 | size_v = Py_ABS(Py_SIZE(v1)); |
| 2737 | size_w = Py_ABS(Py_SIZE(w1)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2738 | assert(size_v >= size_w && size_w >= 2); /* Assert checks by div() */ |
| 2739 | v = _PyLong_New(size_v+1); |
| 2740 | if (v == NULL) { |
| 2741 | *prem = NULL; |
| 2742 | return NULL; |
| 2743 | } |
| 2744 | w = _PyLong_New(size_w); |
| 2745 | if (w == NULL) { |
| 2746 | Py_DECREF(v); |
| 2747 | *prem = NULL; |
| 2748 | return NULL; |
| 2749 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2750 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2751 | /* normalize: shift w1 left so that its top digit is >= PyLong_BASE/2. |
| 2752 | shift v1 left by the same amount. Results go into w and v. */ |
Niklas Fiekas | c5b7900 | 2020-01-16 15:09:19 +0100 | [diff] [blame^] | 2753 | d = PyLong_SHIFT - _Py_bit_length(w1->ob_digit[size_w-1]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2754 | carry = v_lshift(w->ob_digit, w1->ob_digit, size_w, d); |
| 2755 | assert(carry == 0); |
| 2756 | carry = v_lshift(v->ob_digit, v1->ob_digit, size_v, d); |
| 2757 | if (carry != 0 || v->ob_digit[size_v-1] >= w->ob_digit[size_w-1]) { |
| 2758 | v->ob_digit[size_v] = carry; |
| 2759 | size_v++; |
| 2760 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2761 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2762 | /* Now v->ob_digit[size_v-1] < w->ob_digit[size_w-1], so quotient has |
| 2763 | at most (and usually exactly) k = size_v - size_w digits. */ |
| 2764 | k = size_v - size_w; |
| 2765 | assert(k >= 0); |
| 2766 | a = _PyLong_New(k); |
| 2767 | if (a == NULL) { |
| 2768 | Py_DECREF(w); |
| 2769 | Py_DECREF(v); |
| 2770 | *prem = NULL; |
| 2771 | return NULL; |
| 2772 | } |
| 2773 | v0 = v->ob_digit; |
| 2774 | w0 = w->ob_digit; |
| 2775 | wm1 = w0[size_w-1]; |
| 2776 | wm2 = w0[size_w-2]; |
| 2777 | for (vk = v0+k, ak = a->ob_digit + k; vk-- > v0;) { |
| 2778 | /* inner loop: divide vk[0:size_w+1] by w0[0:size_w], giving |
| 2779 | single-digit quotient q, remainder in vk[0:size_w]. */ |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2780 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2781 | SIGCHECK({ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2782 | Py_DECREF(a); |
| 2783 | Py_DECREF(w); |
| 2784 | Py_DECREF(v); |
| 2785 | *prem = NULL; |
| 2786 | return NULL; |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 2787 | }); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2788 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2789 | /* estimate quotient digit q; may overestimate by 1 (rare) */ |
| 2790 | vtop = vk[size_w]; |
| 2791 | assert(vtop <= wm1); |
| 2792 | vv = ((twodigits)vtop << PyLong_SHIFT) | vk[size_w-1]; |
| 2793 | q = (digit)(vv / wm1); |
| 2794 | r = (digit)(vv - (twodigits)wm1 * q); /* r = vv % wm1 */ |
| 2795 | while ((twodigits)wm2 * q > (((twodigits)r << PyLong_SHIFT) |
| 2796 | | vk[size_w-2])) { |
| 2797 | --q; |
| 2798 | r += wm1; |
| 2799 | if (r >= PyLong_BASE) |
| 2800 | break; |
| 2801 | } |
| 2802 | assert(q <= PyLong_BASE); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2803 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2804 | /* subtract q*w0[0:size_w] from vk[0:size_w+1] */ |
| 2805 | zhi = 0; |
| 2806 | for (i = 0; i < size_w; ++i) { |
| 2807 | /* invariants: -PyLong_BASE <= -q <= zhi <= 0; |
| 2808 | -PyLong_BASE * q <= z < PyLong_BASE */ |
| 2809 | z = (sdigit)vk[i] + zhi - |
| 2810 | (stwodigits)q * (stwodigits)w0[i]; |
| 2811 | vk[i] = (digit)z & PyLong_MASK; |
| 2812 | zhi = (sdigit)Py_ARITHMETIC_RIGHT_SHIFT(stwodigits, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2813 | z, PyLong_SHIFT); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2814 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2815 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2816 | /* add w back if q was too large (this branch taken rarely) */ |
| 2817 | assert((sdigit)vtop + zhi == -1 || (sdigit)vtop + zhi == 0); |
| 2818 | if ((sdigit)vtop + zhi < 0) { |
| 2819 | carry = 0; |
| 2820 | for (i = 0; i < size_w; ++i) { |
| 2821 | carry += vk[i] + w0[i]; |
| 2822 | vk[i] = carry & PyLong_MASK; |
| 2823 | carry >>= PyLong_SHIFT; |
| 2824 | } |
| 2825 | --q; |
| 2826 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2827 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2828 | /* store quotient digit */ |
| 2829 | assert(q < PyLong_BASE); |
| 2830 | *--ak = q; |
| 2831 | } |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 2832 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2833 | /* unshift remainder; we reuse w to store the result */ |
| 2834 | carry = v_rshift(w0, v0, size_w, d); |
| 2835 | assert(carry==0); |
| 2836 | Py_DECREF(v); |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 2837 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2838 | *prem = long_normalize(w); |
| 2839 | return long_normalize(a); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2840 | } |
| 2841 | |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2842 | /* For a nonzero PyLong a, express a in the form x * 2**e, with 0.5 <= |
| 2843 | abs(x) < 1.0 and e >= 0; return x and put e in *e. Here x is |
| 2844 | rounded to DBL_MANT_DIG significant bits using round-half-to-even. |
| 2845 | If a == 0, return 0.0 and set *e = 0. If the resulting exponent |
| 2846 | e is larger than PY_SSIZE_T_MAX, raise OverflowError and return |
| 2847 | -1.0. */ |
| 2848 | |
| 2849 | /* attempt to define 2.0**DBL_MANT_DIG as a compile-time constant */ |
| 2850 | #if DBL_MANT_DIG == 53 |
| 2851 | #define EXP2_DBL_MANT_DIG 9007199254740992.0 |
| 2852 | #else |
| 2853 | #define EXP2_DBL_MANT_DIG (ldexp(1.0, DBL_MANT_DIG)) |
| 2854 | #endif |
| 2855 | |
| 2856 | double |
| 2857 | _PyLong_Frexp(PyLongObject *a, Py_ssize_t *e) |
| 2858 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2859 | Py_ssize_t a_size, a_bits, shift_digits, shift_bits, x_size; |
| 2860 | /* See below for why x_digits is always large enough. */ |
| 2861 | digit rem, x_digits[2 + (DBL_MANT_DIG + 1) / PyLong_SHIFT]; |
| 2862 | double dx; |
| 2863 | /* Correction term for round-half-to-even rounding. For a digit x, |
| 2864 | "x + half_even_correction[x & 7]" gives x rounded to the nearest |
| 2865 | multiple of 4, rounding ties to a multiple of 8. */ |
| 2866 | 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] | 2867 | |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 2868 | a_size = Py_ABS(Py_SIZE(a)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2869 | if (a_size == 0) { |
| 2870 | /* Special case for 0: significand 0.0, exponent 0. */ |
| 2871 | *e = 0; |
| 2872 | return 0.0; |
| 2873 | } |
Niklas Fiekas | c5b7900 | 2020-01-16 15:09:19 +0100 | [diff] [blame^] | 2874 | a_bits = _Py_bit_length(a->ob_digit[a_size-1]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2875 | /* The following is an overflow-free version of the check |
| 2876 | "if ((a_size - 1) * PyLong_SHIFT + a_bits > PY_SSIZE_T_MAX) ..." */ |
| 2877 | if (a_size >= (PY_SSIZE_T_MAX - 1) / PyLong_SHIFT + 1 && |
| 2878 | (a_size > (PY_SSIZE_T_MAX - 1) / PyLong_SHIFT + 1 || |
| 2879 | a_bits > (PY_SSIZE_T_MAX - 1) % PyLong_SHIFT + 1)) |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2880 | goto overflow; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2881 | a_bits = (a_size - 1) * PyLong_SHIFT + a_bits; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2882 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2883 | /* Shift the first DBL_MANT_DIG + 2 bits of a into x_digits[0:x_size] |
| 2884 | (shifting left if a_bits <= DBL_MANT_DIG + 2). |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2885 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2886 | Number of digits needed for result: write // for floor division. |
| 2887 | Then if shifting left, we end up using |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2888 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2889 | 1 + a_size + (DBL_MANT_DIG + 2 - a_bits) // PyLong_SHIFT |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2890 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2891 | digits. If shifting right, we use |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2892 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2893 | a_size - (a_bits - DBL_MANT_DIG - 2) // PyLong_SHIFT |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2894 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2895 | digits. Using a_size = 1 + (a_bits - 1) // PyLong_SHIFT along with |
| 2896 | the inequalities |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2897 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2898 | m // PyLong_SHIFT + n // PyLong_SHIFT <= (m + n) // PyLong_SHIFT |
| 2899 | m // PyLong_SHIFT - n // PyLong_SHIFT <= |
| 2900 | 1 + (m - n - 1) // PyLong_SHIFT, |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2901 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2902 | 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] | 2903 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2904 | x_size <= 2 + (DBL_MANT_DIG + 1) // PyLong_SHIFT |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2905 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2906 | in both cases. |
| 2907 | */ |
| 2908 | if (a_bits <= DBL_MANT_DIG + 2) { |
| 2909 | shift_digits = (DBL_MANT_DIG + 2 - a_bits) / PyLong_SHIFT; |
| 2910 | shift_bits = (DBL_MANT_DIG + 2 - a_bits) % PyLong_SHIFT; |
| 2911 | x_size = 0; |
| 2912 | while (x_size < shift_digits) |
| 2913 | x_digits[x_size++] = 0; |
| 2914 | rem = v_lshift(x_digits + x_size, a->ob_digit, a_size, |
| 2915 | (int)shift_bits); |
| 2916 | x_size += a_size; |
| 2917 | x_digits[x_size++] = rem; |
| 2918 | } |
| 2919 | else { |
| 2920 | shift_digits = (a_bits - DBL_MANT_DIG - 2) / PyLong_SHIFT; |
| 2921 | shift_bits = (a_bits - DBL_MANT_DIG - 2) % PyLong_SHIFT; |
| 2922 | rem = v_rshift(x_digits, a->ob_digit + shift_digits, |
| 2923 | a_size - shift_digits, (int)shift_bits); |
| 2924 | x_size = a_size - shift_digits; |
| 2925 | /* For correct rounding below, we need the least significant |
| 2926 | bit of x to be 'sticky' for this shift: if any of the bits |
| 2927 | shifted out was nonzero, we set the least significant bit |
| 2928 | of x. */ |
| 2929 | if (rem) |
| 2930 | x_digits[0] |= 1; |
| 2931 | else |
| 2932 | while (shift_digits > 0) |
| 2933 | if (a->ob_digit[--shift_digits]) { |
| 2934 | x_digits[0] |= 1; |
| 2935 | break; |
| 2936 | } |
| 2937 | } |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2938 | 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] | 2939 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2940 | /* Round, and convert to double. */ |
| 2941 | x_digits[0] += half_even_correction[x_digits[0] & 7]; |
| 2942 | dx = x_digits[--x_size]; |
| 2943 | while (x_size > 0) |
| 2944 | dx = dx * PyLong_BASE + x_digits[--x_size]; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2945 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2946 | /* Rescale; make correction if result is 1.0. */ |
| 2947 | dx /= 4.0 * EXP2_DBL_MANT_DIG; |
| 2948 | if (dx == 1.0) { |
| 2949 | if (a_bits == PY_SSIZE_T_MAX) |
| 2950 | goto overflow; |
| 2951 | dx = 0.5; |
| 2952 | a_bits += 1; |
| 2953 | } |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2954 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2955 | *e = a_bits; |
| 2956 | return Py_SIZE(a) < 0 ? -dx : dx; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2957 | |
| 2958 | overflow: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2959 | /* exponent > PY_SSIZE_T_MAX */ |
| 2960 | PyErr_SetString(PyExc_OverflowError, |
| 2961 | "huge integer: number of bits overflows a Py_ssize_t"); |
| 2962 | *e = 0; |
| 2963 | return -1.0; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2964 | } |
| 2965 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2966 | /* 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] | 2967 | using the round-half-to-even rule in the case of a tie. */ |
| 2968 | |
| 2969 | double |
| 2970 | PyLong_AsDouble(PyObject *v) |
| 2971 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2972 | Py_ssize_t exponent; |
| 2973 | double x; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2974 | |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 2975 | if (v == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2976 | PyErr_BadInternalCall(); |
| 2977 | return -1.0; |
| 2978 | } |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 2979 | if (!PyLong_Check(v)) { |
| 2980 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 2981 | return -1.0; |
| 2982 | } |
Yury Selivanov | 186c30b | 2016-02-05 19:40:01 -0500 | [diff] [blame] | 2983 | if (Py_ABS(Py_SIZE(v)) <= 1) { |
Yury Selivanov | a0fcaca | 2016-02-06 12:21:33 -0500 | [diff] [blame] | 2984 | /* Fast path; single digit long (31 bits) will cast safely |
Mark Dickinson | 1dc3c89 | 2016-08-21 10:33:36 +0100 | [diff] [blame] | 2985 | to double. This improves performance of FP/long operations |
| 2986 | by 20%. |
Yury Selivanov | 186c30b | 2016-02-05 19:40:01 -0500 | [diff] [blame] | 2987 | */ |
| 2988 | return (double)MEDIUM_VALUE((PyLongObject *)v); |
| 2989 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2990 | x = _PyLong_Frexp((PyLongObject *)v, &exponent); |
| 2991 | if ((x == -1.0 && PyErr_Occurred()) || exponent > DBL_MAX_EXP) { |
| 2992 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 02515f7 | 2013-08-03 12:08:22 +0100 | [diff] [blame] | 2993 | "int too large to convert to float"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2994 | return -1.0; |
| 2995 | } |
| 2996 | return ldexp(x, (int)exponent); |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2997 | } |
| 2998 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2999 | /* Methods */ |
| 3000 | |
HongWeipeng | 42acb7b | 2019-09-18 23:10:15 +0800 | [diff] [blame] | 3001 | /* if a < b, return a negative number |
| 3002 | if a == b, return 0 |
| 3003 | if a > b, return a positive number */ |
| 3004 | |
| 3005 | static Py_ssize_t |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3006 | long_compare(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3007 | { |
HongWeipeng | 42acb7b | 2019-09-18 23:10:15 +0800 | [diff] [blame] | 3008 | Py_ssize_t sign = Py_SIZE(a) - Py_SIZE(b); |
| 3009 | if (sign == 0) { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3010 | Py_ssize_t i = Py_ABS(Py_SIZE(a)); |
HongWeipeng | 42acb7b | 2019-09-18 23:10:15 +0800 | [diff] [blame] | 3011 | sdigit diff = 0; |
| 3012 | while (--i >= 0) { |
| 3013 | diff = (sdigit) a->ob_digit[i] - (sdigit) b->ob_digit[i]; |
| 3014 | if (diff) { |
| 3015 | break; |
| 3016 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3017 | } |
HongWeipeng | 42acb7b | 2019-09-18 23:10:15 +0800 | [diff] [blame] | 3018 | sign = Py_SIZE(a) < 0 ? -diff : diff; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3019 | } |
HongWeipeng | 42acb7b | 2019-09-18 23:10:15 +0800 | [diff] [blame] | 3020 | return sign; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3021 | } |
| 3022 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3023 | static PyObject * |
| 3024 | long_richcompare(PyObject *self, PyObject *other, int op) |
| 3025 | { |
HongWeipeng | 42acb7b | 2019-09-18 23:10:15 +0800 | [diff] [blame] | 3026 | Py_ssize_t result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3027 | CHECK_BINOP(self, other); |
| 3028 | if (self == other) |
| 3029 | result = 0; |
| 3030 | else |
| 3031 | result = long_compare((PyLongObject*)self, (PyLongObject*)other); |
stratakis | e8b1965 | 2017-11-02 11:32:54 +0100 | [diff] [blame] | 3032 | Py_RETURN_RICHCOMPARE(result, 0, op); |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3033 | } |
| 3034 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3035 | static Py_hash_t |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3036 | long_hash(PyLongObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 3037 | { |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 3038 | Py_uhash_t x; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3039 | Py_ssize_t i; |
| 3040 | int sign; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 3041 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3042 | i = Py_SIZE(v); |
| 3043 | switch(i) { |
| 3044 | case -1: return v->ob_digit[0]==1 ? -2 : -(sdigit)v->ob_digit[0]; |
| 3045 | case 0: return 0; |
| 3046 | case 1: return v->ob_digit[0]; |
| 3047 | } |
| 3048 | sign = 1; |
| 3049 | x = 0; |
| 3050 | if (i < 0) { |
| 3051 | sign = -1; |
| 3052 | i = -(i); |
| 3053 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3054 | while (--i >= 0) { |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 3055 | /* Here x is a quantity in the range [0, _PyHASH_MODULUS); we |
| 3056 | want to compute x * 2**PyLong_SHIFT + v->ob_digit[i] modulo |
| 3057 | _PyHASH_MODULUS. |
| 3058 | |
| 3059 | The computation of x * 2**PyLong_SHIFT % _PyHASH_MODULUS |
| 3060 | amounts to a rotation of the bits of x. To see this, write |
| 3061 | |
| 3062 | x * 2**PyLong_SHIFT = y * 2**_PyHASH_BITS + z |
| 3063 | |
| 3064 | where y = x >> (_PyHASH_BITS - PyLong_SHIFT) gives the top |
| 3065 | PyLong_SHIFT bits of x (those that are shifted out of the |
| 3066 | original _PyHASH_BITS bits, and z = (x << PyLong_SHIFT) & |
| 3067 | _PyHASH_MODULUS gives the bottom _PyHASH_BITS - PyLong_SHIFT |
| 3068 | bits of x, shifted up. Then since 2**_PyHASH_BITS is |
| 3069 | congruent to 1 modulo _PyHASH_MODULUS, y*2**_PyHASH_BITS is |
| 3070 | congruent to y modulo _PyHASH_MODULUS. So |
| 3071 | |
| 3072 | x * 2**PyLong_SHIFT = y + z (mod _PyHASH_MODULUS). |
| 3073 | |
| 3074 | The right-hand side is just the result of rotating the |
| 3075 | _PyHASH_BITS bits of x left by PyLong_SHIFT places; since |
| 3076 | not all _PyHASH_BITS bits of x are 1s, the same is true |
| 3077 | after rotation, so 0 <= y+z < _PyHASH_MODULUS and y + z is |
| 3078 | the reduction of x*2**PyLong_SHIFT modulo |
| 3079 | _PyHASH_MODULUS. */ |
| 3080 | x = ((x << PyLong_SHIFT) & _PyHASH_MODULUS) | |
| 3081 | (x >> (_PyHASH_BITS - PyLong_SHIFT)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3082 | x += v->ob_digit[i]; |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 3083 | if (x >= _PyHASH_MODULUS) |
| 3084 | x -= _PyHASH_MODULUS; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3085 | } |
| 3086 | x = x * sign; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 3087 | if (x == (Py_uhash_t)-1) |
| 3088 | x = (Py_uhash_t)-2; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3089 | return (Py_hash_t)x; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 3090 | } |
| 3091 | |
| 3092 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 3093 | /* Add the absolute values of two integers. */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3094 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3095 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3096 | x_add(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3097 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3098 | 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] | 3099 | PyLongObject *z; |
| 3100 | Py_ssize_t i; |
| 3101 | digit carry = 0; |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 3102 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3103 | /* Ensure a is the larger of the two: */ |
| 3104 | if (size_a < size_b) { |
| 3105 | { PyLongObject *temp = a; a = b; b = temp; } |
| 3106 | { Py_ssize_t size_temp = size_a; |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3107 | size_a = size_b; |
| 3108 | size_b = size_temp; } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3109 | } |
| 3110 | z = _PyLong_New(size_a+1); |
| 3111 | if (z == NULL) |
| 3112 | return NULL; |
| 3113 | for (i = 0; i < size_b; ++i) { |
| 3114 | carry += a->ob_digit[i] + b->ob_digit[i]; |
| 3115 | z->ob_digit[i] = carry & PyLong_MASK; |
| 3116 | carry >>= PyLong_SHIFT; |
| 3117 | } |
| 3118 | for (; i < size_a; ++i) { |
| 3119 | carry += a->ob_digit[i]; |
| 3120 | z->ob_digit[i] = carry & PyLong_MASK; |
| 3121 | carry >>= PyLong_SHIFT; |
| 3122 | } |
| 3123 | z->ob_digit[i] = carry; |
| 3124 | return long_normalize(z); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3125 | } |
| 3126 | |
| 3127 | /* Subtract the absolute values of two integers. */ |
| 3128 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3129 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3130 | x_sub(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3131 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3132 | 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] | 3133 | PyLongObject *z; |
| 3134 | Py_ssize_t i; |
| 3135 | int sign = 1; |
| 3136 | digit borrow = 0; |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 3137 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3138 | /* Ensure a is the larger of the two: */ |
| 3139 | if (size_a < size_b) { |
| 3140 | sign = -1; |
| 3141 | { PyLongObject *temp = a; a = b; b = temp; } |
| 3142 | { Py_ssize_t size_temp = size_a; |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3143 | size_a = size_b; |
| 3144 | size_b = size_temp; } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3145 | } |
| 3146 | else if (size_a == size_b) { |
| 3147 | /* Find highest digit where a and b differ: */ |
| 3148 | i = size_a; |
| 3149 | while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) |
| 3150 | ; |
| 3151 | if (i < 0) |
| 3152 | return (PyLongObject *)PyLong_FromLong(0); |
| 3153 | if (a->ob_digit[i] < b->ob_digit[i]) { |
| 3154 | sign = -1; |
| 3155 | { PyLongObject *temp = a; a = b; b = temp; } |
| 3156 | } |
| 3157 | size_a = size_b = i+1; |
| 3158 | } |
| 3159 | z = _PyLong_New(size_a); |
| 3160 | if (z == NULL) |
| 3161 | return NULL; |
| 3162 | for (i = 0; i < size_b; ++i) { |
| 3163 | /* The following assumes unsigned arithmetic |
| 3164 | works module 2**N for some N>PyLong_SHIFT. */ |
| 3165 | borrow = a->ob_digit[i] - b->ob_digit[i] - borrow; |
| 3166 | z->ob_digit[i] = borrow & PyLong_MASK; |
| 3167 | borrow >>= PyLong_SHIFT; |
| 3168 | borrow &= 1; /* Keep only one sign bit */ |
| 3169 | } |
| 3170 | for (; i < size_a; ++i) { |
| 3171 | borrow = a->ob_digit[i] - borrow; |
| 3172 | z->ob_digit[i] = borrow & PyLong_MASK; |
| 3173 | borrow >>= PyLong_SHIFT; |
| 3174 | borrow &= 1; /* Keep only one sign bit */ |
| 3175 | } |
| 3176 | assert(borrow == 0); |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 3177 | if (sign < 0) { |
Victor Stinner | 8bcf312 | 2016-08-17 19:48:33 +0200 | [diff] [blame] | 3178 | Py_SIZE(z) = -Py_SIZE(z); |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 3179 | } |
HongWeipeng | 036fe85 | 2019-11-26 15:54:49 +0800 | [diff] [blame] | 3180 | return maybe_small_long(long_normalize(z)); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3181 | } |
| 3182 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3183 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3184 | long_add(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3185 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3186 | PyLongObject *z; |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 3187 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3188 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3189 | |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3190 | 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] | 3191 | return PyLong_FromLong(MEDIUM_VALUE(a) + MEDIUM_VALUE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3192 | } |
| 3193 | if (Py_SIZE(a) < 0) { |
| 3194 | if (Py_SIZE(b) < 0) { |
| 3195 | z = x_add(a, b); |
Serhiy Storchaka | e63e5d6 | 2016-06-04 00:06:45 +0300 | [diff] [blame] | 3196 | if (z != NULL) { |
| 3197 | /* x_add received at least one multiple-digit int, |
| 3198 | and thus z must be a multiple-digit int. |
| 3199 | That also means z is not an element of |
| 3200 | small_ints, so negating it in-place is safe. */ |
| 3201 | assert(Py_REFCNT(z) == 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3202 | Py_SIZE(z) = -(Py_SIZE(z)); |
Serhiy Storchaka | e63e5d6 | 2016-06-04 00:06:45 +0300 | [diff] [blame] | 3203 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3204 | } |
| 3205 | else |
| 3206 | z = x_sub(b, a); |
| 3207 | } |
| 3208 | else { |
| 3209 | if (Py_SIZE(b) < 0) |
| 3210 | z = x_sub(a, b); |
| 3211 | else |
| 3212 | z = x_add(a, b); |
| 3213 | } |
| 3214 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3215 | } |
| 3216 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3217 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3218 | long_sub(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3219 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3220 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3221 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3222 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3223 | |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3224 | 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] | 3225 | return PyLong_FromLong(MEDIUM_VALUE(a) - MEDIUM_VALUE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3226 | } |
| 3227 | if (Py_SIZE(a) < 0) { |
HongWeipeng | 036fe85 | 2019-11-26 15:54:49 +0800 | [diff] [blame] | 3228 | if (Py_SIZE(b) < 0) { |
| 3229 | z = x_sub(b, a); |
| 3230 | } |
| 3231 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3232 | z = x_add(a, b); |
HongWeipeng | 036fe85 | 2019-11-26 15:54:49 +0800 | [diff] [blame] | 3233 | if (z != NULL) { |
| 3234 | assert(Py_SIZE(z) == 0 || Py_REFCNT(z) == 1); |
| 3235 | Py_SIZE(z) = -(Py_SIZE(z)); |
| 3236 | } |
Serhiy Storchaka | e63e5d6 | 2016-06-04 00:06:45 +0300 | [diff] [blame] | 3237 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3238 | } |
| 3239 | else { |
| 3240 | if (Py_SIZE(b) < 0) |
| 3241 | z = x_add(a, b); |
| 3242 | else |
| 3243 | z = x_sub(a, b); |
| 3244 | } |
| 3245 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3246 | } |
| 3247 | |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3248 | /* Grade school multiplication, ignoring the signs. |
| 3249 | * Returns the absolute value of the product, or NULL if error. |
| 3250 | */ |
| 3251 | static PyLongObject * |
| 3252 | x_mul(PyLongObject *a, PyLongObject *b) |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3253 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3254 | PyLongObject *z; |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3255 | Py_ssize_t size_a = Py_ABS(Py_SIZE(a)); |
| 3256 | Py_ssize_t size_b = Py_ABS(Py_SIZE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3257 | Py_ssize_t i; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3258 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3259 | z = _PyLong_New(size_a + size_b); |
| 3260 | if (z == NULL) |
| 3261 | return NULL; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3262 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3263 | memset(z->ob_digit, 0, Py_SIZE(z) * sizeof(digit)); |
| 3264 | if (a == b) { |
| 3265 | /* Efficient squaring per HAC, Algorithm 14.16: |
| 3266 | * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf |
| 3267 | * Gives slightly less than a 2x speedup when a == b, |
| 3268 | * via exploiting that each entry in the multiplication |
| 3269 | * pyramid appears twice (except for the size_a squares). |
| 3270 | */ |
| 3271 | for (i = 0; i < size_a; ++i) { |
| 3272 | twodigits carry; |
| 3273 | twodigits f = a->ob_digit[i]; |
| 3274 | digit *pz = z->ob_digit + (i << 1); |
| 3275 | digit *pa = a->ob_digit + i + 1; |
| 3276 | digit *paend = a->ob_digit + size_a; |
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 | SIGCHECK({ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3279 | Py_DECREF(z); |
| 3280 | return NULL; |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 3281 | }); |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 3282 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3283 | carry = *pz + f * f; |
| 3284 | *pz++ = (digit)(carry & PyLong_MASK); |
| 3285 | carry >>= PyLong_SHIFT; |
| 3286 | assert(carry <= PyLong_MASK); |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 3287 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3288 | /* Now f is added in twice in each column of the |
| 3289 | * pyramid it appears. Same as adding f<<1 once. |
| 3290 | */ |
| 3291 | f <<= 1; |
| 3292 | while (pa < paend) { |
| 3293 | carry += *pz + *pa++ * f; |
| 3294 | *pz++ = (digit)(carry & PyLong_MASK); |
| 3295 | carry >>= PyLong_SHIFT; |
| 3296 | assert(carry <= (PyLong_MASK << 1)); |
| 3297 | } |
| 3298 | if (carry) { |
| 3299 | carry += *pz; |
| 3300 | *pz++ = (digit)(carry & PyLong_MASK); |
| 3301 | carry >>= PyLong_SHIFT; |
| 3302 | } |
| 3303 | if (carry) |
| 3304 | *pz += (digit)(carry & PyLong_MASK); |
| 3305 | assert((carry >> PyLong_SHIFT) == 0); |
| 3306 | } |
| 3307 | } |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 3308 | else { /* a is not the same as b -- gradeschool int mult */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3309 | for (i = 0; i < size_a; ++i) { |
| 3310 | twodigits carry = 0; |
| 3311 | twodigits f = a->ob_digit[i]; |
| 3312 | digit *pz = z->ob_digit + i; |
| 3313 | digit *pb = b->ob_digit; |
| 3314 | digit *pbend = b->ob_digit + size_b; |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 3315 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3316 | SIGCHECK({ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3317 | Py_DECREF(z); |
| 3318 | return NULL; |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 3319 | }); |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 3320 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3321 | while (pb < pbend) { |
| 3322 | carry += *pz + *pb++ * f; |
| 3323 | *pz++ = (digit)(carry & PyLong_MASK); |
| 3324 | carry >>= PyLong_SHIFT; |
| 3325 | assert(carry <= PyLong_MASK); |
| 3326 | } |
| 3327 | if (carry) |
| 3328 | *pz += (digit)(carry & PyLong_MASK); |
| 3329 | assert((carry >> PyLong_SHIFT) == 0); |
| 3330 | } |
| 3331 | } |
| 3332 | return long_normalize(z); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3333 | } |
| 3334 | |
| 3335 | /* A helper for Karatsuba multiplication (k_mul). |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 3336 | Takes an int "n" and an integer "size" representing the place to |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3337 | split, and sets low and high such that abs(n) == (high << size) + low, |
| 3338 | viewing the shift as being by digits. The sign bit is ignored, and |
| 3339 | the return values are >= 0. |
| 3340 | Returns 0 on success, -1 on failure. |
| 3341 | */ |
| 3342 | static int |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3343 | kmul_split(PyLongObject *n, |
| 3344 | Py_ssize_t size, |
| 3345 | PyLongObject **high, |
| 3346 | PyLongObject **low) |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3347 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3348 | PyLongObject *hi, *lo; |
| 3349 | Py_ssize_t size_lo, size_hi; |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3350 | const Py_ssize_t size_n = Py_ABS(Py_SIZE(n)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3351 | |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 3352 | size_lo = Py_MIN(size_n, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3353 | size_hi = size_n - size_lo; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3354 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3355 | if ((hi = _PyLong_New(size_hi)) == NULL) |
| 3356 | return -1; |
| 3357 | if ((lo = _PyLong_New(size_lo)) == NULL) { |
| 3358 | Py_DECREF(hi); |
| 3359 | return -1; |
| 3360 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3361 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3362 | memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit)); |
| 3363 | 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] | 3364 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3365 | *high = long_normalize(hi); |
| 3366 | *low = long_normalize(lo); |
| 3367 | return 0; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3368 | } |
| 3369 | |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3370 | static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b); |
| 3371 | |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3372 | /* Karatsuba multiplication. Ignores the input signs, and returns the |
| 3373 | * absolute value of the product (or NULL if error). |
| 3374 | * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295). |
| 3375 | */ |
| 3376 | static PyLongObject * |
| 3377 | k_mul(PyLongObject *a, PyLongObject *b) |
| 3378 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3379 | Py_ssize_t asize = Py_ABS(Py_SIZE(a)); |
| 3380 | Py_ssize_t bsize = Py_ABS(Py_SIZE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3381 | PyLongObject *ah = NULL; |
| 3382 | PyLongObject *al = NULL; |
| 3383 | PyLongObject *bh = NULL; |
| 3384 | PyLongObject *bl = NULL; |
| 3385 | PyLongObject *ret = NULL; |
| 3386 | PyLongObject *t1, *t2, *t3; |
| 3387 | Py_ssize_t shift; /* the number of digits we split off */ |
| 3388 | Py_ssize_t i; |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 3389 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3390 | /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl |
| 3391 | * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl |
| 3392 | * Then the original product is |
| 3393 | * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl |
| 3394 | * By picking X to be a power of 2, "*X" is just shifting, and it's |
| 3395 | * been reduced to 3 multiplies on numbers half the size. |
| 3396 | */ |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3397 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3398 | /* We want to split based on the larger number; fiddle so that b |
| 3399 | * is largest. |
| 3400 | */ |
| 3401 | if (asize > bsize) { |
| 3402 | t1 = a; |
| 3403 | a = b; |
| 3404 | b = t1; |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 3405 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3406 | i = asize; |
| 3407 | asize = bsize; |
| 3408 | bsize = i; |
| 3409 | } |
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 | /* Use gradeschool math when either number is too small. */ |
| 3412 | i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF; |
| 3413 | if (asize <= i) { |
| 3414 | if (asize == 0) |
| 3415 | return (PyLongObject *)PyLong_FromLong(0); |
| 3416 | else |
| 3417 | return x_mul(a, b); |
| 3418 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3419 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3420 | /* If a is small compared to b, splitting on b gives a degenerate |
| 3421 | * case with ah==0, and Karatsuba may be (even much) less efficient |
| 3422 | * than "grade school" then. However, we can still win, by viewing |
| 3423 | * b as a string of "big digits", each of width a->ob_size. That |
| 3424 | * leads to a sequence of balanced calls to k_mul. |
| 3425 | */ |
| 3426 | if (2 * asize <= bsize) |
| 3427 | return k_lopsided_mul(a, b); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3428 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3429 | /* Split a & b into hi & lo pieces. */ |
| 3430 | shift = bsize >> 1; |
| 3431 | if (kmul_split(a, shift, &ah, &al) < 0) goto fail; |
| 3432 | assert(Py_SIZE(ah) > 0); /* the split isn't degenerate */ |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3433 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3434 | if (a == b) { |
| 3435 | bh = ah; |
| 3436 | bl = al; |
| 3437 | Py_INCREF(bh); |
| 3438 | Py_INCREF(bl); |
| 3439 | } |
| 3440 | else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3441 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3442 | /* The plan: |
| 3443 | * 1. Allocate result space (asize + bsize digits: that's always |
| 3444 | * enough). |
| 3445 | * 2. Compute ah*bh, and copy into result at 2*shift. |
| 3446 | * 3. Compute al*bl, and copy into result at 0. Note that this |
| 3447 | * can't overlap with #2. |
| 3448 | * 4. Subtract al*bl from the result, starting at shift. This may |
| 3449 | * underflow (borrow out of the high digit), but we don't care: |
| 3450 | * we're effectively doing unsigned arithmetic mod |
| 3451 | * BASE**(sizea + sizeb), and so long as the *final* result fits, |
| 3452 | * borrows and carries out of the high digit can be ignored. |
| 3453 | * 5. Subtract ah*bh from the result, starting at shift. |
| 3454 | * 6. Compute (ah+al)*(bh+bl), and add it into the result starting |
| 3455 | * at shift. |
| 3456 | */ |
Tim Peters | d64c1de | 2002-08-12 17:36:03 +0000 | [diff] [blame] | 3457 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3458 | /* 1. Allocate result space. */ |
| 3459 | ret = _PyLong_New(asize + bsize); |
| 3460 | if (ret == NULL) goto fail; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3461 | #ifdef Py_DEBUG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3462 | /* Fill with trash, to catch reference to uninitialized digits. */ |
| 3463 | memset(ret->ob_digit, 0xDF, Py_SIZE(ret) * sizeof(digit)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3464 | #endif |
Tim Peters | 44121a6 | 2002-08-12 06:17:58 +0000 | [diff] [blame] | 3465 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3466 | /* 2. t1 <- ah*bh, and copy into high digits of result. */ |
| 3467 | if ((t1 = k_mul(ah, bh)) == NULL) goto fail; |
| 3468 | assert(Py_SIZE(t1) >= 0); |
| 3469 | assert(2*shift + Py_SIZE(t1) <= Py_SIZE(ret)); |
| 3470 | memcpy(ret->ob_digit + 2*shift, t1->ob_digit, |
| 3471 | Py_SIZE(t1) * sizeof(digit)); |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 3472 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3473 | /* Zero-out the digits higher than the ah*bh copy. */ |
| 3474 | i = Py_SIZE(ret) - 2*shift - Py_SIZE(t1); |
| 3475 | if (i) |
| 3476 | memset(ret->ob_digit + 2*shift + Py_SIZE(t1), 0, |
| 3477 | i * sizeof(digit)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3478 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3479 | /* 3. t2 <- al*bl, and copy into the low digits. */ |
| 3480 | if ((t2 = k_mul(al, bl)) == NULL) { |
| 3481 | Py_DECREF(t1); |
| 3482 | goto fail; |
| 3483 | } |
| 3484 | assert(Py_SIZE(t2) >= 0); |
| 3485 | assert(Py_SIZE(t2) <= 2*shift); /* no overlap with high digits */ |
| 3486 | memcpy(ret->ob_digit, t2->ob_digit, Py_SIZE(t2) * sizeof(digit)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3487 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3488 | /* Zero out remaining digits. */ |
| 3489 | i = 2*shift - Py_SIZE(t2); /* number of uninitialized digits */ |
| 3490 | if (i) |
| 3491 | memset(ret->ob_digit + Py_SIZE(t2), 0, i * sizeof(digit)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3492 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3493 | /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first |
| 3494 | * because it's fresher in cache. |
| 3495 | */ |
| 3496 | i = Py_SIZE(ret) - shift; /* # digits after shift */ |
| 3497 | (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, Py_SIZE(t2)); |
| 3498 | Py_DECREF(t2); |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 3499 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3500 | (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, Py_SIZE(t1)); |
| 3501 | Py_DECREF(t1); |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 3502 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3503 | /* 6. t3 <- (ah+al)(bh+bl), and add into result. */ |
| 3504 | if ((t1 = x_add(ah, al)) == NULL) goto fail; |
| 3505 | Py_DECREF(ah); |
| 3506 | Py_DECREF(al); |
| 3507 | ah = al = NULL; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3508 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3509 | if (a == b) { |
| 3510 | t2 = t1; |
| 3511 | Py_INCREF(t2); |
| 3512 | } |
| 3513 | else if ((t2 = x_add(bh, bl)) == NULL) { |
| 3514 | Py_DECREF(t1); |
| 3515 | goto fail; |
| 3516 | } |
| 3517 | Py_DECREF(bh); |
| 3518 | Py_DECREF(bl); |
| 3519 | bh = bl = NULL; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3520 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3521 | t3 = k_mul(t1, t2); |
| 3522 | Py_DECREF(t1); |
| 3523 | Py_DECREF(t2); |
| 3524 | if (t3 == NULL) goto fail; |
| 3525 | assert(Py_SIZE(t3) >= 0); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3526 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3527 | /* Add t3. It's not obvious why we can't run out of room here. |
| 3528 | * See the (*) comment after this function. |
| 3529 | */ |
| 3530 | (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, Py_SIZE(t3)); |
| 3531 | Py_DECREF(t3); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3532 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3533 | return long_normalize(ret); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3534 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3535 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3536 | Py_XDECREF(ret); |
| 3537 | Py_XDECREF(ah); |
| 3538 | Py_XDECREF(al); |
| 3539 | Py_XDECREF(bh); |
| 3540 | Py_XDECREF(bl); |
| 3541 | return NULL; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3542 | } |
| 3543 | |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3544 | /* (*) Why adding t3 can't "run out of room" above. |
| 3545 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3546 | Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts |
| 3547 | to start with: |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3548 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3549 | 1. For any integer i, i = c(i/2) + f(i/2). In particular, |
| 3550 | bsize = c(bsize/2) + f(bsize/2). |
| 3551 | 2. shift = f(bsize/2) |
| 3552 | 3. asize <= bsize |
| 3553 | 4. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this |
| 3554 | routine, so asize > bsize/2 >= f(bsize/2) in this routine. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3555 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3556 | We allocated asize + bsize result digits, and add t3 into them at an offset |
| 3557 | of shift. This leaves asize+bsize-shift allocated digit positions for t3 |
| 3558 | to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) = |
| 3559 | asize + c(bsize/2) available digit positions. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3560 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3561 | bh has c(bsize/2) digits, and bl at most f(size/2) digits. So bh+hl has |
| 3562 | at most c(bsize/2) digits + 1 bit. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3563 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3564 | If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2) |
| 3565 | digits, and al has at most f(bsize/2) digits in any case. So ah+al has at |
| 3566 | most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3567 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3568 | The product (ah+al)*(bh+bl) therefore has at most |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3569 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3570 | 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] | 3571 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3572 | and we have asize + c(bsize/2) available digit positions. We need to show |
| 3573 | this is always enough. An instance of c(bsize/2) cancels out in both, so |
| 3574 | the question reduces to whether asize digits is enough to hold |
| 3575 | (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits. If asize < bsize, |
| 3576 | then we're asking whether asize digits >= f(bsize/2) digits + 2 bits. By #4, |
| 3577 | 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] | 3578 | 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] | 3579 | 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] | 3580 | c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits |
| 3581 | is enough to hold 2 bits. This is so if bsize >= 2, which holds because |
| 3582 | bsize >= KARATSUBA_CUTOFF >= 2. |
Tim Peters | 8e966ee | 2002-08-14 16:36:23 +0000 | [diff] [blame] | 3583 | |
Tim Peters | 48d52c0 | 2002-08-14 17:07:32 +0000 | [diff] [blame] | 3584 | Note that since there's always enough room for (ah+al)*(bh+bl), and that's |
| 3585 | clearly >= each of ah*bh and al*bl, there's always enough room to subtract |
| 3586 | ah*bh and al*bl too. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3587 | */ |
| 3588 | |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3589 | /* b has at least twice the digits of a, and a is big enough that Karatsuba |
| 3590 | * would pay off *if* the inputs had balanced sizes. View b as a sequence |
| 3591 | * of slices, each with a->ob_size digits, and multiply the slices by a, |
| 3592 | * one at a time. This gives k_mul balanced inputs to work with, and is |
| 3593 | * also cache-friendly (we compute one double-width slice of the result |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 3594 | * at a time, then move on, never backtracking except for the helpful |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3595 | * single-width slice overlap between successive partial sums). |
| 3596 | */ |
| 3597 | static PyLongObject * |
| 3598 | k_lopsided_mul(PyLongObject *a, PyLongObject *b) |
| 3599 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3600 | const Py_ssize_t asize = Py_ABS(Py_SIZE(a)); |
| 3601 | Py_ssize_t bsize = Py_ABS(Py_SIZE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3602 | Py_ssize_t nbdone; /* # of b digits already multiplied */ |
| 3603 | PyLongObject *ret; |
| 3604 | PyLongObject *bslice = NULL; |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3605 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3606 | assert(asize > KARATSUBA_CUTOFF); |
| 3607 | assert(2 * asize <= bsize); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3608 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3609 | /* Allocate result space, and zero it out. */ |
| 3610 | ret = _PyLong_New(asize + bsize); |
| 3611 | if (ret == NULL) |
| 3612 | return NULL; |
| 3613 | memset(ret->ob_digit, 0, Py_SIZE(ret) * sizeof(digit)); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3614 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3615 | /* Successive slices of b are copied into bslice. */ |
| 3616 | bslice = _PyLong_New(asize); |
| 3617 | if (bslice == NULL) |
| 3618 | goto fail; |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3619 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3620 | nbdone = 0; |
| 3621 | while (bsize > 0) { |
| 3622 | PyLongObject *product; |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 3623 | const Py_ssize_t nbtouse = Py_MIN(bsize, asize); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3624 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3625 | /* Multiply the next slice of b by a. */ |
| 3626 | memcpy(bslice->ob_digit, b->ob_digit + nbdone, |
| 3627 | nbtouse * sizeof(digit)); |
| 3628 | Py_SIZE(bslice) = nbtouse; |
| 3629 | product = k_mul(a, bslice); |
| 3630 | if (product == NULL) |
| 3631 | goto fail; |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3632 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3633 | /* Add into result. */ |
| 3634 | (void)v_iadd(ret->ob_digit + nbdone, Py_SIZE(ret) - nbdone, |
| 3635 | product->ob_digit, Py_SIZE(product)); |
| 3636 | Py_DECREF(product); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3637 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3638 | bsize -= nbtouse; |
| 3639 | nbdone += nbtouse; |
| 3640 | } |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3641 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3642 | Py_DECREF(bslice); |
| 3643 | return long_normalize(ret); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3644 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3645 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3646 | Py_DECREF(ret); |
| 3647 | Py_XDECREF(bslice); |
| 3648 | return NULL; |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3649 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3650 | |
| 3651 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3652 | long_mul(PyLongObject *a, PyLongObject *b) |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3653 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3654 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3655 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3656 | CHECK_BINOP(a, b); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3657 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3658 | /* fast path for single-digit multiplication */ |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3659 | 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] | 3660 | stwodigits v = (stwodigits)(MEDIUM_VALUE(a)) * MEDIUM_VALUE(b); |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 3661 | return PyLong_FromLongLong((long long)v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3662 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3663 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3664 | z = k_mul(a, b); |
| 3665 | /* Negate if exactly one of the inputs is negative. */ |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 3666 | if (((Py_SIZE(a) ^ Py_SIZE(b)) < 0) && z) { |
| 3667 | _PyLong_Negate(&z); |
| 3668 | if (z == NULL) |
| 3669 | return NULL; |
| 3670 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3671 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3672 | } |
| 3673 | |
Yury Selivanov | e0b2309 | 2016-02-11 10:26:27 -0500 | [diff] [blame] | 3674 | /* Fast modulo division for single-digit longs. */ |
| 3675 | static PyObject * |
| 3676 | fast_mod(PyLongObject *a, PyLongObject *b) |
| 3677 | { |
| 3678 | sdigit left = a->ob_digit[0]; |
| 3679 | sdigit right = b->ob_digit[0]; |
| 3680 | sdigit mod; |
| 3681 | |
| 3682 | assert(Py_ABS(Py_SIZE(a)) == 1); |
| 3683 | assert(Py_ABS(Py_SIZE(b)) == 1); |
| 3684 | |
| 3685 | if (Py_SIZE(a) == Py_SIZE(b)) { |
| 3686 | /* 'a' and 'b' have the same sign. */ |
| 3687 | mod = left % right; |
| 3688 | } |
| 3689 | else { |
| 3690 | /* Either 'a' or 'b' is negative. */ |
| 3691 | mod = right - 1 - (left - 1) % right; |
| 3692 | } |
| 3693 | |
Victor Stinner | f963c13 | 2016-03-23 18:36:54 +0100 | [diff] [blame] | 3694 | return PyLong_FromLong(mod * (sdigit)Py_SIZE(b)); |
Yury Selivanov | e0b2309 | 2016-02-11 10:26:27 -0500 | [diff] [blame] | 3695 | } |
| 3696 | |
| 3697 | /* Fast floor division for single-digit longs. */ |
| 3698 | static PyObject * |
| 3699 | fast_floor_div(PyLongObject *a, PyLongObject *b) |
| 3700 | { |
| 3701 | sdigit left = a->ob_digit[0]; |
| 3702 | sdigit right = b->ob_digit[0]; |
| 3703 | sdigit div; |
| 3704 | |
| 3705 | assert(Py_ABS(Py_SIZE(a)) == 1); |
| 3706 | assert(Py_ABS(Py_SIZE(b)) == 1); |
| 3707 | |
| 3708 | if (Py_SIZE(a) == Py_SIZE(b)) { |
| 3709 | /* 'a' and 'b' have the same sign. */ |
| 3710 | div = left / right; |
| 3711 | } |
| 3712 | else { |
| 3713 | /* Either 'a' or 'b' is negative. */ |
| 3714 | div = -1 - (left - 1) / right; |
| 3715 | } |
| 3716 | |
| 3717 | return PyLong_FromLong(div); |
| 3718 | } |
| 3719 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3720 | /* The / and % operators are now defined in terms of divmod(). |
| 3721 | The expression a mod b has the value a - b*floor(a/b). |
| 3722 | The long_divrem function gives the remainder after division of |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3723 | |a| by |b|, with the sign of a. This is also expressed |
| 3724 | as a - b*trunc(a/b), if trunc truncates towards zero. |
| 3725 | Some examples: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3726 | a b a rem b a mod b |
| 3727 | 13 10 3 3 |
| 3728 | -13 10 -3 7 |
| 3729 | 13 -10 3 -7 |
| 3730 | -13 -10 -3 -3 |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3731 | 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] | 3732 | have different signs. We then subtract one from the 'div' |
| 3733 | part of the outcome to keep the invariant intact. */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3734 | |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3735 | /* Compute |
| 3736 | * *pdiv, *pmod = divmod(v, w) |
| 3737 | * NULL can be passed for pdiv or pmod, in which case that part of |
| 3738 | * the result is simply thrown away. The caller owns a reference to |
| 3739 | * each of these it requests (does not pass NULL for). |
| 3740 | */ |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3741 | static int |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3742 | l_divmod(PyLongObject *v, PyLongObject *w, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3743 | PyLongObject **pdiv, PyLongObject **pmod) |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3744 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3745 | PyLongObject *div, *mod; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3746 | |
Yury Selivanov | e0b2309 | 2016-02-11 10:26:27 -0500 | [diff] [blame] | 3747 | if (Py_ABS(Py_SIZE(v)) == 1 && Py_ABS(Py_SIZE(w)) == 1) { |
| 3748 | /* Fast path for single-digit longs */ |
| 3749 | div = NULL; |
| 3750 | if (pdiv != NULL) { |
| 3751 | div = (PyLongObject *)fast_floor_div(v, w); |
| 3752 | if (div == NULL) { |
| 3753 | return -1; |
| 3754 | } |
| 3755 | } |
| 3756 | if (pmod != NULL) { |
| 3757 | mod = (PyLongObject *)fast_mod(v, w); |
| 3758 | if (mod == NULL) { |
| 3759 | Py_XDECREF(div); |
| 3760 | return -1; |
| 3761 | } |
| 3762 | *pmod = mod; |
| 3763 | } |
| 3764 | if (pdiv != NULL) { |
| 3765 | /* We only want to set `*pdiv` when `*pmod` is |
| 3766 | set successfully. */ |
| 3767 | *pdiv = div; |
| 3768 | } |
| 3769 | return 0; |
| 3770 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3771 | if (long_divrem(v, w, &div, &mod) < 0) |
| 3772 | return -1; |
| 3773 | if ((Py_SIZE(mod) < 0 && Py_SIZE(w) > 0) || |
| 3774 | (Py_SIZE(mod) > 0 && Py_SIZE(w) < 0)) { |
| 3775 | PyLongObject *temp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3776 | temp = (PyLongObject *) long_add(mod, w); |
| 3777 | Py_DECREF(mod); |
| 3778 | mod = temp; |
| 3779 | if (mod == NULL) { |
| 3780 | Py_DECREF(div); |
| 3781 | return -1; |
| 3782 | } |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 3783 | temp = (PyLongObject *) long_sub(div, (PyLongObject *)_PyLong_One); |
| 3784 | if (temp == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3785 | Py_DECREF(mod); |
| 3786 | Py_DECREF(div); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3787 | return -1; |
| 3788 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3789 | Py_DECREF(div); |
| 3790 | div = temp; |
| 3791 | } |
| 3792 | if (pdiv != NULL) |
| 3793 | *pdiv = div; |
| 3794 | else |
| 3795 | Py_DECREF(div); |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3796 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3797 | if (pmod != NULL) |
| 3798 | *pmod = mod; |
| 3799 | else |
| 3800 | Py_DECREF(mod); |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3801 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3802 | return 0; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3803 | } |
| 3804 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3805 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3806 | long_div(PyObject *a, PyObject *b) |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3807 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3808 | PyLongObject *div; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3809 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3810 | CHECK_BINOP(a, b); |
Yury Selivanov | e0b2309 | 2016-02-11 10:26:27 -0500 | [diff] [blame] | 3811 | |
| 3812 | if (Py_ABS(Py_SIZE(a)) == 1 && Py_ABS(Py_SIZE(b)) == 1) { |
| 3813 | return fast_floor_div((PyLongObject*)a, (PyLongObject*)b); |
| 3814 | } |
| 3815 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3816 | if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, NULL) < 0) |
| 3817 | div = NULL; |
| 3818 | return (PyObject *)div; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3819 | } |
| 3820 | |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3821 | /* PyLong/PyLong -> float, with correctly rounded result. */ |
| 3822 | |
| 3823 | #define MANT_DIG_DIGITS (DBL_MANT_DIG / PyLong_SHIFT) |
| 3824 | #define MANT_DIG_BITS (DBL_MANT_DIG % PyLong_SHIFT) |
| 3825 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3826 | static PyObject * |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3827 | long_true_divide(PyObject *v, PyObject *w) |
Tim Peters | 20dab9f | 2001-09-04 05:31:47 +0000 | [diff] [blame] | 3828 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3829 | PyLongObject *a, *b, *x; |
| 3830 | Py_ssize_t a_size, b_size, shift, extra_bits, diff, x_size, x_bits; |
| 3831 | digit mask, low; |
| 3832 | int inexact, negate, a_is_small, b_is_small; |
| 3833 | double dx, result; |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 3834 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3835 | CHECK_BINOP(v, w); |
| 3836 | a = (PyLongObject *)v; |
| 3837 | b = (PyLongObject *)w; |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 3838 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3839 | /* |
| 3840 | Method in a nutshell: |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3841 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3842 | 0. reduce to case a, b > 0; filter out obvious underflow/overflow |
| 3843 | 1. choose a suitable integer 'shift' |
| 3844 | 2. use integer arithmetic to compute x = floor(2**-shift*a/b) |
| 3845 | 3. adjust x for correct rounding |
| 3846 | 4. convert x to a double dx with the same value |
| 3847 | 5. return ldexp(dx, shift). |
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 | In more detail: |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3850 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3851 | 0. For any a, a/0 raises ZeroDivisionError; for nonzero b, 0/b |
| 3852 | returns either 0.0 or -0.0, depending on the sign of b. For a and |
| 3853 | b both nonzero, ignore signs of a and b, and add the sign back in |
| 3854 | at the end. Now write a_bits and b_bits for the bit lengths of a |
| 3855 | and b respectively (that is, a_bits = 1 + floor(log_2(a)); likewise |
| 3856 | for b). Then |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3857 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3858 | 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] | 3859 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3860 | So if a_bits - b_bits > DBL_MAX_EXP then a/b > 2**DBL_MAX_EXP and |
| 3861 | so overflows. Similarly, if a_bits - b_bits < DBL_MIN_EXP - |
| 3862 | DBL_MANT_DIG - 1 then a/b underflows to 0. With these cases out of |
| 3863 | the way, we can assume that |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3864 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3865 | 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] | 3866 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3867 | 1. The integer 'shift' is chosen so that x has the right number of |
| 3868 | bits for a double, plus two or three extra bits that will be used |
| 3869 | in the rounding decisions. Writing a_bits and b_bits for the |
| 3870 | number of significant bits in a and b respectively, a |
| 3871 | straightforward formula for shift is: |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3872 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3873 | shift = a_bits - b_bits - DBL_MANT_DIG - 2 |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3874 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3875 | This is fine in the usual case, but if a/b is smaller than the |
| 3876 | smallest normal float then it can lead to double rounding on an |
| 3877 | IEEE 754 platform, giving incorrectly rounded results. So we |
| 3878 | adjust the formula slightly. The actual formula used is: |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3879 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3880 | 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] | 3881 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3882 | 2. The quantity x is computed by first shifting a (left -shift bits |
| 3883 | if shift <= 0, right shift bits if shift > 0) and then dividing by |
| 3884 | b. For both the shift and the division, we keep track of whether |
| 3885 | the result is inexact, in a flag 'inexact'; this information is |
| 3886 | needed at the rounding stage. |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3887 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3888 | With the choice of shift above, together with our assumption that |
| 3889 | a_bits - b_bits >= DBL_MIN_EXP - DBL_MANT_DIG - 1, it follows |
| 3890 | that x >= 1. |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3891 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3892 | 3. Now x * 2**shift <= a/b < (x+1) * 2**shift. We want to replace |
| 3893 | this with an exactly representable float of the form |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3894 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3895 | round(x/2**extra_bits) * 2**(extra_bits+shift). |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3896 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3897 | For float representability, we need x/2**extra_bits < |
| 3898 | 2**DBL_MANT_DIG and extra_bits + shift >= DBL_MIN_EXP - |
| 3899 | DBL_MANT_DIG. This translates to the condition: |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3900 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3901 | extra_bits >= MAX(x_bits, DBL_MIN_EXP - shift) - DBL_MANT_DIG |
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 | To round, we just modify the bottom digit of x in-place; this can |
| 3904 | end up giving a digit with value > PyLONG_MASK, but that's not a |
| 3905 | problem since digits can hold values up to 2*PyLONG_MASK+1. |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3906 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3907 | With the original choices for shift above, extra_bits will always |
| 3908 | be 2 or 3. Then rounding under the round-half-to-even rule, we |
| 3909 | round up iff the most significant of the extra bits is 1, and |
| 3910 | either: (a) the computation of x in step 2 had an inexact result, |
| 3911 | or (b) at least one other of the extra bits is 1, or (c) the least |
| 3912 | significant bit of x (above those to be rounded) is 1. |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3913 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3914 | 4. Conversion to a double is straightforward; all floating-point |
| 3915 | operations involved in the conversion are exact, so there's no |
| 3916 | danger of rounding errors. |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3917 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3918 | 5. Use ldexp(x, shift) to compute x*2**shift, the final result. |
| 3919 | The result will always be exactly representable as a double, except |
| 3920 | in the case that it overflows. To avoid dependence on the exact |
| 3921 | behaviour of ldexp on overflow, we check for overflow before |
| 3922 | applying ldexp. The result of ldexp is adjusted for sign before |
| 3923 | returning. |
| 3924 | */ |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3925 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3926 | /* Reduce to case where a and b are both positive. */ |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3927 | a_size = Py_ABS(Py_SIZE(a)); |
| 3928 | b_size = Py_ABS(Py_SIZE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3929 | negate = (Py_SIZE(a) < 0) ^ (Py_SIZE(b) < 0); |
| 3930 | if (b_size == 0) { |
| 3931 | PyErr_SetString(PyExc_ZeroDivisionError, |
| 3932 | "division by zero"); |
| 3933 | goto error; |
| 3934 | } |
| 3935 | if (a_size == 0) |
| 3936 | goto underflow_or_zero; |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3937 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3938 | /* Fast path for a and b small (exactly representable in a double). |
| 3939 | Relies on floating-point division being correctly rounded; results |
| 3940 | may be subject to double rounding on x86 machines that operate with |
| 3941 | the x87 FPU set to 64-bit precision. */ |
| 3942 | a_is_small = a_size <= MANT_DIG_DIGITS || |
| 3943 | (a_size == MANT_DIG_DIGITS+1 && |
| 3944 | a->ob_digit[MANT_DIG_DIGITS] >> MANT_DIG_BITS == 0); |
| 3945 | b_is_small = b_size <= MANT_DIG_DIGITS || |
| 3946 | (b_size == MANT_DIG_DIGITS+1 && |
| 3947 | b->ob_digit[MANT_DIG_DIGITS] >> MANT_DIG_BITS == 0); |
| 3948 | if (a_is_small && b_is_small) { |
| 3949 | double da, db; |
| 3950 | da = a->ob_digit[--a_size]; |
| 3951 | while (a_size > 0) |
| 3952 | da = da * PyLong_BASE + a->ob_digit[--a_size]; |
| 3953 | db = b->ob_digit[--b_size]; |
| 3954 | while (b_size > 0) |
| 3955 | db = db * PyLong_BASE + b->ob_digit[--b_size]; |
| 3956 | result = da / db; |
| 3957 | goto success; |
| 3958 | } |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 3959 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3960 | /* Catch obvious cases of underflow and overflow */ |
| 3961 | diff = a_size - b_size; |
| 3962 | if (diff > PY_SSIZE_T_MAX/PyLong_SHIFT - 1) |
| 3963 | /* Extreme overflow */ |
| 3964 | goto overflow; |
| 3965 | else if (diff < 1 - PY_SSIZE_T_MAX/PyLong_SHIFT) |
| 3966 | /* Extreme underflow */ |
| 3967 | goto underflow_or_zero; |
| 3968 | /* Next line is now safe from overflowing a Py_ssize_t */ |
Niklas Fiekas | c5b7900 | 2020-01-16 15:09:19 +0100 | [diff] [blame^] | 3969 | diff = diff * PyLong_SHIFT + _Py_bit_length(a->ob_digit[a_size - 1]) - |
| 3970 | _Py_bit_length(b->ob_digit[b_size - 1]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3971 | /* Now diff = a_bits - b_bits. */ |
| 3972 | if (diff > DBL_MAX_EXP) |
| 3973 | goto overflow; |
| 3974 | else if (diff < DBL_MIN_EXP - DBL_MANT_DIG - 1) |
| 3975 | goto underflow_or_zero; |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 3976 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3977 | /* Choose value for shift; see comments for step 1 above. */ |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 3978 | shift = Py_MAX(diff, DBL_MIN_EXP) - DBL_MANT_DIG - 2; |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3979 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3980 | inexact = 0; |
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 | /* x = abs(a * 2**-shift) */ |
| 3983 | if (shift <= 0) { |
| 3984 | Py_ssize_t i, shift_digits = -shift / PyLong_SHIFT; |
| 3985 | digit rem; |
| 3986 | /* x = a << -shift */ |
| 3987 | if (a_size >= PY_SSIZE_T_MAX - 1 - shift_digits) { |
| 3988 | /* In practice, it's probably impossible to end up |
| 3989 | here. Both a and b would have to be enormous, |
| 3990 | using close to SIZE_T_MAX bytes of memory each. */ |
| 3991 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3992 | "intermediate overflow during division"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3993 | goto error; |
| 3994 | } |
| 3995 | x = _PyLong_New(a_size + shift_digits + 1); |
| 3996 | if (x == NULL) |
| 3997 | goto error; |
| 3998 | for (i = 0; i < shift_digits; i++) |
| 3999 | x->ob_digit[i] = 0; |
| 4000 | rem = v_lshift(x->ob_digit + shift_digits, a->ob_digit, |
| 4001 | a_size, -shift % PyLong_SHIFT); |
| 4002 | x->ob_digit[a_size + shift_digits] = rem; |
| 4003 | } |
| 4004 | else { |
| 4005 | Py_ssize_t shift_digits = shift / PyLong_SHIFT; |
| 4006 | digit rem; |
| 4007 | /* x = a >> shift */ |
| 4008 | assert(a_size >= shift_digits); |
| 4009 | x = _PyLong_New(a_size - shift_digits); |
| 4010 | if (x == NULL) |
| 4011 | goto error; |
| 4012 | rem = v_rshift(x->ob_digit, a->ob_digit + shift_digits, |
| 4013 | a_size - shift_digits, shift % PyLong_SHIFT); |
| 4014 | /* set inexact if any of the bits shifted out is nonzero */ |
| 4015 | if (rem) |
| 4016 | inexact = 1; |
| 4017 | while (!inexact && shift_digits > 0) |
| 4018 | if (a->ob_digit[--shift_digits]) |
| 4019 | inexact = 1; |
| 4020 | } |
| 4021 | long_normalize(x); |
| 4022 | x_size = Py_SIZE(x); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 4023 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4024 | /* x //= b. If the remainder is nonzero, set inexact. We own the only |
| 4025 | reference to x, so it's safe to modify it in-place. */ |
| 4026 | if (b_size == 1) { |
| 4027 | digit rem = inplace_divrem1(x->ob_digit, x->ob_digit, x_size, |
| 4028 | b->ob_digit[0]); |
| 4029 | long_normalize(x); |
| 4030 | if (rem) |
| 4031 | inexact = 1; |
| 4032 | } |
| 4033 | else { |
| 4034 | PyLongObject *div, *rem; |
| 4035 | div = x_divrem(x, b, &rem); |
| 4036 | Py_DECREF(x); |
| 4037 | x = div; |
| 4038 | if (x == NULL) |
| 4039 | goto error; |
| 4040 | if (Py_SIZE(rem)) |
| 4041 | inexact = 1; |
| 4042 | Py_DECREF(rem); |
| 4043 | } |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 4044 | x_size = Py_ABS(Py_SIZE(x)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4045 | assert(x_size > 0); /* result of division is never zero */ |
Niklas Fiekas | c5b7900 | 2020-01-16 15:09:19 +0100 | [diff] [blame^] | 4046 | x_bits = (x_size-1)*PyLong_SHIFT+_Py_bit_length(x->ob_digit[x_size-1]); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 4047 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4048 | /* The number of extra bits that have to be rounded away. */ |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 4049 | 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] | 4050 | assert(extra_bits == 2 || extra_bits == 3); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 4051 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4052 | /* Round by directly modifying the low digit of x. */ |
| 4053 | mask = (digit)1 << (extra_bits - 1); |
| 4054 | low = x->ob_digit[0] | inexact; |
Mark Dickinson | dc590a4 | 2016-08-21 10:23:23 +0100 | [diff] [blame] | 4055 | if ((low & mask) && (low & (3U*mask-1U))) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4056 | low += mask; |
Mark Dickinson | dc590a4 | 2016-08-21 10:23:23 +0100 | [diff] [blame] | 4057 | x->ob_digit[0] = low & ~(2U*mask-1U); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 4058 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4059 | /* Convert x to a double dx; the conversion is exact. */ |
| 4060 | dx = x->ob_digit[--x_size]; |
| 4061 | while (x_size > 0) |
| 4062 | dx = dx * PyLong_BASE + x->ob_digit[--x_size]; |
| 4063 | Py_DECREF(x); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 4064 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4065 | /* Check whether ldexp result will overflow a double. */ |
| 4066 | if (shift + x_bits >= DBL_MAX_EXP && |
| 4067 | (shift + x_bits > DBL_MAX_EXP || dx == ldexp(1.0, (int)x_bits))) |
| 4068 | goto overflow; |
| 4069 | result = ldexp(dx, (int)shift); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 4070 | |
| 4071 | success: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4072 | return PyFloat_FromDouble(negate ? -result : result); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 4073 | |
| 4074 | underflow_or_zero: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4075 | return PyFloat_FromDouble(negate ? -0.0 : 0.0); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 4076 | |
| 4077 | overflow: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4078 | PyErr_SetString(PyExc_OverflowError, |
| 4079 | "integer division result too large for a float"); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 4080 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4081 | return NULL; |
Tim Peters | 20dab9f | 2001-09-04 05:31:47 +0000 | [diff] [blame] | 4082 | } |
| 4083 | |
| 4084 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 4085 | long_mod(PyObject *a, PyObject *b) |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 4086 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4087 | PyLongObject *mod; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 4088 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4089 | CHECK_BINOP(a, b); |
| 4090 | |
Yury Selivanov | e0b2309 | 2016-02-11 10:26:27 -0500 | [diff] [blame] | 4091 | if (Py_ABS(Py_SIZE(a)) == 1 && Py_ABS(Py_SIZE(b)) == 1) { |
| 4092 | return fast_mod((PyLongObject*)a, (PyLongObject*)b); |
| 4093 | } |
| 4094 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4095 | if (l_divmod((PyLongObject*)a, (PyLongObject*)b, NULL, &mod) < 0) |
| 4096 | mod = NULL; |
| 4097 | return (PyObject *)mod; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 4098 | } |
| 4099 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4100 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 4101 | long_divmod(PyObject *a, PyObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4102 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4103 | PyLongObject *div, *mod; |
| 4104 | PyObject *z; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 4105 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4106 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 4107 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4108 | if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, &mod) < 0) { |
| 4109 | return NULL; |
| 4110 | } |
| 4111 | z = PyTuple_New(2); |
| 4112 | if (z != NULL) { |
Sergey Fedoseev | ea6207d | 2019-02-21 15:01:11 +0500 | [diff] [blame] | 4113 | PyTuple_SET_ITEM(z, 0, (PyObject *) div); |
| 4114 | PyTuple_SET_ITEM(z, 1, (PyObject *) mod); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4115 | } |
| 4116 | else { |
| 4117 | Py_DECREF(div); |
| 4118 | Py_DECREF(mod); |
| 4119 | } |
| 4120 | return z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4121 | } |
| 4122 | |
Mark Dickinson | c529967 | 2019-06-02 10:24:06 +0100 | [diff] [blame] | 4123 | |
| 4124 | /* Compute an inverse to a modulo n, or raise ValueError if a is not |
| 4125 | invertible modulo n. Assumes n is positive. The inverse returned |
| 4126 | is whatever falls out of the extended Euclidean algorithm: it may |
| 4127 | be either positive or negative, but will be smaller than n in |
| 4128 | absolute value. |
| 4129 | |
| 4130 | Pure Python equivalent for long_invmod: |
| 4131 | |
| 4132 | def invmod(a, n): |
| 4133 | b, c = 1, 0 |
| 4134 | while n: |
| 4135 | q, r = divmod(a, n) |
| 4136 | a, b, c, n = n, c, b - q*c, r |
| 4137 | |
| 4138 | # at this point a is the gcd of the original inputs |
| 4139 | if a == 1: |
| 4140 | return b |
| 4141 | raise ValueError("Not invertible") |
| 4142 | */ |
| 4143 | |
| 4144 | static PyLongObject * |
| 4145 | long_invmod(PyLongObject *a, PyLongObject *n) |
| 4146 | { |
| 4147 | PyLongObject *b, *c; |
| 4148 | |
| 4149 | /* Should only ever be called for positive n */ |
| 4150 | assert(Py_SIZE(n) > 0); |
| 4151 | |
| 4152 | b = (PyLongObject *)PyLong_FromLong(1L); |
| 4153 | if (b == NULL) { |
| 4154 | return NULL; |
| 4155 | } |
| 4156 | c = (PyLongObject *)PyLong_FromLong(0L); |
| 4157 | if (c == NULL) { |
| 4158 | Py_DECREF(b); |
| 4159 | return NULL; |
| 4160 | } |
| 4161 | Py_INCREF(a); |
| 4162 | Py_INCREF(n); |
| 4163 | |
| 4164 | /* references now owned: a, b, c, n */ |
| 4165 | while (Py_SIZE(n) != 0) { |
| 4166 | PyLongObject *q, *r, *s, *t; |
| 4167 | |
| 4168 | if (l_divmod(a, n, &q, &r) == -1) { |
| 4169 | goto Error; |
| 4170 | } |
| 4171 | Py_DECREF(a); |
| 4172 | a = n; |
| 4173 | n = r; |
| 4174 | t = (PyLongObject *)long_mul(q, c); |
| 4175 | Py_DECREF(q); |
| 4176 | if (t == NULL) { |
| 4177 | goto Error; |
| 4178 | } |
| 4179 | s = (PyLongObject *)long_sub(b, t); |
| 4180 | Py_DECREF(t); |
| 4181 | if (s == NULL) { |
| 4182 | goto Error; |
| 4183 | } |
| 4184 | Py_DECREF(b); |
| 4185 | b = c; |
| 4186 | c = s; |
| 4187 | } |
| 4188 | /* references now owned: a, b, c, n */ |
| 4189 | |
| 4190 | Py_DECREF(c); |
| 4191 | Py_DECREF(n); |
Petr Viktorin | 1e375c6 | 2019-06-03 02:28:29 +0200 | [diff] [blame] | 4192 | if (long_compare(a, (PyLongObject *)_PyLong_One)) { |
Mark Dickinson | c529967 | 2019-06-02 10:24:06 +0100 | [diff] [blame] | 4193 | /* a != 1; we don't have an inverse. */ |
| 4194 | Py_DECREF(a); |
| 4195 | Py_DECREF(b); |
| 4196 | PyErr_SetString(PyExc_ValueError, |
| 4197 | "base is not invertible for the given modulus"); |
| 4198 | return NULL; |
| 4199 | } |
| 4200 | else { |
| 4201 | /* a == 1; b gives an inverse modulo n */ |
| 4202 | Py_DECREF(a); |
| 4203 | return b; |
| 4204 | } |
| 4205 | |
| 4206 | Error: |
| 4207 | Py_DECREF(a); |
| 4208 | Py_DECREF(b); |
| 4209 | Py_DECREF(c); |
| 4210 | Py_DECREF(n); |
| 4211 | return NULL; |
| 4212 | } |
| 4213 | |
| 4214 | |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4215 | /* pow(v, w, x) */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4216 | static PyObject * |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 4217 | long_pow(PyObject *v, PyObject *w, PyObject *x) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4218 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4219 | PyLongObject *a, *b, *c; /* a,b,c = v,w,x */ |
| 4220 | int negativeOutput = 0; /* if x<0 return negative output */ |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 4221 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4222 | PyLongObject *z = NULL; /* accumulated result */ |
| 4223 | Py_ssize_t i, j, k; /* counters */ |
| 4224 | PyLongObject *temp = NULL; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4225 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4226 | /* 5-ary values. If the exponent is large enough, table is |
| 4227 | * precomputed so that table[i] == a**i % c for i in range(32). |
| 4228 | */ |
| 4229 | PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 4230 | 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] | 4231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4232 | /* a, b, c = v, w, x */ |
| 4233 | CHECK_BINOP(v, w); |
| 4234 | a = (PyLongObject*)v; Py_INCREF(a); |
| 4235 | b = (PyLongObject*)w; Py_INCREF(b); |
| 4236 | if (PyLong_Check(x)) { |
| 4237 | c = (PyLongObject *)x; |
| 4238 | Py_INCREF(x); |
| 4239 | } |
| 4240 | else if (x == Py_None) |
| 4241 | c = NULL; |
| 4242 | else { |
| 4243 | Py_DECREF(a); |
| 4244 | Py_DECREF(b); |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 4245 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4246 | } |
Tim Peters | 4c483c4 | 2001-09-05 06:24:58 +0000 | [diff] [blame] | 4247 | |
Mark Dickinson | c529967 | 2019-06-02 10:24:06 +0100 | [diff] [blame] | 4248 | if (Py_SIZE(b) < 0 && c == NULL) { |
| 4249 | /* if exponent is negative and there's no modulus: |
| 4250 | return a float. This works because we know |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4251 | that this calls float_pow() which converts its |
| 4252 | arguments to double. */ |
Mark Dickinson | c529967 | 2019-06-02 10:24:06 +0100 | [diff] [blame] | 4253 | Py_DECREF(a); |
| 4254 | Py_DECREF(b); |
| 4255 | return PyFloat_Type.tp_as_number->nb_power(v, w, x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4256 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4257 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4258 | if (c) { |
| 4259 | /* if modulus == 0: |
| 4260 | raise ValueError() */ |
| 4261 | if (Py_SIZE(c) == 0) { |
| 4262 | PyErr_SetString(PyExc_ValueError, |
| 4263 | "pow() 3rd argument cannot be 0"); |
| 4264 | goto Error; |
| 4265 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4266 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4267 | /* if modulus < 0: |
| 4268 | negativeOutput = True |
| 4269 | modulus = -modulus */ |
| 4270 | if (Py_SIZE(c) < 0) { |
| 4271 | negativeOutput = 1; |
| 4272 | temp = (PyLongObject *)_PyLong_Copy(c); |
| 4273 | if (temp == NULL) |
| 4274 | goto Error; |
| 4275 | Py_DECREF(c); |
| 4276 | c = temp; |
| 4277 | temp = NULL; |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 4278 | _PyLong_Negate(&c); |
| 4279 | if (c == NULL) |
| 4280 | goto Error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4281 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4282 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4283 | /* if modulus == 1: |
| 4284 | return 0 */ |
| 4285 | if ((Py_SIZE(c) == 1) && (c->ob_digit[0] == 1)) { |
| 4286 | z = (PyLongObject *)PyLong_FromLong(0L); |
| 4287 | goto Done; |
| 4288 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4289 | |
Mark Dickinson | c529967 | 2019-06-02 10:24:06 +0100 | [diff] [blame] | 4290 | /* if exponent is negative, negate the exponent and |
| 4291 | replace the base with a modular inverse */ |
| 4292 | if (Py_SIZE(b) < 0) { |
| 4293 | temp = (PyLongObject *)_PyLong_Copy(b); |
| 4294 | if (temp == NULL) |
| 4295 | goto Error; |
| 4296 | Py_DECREF(b); |
| 4297 | b = temp; |
| 4298 | temp = NULL; |
| 4299 | _PyLong_Negate(&b); |
| 4300 | if (b == NULL) |
| 4301 | goto Error; |
| 4302 | |
| 4303 | temp = long_invmod(a, c); |
| 4304 | if (temp == NULL) |
| 4305 | goto Error; |
| 4306 | Py_DECREF(a); |
| 4307 | a = temp; |
| 4308 | } |
| 4309 | |
Tim Peters | 81a9315 | 2013-10-05 16:53:52 -0500 | [diff] [blame] | 4310 | /* Reduce base by modulus in some cases: |
| 4311 | 1. If base < 0. Forcing the base non-negative makes things easier. |
| 4312 | 2. If base is obviously larger than the modulus. The "small |
| 4313 | exponent" case later can multiply directly by base repeatedly, |
| 4314 | while the "large exponent" case multiplies directly by base 31 |
| 4315 | times. It can be unboundedly faster to multiply by |
| 4316 | base % modulus instead. |
| 4317 | We could _always_ do this reduction, but l_divmod() isn't cheap, |
| 4318 | so we only do it when it buys something. */ |
| 4319 | if (Py_SIZE(a) < 0 || Py_SIZE(a) > Py_SIZE(c)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4320 | if (l_divmod(a, c, NULL, &temp) < 0) |
| 4321 | goto Error; |
| 4322 | Py_DECREF(a); |
| 4323 | a = temp; |
| 4324 | temp = NULL; |
| 4325 | } |
| 4326 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4327 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4328 | /* At this point a, b, and c are guaranteed non-negative UNLESS |
| 4329 | c is NULL, in which case a may be negative. */ |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4330 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4331 | z = (PyLongObject *)PyLong_FromLong(1L); |
| 4332 | if (z == NULL) |
| 4333 | goto Error; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4334 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4335 | /* Perform a modular reduction, X = X % c, but leave X alone if c |
| 4336 | * is NULL. |
| 4337 | */ |
| 4338 | #define REDUCE(X) \ |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4339 | do { \ |
| 4340 | if (c != NULL) { \ |
| 4341 | if (l_divmod(X, c, NULL, &temp) < 0) \ |
| 4342 | goto Error; \ |
| 4343 | Py_XDECREF(X); \ |
| 4344 | X = temp; \ |
| 4345 | temp = NULL; \ |
| 4346 | } \ |
| 4347 | } while(0) |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4348 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4349 | /* Multiply two values, then reduce the result: |
| 4350 | result = X*Y % c. If c is NULL, skip the mod. */ |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4351 | #define MULT(X, Y, result) \ |
| 4352 | do { \ |
| 4353 | temp = (PyLongObject *)long_mul(X, Y); \ |
| 4354 | if (temp == NULL) \ |
| 4355 | goto Error; \ |
| 4356 | Py_XDECREF(result); \ |
| 4357 | result = temp; \ |
| 4358 | temp = NULL; \ |
| 4359 | REDUCE(result); \ |
| 4360 | } while(0) |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4361 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4362 | if (Py_SIZE(b) <= FIVEARY_CUTOFF) { |
| 4363 | /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */ |
| 4364 | /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */ |
| 4365 | for (i = Py_SIZE(b) - 1; i >= 0; --i) { |
| 4366 | digit bi = b->ob_digit[i]; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4367 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4368 | for (j = (digit)1 << (PyLong_SHIFT-1); j != 0; j >>= 1) { |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4369 | MULT(z, z, z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4370 | if (bi & j) |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4371 | MULT(z, a, z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4372 | } |
| 4373 | } |
| 4374 | } |
| 4375 | else { |
| 4376 | /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */ |
| 4377 | Py_INCREF(z); /* still holds 1L */ |
| 4378 | table[0] = z; |
| 4379 | for (i = 1; i < 32; ++i) |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4380 | MULT(table[i-1], a, table[i]); |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4381 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4382 | for (i = Py_SIZE(b) - 1; i >= 0; --i) { |
| 4383 | const digit bi = b->ob_digit[i]; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4384 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4385 | for (j = PyLong_SHIFT - 5; j >= 0; j -= 5) { |
| 4386 | const int index = (bi >> j) & 0x1f; |
| 4387 | for (k = 0; k < 5; ++k) |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4388 | MULT(z, z, z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4389 | if (index) |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4390 | MULT(z, table[index], z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4391 | } |
| 4392 | } |
| 4393 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4394 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4395 | if (negativeOutput && (Py_SIZE(z) != 0)) { |
| 4396 | temp = (PyLongObject *)long_sub(z, c); |
| 4397 | if (temp == NULL) |
| 4398 | goto Error; |
| 4399 | Py_DECREF(z); |
| 4400 | z = temp; |
| 4401 | temp = NULL; |
| 4402 | } |
| 4403 | goto Done; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4404 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4405 | Error: |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 4406 | Py_CLEAR(z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4407 | /* fall through */ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4408 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4409 | if (Py_SIZE(b) > FIVEARY_CUTOFF) { |
| 4410 | for (i = 0; i < 32; ++i) |
| 4411 | Py_XDECREF(table[i]); |
| 4412 | } |
| 4413 | Py_DECREF(a); |
| 4414 | Py_DECREF(b); |
| 4415 | Py_XDECREF(c); |
| 4416 | Py_XDECREF(temp); |
| 4417 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4418 | } |
| 4419 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4420 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 4421 | long_invert(PyLongObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4422 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4423 | /* Implement ~x as -(x+1) */ |
| 4424 | PyLongObject *x; |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 4425 | if (Py_ABS(Py_SIZE(v)) <=1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4426 | return PyLong_FromLong(-(MEDIUM_VALUE(v)+1)); |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 4427 | x = (PyLongObject *) long_add(v, (PyLongObject *)_PyLong_One); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4428 | if (x == NULL) |
| 4429 | return NULL; |
Mark Dickinson | 583c6e8 | 2016-08-29 16:40:29 +0100 | [diff] [blame] | 4430 | _PyLong_Negate(&x); |
| 4431 | /* No need for maybe_small_long here, since any small |
| 4432 | longs will have been caught in the Py_SIZE <= 1 fast path. */ |
| 4433 | return (PyObject *)x; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4434 | } |
| 4435 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4436 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 4437 | long_neg(PyLongObject *v) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4438 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4439 | PyLongObject *z; |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 4440 | if (Py_ABS(Py_SIZE(v)) <= 1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4441 | return PyLong_FromLong(-MEDIUM_VALUE(v)); |
| 4442 | z = (PyLongObject *)_PyLong_Copy(v); |
| 4443 | if (z != NULL) |
| 4444 | Py_SIZE(z) = -(Py_SIZE(v)); |
| 4445 | return (PyObject *)z; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4446 | } |
| 4447 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4448 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 4449 | long_abs(PyLongObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4450 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4451 | if (Py_SIZE(v) < 0) |
| 4452 | return long_neg(v); |
| 4453 | else |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 4454 | return long_long((PyObject *)v); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4455 | } |
| 4456 | |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 4457 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 4458 | long_bool(PyLongObject *v) |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 4459 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4460 | return Py_SIZE(v) != 0; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4461 | } |
| 4462 | |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4463 | /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */ |
| 4464 | static int |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4465 | divmod_shift(PyObject *shiftby, Py_ssize_t *wordshift, digit *remshift) |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4466 | { |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4467 | assert(PyLong_Check(shiftby)); |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4468 | assert(Py_SIZE(shiftby) >= 0); |
| 4469 | Py_ssize_t lshiftby = PyLong_AsSsize_t((PyObject *)shiftby); |
| 4470 | if (lshiftby >= 0) { |
| 4471 | *wordshift = lshiftby / PyLong_SHIFT; |
| 4472 | *remshift = lshiftby % PyLong_SHIFT; |
| 4473 | return 0; |
| 4474 | } |
| 4475 | /* PyLong_Check(shiftby) is true and Py_SIZE(shiftby) >= 0, so it must |
| 4476 | be that PyLong_AsSsize_t raised an OverflowError. */ |
| 4477 | assert(PyErr_ExceptionMatches(PyExc_OverflowError)); |
| 4478 | PyErr_Clear(); |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4479 | PyLongObject *wordshift_obj = divrem1((PyLongObject *)shiftby, PyLong_SHIFT, remshift); |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4480 | if (wordshift_obj == NULL) { |
| 4481 | return -1; |
| 4482 | } |
| 4483 | *wordshift = PyLong_AsSsize_t((PyObject *)wordshift_obj); |
| 4484 | Py_DECREF(wordshift_obj); |
| 4485 | if (*wordshift >= 0 && *wordshift < PY_SSIZE_T_MAX / (Py_ssize_t)sizeof(digit)) { |
| 4486 | return 0; |
| 4487 | } |
| 4488 | PyErr_Clear(); |
| 4489 | /* Clip the value. With such large wordshift the right shift |
| 4490 | returns 0 and the left shift raises an error in _PyLong_New(). */ |
| 4491 | *wordshift = PY_SSIZE_T_MAX / sizeof(digit); |
| 4492 | *remshift = 0; |
| 4493 | return 0; |
| 4494 | } |
| 4495 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4496 | static PyObject * |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4497 | long_rshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4498 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4499 | PyLongObject *z = NULL; |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4500 | Py_ssize_t newsize, hishift, i, j; |
| 4501 | digit lomask, himask; |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4502 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4503 | if (Py_SIZE(a) < 0) { |
| 4504 | /* Right shifting negative numbers is harder */ |
| 4505 | PyLongObject *a1, *a2; |
| 4506 | a1 = (PyLongObject *) long_invert(a); |
| 4507 | if (a1 == NULL) |
Mark Dickinson | 92ca535 | 2016-09-17 17:50:50 +0100 | [diff] [blame] | 4508 | return NULL; |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4509 | a2 = (PyLongObject *) long_rshift1(a1, wordshift, remshift); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4510 | Py_DECREF(a1); |
| 4511 | if (a2 == NULL) |
Mark Dickinson | 92ca535 | 2016-09-17 17:50:50 +0100 | [diff] [blame] | 4512 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4513 | z = (PyLongObject *) long_invert(a2); |
| 4514 | Py_DECREF(a2); |
| 4515 | } |
| 4516 | else { |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4517 | newsize = Py_SIZE(a) - wordshift; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4518 | if (newsize <= 0) |
| 4519 | return PyLong_FromLong(0); |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4520 | hishift = PyLong_SHIFT - remshift; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4521 | lomask = ((digit)1 << hishift) - 1; |
| 4522 | himask = PyLong_MASK ^ lomask; |
| 4523 | z = _PyLong_New(newsize); |
| 4524 | if (z == NULL) |
Mark Dickinson | 92ca535 | 2016-09-17 17:50:50 +0100 | [diff] [blame] | 4525 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4526 | for (i = 0, j = wordshift; i < newsize; i++, j++) { |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4527 | z->ob_digit[i] = (a->ob_digit[j] >> remshift) & lomask; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4528 | if (i+1 < newsize) |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4529 | z->ob_digit[i] |= (a->ob_digit[j+1] << hishift) & himask; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4530 | } |
Mark Dickinson | 92ca535 | 2016-09-17 17:50:50 +0100 | [diff] [blame] | 4531 | z = maybe_small_long(long_normalize(z)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4532 | } |
Mark Dickinson | 92ca535 | 2016-09-17 17:50:50 +0100 | [diff] [blame] | 4533 | return (PyObject *)z; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4534 | } |
| 4535 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4536 | static PyObject * |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4537 | long_rshift(PyObject *a, PyObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4538 | { |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4539 | Py_ssize_t wordshift; |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4540 | digit remshift; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4541 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4542 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 4543 | |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4544 | if (Py_SIZE(b) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4545 | PyErr_SetString(PyExc_ValueError, "negative shift count"); |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 4546 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4547 | } |
Mark Dickinson | 82a9527 | 2016-08-29 19:27:06 +0100 | [diff] [blame] | 4548 | if (Py_SIZE(a) == 0) { |
| 4549 | return PyLong_FromLong(0); |
| 4550 | } |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4551 | if (divmod_shift(b, &wordshift, &remshift) < 0) |
| 4552 | return NULL; |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4553 | return long_rshift1((PyLongObject *)a, wordshift, remshift); |
| 4554 | } |
| 4555 | |
| 4556 | /* Return a >> shiftby. */ |
| 4557 | PyObject * |
| 4558 | _PyLong_Rshift(PyObject *a, size_t shiftby) |
| 4559 | { |
| 4560 | Py_ssize_t wordshift; |
| 4561 | digit remshift; |
| 4562 | |
| 4563 | assert(PyLong_Check(a)); |
| 4564 | if (Py_SIZE(a) == 0) { |
| 4565 | return PyLong_FromLong(0); |
| 4566 | } |
| 4567 | wordshift = shiftby / PyLong_SHIFT; |
| 4568 | remshift = shiftby % PyLong_SHIFT; |
| 4569 | return long_rshift1((PyLongObject *)a, wordshift, remshift); |
| 4570 | } |
| 4571 | |
| 4572 | static PyObject * |
| 4573 | long_lshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift) |
| 4574 | { |
| 4575 | /* This version due to Tim Peters */ |
| 4576 | PyLongObject *z = NULL; |
| 4577 | Py_ssize_t oldsize, newsize, i, j; |
| 4578 | twodigits accum; |
| 4579 | |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 4580 | oldsize = Py_ABS(Py_SIZE(a)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4581 | newsize = oldsize + wordshift; |
| 4582 | if (remshift) |
| 4583 | ++newsize; |
| 4584 | z = _PyLong_New(newsize); |
| 4585 | if (z == NULL) |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 4586 | return NULL; |
| 4587 | if (Py_SIZE(a) < 0) { |
| 4588 | assert(Py_REFCNT(z) == 1); |
| 4589 | Py_SIZE(z) = -Py_SIZE(z); |
| 4590 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4591 | for (i = 0; i < wordshift; i++) |
| 4592 | z->ob_digit[i] = 0; |
| 4593 | accum = 0; |
| 4594 | for (i = wordshift, j = 0; j < oldsize; i++, j++) { |
| 4595 | accum |= (twodigits)a->ob_digit[j] << remshift; |
| 4596 | z->ob_digit[i] = (digit)(accum & PyLong_MASK); |
| 4597 | accum >>= PyLong_SHIFT; |
| 4598 | } |
| 4599 | if (remshift) |
| 4600 | z->ob_digit[newsize-1] = (digit)accum; |
| 4601 | else |
| 4602 | assert(!accum); |
| 4603 | z = long_normalize(z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4604 | return (PyObject *) maybe_small_long(z); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4605 | } |
| 4606 | |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4607 | static PyObject * |
| 4608 | long_lshift(PyObject *a, PyObject *b) |
| 4609 | { |
| 4610 | Py_ssize_t wordshift; |
| 4611 | digit remshift; |
| 4612 | |
| 4613 | CHECK_BINOP(a, b); |
| 4614 | |
| 4615 | if (Py_SIZE(b) < 0) { |
| 4616 | PyErr_SetString(PyExc_ValueError, "negative shift count"); |
| 4617 | return NULL; |
| 4618 | } |
| 4619 | if (Py_SIZE(a) == 0) { |
| 4620 | return PyLong_FromLong(0); |
| 4621 | } |
| 4622 | if (divmod_shift(b, &wordshift, &remshift) < 0) |
| 4623 | return NULL; |
| 4624 | return long_lshift1((PyLongObject *)a, wordshift, remshift); |
| 4625 | } |
| 4626 | |
| 4627 | /* Return a << shiftby. */ |
| 4628 | PyObject * |
| 4629 | _PyLong_Lshift(PyObject *a, size_t shiftby) |
| 4630 | { |
| 4631 | Py_ssize_t wordshift; |
| 4632 | digit remshift; |
| 4633 | |
| 4634 | assert(PyLong_Check(a)); |
| 4635 | if (Py_SIZE(a) == 0) { |
| 4636 | return PyLong_FromLong(0); |
| 4637 | } |
| 4638 | wordshift = shiftby / PyLong_SHIFT; |
| 4639 | remshift = shiftby % PyLong_SHIFT; |
| 4640 | return long_lshift1((PyLongObject *)a, wordshift, remshift); |
| 4641 | } |
| 4642 | |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4643 | /* Compute two's complement of digit vector a[0:m], writing result to |
| 4644 | z[0:m]. The digit vector a need not be normalized, but should not |
| 4645 | be entirely zero. a and z may point to the same digit vector. */ |
| 4646 | |
| 4647 | static void |
| 4648 | v_complement(digit *z, digit *a, Py_ssize_t m) |
| 4649 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4650 | Py_ssize_t i; |
| 4651 | digit carry = 1; |
| 4652 | for (i = 0; i < m; ++i) { |
| 4653 | carry += a[i] ^ PyLong_MASK; |
| 4654 | z[i] = carry & PyLong_MASK; |
| 4655 | carry >>= PyLong_SHIFT; |
| 4656 | } |
| 4657 | assert(carry == 0); |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4658 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 4659 | |
| 4660 | /* Bitwise and/xor/or operations */ |
| 4661 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4662 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 4663 | long_bitwise(PyLongObject *a, |
Benjamin Peterson | 513762f | 2012-12-26 16:43:33 -0600 | [diff] [blame] | 4664 | char op, /* '&', '|', '^' */ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4665 | PyLongObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4666 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4667 | int nega, negb, negz; |
| 4668 | Py_ssize_t size_a, size_b, size_z, i; |
| 4669 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4670 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4671 | /* Bitwise operations for negative numbers operate as though |
| 4672 | on a two's complement representation. So convert arguments |
| 4673 | from sign-magnitude to two's complement, and convert the |
| 4674 | result back to sign-magnitude at the end. */ |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4675 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4676 | /* If a is negative, replace it by its two's complement. */ |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 4677 | size_a = Py_ABS(Py_SIZE(a)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4678 | nega = Py_SIZE(a) < 0; |
| 4679 | if (nega) { |
| 4680 | z = _PyLong_New(size_a); |
| 4681 | if (z == NULL) |
| 4682 | return NULL; |
| 4683 | v_complement(z->ob_digit, a->ob_digit, size_a); |
| 4684 | a = z; |
| 4685 | } |
| 4686 | else |
| 4687 | /* Keep reference count consistent. */ |
| 4688 | Py_INCREF(a); |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4689 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4690 | /* Same for b. */ |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 4691 | size_b = Py_ABS(Py_SIZE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4692 | negb = Py_SIZE(b) < 0; |
| 4693 | if (negb) { |
| 4694 | z = _PyLong_New(size_b); |
| 4695 | if (z == NULL) { |
| 4696 | Py_DECREF(a); |
| 4697 | return NULL; |
| 4698 | } |
| 4699 | v_complement(z->ob_digit, b->ob_digit, size_b); |
| 4700 | b = z; |
| 4701 | } |
| 4702 | else |
| 4703 | Py_INCREF(b); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4704 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4705 | /* Swap a and b if necessary to ensure size_a >= size_b. */ |
| 4706 | if (size_a < size_b) { |
| 4707 | z = a; a = b; b = z; |
| 4708 | size_z = size_a; size_a = size_b; size_b = size_z; |
| 4709 | negz = nega; nega = negb; negb = negz; |
| 4710 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4711 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4712 | /* JRH: The original logic here was to allocate the result value (z) |
| 4713 | as the longer of the two operands. However, there are some cases |
| 4714 | where the result is guaranteed to be shorter than that: AND of two |
| 4715 | positives, OR of two negatives: use the shorter number. AND with |
| 4716 | mixed signs: use the positive number. OR with mixed signs: use the |
| 4717 | negative number. |
| 4718 | */ |
| 4719 | switch (op) { |
| 4720 | case '^': |
| 4721 | negz = nega ^ negb; |
| 4722 | size_z = size_a; |
| 4723 | break; |
| 4724 | case '&': |
| 4725 | negz = nega & negb; |
| 4726 | size_z = negb ? size_a : size_b; |
| 4727 | break; |
| 4728 | case '|': |
| 4729 | negz = nega | negb; |
| 4730 | size_z = negb ? size_b : size_a; |
| 4731 | break; |
| 4732 | default: |
stratakis | a10d426 | 2019-03-18 18:59:20 +0100 | [diff] [blame] | 4733 | Py_UNREACHABLE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4734 | } |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 4735 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4736 | /* We allow an extra digit if z is negative, to make sure that |
| 4737 | the final two's complement of z doesn't overflow. */ |
| 4738 | z = _PyLong_New(size_z + negz); |
| 4739 | if (z == NULL) { |
| 4740 | Py_DECREF(a); |
| 4741 | Py_DECREF(b); |
| 4742 | return NULL; |
| 4743 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4744 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4745 | /* Compute digits for overlap of a and b. */ |
| 4746 | switch(op) { |
| 4747 | case '&': |
| 4748 | for (i = 0; i < size_b; ++i) |
| 4749 | z->ob_digit[i] = a->ob_digit[i] & b->ob_digit[i]; |
| 4750 | break; |
| 4751 | case '|': |
| 4752 | for (i = 0; i < size_b; ++i) |
| 4753 | z->ob_digit[i] = a->ob_digit[i] | b->ob_digit[i]; |
| 4754 | break; |
| 4755 | case '^': |
| 4756 | for (i = 0; i < size_b; ++i) |
| 4757 | z->ob_digit[i] = a->ob_digit[i] ^ b->ob_digit[i]; |
| 4758 | break; |
| 4759 | default: |
stratakis | a10d426 | 2019-03-18 18:59:20 +0100 | [diff] [blame] | 4760 | Py_UNREACHABLE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4761 | } |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4762 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4763 | /* Copy any remaining digits of a, inverting if necessary. */ |
| 4764 | if (op == '^' && negb) |
| 4765 | for (; i < size_z; ++i) |
| 4766 | z->ob_digit[i] = a->ob_digit[i] ^ PyLong_MASK; |
| 4767 | else if (i < size_z) |
| 4768 | memcpy(&z->ob_digit[i], &a->ob_digit[i], |
| 4769 | (size_z-i)*sizeof(digit)); |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4770 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4771 | /* Complement result if negative. */ |
| 4772 | if (negz) { |
| 4773 | Py_SIZE(z) = -(Py_SIZE(z)); |
| 4774 | z->ob_digit[size_z] = PyLong_MASK; |
| 4775 | v_complement(z->ob_digit, z->ob_digit, size_z+1); |
| 4776 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4777 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4778 | Py_DECREF(a); |
| 4779 | Py_DECREF(b); |
| 4780 | return (PyObject *)maybe_small_long(long_normalize(z)); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4781 | } |
| 4782 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4783 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 4784 | long_and(PyObject *a, PyObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4785 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4786 | PyObject *c; |
| 4787 | CHECK_BINOP(a, b); |
| 4788 | c = long_bitwise((PyLongObject*)a, '&', (PyLongObject*)b); |
| 4789 | return c; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4790 | } |
| 4791 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4792 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 4793 | long_xor(PyObject *a, PyObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4794 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4795 | PyObject *c; |
| 4796 | CHECK_BINOP(a, b); |
| 4797 | c = long_bitwise((PyLongObject*)a, '^', (PyLongObject*)b); |
| 4798 | return c; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4799 | } |
| 4800 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4801 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 4802 | long_or(PyObject *a, PyObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4803 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4804 | PyObject *c; |
| 4805 | CHECK_BINOP(a, b); |
| 4806 | c = long_bitwise((PyLongObject*)a, '|', (PyLongObject*)b); |
| 4807 | return c; |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 4808 | } |
| 4809 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4810 | static PyObject * |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 4811 | long_long(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 4812 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4813 | if (PyLong_CheckExact(v)) |
| 4814 | Py_INCREF(v); |
| 4815 | else |
| 4816 | v = _PyLong_Copy((PyLongObject *)v); |
| 4817 | return v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 4818 | } |
| 4819 | |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 4820 | PyObject * |
| 4821 | _PyLong_GCD(PyObject *aarg, PyObject *barg) |
| 4822 | { |
| 4823 | PyLongObject *a, *b, *c = NULL, *d = NULL, *r; |
| 4824 | stwodigits x, y, q, s, t, c_carry, d_carry; |
| 4825 | stwodigits A, B, C, D, T; |
| 4826 | int nbits, k; |
| 4827 | Py_ssize_t size_a, size_b, alloc_a, alloc_b; |
| 4828 | digit *a_digit, *b_digit, *c_digit, *d_digit, *a_end, *b_end; |
| 4829 | |
| 4830 | a = (PyLongObject *)aarg; |
| 4831 | b = (PyLongObject *)barg; |
| 4832 | size_a = Py_SIZE(a); |
| 4833 | size_b = Py_SIZE(b); |
| 4834 | if (-2 <= size_a && size_a <= 2 && -2 <= size_b && size_b <= 2) { |
| 4835 | Py_INCREF(a); |
| 4836 | Py_INCREF(b); |
| 4837 | goto simple; |
| 4838 | } |
| 4839 | |
| 4840 | /* Initial reduction: make sure that 0 <= b <= a. */ |
| 4841 | a = (PyLongObject *)long_abs(a); |
| 4842 | if (a == NULL) |
| 4843 | return NULL; |
| 4844 | b = (PyLongObject *)long_abs(b); |
| 4845 | if (b == NULL) { |
| 4846 | Py_DECREF(a); |
| 4847 | return NULL; |
| 4848 | } |
| 4849 | if (long_compare(a, b) < 0) { |
| 4850 | r = a; |
| 4851 | a = b; |
| 4852 | b = r; |
| 4853 | } |
| 4854 | /* We now own references to a and b */ |
| 4855 | |
| 4856 | alloc_a = Py_SIZE(a); |
| 4857 | alloc_b = Py_SIZE(b); |
| 4858 | /* reduce until a fits into 2 digits */ |
| 4859 | while ((size_a = Py_SIZE(a)) > 2) { |
Niklas Fiekas | c5b7900 | 2020-01-16 15:09:19 +0100 | [diff] [blame^] | 4860 | nbits = _Py_bit_length(a->ob_digit[size_a-1]); |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 4861 | /* extract top 2*PyLong_SHIFT bits of a into x, along with |
| 4862 | corresponding bits of b into y */ |
| 4863 | size_b = Py_SIZE(b); |
| 4864 | assert(size_b <= size_a); |
| 4865 | if (size_b == 0) { |
| 4866 | if (size_a < alloc_a) { |
| 4867 | r = (PyLongObject *)_PyLong_Copy(a); |
| 4868 | Py_DECREF(a); |
| 4869 | } |
| 4870 | else |
| 4871 | r = a; |
| 4872 | Py_DECREF(b); |
| 4873 | Py_XDECREF(c); |
| 4874 | Py_XDECREF(d); |
| 4875 | return (PyObject *)r; |
| 4876 | } |
| 4877 | x = (((twodigits)a->ob_digit[size_a-1] << (2*PyLong_SHIFT-nbits)) | |
| 4878 | ((twodigits)a->ob_digit[size_a-2] << (PyLong_SHIFT-nbits)) | |
| 4879 | (a->ob_digit[size_a-3] >> nbits)); |
| 4880 | |
| 4881 | y = ((size_b >= size_a - 2 ? b->ob_digit[size_a-3] >> nbits : 0) | |
| 4882 | (size_b >= size_a - 1 ? (twodigits)b->ob_digit[size_a-2] << (PyLong_SHIFT-nbits) : 0) | |
| 4883 | (size_b >= size_a ? (twodigits)b->ob_digit[size_a-1] << (2*PyLong_SHIFT-nbits) : 0)); |
| 4884 | |
| 4885 | /* inner loop of Lehmer's algorithm; A, B, C, D never grow |
| 4886 | larger than PyLong_MASK during the algorithm. */ |
| 4887 | A = 1; B = 0; C = 0; D = 1; |
| 4888 | for (k=0;; k++) { |
| 4889 | if (y-C == 0) |
| 4890 | break; |
| 4891 | q = (x+(A-1))/(y-C); |
| 4892 | s = B+q*D; |
| 4893 | t = x-q*y; |
| 4894 | if (s > t) |
| 4895 | break; |
| 4896 | x = y; y = t; |
| 4897 | t = A+q*C; A = D; B = C; C = s; D = t; |
| 4898 | } |
| 4899 | |
| 4900 | if (k == 0) { |
| 4901 | /* no progress; do a Euclidean step */ |
| 4902 | if (l_divmod(a, b, NULL, &r) < 0) |
| 4903 | goto error; |
| 4904 | Py_DECREF(a); |
| 4905 | a = b; |
| 4906 | b = r; |
| 4907 | alloc_a = alloc_b; |
| 4908 | alloc_b = Py_SIZE(b); |
| 4909 | continue; |
| 4910 | } |
| 4911 | |
| 4912 | /* |
| 4913 | a, b = A*b-B*a, D*a-C*b if k is odd |
| 4914 | a, b = A*a-B*b, D*b-C*a if k is even |
| 4915 | */ |
| 4916 | if (k&1) { |
| 4917 | T = -A; A = -B; B = T; |
| 4918 | T = -C; C = -D; D = T; |
| 4919 | } |
| 4920 | if (c != NULL) |
| 4921 | Py_SIZE(c) = size_a; |
| 4922 | else if (Py_REFCNT(a) == 1) { |
| 4923 | Py_INCREF(a); |
| 4924 | c = a; |
| 4925 | } |
| 4926 | else { |
| 4927 | alloc_a = size_a; |
| 4928 | c = _PyLong_New(size_a); |
| 4929 | if (c == NULL) |
| 4930 | goto error; |
| 4931 | } |
| 4932 | |
| 4933 | if (d != NULL) |
| 4934 | Py_SIZE(d) = size_a; |
| 4935 | else if (Py_REFCNT(b) == 1 && size_a <= alloc_b) { |
| 4936 | Py_INCREF(b); |
| 4937 | d = b; |
| 4938 | Py_SIZE(d) = size_a; |
| 4939 | } |
| 4940 | else { |
| 4941 | alloc_b = size_a; |
| 4942 | d = _PyLong_New(size_a); |
| 4943 | if (d == NULL) |
| 4944 | goto error; |
| 4945 | } |
| 4946 | a_end = a->ob_digit + size_a; |
| 4947 | b_end = b->ob_digit + size_b; |
| 4948 | |
| 4949 | /* compute new a and new b in parallel */ |
| 4950 | a_digit = a->ob_digit; |
| 4951 | b_digit = b->ob_digit; |
| 4952 | c_digit = c->ob_digit; |
| 4953 | d_digit = d->ob_digit; |
| 4954 | c_carry = 0; |
| 4955 | d_carry = 0; |
| 4956 | while (b_digit < b_end) { |
| 4957 | c_carry += (A * *a_digit) - (B * *b_digit); |
| 4958 | d_carry += (D * *b_digit++) - (C * *a_digit++); |
| 4959 | *c_digit++ = (digit)(c_carry & PyLong_MASK); |
| 4960 | *d_digit++ = (digit)(d_carry & PyLong_MASK); |
| 4961 | c_carry >>= PyLong_SHIFT; |
| 4962 | d_carry >>= PyLong_SHIFT; |
| 4963 | } |
| 4964 | while (a_digit < a_end) { |
| 4965 | c_carry += A * *a_digit; |
| 4966 | d_carry -= C * *a_digit++; |
| 4967 | *c_digit++ = (digit)(c_carry & PyLong_MASK); |
| 4968 | *d_digit++ = (digit)(d_carry & PyLong_MASK); |
| 4969 | c_carry >>= PyLong_SHIFT; |
| 4970 | d_carry >>= PyLong_SHIFT; |
| 4971 | } |
| 4972 | assert(c_carry == 0); |
| 4973 | assert(d_carry == 0); |
| 4974 | |
| 4975 | Py_INCREF(c); |
| 4976 | Py_INCREF(d); |
| 4977 | Py_DECREF(a); |
| 4978 | Py_DECREF(b); |
| 4979 | a = long_normalize(c); |
| 4980 | b = long_normalize(d); |
| 4981 | } |
| 4982 | Py_XDECREF(c); |
| 4983 | Py_XDECREF(d); |
| 4984 | |
| 4985 | simple: |
| 4986 | assert(Py_REFCNT(a) > 0); |
| 4987 | assert(Py_REFCNT(b) > 0); |
Victor Stinner | 5783fd2 | 2015-09-19 13:39:03 +0200 | [diff] [blame] | 4988 | /* Issue #24999: use two shifts instead of ">> 2*PyLong_SHIFT" to avoid |
| 4989 | undefined behaviour when LONG_MAX type is smaller than 60 bits */ |
| 4990 | #if LONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 4991 | /* a fits into a long, so b must too */ |
| 4992 | x = PyLong_AsLong((PyObject *)a); |
| 4993 | y = PyLong_AsLong((PyObject *)b); |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 4994 | #elif LLONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 4995 | x = PyLong_AsLongLong((PyObject *)a); |
| 4996 | y = PyLong_AsLongLong((PyObject *)b); |
| 4997 | #else |
| 4998 | # error "_PyLong_GCD" |
| 4999 | #endif |
| 5000 | x = Py_ABS(x); |
| 5001 | y = Py_ABS(y); |
| 5002 | Py_DECREF(a); |
| 5003 | Py_DECREF(b); |
| 5004 | |
| 5005 | /* usual Euclidean algorithm for longs */ |
| 5006 | while (y != 0) { |
| 5007 | t = y; |
| 5008 | y = x % y; |
| 5009 | x = t; |
| 5010 | } |
Victor Stinner | 5783fd2 | 2015-09-19 13:39:03 +0200 | [diff] [blame] | 5011 | #if LONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 5012 | return PyLong_FromLong(x); |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 5013 | #elif LLONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 5014 | return PyLong_FromLongLong(x); |
| 5015 | #else |
| 5016 | # error "_PyLong_GCD" |
| 5017 | #endif |
| 5018 | |
| 5019 | error: |
| 5020 | Py_DECREF(a); |
| 5021 | Py_DECREF(b); |
| 5022 | Py_XDECREF(c); |
| 5023 | Py_XDECREF(d); |
| 5024 | return NULL; |
| 5025 | } |
| 5026 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 5027 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 5028 | long_float(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 5029 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5030 | double result; |
| 5031 | result = PyLong_AsDouble(v); |
| 5032 | if (result == -1.0 && PyErr_Occurred()) |
| 5033 | return NULL; |
| 5034 | return PyFloat_FromDouble(result); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 5035 | } |
| 5036 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 5037 | static PyObject * |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 5038 | long_subtype_new(PyTypeObject *type, PyObject *x, PyObject *obase); |
| 5039 | |
| 5040 | /*[clinic input] |
| 5041 | @classmethod |
| 5042 | int.__new__ as long_new |
| 5043 | x: object(c_default="NULL") = 0 |
| 5044 | / |
| 5045 | base as obase: object(c_default="NULL") = 10 |
| 5046 | [clinic start generated code]*/ |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 5047 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5048 | static PyObject * |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 5049 | long_new_impl(PyTypeObject *type, PyObject *x, PyObject *obase) |
| 5050 | /*[clinic end generated code: output=e47cfe777ab0f24c input=81c98f418af9eb6f]*/ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5051 | { |
Gregory P. Smith | a689e52 | 2012-12-25 22:38:32 -0800 | [diff] [blame] | 5052 | Py_ssize_t base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5053 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5054 | if (type != &PyLong_Type) |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 5055 | return long_subtype_new(type, x, obase); /* Wimp out */ |
Serhiy Storchaka | 0b386d5 | 2012-12-28 09:42:11 +0200 | [diff] [blame] | 5056 | if (x == NULL) { |
| 5057 | if (obase != NULL) { |
| 5058 | PyErr_SetString(PyExc_TypeError, |
| 5059 | "int() missing string argument"); |
| 5060 | return NULL; |
| 5061 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5062 | return PyLong_FromLong(0L); |
Serhiy Storchaka | 0b386d5 | 2012-12-28 09:42:11 +0200 | [diff] [blame] | 5063 | } |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 5064 | if (obase == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5065 | return PyNumber_Long(x); |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 5066 | |
Gregory P. Smith | a689e52 | 2012-12-25 22:38:32 -0800 | [diff] [blame] | 5067 | base = PyNumber_AsSsize_t(obase, NULL); |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 5068 | if (base == -1 && PyErr_Occurred()) |
| 5069 | return NULL; |
Gregory P. Smith | a689e52 | 2012-12-25 22:38:32 -0800 | [diff] [blame] | 5070 | if ((base != 0 && base < 2) || base > 36) { |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 5071 | PyErr_SetString(PyExc_ValueError, |
Sanyam Khurana | 28b6248 | 2017-11-14 03:19:26 +0530 | [diff] [blame] | 5072 | "int() base must be >= 2 and <= 36, or 0"); |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 5073 | return NULL; |
| 5074 | } |
| 5075 | |
| 5076 | if (PyUnicode_Check(x)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5077 | return PyLong_FromUnicodeObject(x, (int)base); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5078 | else if (PyByteArray_Check(x) || PyBytes_Check(x)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5079 | char *string; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5080 | if (PyByteArray_Check(x)) |
| 5081 | string = PyByteArray_AS_STRING(x); |
| 5082 | else |
| 5083 | string = PyBytes_AS_STRING(x); |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 5084 | return _PyLong_FromBytes(string, Py_SIZE(x), (int)base); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5085 | } |
| 5086 | else { |
| 5087 | PyErr_SetString(PyExc_TypeError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 5088 | "int() can't convert non-string with explicit base"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5089 | return NULL; |
| 5090 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5091 | } |
| 5092 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 5093 | /* Wimpy, slow approach to tp_new calls for subtypes of int: |
| 5094 | first create a regular int from whatever arguments we got, |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 5095 | then allocate a subtype instance and initialize it from |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 5096 | the regular int. The regular int is then thrown away. |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 5097 | */ |
| 5098 | static PyObject * |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 5099 | long_subtype_new(PyTypeObject *type, PyObject *x, PyObject *obase) |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 5100 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5101 | PyLongObject *tmp, *newobj; |
| 5102 | Py_ssize_t i, n; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 5103 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5104 | assert(PyType_IsSubtype(type, &PyLong_Type)); |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 5105 | tmp = (PyLongObject *)long_new_impl(&PyLong_Type, x, obase); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5106 | if (tmp == NULL) |
| 5107 | return NULL; |
Serhiy Storchaka | 1509580 | 2015-11-25 15:47:01 +0200 | [diff] [blame] | 5108 | assert(PyLong_Check(tmp)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5109 | n = Py_SIZE(tmp); |
| 5110 | if (n < 0) |
| 5111 | n = -n; |
| 5112 | newobj = (PyLongObject *)type->tp_alloc(type, n); |
| 5113 | if (newobj == NULL) { |
| 5114 | Py_DECREF(tmp); |
| 5115 | return NULL; |
| 5116 | } |
| 5117 | assert(PyLong_Check(newobj)); |
| 5118 | Py_SIZE(newobj) = Py_SIZE(tmp); |
| 5119 | for (i = 0; i < n; i++) |
| 5120 | newobj->ob_digit[i] = tmp->ob_digit[i]; |
| 5121 | Py_DECREF(tmp); |
| 5122 | return (PyObject *)newobj; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 5123 | } |
| 5124 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5125 | /*[clinic input] |
| 5126 | int.__getnewargs__ |
| 5127 | [clinic start generated code]*/ |
| 5128 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 5129 | static PyObject * |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5130 | int___getnewargs___impl(PyObject *self) |
| 5131 | /*[clinic end generated code: output=839a49de3f00b61b input=5904770ab1fb8c75]*/ |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 5132 | { |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5133 | return Py_BuildValue("(N)", _PyLong_Copy((PyLongObject *)self)); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 5134 | } |
| 5135 | |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5136 | static PyObject * |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5137 | long_get0(PyObject *Py_UNUSED(self), void *Py_UNUSED(context)) |
| 5138 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5139 | return PyLong_FromLong(0L); |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 5140 | } |
| 5141 | |
| 5142 | static PyObject * |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5143 | long_get1(PyObject *Py_UNUSED(self), void *Py_UNUSED(ignored)) |
| 5144 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5145 | return PyLong_FromLong(1L); |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5146 | } |
| 5147 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5148 | /*[clinic input] |
| 5149 | int.__format__ |
| 5150 | |
| 5151 | format_spec: unicode |
| 5152 | / |
| 5153 | [clinic start generated code]*/ |
| 5154 | |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 5155 | static PyObject * |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5156 | int___format___impl(PyObject *self, PyObject *format_spec) |
| 5157 | /*[clinic end generated code: output=b4929dee9ae18689 input=e31944a9b3e428b7]*/ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 5158 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 5159 | _PyUnicodeWriter writer; |
| 5160 | int ret; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 5161 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5162 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 5163 | ret = _PyLong_FormatAdvancedWriter( |
| 5164 | &writer, |
| 5165 | self, |
| 5166 | format_spec, 0, PyUnicode_GET_LENGTH(format_spec)); |
| 5167 | if (ret == -1) { |
| 5168 | _PyUnicodeWriter_Dealloc(&writer); |
| 5169 | return NULL; |
| 5170 | } |
| 5171 | return _PyUnicodeWriter_Finish(&writer); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 5172 | } |
| 5173 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5174 | /* Return a pair (q, r) such that a = b * q + r, and |
| 5175 | abs(r) <= abs(b)/2, with equality possible only if q is even. |
| 5176 | In other words, q == a / b, rounded to the nearest integer using |
| 5177 | round-half-to-even. */ |
| 5178 | |
| 5179 | PyObject * |
Mark Dickinson | fa68a61 | 2010-06-07 18:47:09 +0000 | [diff] [blame] | 5180 | _PyLong_DivmodNear(PyObject *a, PyObject *b) |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5181 | { |
| 5182 | PyLongObject *quo = NULL, *rem = NULL; |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 5183 | PyObject *twice_rem, *result, *temp; |
HongWeipeng | 42acb7b | 2019-09-18 23:10:15 +0800 | [diff] [blame] | 5184 | int quo_is_odd, quo_is_neg; |
| 5185 | Py_ssize_t cmp; |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5186 | |
| 5187 | /* Equivalent Python code: |
| 5188 | |
| 5189 | def divmod_near(a, b): |
| 5190 | q, r = divmod(a, b) |
| 5191 | # round up if either r / b > 0.5, or r / b == 0.5 and q is odd. |
| 5192 | # The expression r / b > 0.5 is equivalent to 2 * r > b if b is |
| 5193 | # positive, 2 * r < b if b negative. |
| 5194 | greater_than_half = 2*r > b if b > 0 else 2*r < b |
| 5195 | exactly_half = 2*r == b |
| 5196 | if greater_than_half or exactly_half and q % 2 == 1: |
| 5197 | q += 1 |
| 5198 | r -= b |
| 5199 | return q, r |
| 5200 | |
| 5201 | */ |
| 5202 | if (!PyLong_Check(a) || !PyLong_Check(b)) { |
| 5203 | PyErr_SetString(PyExc_TypeError, |
| 5204 | "non-integer arguments in division"); |
| 5205 | return NULL; |
| 5206 | } |
| 5207 | |
| 5208 | /* Do a and b have different signs? If so, quotient is negative. */ |
| 5209 | quo_is_neg = (Py_SIZE(a) < 0) != (Py_SIZE(b) < 0); |
| 5210 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5211 | if (long_divrem((PyLongObject*)a, (PyLongObject*)b, &quo, &rem) < 0) |
| 5212 | goto error; |
| 5213 | |
| 5214 | /* compare twice the remainder with the divisor, to see |
| 5215 | if we need to adjust the quotient and remainder */ |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 5216 | twice_rem = long_lshift((PyObject *)rem, _PyLong_One); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5217 | if (twice_rem == NULL) |
| 5218 | goto error; |
| 5219 | if (quo_is_neg) { |
| 5220 | temp = long_neg((PyLongObject*)twice_rem); |
| 5221 | Py_DECREF(twice_rem); |
| 5222 | twice_rem = temp; |
| 5223 | if (twice_rem == NULL) |
| 5224 | goto error; |
| 5225 | } |
| 5226 | cmp = long_compare((PyLongObject *)twice_rem, (PyLongObject *)b); |
| 5227 | Py_DECREF(twice_rem); |
| 5228 | |
| 5229 | quo_is_odd = Py_SIZE(quo) != 0 && ((quo->ob_digit[0] & 1) != 0); |
| 5230 | if ((Py_SIZE(b) < 0 ? cmp < 0 : cmp > 0) || (cmp == 0 && quo_is_odd)) { |
| 5231 | /* fix up quotient */ |
| 5232 | if (quo_is_neg) |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 5233 | temp = long_sub(quo, (PyLongObject *)_PyLong_One); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5234 | else |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 5235 | temp = long_add(quo, (PyLongObject *)_PyLong_One); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5236 | Py_DECREF(quo); |
| 5237 | quo = (PyLongObject *)temp; |
| 5238 | if (quo == NULL) |
| 5239 | goto error; |
| 5240 | /* and remainder */ |
| 5241 | if (quo_is_neg) |
| 5242 | temp = long_add(rem, (PyLongObject *)b); |
| 5243 | else |
| 5244 | temp = long_sub(rem, (PyLongObject *)b); |
| 5245 | Py_DECREF(rem); |
| 5246 | rem = (PyLongObject *)temp; |
| 5247 | if (rem == NULL) |
| 5248 | goto error; |
| 5249 | } |
| 5250 | |
| 5251 | result = PyTuple_New(2); |
| 5252 | if (result == NULL) |
| 5253 | goto error; |
| 5254 | |
| 5255 | /* PyTuple_SET_ITEM steals references */ |
| 5256 | PyTuple_SET_ITEM(result, 0, (PyObject *)quo); |
| 5257 | PyTuple_SET_ITEM(result, 1, (PyObject *)rem); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5258 | return result; |
| 5259 | |
| 5260 | error: |
| 5261 | Py_XDECREF(quo); |
| 5262 | Py_XDECREF(rem); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5263 | return NULL; |
| 5264 | } |
| 5265 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 5266 | static PyObject * |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 5267 | long_round(PyObject *self, PyObject *args) |
| 5268 | { |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5269 | PyObject *o_ndigits=NULL, *temp, *result, *ndigits; |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 5270 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5271 | /* To round an integer m to the nearest 10**n (n positive), we make use of |
| 5272 | * the divmod_near operation, defined by: |
| 5273 | * |
| 5274 | * divmod_near(a, b) = (q, r) |
| 5275 | * |
| 5276 | * where q is the nearest integer to the quotient a / b (the |
| 5277 | * nearest even integer in the case of a tie) and r == a - q * b. |
| 5278 | * Hence q * b = a - r is the nearest multiple of b to a, |
| 5279 | * preferring even multiples in the case of a tie. |
| 5280 | * |
| 5281 | * So the nearest multiple of 10**n to m is: |
| 5282 | * |
| 5283 | * m - divmod_near(m, 10**n)[1]. |
| 5284 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5285 | if (!PyArg_ParseTuple(args, "|O", &o_ndigits)) |
| 5286 | return NULL; |
| 5287 | if (o_ndigits == NULL) |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5288 | return long_long(self); |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 5289 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5290 | ndigits = PyNumber_Index(o_ndigits); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5291 | if (ndigits == NULL) |
| 5292 | return NULL; |
Mark Dickinson | 1124e71 | 2009-01-28 21:25:58 +0000 | [diff] [blame] | 5293 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5294 | /* if ndigits >= 0 then no rounding is necessary; return self unchanged */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5295 | if (Py_SIZE(ndigits) >= 0) { |
| 5296 | Py_DECREF(ndigits); |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5297 | return long_long(self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5298 | } |
Mark Dickinson | 1124e71 | 2009-01-28 21:25:58 +0000 | [diff] [blame] | 5299 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5300 | /* result = self - divmod_near(self, 10 ** -ndigits)[1] */ |
| 5301 | temp = long_neg((PyLongObject*)ndigits); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5302 | Py_DECREF(ndigits); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5303 | ndigits = temp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5304 | if (ndigits == NULL) |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5305 | return NULL; |
Mark Dickinson | 1124e71 | 2009-01-28 21:25:58 +0000 | [diff] [blame] | 5306 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5307 | result = PyLong_FromLong(10L); |
| 5308 | if (result == NULL) { |
| 5309 | Py_DECREF(ndigits); |
| 5310 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5311 | } |
Mark Dickinson | 1124e71 | 2009-01-28 21:25:58 +0000 | [diff] [blame] | 5312 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5313 | temp = long_pow(result, ndigits, Py_None); |
| 5314 | Py_DECREF(ndigits); |
| 5315 | Py_DECREF(result); |
| 5316 | result = temp; |
| 5317 | if (result == NULL) |
| 5318 | return NULL; |
| 5319 | |
Mark Dickinson | fa68a61 | 2010-06-07 18:47:09 +0000 | [diff] [blame] | 5320 | temp = _PyLong_DivmodNear(self, result); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5321 | Py_DECREF(result); |
| 5322 | result = temp; |
| 5323 | if (result == NULL) |
| 5324 | return NULL; |
| 5325 | |
| 5326 | temp = long_sub((PyLongObject *)self, |
| 5327 | (PyLongObject *)PyTuple_GET_ITEM(result, 1)); |
| 5328 | Py_DECREF(result); |
| 5329 | result = temp; |
| 5330 | |
| 5331 | return result; |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 5332 | } |
| 5333 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5334 | /*[clinic input] |
| 5335 | int.__sizeof__ -> Py_ssize_t |
| 5336 | |
| 5337 | Returns size in memory, in bytes. |
| 5338 | [clinic start generated code]*/ |
| 5339 | |
| 5340 | static Py_ssize_t |
| 5341 | int___sizeof___impl(PyObject *self) |
| 5342 | /*[clinic end generated code: output=3303f008eaa6a0a5 input=9b51620c76fc4507]*/ |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 5343 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5344 | Py_ssize_t res; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 5345 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5346 | res = offsetof(PyLongObject, ob_digit) + Py_ABS(Py_SIZE(self))*sizeof(digit); |
| 5347 | return res; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 5348 | } |
| 5349 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5350 | /*[clinic input] |
| 5351 | int.bit_length |
| 5352 | |
| 5353 | Number of bits necessary to represent self in binary. |
| 5354 | |
| 5355 | >>> bin(37) |
| 5356 | '0b100101' |
| 5357 | >>> (37).bit_length() |
| 5358 | 6 |
| 5359 | [clinic start generated code]*/ |
| 5360 | |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5361 | static PyObject * |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5362 | int_bit_length_impl(PyObject *self) |
| 5363 | /*[clinic end generated code: output=fc1977c9353d6a59 input=e4eb7a587e849a32]*/ |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5364 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5365 | PyLongObject *result, *x, *y; |
Serhiy Storchaka | b2e64f9 | 2016-11-08 20:34:22 +0200 | [diff] [blame] | 5366 | Py_ssize_t ndigits; |
| 5367 | int msd_bits; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5368 | digit msd; |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5369 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5370 | assert(self != NULL); |
| 5371 | assert(PyLong_Check(self)); |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5372 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5373 | ndigits = Py_ABS(Py_SIZE(self)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5374 | if (ndigits == 0) |
| 5375 | return PyLong_FromLong(0); |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5376 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5377 | msd = ((PyLongObject *)self)->ob_digit[ndigits-1]; |
Niklas Fiekas | c5b7900 | 2020-01-16 15:09:19 +0100 | [diff] [blame^] | 5378 | msd_bits = _Py_bit_length(msd); |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5379 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5380 | if (ndigits <= PY_SSIZE_T_MAX/PyLong_SHIFT) |
| 5381 | return PyLong_FromSsize_t((ndigits-1)*PyLong_SHIFT + msd_bits); |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5382 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5383 | /* expression above may overflow; use Python integers instead */ |
| 5384 | result = (PyLongObject *)PyLong_FromSsize_t(ndigits - 1); |
| 5385 | if (result == NULL) |
| 5386 | return NULL; |
| 5387 | x = (PyLongObject *)PyLong_FromLong(PyLong_SHIFT); |
| 5388 | if (x == NULL) |
| 5389 | goto error; |
| 5390 | y = (PyLongObject *)long_mul(result, x); |
| 5391 | Py_DECREF(x); |
| 5392 | if (y == NULL) |
| 5393 | goto error; |
| 5394 | Py_DECREF(result); |
| 5395 | result = y; |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5396 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5397 | x = (PyLongObject *)PyLong_FromLong((long)msd_bits); |
| 5398 | if (x == NULL) |
| 5399 | goto error; |
| 5400 | y = (PyLongObject *)long_add(result, x); |
| 5401 | Py_DECREF(x); |
| 5402 | if (y == NULL) |
| 5403 | goto error; |
| 5404 | Py_DECREF(result); |
| 5405 | result = y; |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5406 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5407 | return (PyObject *)result; |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5408 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 5409 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5410 | Py_DECREF(result); |
| 5411 | return NULL; |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5412 | } |
| 5413 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 5414 | #if 0 |
| 5415 | static PyObject * |
| 5416 | long_is_finite(PyObject *v) |
| 5417 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5418 | Py_RETURN_TRUE; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 5419 | } |
| 5420 | #endif |
| 5421 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5422 | /*[clinic input] |
Lisa Roach | 5ac7043 | 2018-09-13 23:56:23 -0700 | [diff] [blame] | 5423 | int.as_integer_ratio |
| 5424 | |
| 5425 | Return integer ratio. |
| 5426 | |
| 5427 | Return a pair of integers, whose ratio is exactly equal to the original int |
| 5428 | and with a positive denominator. |
| 5429 | |
| 5430 | >>> (10).as_integer_ratio() |
| 5431 | (10, 1) |
| 5432 | >>> (-10).as_integer_ratio() |
| 5433 | (-10, 1) |
| 5434 | >>> (0).as_integer_ratio() |
| 5435 | (0, 1) |
| 5436 | [clinic start generated code]*/ |
| 5437 | |
| 5438 | static PyObject * |
| 5439 | int_as_integer_ratio_impl(PyObject *self) |
| 5440 | /*[clinic end generated code: output=e60803ae1cc8621a input=55ce3058e15de393]*/ |
| 5441 | { |
Raymond Hettinger | 00bc08e | 2018-09-14 01:00:11 -0700 | [diff] [blame] | 5442 | PyObject *ratio_tuple; |
Serhiy Storchaka | b2e2025 | 2018-10-20 00:46:31 +0300 | [diff] [blame] | 5443 | PyObject *numerator = long_long(self); |
Raymond Hettinger | 00bc08e | 2018-09-14 01:00:11 -0700 | [diff] [blame] | 5444 | if (numerator == NULL) { |
| 5445 | return NULL; |
| 5446 | } |
| 5447 | ratio_tuple = PyTuple_Pack(2, numerator, _PyLong_One); |
| 5448 | Py_DECREF(numerator); |
| 5449 | return ratio_tuple; |
Lisa Roach | 5ac7043 | 2018-09-13 23:56:23 -0700 | [diff] [blame] | 5450 | } |
| 5451 | |
| 5452 | /*[clinic input] |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5453 | int.to_bytes |
| 5454 | |
| 5455 | length: Py_ssize_t |
| 5456 | Length of bytes object to use. An OverflowError is raised if the |
| 5457 | integer is not representable with the given number of bytes. |
| 5458 | byteorder: unicode |
| 5459 | The byte order used to represent the integer. If byteorder is 'big', |
| 5460 | the most significant byte is at the beginning of the byte array. If |
| 5461 | byteorder is 'little', the most significant byte is at the end of the |
| 5462 | byte array. To request the native byte order of the host system, use |
| 5463 | `sys.byteorder' as the byte order value. |
| 5464 | * |
| 5465 | signed as is_signed: bool = False |
| 5466 | Determines whether two's complement is used to represent the integer. |
| 5467 | If signed is False and a negative integer is given, an OverflowError |
| 5468 | is raised. |
| 5469 | |
| 5470 | Return an array of bytes representing an integer. |
| 5471 | [clinic start generated code]*/ |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5472 | |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5473 | static PyObject * |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5474 | int_to_bytes_impl(PyObject *self, Py_ssize_t length, PyObject *byteorder, |
| 5475 | int is_signed) |
| 5476 | /*[clinic end generated code: output=89c801df114050a3 input=ddac63f4c7bf414c]*/ |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5477 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5478 | int little_endian; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5479 | PyObject *bytes; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5480 | |
Serhiy Storchaka | 196a7bc | 2017-02-02 16:54:45 +0200 | [diff] [blame] | 5481 | if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_little)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5482 | little_endian = 1; |
Serhiy Storchaka | 196a7bc | 2017-02-02 16:54:45 +0200 | [diff] [blame] | 5483 | else if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_big)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5484 | little_endian = 0; |
| 5485 | else { |
| 5486 | PyErr_SetString(PyExc_ValueError, |
| 5487 | "byteorder must be either 'little' or 'big'"); |
| 5488 | return NULL; |
| 5489 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5490 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5491 | if (length < 0) { |
| 5492 | PyErr_SetString(PyExc_ValueError, |
| 5493 | "length argument must be non-negative"); |
| 5494 | return NULL; |
| 5495 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5496 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5497 | bytes = PyBytes_FromStringAndSize(NULL, length); |
| 5498 | if (bytes == NULL) |
| 5499 | return NULL; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5500 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5501 | if (_PyLong_AsByteArray((PyLongObject *)self, |
| 5502 | (unsigned char *)PyBytes_AS_STRING(bytes), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5503 | length, little_endian, is_signed) < 0) { |
| 5504 | Py_DECREF(bytes); |
| 5505 | return NULL; |
| 5506 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5507 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5508 | return bytes; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5509 | } |
| 5510 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5511 | /*[clinic input] |
| 5512 | @classmethod |
| 5513 | int.from_bytes |
| 5514 | |
| 5515 | bytes as bytes_obj: object |
| 5516 | Holds the array of bytes to convert. The argument must either |
| 5517 | support the buffer protocol or be an iterable object producing bytes. |
| 5518 | Bytes and bytearray are examples of built-in objects that support the |
| 5519 | buffer protocol. |
| 5520 | byteorder: unicode |
| 5521 | The byte order used to represent the integer. If byteorder is 'big', |
| 5522 | the most significant byte is at the beginning of the byte array. If |
| 5523 | byteorder is 'little', the most significant byte is at the end of the |
| 5524 | byte array. To request the native byte order of the host system, use |
| 5525 | `sys.byteorder' as the byte order value. |
| 5526 | * |
| 5527 | signed as is_signed: bool = False |
| 5528 | Indicates whether two's complement is used to represent the integer. |
| 5529 | |
| 5530 | Return the integer represented by the given array of bytes. |
| 5531 | [clinic start generated code]*/ |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5532 | |
| 5533 | static PyObject * |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5534 | int_from_bytes_impl(PyTypeObject *type, PyObject *bytes_obj, |
| 5535 | PyObject *byteorder, int is_signed) |
| 5536 | /*[clinic end generated code: output=efc5d68e31f9314f input=cdf98332b6a821b0]*/ |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5537 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5538 | int little_endian; |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5539 | PyObject *long_obj, *bytes; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5540 | |
Serhiy Storchaka | 196a7bc | 2017-02-02 16:54:45 +0200 | [diff] [blame] | 5541 | if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_little)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5542 | little_endian = 1; |
Serhiy Storchaka | 196a7bc | 2017-02-02 16:54:45 +0200 | [diff] [blame] | 5543 | else if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_big)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5544 | little_endian = 0; |
| 5545 | else { |
| 5546 | PyErr_SetString(PyExc_ValueError, |
| 5547 | "byteorder must be either 'little' or 'big'"); |
| 5548 | return NULL; |
| 5549 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5550 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5551 | bytes = PyObject_Bytes(bytes_obj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5552 | if (bytes == NULL) |
| 5553 | return NULL; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5554 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5555 | long_obj = _PyLong_FromByteArray( |
| 5556 | (unsigned char *)PyBytes_AS_STRING(bytes), Py_SIZE(bytes), |
| 5557 | little_endian, is_signed); |
| 5558 | Py_DECREF(bytes); |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5559 | |
Zackery Spytz | 7bb9cd0 | 2018-10-05 15:02:23 -0600 | [diff] [blame] | 5560 | if (long_obj != NULL && type != &PyLong_Type) { |
Jeroen Demeyer | 196a530 | 2019-07-04 12:31:34 +0200 | [diff] [blame] | 5561 | Py_SETREF(long_obj, _PyObject_CallOneArg((PyObject *)type, long_obj)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5562 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5563 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5564 | return long_obj; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5565 | } |
| 5566 | |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5567 | static PyObject * |
| 5568 | long_long_meth(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 5569 | { |
| 5570 | return long_long(self); |
| 5571 | } |
| 5572 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 5573 | static PyMethodDef long_methods[] = { |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5574 | {"conjugate", long_long_meth, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5575 | "Returns self, the complex conjugate of any int."}, |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5576 | INT_BIT_LENGTH_METHODDEF |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 5577 | #if 0 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5578 | {"is_finite", (PyCFunction)long_is_finite, METH_NOARGS, |
| 5579 | "Returns always True."}, |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 5580 | #endif |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5581 | INT_TO_BYTES_METHODDEF |
| 5582 | INT_FROM_BYTES_METHODDEF |
Lisa Roach | 5ac7043 | 2018-09-13 23:56:23 -0700 | [diff] [blame] | 5583 | INT_AS_INTEGER_RATIO_METHODDEF |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5584 | {"__trunc__", long_long_meth, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5585 | "Truncating an Integral returns itself."}, |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5586 | {"__floor__", long_long_meth, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5587 | "Flooring an Integral returns itself."}, |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5588 | {"__ceil__", long_long_meth, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5589 | "Ceiling of an Integral returns itself."}, |
| 5590 | {"__round__", (PyCFunction)long_round, METH_VARARGS, |
| 5591 | "Rounding an Integral returns itself.\n" |
| 5592 | "Rounding with an ndigits argument also returns an integer."}, |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5593 | INT___GETNEWARGS___METHODDEF |
| 5594 | INT___FORMAT___METHODDEF |
| 5595 | INT___SIZEOF___METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5596 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 5597 | }; |
| 5598 | |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5599 | static PyGetSetDef long_getset[] = { |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 5600 | {"real", |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5601 | (getter)long_long_meth, (setter)NULL, |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5602 | "the real part of a complex number", |
| 5603 | NULL}, |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 5604 | {"imag", |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5605 | long_get0, (setter)NULL, |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5606 | "the imaginary part of a complex number", |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 5607 | NULL}, |
| 5608 | {"numerator", |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5609 | (getter)long_long_meth, (setter)NULL, |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5610 | "the numerator of a rational number in lowest terms", |
| 5611 | NULL}, |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 5612 | {"denominator", |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5613 | long_get1, (setter)NULL, |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5614 | "the denominator of a rational number in lowest terms", |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 5615 | NULL}, |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5616 | {NULL} /* Sentinel */ |
| 5617 | }; |
| 5618 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5619 | PyDoc_STRVAR(long_doc, |
svelankar | 390a096 | 2017-03-08 19:29:01 -0500 | [diff] [blame] | 5620 | "int([x]) -> integer\n\ |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 5621 | int(x, base=10) -> integer\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5622 | \n\ |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 5623 | Convert a number or string to an integer, or return 0 if no arguments\n\ |
| 5624 | are given. If x is a number, return x.__int__(). For floating point\n\ |
| 5625 | numbers, this truncates towards zero.\n\ |
| 5626 | \n\ |
| 5627 | If x is not a number or if base is given, then x must be a string,\n\ |
| 5628 | bytes, or bytearray instance representing an integer literal in the\n\ |
| 5629 | given base. The literal can be preceded by '+' or '-' and be surrounded\n\ |
| 5630 | by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.\n\ |
| 5631 | Base 0 means to interpret the base from the string as an integer literal.\n\ |
| 5632 | >>> int('0b100', base=0)\n\ |
| 5633 | 4"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5634 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 5635 | static PyNumberMethods long_as_number = { |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 5636 | (binaryfunc)long_add, /*nb_add*/ |
| 5637 | (binaryfunc)long_sub, /*nb_subtract*/ |
| 5638 | (binaryfunc)long_mul, /*nb_multiply*/ |
| 5639 | long_mod, /*nb_remainder*/ |
| 5640 | long_divmod, /*nb_divmod*/ |
| 5641 | long_pow, /*nb_power*/ |
| 5642 | (unaryfunc)long_neg, /*nb_negative*/ |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5643 | long_long, /*tp_positive*/ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 5644 | (unaryfunc)long_abs, /*tp_absolute*/ |
| 5645 | (inquiry)long_bool, /*tp_bool*/ |
| 5646 | (unaryfunc)long_invert, /*nb_invert*/ |
| 5647 | long_lshift, /*nb_lshift*/ |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 5648 | long_rshift, /*nb_rshift*/ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 5649 | long_and, /*nb_and*/ |
| 5650 | long_xor, /*nb_xor*/ |
| 5651 | long_or, /*nb_or*/ |
| 5652 | long_long, /*nb_int*/ |
| 5653 | 0, /*nb_reserved*/ |
| 5654 | long_float, /*nb_float*/ |
| 5655 | 0, /* nb_inplace_add */ |
| 5656 | 0, /* nb_inplace_subtract */ |
| 5657 | 0, /* nb_inplace_multiply */ |
| 5658 | 0, /* nb_inplace_remainder */ |
| 5659 | 0, /* nb_inplace_power */ |
| 5660 | 0, /* nb_inplace_lshift */ |
| 5661 | 0, /* nb_inplace_rshift */ |
| 5662 | 0, /* nb_inplace_and */ |
| 5663 | 0, /* nb_inplace_xor */ |
| 5664 | 0, /* nb_inplace_or */ |
| 5665 | long_div, /* nb_floor_divide */ |
| 5666 | long_true_divide, /* nb_true_divide */ |
| 5667 | 0, /* nb_inplace_floor_divide */ |
| 5668 | 0, /* nb_inplace_true_divide */ |
| 5669 | long_long, /* nb_index */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 5670 | }; |
| 5671 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 5672 | PyTypeObject PyLong_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5673 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 5674 | "int", /* tp_name */ |
| 5675 | offsetof(PyLongObject, ob_digit), /* tp_basicsize */ |
| 5676 | sizeof(digit), /* tp_itemsize */ |
Inada Naoki | 7d40869 | 2019-05-29 17:23:27 +0900 | [diff] [blame] | 5677 | 0, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5678 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5679 | 0, /* tp_getattr */ |
| 5680 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5681 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5682 | long_to_decimal_string, /* tp_repr */ |
| 5683 | &long_as_number, /* tp_as_number */ |
| 5684 | 0, /* tp_as_sequence */ |
| 5685 | 0, /* tp_as_mapping */ |
| 5686 | (hashfunc)long_hash, /* tp_hash */ |
| 5687 | 0, /* tp_call */ |
Serhiy Storchaka | 96aeaec | 2019-05-06 22:29:40 +0300 | [diff] [blame] | 5688 | 0, /* tp_str */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5689 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 5690 | 0, /* tp_setattro */ |
| 5691 | 0, /* tp_as_buffer */ |
| 5692 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 5693 | Py_TPFLAGS_LONG_SUBCLASS, /* tp_flags */ |
| 5694 | long_doc, /* tp_doc */ |
| 5695 | 0, /* tp_traverse */ |
| 5696 | 0, /* tp_clear */ |
| 5697 | long_richcompare, /* tp_richcompare */ |
| 5698 | 0, /* tp_weaklistoffset */ |
| 5699 | 0, /* tp_iter */ |
| 5700 | 0, /* tp_iternext */ |
| 5701 | long_methods, /* tp_methods */ |
| 5702 | 0, /* tp_members */ |
| 5703 | long_getset, /* tp_getset */ |
| 5704 | 0, /* tp_base */ |
| 5705 | 0, /* tp_dict */ |
| 5706 | 0, /* tp_descr_get */ |
| 5707 | 0, /* tp_descr_set */ |
| 5708 | 0, /* tp_dictoffset */ |
| 5709 | 0, /* tp_init */ |
| 5710 | 0, /* tp_alloc */ |
| 5711 | long_new, /* tp_new */ |
| 5712 | PyObject_Del, /* tp_free */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 5713 | }; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5714 | |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 5715 | static PyTypeObject Int_InfoType; |
| 5716 | |
| 5717 | PyDoc_STRVAR(int_info__doc__, |
| 5718 | "sys.int_info\n\ |
| 5719 | \n\ |
Raymond Hettinger | 7117074 | 2019-09-11 07:17:32 -0700 | [diff] [blame] | 5720 | A named tuple that holds information about Python's\n\ |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 5721 | internal representation of integers. The attributes are read only."); |
| 5722 | |
| 5723 | static PyStructSequence_Field int_info_fields[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5724 | {"bits_per_digit", "size of a digit in bits"}, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 5725 | {"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] | 5726 | {NULL, NULL} |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 5727 | }; |
| 5728 | |
| 5729 | static PyStructSequence_Desc int_info_desc = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5730 | "sys.int_info", /* name */ |
| 5731 | int_info__doc__, /* doc */ |
| 5732 | int_info_fields, /* fields */ |
| 5733 | 2 /* number of fields */ |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 5734 | }; |
| 5735 | |
| 5736 | PyObject * |
| 5737 | PyLong_GetInfo(void) |
| 5738 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5739 | PyObject* int_info; |
| 5740 | int field = 0; |
| 5741 | int_info = PyStructSequence_New(&Int_InfoType); |
| 5742 | if (int_info == NULL) |
| 5743 | return NULL; |
| 5744 | PyStructSequence_SET_ITEM(int_info, field++, |
| 5745 | PyLong_FromLong(PyLong_SHIFT)); |
| 5746 | PyStructSequence_SET_ITEM(int_info, field++, |
| 5747 | PyLong_FromLong(sizeof(digit))); |
| 5748 | if (PyErr_Occurred()) { |
| 5749 | Py_CLEAR(int_info); |
| 5750 | return NULL; |
| 5751 | } |
| 5752 | return int_info; |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 5753 | } |
| 5754 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5755 | int |
Victor Stinner | 630c8df | 2019-12-17 13:02:18 +0100 | [diff] [blame] | 5756 | _PyLong_Init(PyThreadState *tstate) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5757 | { |
| 5758 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
Victor Stinner | 5dcc06f | 2019-11-21 08:51:59 +0100 | [diff] [blame] | 5759 | for (Py_ssize_t i=0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++) { |
| 5760 | sdigit ival = (sdigit)i - NSMALLNEGINTS; |
| 5761 | int size = (ival < 0) ? -1 : ((ival == 0) ? 0 : 1); |
Christian Heimes | dfc12ed | 2008-01-31 15:16:38 +0000 | [diff] [blame] | 5762 | |
Victor Stinner | 5dcc06f | 2019-11-21 08:51:59 +0100 | [diff] [blame] | 5763 | PyLongObject *v = _PyLong_New(1); |
| 5764 | if (!v) { |
| 5765 | return -1; |
| 5766 | } |
Christian Heimes | dfc12ed | 2008-01-31 15:16:38 +0000 | [diff] [blame] | 5767 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5768 | Py_SIZE(v) = size; |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 5769 | v->ob_digit[0] = (digit)abs(ival); |
Victor Stinner | 5dcc06f | 2019-11-21 08:51:59 +0100 | [diff] [blame] | 5770 | |
Victor Stinner | 630c8df | 2019-12-17 13:02:18 +0100 | [diff] [blame] | 5771 | tstate->interp->small_ints[i] = v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5772 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5773 | #endif |
Victor Stinner | 5dcc06f | 2019-11-21 08:51:59 +0100 | [diff] [blame] | 5774 | |
Victor Stinner | 630c8df | 2019-12-17 13:02:18 +0100 | [diff] [blame] | 5775 | if (_Py_IsMainInterpreter(tstate)) { |
| 5776 | _PyLong_Zero = PyLong_FromLong(0); |
| 5777 | if (_PyLong_Zero == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 5778 | return 0; |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 5779 | } |
Victor Stinner | 630c8df | 2019-12-17 13:02:18 +0100 | [diff] [blame] | 5780 | |
| 5781 | _PyLong_One = PyLong_FromLong(1); |
| 5782 | if (_PyLong_One == NULL) { |
| 5783 | return 0; |
| 5784 | } |
| 5785 | |
| 5786 | /* initialize int_info */ |
| 5787 | if (Int_InfoType.tp_name == NULL) { |
| 5788 | if (PyStructSequence_InitType2(&Int_InfoType, &int_info_desc) < 0) { |
| 5789 | return 0; |
| 5790 | } |
| 5791 | } |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 5792 | } |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 5793 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5794 | return 1; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5795 | } |
| 5796 | |
| 5797 | void |
Victor Stinner | 630c8df | 2019-12-17 13:02:18 +0100 | [diff] [blame] | 5798 | _PyLong_Fini(PyThreadState *tstate) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5799 | { |
Victor Stinner | 630c8df | 2019-12-17 13:02:18 +0100 | [diff] [blame] | 5800 | if (_Py_IsMainInterpreter(tstate)) { |
| 5801 | Py_CLEAR(_PyLong_One); |
| 5802 | Py_CLEAR(_PyLong_Zero); |
| 5803 | } |
| 5804 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5805 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
Victor Stinner | 5dcc06f | 2019-11-21 08:51:59 +0100 | [diff] [blame] | 5806 | for (Py_ssize_t i = 0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++) { |
Victor Stinner | 630c8df | 2019-12-17 13:02:18 +0100 | [diff] [blame] | 5807 | Py_CLEAR(tstate->interp->small_ints[i]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5808 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5809 | #endif |
| 5810 | } |