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 | 4a3fe08 | 2020-04-14 14:26:24 +0200 | [diff] [blame] | 6 | #include "pycore_interp.h" // _PY_NSMALLPOSINTS |
| 7 | #include "pycore_pystate.h" // _Py_IsMainInterpreter() |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 8 | #include "longintrepr.h" |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 9 | |
Mark Dickinson | c630039 | 2009-04-20 21:38:00 +0000 | [diff] [blame] | 10 | #include <float.h> |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 11 | #include <ctype.h> |
Mark Dickinson | 5a74bf6 | 2009-02-15 11:04:38 +0000 | [diff] [blame] | 12 | #include <stddef.h> |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 13 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 14 | #include "clinic/longobject.c.h" |
| 15 | /*[clinic input] |
| 16 | class int "PyObject *" "&PyLong_Type" |
| 17 | [clinic start generated code]*/ |
| 18 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=ec0275e3422a36e3]*/ |
| 19 | |
Victor Stinner | 630c8df | 2019-12-17 13:02:18 +0100 | [diff] [blame] | 20 | #define NSMALLPOSINTS _PY_NSMALLPOSINTS |
| 21 | #define NSMALLNEGINTS _PY_NSMALLNEGINTS |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 22 | |
Serhiy Storchaka | 196a7bc | 2017-02-02 16:54:45 +0200 | [diff] [blame] | 23 | _Py_IDENTIFIER(little); |
| 24 | _Py_IDENTIFIER(big); |
| 25 | |
Mark Dickinson | e441674 | 2009-02-15 15:14:57 +0000 | [diff] [blame] | 26 | /* convert a PyLong of size 1, 0 or -1 to an sdigit */ |
Victor Stinner | 08a80b1 | 2013-07-17 22:33:42 +0200 | [diff] [blame] | 27 | #define MEDIUM_VALUE(x) (assert(-1 <= Py_SIZE(x) && Py_SIZE(x) <= 1), \ |
| 28 | Py_SIZE(x) < 0 ? -(sdigit)(x)->ob_digit[0] : \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 29 | (Py_SIZE(x) == 0 ? (sdigit)0 : \ |
| 30 | (sdigit)(x)->ob_digit[0])) |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 31 | |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 32 | PyObject *_PyLong_Zero = NULL; |
| 33 | PyObject *_PyLong_One = NULL; |
| 34 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 35 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
animalize | 6b51998 | 2019-09-06 14:00:56 +0800 | [diff] [blame] | 36 | #define IS_SMALL_INT(ival) (-NSMALLNEGINTS <= (ival) && (ival) < NSMALLPOSINTS) |
Sergey Fedoseev | c6734ee | 2019-09-12 19:41:14 +0500 | [diff] [blame] | 37 | #define IS_SMALL_UINT(ival) ((ival) < NSMALLPOSINTS) |
Greg Price | 5e63ab0 | 2019-08-24 10:19:37 -0700 | [diff] [blame] | 38 | |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 39 | static PyObject * |
Mark Dickinson | e441674 | 2009-02-15 15:14:57 +0000 | [diff] [blame] | 40 | get_small_int(sdigit ival) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 41 | { |
animalize | 6b51998 | 2019-09-06 14:00:56 +0800 | [diff] [blame] | 42 | assert(IS_SMALL_INT(ival)); |
Victor Stinner | 630c8df | 2019-12-17 13:02:18 +0100 | [diff] [blame] | 43 | PyThreadState *tstate = _PyThreadState_GET(); |
| 44 | PyObject *v = (PyObject*)tstate->interp->small_ints[ival + NSMALLNEGINTS]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 45 | Py_INCREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 46 | return v; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 47 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 48 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 49 | static PyLongObject * |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 50 | maybe_small_long(PyLongObject *v) |
| 51 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 52 | if (v && Py_ABS(Py_SIZE(v)) <= 1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 53 | sdigit ival = MEDIUM_VALUE(v); |
animalize | 6b51998 | 2019-09-06 14:00:56 +0800 | [diff] [blame] | 54 | if (IS_SMALL_INT(ival)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 55 | Py_DECREF(v); |
| 56 | return (PyLongObject *)get_small_int(ival); |
| 57 | } |
| 58 | } |
| 59 | return v; |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 60 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 61 | #else |
animalize | 6b51998 | 2019-09-06 14:00:56 +0800 | [diff] [blame] | 62 | #define IS_SMALL_INT(ival) 0 |
Sergey Fedoseev | c6734ee | 2019-09-12 19:41:14 +0500 | [diff] [blame] | 63 | #define IS_SMALL_UINT(ival) 0 |
animalize | 6b51998 | 2019-09-06 14:00:56 +0800 | [diff] [blame] | 64 | #define get_small_int(ival) (Py_UNREACHABLE(), NULL) |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 65 | #define maybe_small_long(val) (val) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 66 | #endif |
| 67 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 68 | /* If a freshly-allocated int is already shared, it must |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 69 | be a small integer, so negating it must go to PyLong_FromLong */ |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 70 | Py_LOCAL_INLINE(void) |
| 71 | _PyLong_Negate(PyLongObject **x_p) |
| 72 | { |
| 73 | PyLongObject *x; |
| 74 | |
| 75 | x = (PyLongObject *)*x_p; |
| 76 | if (Py_REFCNT(x) == 1) { |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 77 | Py_SET_SIZE(x, -Py_SIZE(x)); |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 78 | return; |
| 79 | } |
| 80 | |
| 81 | *x_p = (PyLongObject *)PyLong_FromLong(-MEDIUM_VALUE(x)); |
| 82 | Py_DECREF(x); |
| 83 | } |
| 84 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 85 | /* For int multiplication, use the O(N**2) school algorithm unless |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 86 | * both operands contain more than KARATSUBA_CUTOFF digits (this |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 87 | * being an internal Python int digit, in base BASE). |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 88 | */ |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 89 | #define KARATSUBA_CUTOFF 70 |
| 90 | #define KARATSUBA_SQUARE_CUTOFF (2 * KARATSUBA_CUTOFF) |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 91 | |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 92 | /* For exponentiation, use the binary left-to-right algorithm |
| 93 | * unless the exponent contains more than FIVEARY_CUTOFF digits. |
| 94 | * In that case, do 5 bits at a time. The potential drawback is that |
| 95 | * a table of 2**5 intermediate results is computed. |
| 96 | */ |
| 97 | #define FIVEARY_CUTOFF 8 |
| 98 | |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 99 | #define SIGCHECK(PyTryBlock) \ |
| 100 | do { \ |
| 101 | if (PyErr_CheckSignals()) PyTryBlock \ |
| 102 | } while(0) |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 103 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 104 | /* Normalize (remove leading zeros from) an int object. |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 105 | Doesn't attempt to free the storage--in most cases, due to the nature |
| 106 | of the algorithms used, this could save at most be one word anyway. */ |
| 107 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 108 | static PyLongObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 109 | long_normalize(PyLongObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 110 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 111 | Py_ssize_t j = Py_ABS(Py_SIZE(v)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 112 | Py_ssize_t i = j; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 113 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 114 | while (i > 0 && v->ob_digit[i-1] == 0) |
| 115 | --i; |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 116 | if (i != j) { |
| 117 | Py_SET_SIZE(v, (Py_SIZE(v) < 0) ? -(i) : i); |
| 118 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 119 | return v; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 122 | /* _PyLong_FromNbInt: Convert the given object to a PyLongObject |
| 123 | using the nb_int slot, if available. Raise TypeError if either the |
| 124 | nb_int slot is not available or the result of the call to nb_int |
| 125 | returns something not of type int. |
| 126 | */ |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 127 | PyObject * |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 128 | _PyLong_FromNbInt(PyObject *integral) |
| 129 | { |
| 130 | PyNumberMethods *nb; |
| 131 | PyObject *result; |
| 132 | |
| 133 | /* Fast path for the case that we already have an int. */ |
| 134 | if (PyLong_CheckExact(integral)) { |
| 135 | Py_INCREF(integral); |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 136 | return integral; |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | nb = Py_TYPE(integral)->tp_as_number; |
| 140 | if (nb == NULL || nb->nb_int == NULL) { |
| 141 | PyErr_Format(PyExc_TypeError, |
| 142 | "an integer is required (got type %.200s)", |
| 143 | Py_TYPE(integral)->tp_name); |
| 144 | return NULL; |
| 145 | } |
| 146 | |
| 147 | /* Convert using the nb_int slot, which should return something |
| 148 | of exact type int. */ |
| 149 | result = nb->nb_int(integral); |
| 150 | if (!result || PyLong_CheckExact(result)) |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 151 | return result; |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 152 | if (!PyLong_Check(result)) { |
| 153 | PyErr_Format(PyExc_TypeError, |
| 154 | "__int__ returned non-int (type %.200s)", |
Victor Stinner | 58ac700 | 2020-02-07 03:04:21 +0100 | [diff] [blame] | 155 | Py_TYPE(result)->tp_name); |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 156 | Py_DECREF(result); |
| 157 | return NULL; |
| 158 | } |
| 159 | /* Issue #17576: warn if 'result' not of exact type int. */ |
| 160 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 161 | "__int__ returned non-int (type %.200s). " |
| 162 | "The ability to return an instance of a strict subclass of int " |
| 163 | "is deprecated, and may be removed in a future version of Python.", |
Victor Stinner | 58ac700 | 2020-02-07 03:04:21 +0100 | [diff] [blame] | 164 | Py_TYPE(result)->tp_name)) { |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 165 | Py_DECREF(result); |
| 166 | return NULL; |
| 167 | } |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 168 | return result; |
| 169 | } |
| 170 | |
| 171 | /* Convert the given object to a PyLongObject using the nb_index or |
| 172 | nb_int slots, if available (the latter is deprecated). |
| 173 | Raise TypeError if either nb_index and nb_int slots are not |
| 174 | available or the result of the call to nb_index or nb_int |
| 175 | returns something not of type int. |
| 176 | Should be replaced with PyNumber_Index after the end of the |
| 177 | deprecation period. |
| 178 | */ |
| 179 | PyObject * |
| 180 | _PyLong_FromNbIndexOrNbInt(PyObject *integral) |
| 181 | { |
| 182 | PyNumberMethods *nb; |
| 183 | PyObject *result; |
| 184 | |
| 185 | /* Fast path for the case that we already have an int. */ |
| 186 | if (PyLong_CheckExact(integral)) { |
| 187 | Py_INCREF(integral); |
| 188 | return integral; |
| 189 | } |
| 190 | |
| 191 | nb = Py_TYPE(integral)->tp_as_number; |
| 192 | if (nb == NULL || (nb->nb_index == NULL && nb->nb_int == NULL)) { |
| 193 | PyErr_Format(PyExc_TypeError, |
| 194 | "an integer is required (got type %.200s)", |
| 195 | Py_TYPE(integral)->tp_name); |
| 196 | return NULL; |
| 197 | } |
| 198 | |
| 199 | if (nb->nb_index) { |
| 200 | /* Convert using the nb_index slot, which should return something |
| 201 | of exact type int. */ |
| 202 | result = nb->nb_index(integral); |
| 203 | if (!result || PyLong_CheckExact(result)) |
| 204 | return result; |
| 205 | if (!PyLong_Check(result)) { |
| 206 | PyErr_Format(PyExc_TypeError, |
| 207 | "__index__ returned non-int (type %.200s)", |
Victor Stinner | 58ac700 | 2020-02-07 03:04:21 +0100 | [diff] [blame] | 208 | Py_TYPE(result)->tp_name); |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 209 | Py_DECREF(result); |
| 210 | return NULL; |
| 211 | } |
| 212 | /* Issue #17576: warn if 'result' not of exact type int. */ |
| 213 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 214 | "__index__ returned non-int (type %.200s). " |
| 215 | "The ability to return an instance of a strict subclass of int " |
| 216 | "is deprecated, and may be removed in a future version of Python.", |
Victor Stinner | 58ac700 | 2020-02-07 03:04:21 +0100 | [diff] [blame] | 217 | Py_TYPE(result)->tp_name)) |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 218 | { |
| 219 | Py_DECREF(result); |
| 220 | return NULL; |
| 221 | } |
| 222 | return result; |
| 223 | } |
| 224 | |
| 225 | result = _PyLong_FromNbInt(integral); |
| 226 | if (result && PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 227 | "an integer is required (got type %.200s). " |
| 228 | "Implicit conversion to integers using __int__ is deprecated, " |
| 229 | "and may be removed in a future version of Python.", |
| 230 | Py_TYPE(integral)->tp_name)) |
| 231 | { |
| 232 | Py_DECREF(result); |
| 233 | return NULL; |
| 234 | } |
| 235 | return result; |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 239 | /* Allocate a new int object with size digits. |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 240 | Return NULL and set exception if we run out of memory. */ |
| 241 | |
Mark Dickinson | 5a74bf6 | 2009-02-15 11:04:38 +0000 | [diff] [blame] | 242 | #define MAX_LONG_DIGITS \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 243 | ((PY_SSIZE_T_MAX - offsetof(PyLongObject, ob_digit))/sizeof(digit)) |
Mark Dickinson | 5a74bf6 | 2009-02-15 11:04:38 +0000 | [diff] [blame] | 244 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 245 | PyLongObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 246 | _PyLong_New(Py_ssize_t size) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 247 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 248 | PyLongObject *result; |
| 249 | /* Number of bytes needed is: offsetof(PyLongObject, ob_digit) + |
| 250 | sizeof(digit)*size. Previous incarnations of this code used |
| 251 | sizeof(PyVarObject) instead of the offsetof, but this risks being |
| 252 | incorrect in the presence of padding between the PyVarObject header |
| 253 | and the digits. */ |
| 254 | if (size > (Py_ssize_t)MAX_LONG_DIGITS) { |
| 255 | PyErr_SetString(PyExc_OverflowError, |
| 256 | "too many digits in integer"); |
| 257 | return NULL; |
| 258 | } |
| 259 | result = PyObject_MALLOC(offsetof(PyLongObject, ob_digit) + |
| 260 | size*sizeof(digit)); |
| 261 | if (!result) { |
| 262 | PyErr_NoMemory(); |
| 263 | return NULL; |
| 264 | } |
Victor Stinner | b509d52 | 2018-11-23 14:27:38 +0100 | [diff] [blame] | 265 | return (PyLongObject*)PyObject_INIT_VAR(result, &PyLong_Type, size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 268 | PyObject * |
| 269 | _PyLong_Copy(PyLongObject *src) |
| 270 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 271 | PyLongObject *result; |
| 272 | Py_ssize_t i; |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 273 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 274 | assert(src != NULL); |
| 275 | i = Py_SIZE(src); |
| 276 | if (i < 0) |
| 277 | i = -(i); |
| 278 | if (i < 2) { |
Mark Dickinson | bcc17ee | 2012-04-20 21:42:49 +0100 | [diff] [blame] | 279 | sdigit ival = MEDIUM_VALUE(src); |
animalize | 6b51998 | 2019-09-06 14:00:56 +0800 | [diff] [blame] | 280 | if (IS_SMALL_INT(ival)) { |
Greg Price | 5e63ab0 | 2019-08-24 10:19:37 -0700 | [diff] [blame] | 281 | return get_small_int(ival); |
| 282 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 283 | } |
| 284 | result = _PyLong_New(i); |
| 285 | if (result != NULL) { |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 286 | Py_SET_SIZE(result, Py_SIZE(src)); |
| 287 | while (--i >= 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 288 | result->ob_digit[i] = src->ob_digit[i]; |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 289 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 290 | } |
| 291 | return (PyObject *)result; |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 294 | /* Create a new int object from a C long int */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 295 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 296 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 297 | PyLong_FromLong(long ival) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 298 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 299 | PyLongObject *v; |
| 300 | unsigned long abs_ival; |
| 301 | unsigned long t; /* unsigned so >> doesn't propagate sign bit */ |
| 302 | int ndigits = 0; |
Mark Dickinson | 36820dd | 2016-09-10 20:17:36 +0100 | [diff] [blame] | 303 | int sign; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 304 | |
animalize | 6b51998 | 2019-09-06 14:00:56 +0800 | [diff] [blame] | 305 | if (IS_SMALL_INT(ival)) { |
Greg Price | 5e63ab0 | 2019-08-24 10:19:37 -0700 | [diff] [blame] | 306 | return get_small_int((sdigit)ival); |
| 307 | } |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 308 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 309 | if (ival < 0) { |
| 310 | /* negate: can't write this as abs_ival = -ival since that |
| 311 | invokes undefined behaviour when ival is LONG_MIN */ |
| 312 | abs_ival = 0U-(unsigned long)ival; |
| 313 | sign = -1; |
| 314 | } |
| 315 | else { |
| 316 | abs_ival = (unsigned long)ival; |
Mark Dickinson | 36820dd | 2016-09-10 20:17:36 +0100 | [diff] [blame] | 317 | sign = ival == 0 ? 0 : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 318 | } |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 319 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 320 | /* Fast path for single-digit ints */ |
| 321 | if (!(abs_ival >> PyLong_SHIFT)) { |
| 322 | v = _PyLong_New(1); |
| 323 | if (v) { |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 324 | Py_SET_SIZE(v, sign); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 325 | v->ob_digit[0] = Py_SAFE_DOWNCAST( |
| 326 | abs_ival, unsigned long, digit); |
| 327 | } |
| 328 | return (PyObject*)v; |
| 329 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 330 | |
Mark Dickinson | 249b898 | 2009-04-27 19:41:00 +0000 | [diff] [blame] | 331 | #if PyLong_SHIFT==15 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 332 | /* 2 digits */ |
| 333 | if (!(abs_ival >> 2*PyLong_SHIFT)) { |
| 334 | v = _PyLong_New(2); |
| 335 | if (v) { |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 336 | Py_SET_SIZE(v, 2 * sign); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 337 | v->ob_digit[0] = Py_SAFE_DOWNCAST( |
| 338 | abs_ival & PyLong_MASK, unsigned long, digit); |
| 339 | v->ob_digit[1] = Py_SAFE_DOWNCAST( |
| 340 | abs_ival >> PyLong_SHIFT, unsigned long, digit); |
| 341 | } |
| 342 | return (PyObject*)v; |
| 343 | } |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 344 | #endif |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 345 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 346 | /* Larger numbers: loop to determine number of digits */ |
| 347 | t = abs_ival; |
| 348 | while (t) { |
| 349 | ++ndigits; |
| 350 | t >>= PyLong_SHIFT; |
| 351 | } |
| 352 | v = _PyLong_New(ndigits); |
| 353 | if (v != NULL) { |
| 354 | digit *p = v->ob_digit; |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 355 | Py_SET_SIZE(v, ndigits * sign); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 356 | t = abs_ival; |
| 357 | while (t) { |
| 358 | *p++ = Py_SAFE_DOWNCAST( |
| 359 | t & PyLong_MASK, unsigned long, digit); |
| 360 | t >>= PyLong_SHIFT; |
| 361 | } |
| 362 | } |
| 363 | return (PyObject *)v; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Sergey Fedoseev | c6734ee | 2019-09-12 19:41:14 +0500 | [diff] [blame] | 366 | #define PYLONG_FROM_UINT(INT_TYPE, ival) \ |
| 367 | do { \ |
| 368 | if (IS_SMALL_UINT(ival)) { \ |
Victor Stinner | 6314abc | 2019-10-01 13:29:53 +0200 | [diff] [blame] | 369 | return get_small_int((sdigit)(ival)); \ |
Sergey Fedoseev | c6734ee | 2019-09-12 19:41:14 +0500 | [diff] [blame] | 370 | } \ |
| 371 | /* Count the number of Python digits. */ \ |
| 372 | Py_ssize_t ndigits = 0; \ |
| 373 | INT_TYPE t = (ival); \ |
| 374 | while (t) { \ |
| 375 | ++ndigits; \ |
| 376 | t >>= PyLong_SHIFT; \ |
| 377 | } \ |
| 378 | PyLongObject *v = _PyLong_New(ndigits); \ |
| 379 | if (v == NULL) { \ |
| 380 | return NULL; \ |
| 381 | } \ |
| 382 | digit *p = v->ob_digit; \ |
| 383 | while ((ival)) { \ |
| 384 | *p++ = (digit)((ival) & PyLong_MASK); \ |
| 385 | (ival) >>= PyLong_SHIFT; \ |
| 386 | } \ |
| 387 | return (PyObject *)v; \ |
| 388 | } while(0) |
| 389 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 390 | /* Create a new int object from a C unsigned long int */ |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 391 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 392 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 393 | PyLong_FromUnsignedLong(unsigned long ival) |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 394 | { |
Sergey Fedoseev | c6734ee | 2019-09-12 19:41:14 +0500 | [diff] [blame] | 395 | PYLONG_FROM_UINT(unsigned long, ival); |
| 396 | } |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 397 | |
Sergey Fedoseev | c6734ee | 2019-09-12 19:41:14 +0500 | [diff] [blame] | 398 | /* Create a new int object from a C unsigned long long int. */ |
| 399 | |
| 400 | PyObject * |
| 401 | PyLong_FromUnsignedLongLong(unsigned long long ival) |
| 402 | { |
| 403 | PYLONG_FROM_UINT(unsigned long long, ival); |
| 404 | } |
| 405 | |
| 406 | /* Create a new int object from a C size_t. */ |
| 407 | |
| 408 | PyObject * |
| 409 | PyLong_FromSize_t(size_t ival) |
| 410 | { |
| 411 | PYLONG_FROM_UINT(size_t, ival); |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 414 | /* Create a new int object from a C double */ |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 415 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 416 | PyObject * |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 417 | PyLong_FromDouble(double dval) |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 418 | { |
Sergey Fedoseev | 86a93fd | 2020-05-10 14:15:57 +0500 | [diff] [blame^] | 419 | /* Try to get out cheap if this fits in a long. When a finite value of real |
| 420 | * floating type is converted to an integer type, the value is truncated |
| 421 | * toward zero. If the value of the integral part cannot be represented by |
| 422 | * the integer type, the behavior is undefined. Thus, we must check that |
| 423 | * value is in range (LONG_MIN - 1, LONG_MAX + 1). If a long has more bits |
| 424 | * of precision than a double, casting LONG_MIN - 1 to double may yield an |
| 425 | * approximation, but LONG_MAX + 1 is a power of two and can be represented |
| 426 | * as double exactly (assuming FLT_RADIX is 2 or 16), so for simplicity |
| 427 | * check against [-(LONG_MAX + 1), LONG_MAX + 1). |
| 428 | */ |
| 429 | const double int_max = (unsigned long)LONG_MAX + 1; |
| 430 | if (-int_max < dval && dval < int_max) { |
| 431 | return PyLong_FromLong((long)dval); |
| 432 | } |
| 433 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 434 | PyLongObject *v; |
| 435 | double frac; |
| 436 | int i, ndig, expo, neg; |
| 437 | neg = 0; |
| 438 | if (Py_IS_INFINITY(dval)) { |
| 439 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 440 | "cannot convert float infinity to integer"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 441 | return NULL; |
| 442 | } |
| 443 | if (Py_IS_NAN(dval)) { |
| 444 | PyErr_SetString(PyExc_ValueError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 445 | "cannot convert float NaN to integer"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 446 | return NULL; |
| 447 | } |
| 448 | if (dval < 0.0) { |
| 449 | neg = 1; |
| 450 | dval = -dval; |
| 451 | } |
| 452 | frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */ |
Sergey Fedoseev | 86a93fd | 2020-05-10 14:15:57 +0500 | [diff] [blame^] | 453 | assert(expo > 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 454 | ndig = (expo-1) / PyLong_SHIFT + 1; /* Number of 'digits' in result */ |
| 455 | v = _PyLong_New(ndig); |
| 456 | if (v == NULL) |
| 457 | return NULL; |
| 458 | frac = ldexp(frac, (expo-1) % PyLong_SHIFT + 1); |
| 459 | for (i = ndig; --i >= 0; ) { |
| 460 | digit bits = (digit)frac; |
| 461 | v->ob_digit[i] = bits; |
| 462 | frac = frac - (double)bits; |
| 463 | frac = ldexp(frac, PyLong_SHIFT); |
| 464 | } |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 465 | if (neg) { |
| 466 | Py_SET_SIZE(v, -(Py_SIZE(v))); |
| 467 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 468 | return (PyObject *)v; |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 471 | /* Checking for overflow in PyLong_AsLong is a PITA since C doesn't define |
| 472 | * anything about what happens when a signed integer operation overflows, |
| 473 | * 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] | 474 | * then. The bit pattern for the largest positive signed long is |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 475 | * (unsigned long)LONG_MAX, and for the smallest negative signed long |
| 476 | * it is abs(LONG_MIN), which we could write -(unsigned long)LONG_MIN. |
| 477 | * However, some other compilers warn about applying unary minus to an |
| 478 | * unsigned operand. Hence the weird "0-". |
| 479 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 480 | #define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN) |
| 481 | #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] | 482 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 483 | /* 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] | 484 | method. |
| 485 | |
| 486 | On overflow, return -1 and set *overflow to 1 or -1 depending on the sign of |
| 487 | the result. Otherwise *overflow is 0. |
| 488 | |
| 489 | For other errors (e.g., TypeError), return -1 and set an error condition. |
| 490 | In this case *overflow will be 0. |
| 491 | */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 492 | |
| 493 | long |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 494 | PyLong_AsLongAndOverflow(PyObject *vv, int *overflow) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 495 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 496 | /* This version by Tim Peters */ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 497 | PyLongObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 498 | unsigned long x, prev; |
| 499 | long res; |
| 500 | Py_ssize_t i; |
| 501 | int sign; |
| 502 | int do_decref = 0; /* if nb_int was called */ |
Guido van Rossum | f753181 | 1998-05-26 14:33:37 +0000 | [diff] [blame] | 503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 504 | *overflow = 0; |
| 505 | if (vv == NULL) { |
| 506 | PyErr_BadInternalCall(); |
| 507 | return -1; |
| 508 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 509 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 510 | if (PyLong_Check(vv)) { |
| 511 | v = (PyLongObject *)vv; |
| 512 | } |
| 513 | else { |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 514 | v = (PyLongObject *)_PyLong_FromNbIndexOrNbInt(vv); |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 515 | if (v == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 516 | return -1; |
| 517 | do_decref = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 518 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 519 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 520 | res = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 521 | i = Py_SIZE(v); |
Guido van Rossum | f753181 | 1998-05-26 14:33:37 +0000 | [diff] [blame] | 522 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 523 | switch (i) { |
| 524 | case -1: |
| 525 | res = -(sdigit)v->ob_digit[0]; |
| 526 | break; |
| 527 | case 0: |
| 528 | res = 0; |
| 529 | break; |
| 530 | case 1: |
| 531 | res = v->ob_digit[0]; |
| 532 | break; |
| 533 | default: |
| 534 | sign = 1; |
| 535 | x = 0; |
| 536 | if (i < 0) { |
| 537 | sign = -1; |
| 538 | i = -(i); |
| 539 | } |
| 540 | while (--i >= 0) { |
| 541 | prev = x; |
| 542 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 543 | if ((x >> PyLong_SHIFT) != prev) { |
| 544 | *overflow = sign; |
| 545 | goto exit; |
| 546 | } |
| 547 | } |
| 548 | /* Haven't lost any bits, but casting to long requires extra |
| 549 | * care (see comment above). |
| 550 | */ |
| 551 | if (x <= (unsigned long)LONG_MAX) { |
| 552 | res = (long)x * sign; |
| 553 | } |
| 554 | else if (sign < 0 && x == PY_ABS_LONG_MIN) { |
| 555 | res = LONG_MIN; |
| 556 | } |
| 557 | else { |
| 558 | *overflow = sign; |
| 559 | /* res is already set to -1 */ |
| 560 | } |
| 561 | } |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 562 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 563 | if (do_decref) { |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 564 | Py_DECREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 565 | } |
| 566 | return res; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 569 | /* 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] | 570 | method. Return -1 and set an error if overflow occurs. */ |
| 571 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 572 | long |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 573 | PyLong_AsLong(PyObject *obj) |
| 574 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 575 | int overflow; |
| 576 | long result = PyLong_AsLongAndOverflow(obj, &overflow); |
| 577 | if (overflow) { |
| 578 | /* XXX: could be cute and give a different |
| 579 | message for overflow == -1 */ |
| 580 | PyErr_SetString(PyExc_OverflowError, |
| 581 | "Python int too large to convert to C long"); |
| 582 | } |
| 583 | return result; |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 586 | /* 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] | 587 | method. Return -1 and set an error if overflow occurs. */ |
| 588 | |
| 589 | int |
| 590 | _PyLong_AsInt(PyObject *obj) |
| 591 | { |
| 592 | int overflow; |
| 593 | long result = PyLong_AsLongAndOverflow(obj, &overflow); |
| 594 | if (overflow || result > INT_MAX || result < INT_MIN) { |
| 595 | /* XXX: could be cute and give a different |
| 596 | message for overflow == -1 */ |
| 597 | PyErr_SetString(PyExc_OverflowError, |
| 598 | "Python int too large to convert to C int"); |
| 599 | return -1; |
| 600 | } |
| 601 | return (int)result; |
| 602 | } |
| 603 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 604 | /* Get a Py_ssize_t from an int object. |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 605 | Returns -1 and sets an error condition if overflow occurs. */ |
| 606 | |
| 607 | Py_ssize_t |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 608 | PyLong_AsSsize_t(PyObject *vv) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 609 | PyLongObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 610 | size_t x, prev; |
| 611 | Py_ssize_t i; |
| 612 | int sign; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 613 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 614 | if (vv == NULL) { |
| 615 | PyErr_BadInternalCall(); |
| 616 | return -1; |
| 617 | } |
| 618 | if (!PyLong_Check(vv)) { |
| 619 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 620 | return -1; |
| 621 | } |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 622 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 623 | v = (PyLongObject *)vv; |
| 624 | i = Py_SIZE(v); |
| 625 | switch (i) { |
| 626 | case -1: return -(sdigit)v->ob_digit[0]; |
| 627 | case 0: return 0; |
| 628 | case 1: return v->ob_digit[0]; |
| 629 | } |
| 630 | sign = 1; |
| 631 | x = 0; |
| 632 | if (i < 0) { |
| 633 | sign = -1; |
| 634 | i = -(i); |
| 635 | } |
| 636 | while (--i >= 0) { |
| 637 | prev = x; |
| 638 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 639 | if ((x >> PyLong_SHIFT) != prev) |
| 640 | goto overflow; |
| 641 | } |
| 642 | /* Haven't lost any bits, but casting to a signed type requires |
| 643 | * extra care (see comment above). |
| 644 | */ |
| 645 | if (x <= (size_t)PY_SSIZE_T_MAX) { |
| 646 | return (Py_ssize_t)x * sign; |
| 647 | } |
| 648 | else if (sign < 0 && x == PY_ABS_SSIZE_T_MIN) { |
| 649 | return PY_SSIZE_T_MIN; |
| 650 | } |
| 651 | /* else overflow */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 652 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 653 | overflow: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 654 | PyErr_SetString(PyExc_OverflowError, |
| 655 | "Python int too large to convert to C ssize_t"); |
| 656 | return -1; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 659 | /* Get a C unsigned long int from an int object. |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 660 | Returns -1 and sets an error condition if overflow occurs. */ |
| 661 | |
| 662 | unsigned long |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 663 | PyLong_AsUnsignedLong(PyObject *vv) |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 664 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 665 | PyLongObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 666 | unsigned long x, prev; |
| 667 | Py_ssize_t i; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 668 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 669 | if (vv == NULL) { |
| 670 | PyErr_BadInternalCall(); |
| 671 | return (unsigned long)-1; |
| 672 | } |
| 673 | if (!PyLong_Check(vv)) { |
| 674 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 675 | return (unsigned long)-1; |
| 676 | } |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 677 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 678 | v = (PyLongObject *)vv; |
| 679 | i = Py_SIZE(v); |
| 680 | x = 0; |
| 681 | if (i < 0) { |
| 682 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 683 | "can't convert negative value to unsigned int"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 684 | return (unsigned long) -1; |
| 685 | } |
| 686 | switch (i) { |
| 687 | case 0: return 0; |
| 688 | case 1: return v->ob_digit[0]; |
| 689 | } |
| 690 | while (--i >= 0) { |
| 691 | prev = x; |
| 692 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 693 | if ((x >> PyLong_SHIFT) != prev) { |
| 694 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 02515f7 | 2013-08-03 12:08:22 +0100 | [diff] [blame] | 695 | "Python int too large to convert " |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 696 | "to C unsigned long"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 697 | return (unsigned long) -1; |
| 698 | } |
| 699 | } |
| 700 | return x; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 703 | /* 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] | 704 | an error condition if overflow occurs. */ |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 705 | |
| 706 | size_t |
| 707 | PyLong_AsSize_t(PyObject *vv) |
| 708 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 709 | PyLongObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 710 | size_t x, prev; |
| 711 | Py_ssize_t i; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 712 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 713 | if (vv == NULL) { |
| 714 | PyErr_BadInternalCall(); |
| 715 | return (size_t) -1; |
| 716 | } |
| 717 | if (!PyLong_Check(vv)) { |
| 718 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 719 | return (size_t)-1; |
| 720 | } |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 721 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 722 | v = (PyLongObject *)vv; |
| 723 | i = Py_SIZE(v); |
| 724 | x = 0; |
| 725 | if (i < 0) { |
| 726 | PyErr_SetString(PyExc_OverflowError, |
| 727 | "can't convert negative value to size_t"); |
| 728 | return (size_t) -1; |
| 729 | } |
| 730 | switch (i) { |
| 731 | case 0: return 0; |
| 732 | case 1: return v->ob_digit[0]; |
| 733 | } |
| 734 | while (--i >= 0) { |
| 735 | prev = x; |
| 736 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 737 | if ((x >> PyLong_SHIFT) != prev) { |
| 738 | PyErr_SetString(PyExc_OverflowError, |
| 739 | "Python int too large to convert to C size_t"); |
Stefan Krah | b77c6c6 | 2011-09-12 16:22:47 +0200 | [diff] [blame] | 740 | return (size_t) -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 741 | } |
| 742 | } |
| 743 | return x; |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 746 | /* 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] | 747 | Returns -1 and sets an error condition if an error occurs. */ |
| 748 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 749 | static unsigned long |
| 750 | _PyLong_AsUnsignedLongMask(PyObject *vv) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 751 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 752 | PyLongObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 753 | unsigned long x; |
| 754 | Py_ssize_t i; |
| 755 | int sign; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 756 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 757 | if (vv == NULL || !PyLong_Check(vv)) { |
| 758 | PyErr_BadInternalCall(); |
| 759 | return (unsigned long) -1; |
| 760 | } |
| 761 | v = (PyLongObject *)vv; |
| 762 | i = Py_SIZE(v); |
| 763 | switch (i) { |
| 764 | case 0: return 0; |
| 765 | case 1: return v->ob_digit[0]; |
| 766 | } |
| 767 | sign = 1; |
| 768 | x = 0; |
| 769 | if (i < 0) { |
| 770 | sign = -1; |
| 771 | i = -i; |
| 772 | } |
| 773 | while (--i >= 0) { |
| 774 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 775 | } |
| 776 | return x * sign; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 779 | unsigned long |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 780 | PyLong_AsUnsignedLongMask(PyObject *op) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 781 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 782 | PyLongObject *lo; |
| 783 | unsigned long val; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 784 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 785 | if (op == NULL) { |
| 786 | PyErr_BadInternalCall(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 787 | return (unsigned long)-1; |
| 788 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 789 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 790 | if (PyLong_Check(op)) { |
| 791 | return _PyLong_AsUnsignedLongMask(op); |
| 792 | } |
| 793 | |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 794 | lo = (PyLongObject *)_PyLong_FromNbIndexOrNbInt(op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 795 | if (lo == NULL) |
| 796 | return (unsigned long)-1; |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 797 | |
| 798 | val = _PyLong_AsUnsignedLongMask((PyObject *)lo); |
| 799 | Py_DECREF(lo); |
| 800 | return val; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 803 | int |
| 804 | _PyLong_Sign(PyObject *vv) |
| 805 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 806 | PyLongObject *v = (PyLongObject *)vv; |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 808 | assert(v != NULL); |
| 809 | assert(PyLong_Check(v)); |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 810 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 811 | return Py_SIZE(v) == 0 ? 0 : (Py_SIZE(v) < 0 ? -1 : 1); |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 814 | size_t |
| 815 | _PyLong_NumBits(PyObject *vv) |
| 816 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 817 | PyLongObject *v = (PyLongObject *)vv; |
| 818 | size_t result = 0; |
| 819 | Py_ssize_t ndigits; |
Serhiy Storchaka | b2e64f9 | 2016-11-08 20:34:22 +0200 | [diff] [blame] | 820 | int msd_bits; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 821 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 822 | assert(v != NULL); |
| 823 | assert(PyLong_Check(v)); |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 824 | ndigits = Py_ABS(Py_SIZE(v)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 825 | assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0); |
| 826 | if (ndigits > 0) { |
| 827 | digit msd = v->ob_digit[ndigits - 1]; |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 828 | if ((size_t)(ndigits - 1) > SIZE_MAX / (size_t)PyLong_SHIFT) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 829 | goto Overflow; |
Mark Dickinson | fc9adb6 | 2012-10-06 18:50:02 +0100 | [diff] [blame] | 830 | result = (size_t)(ndigits - 1) * (size_t)PyLong_SHIFT; |
Niklas Fiekas | c5b7900 | 2020-01-16 15:09:19 +0100 | [diff] [blame] | 831 | msd_bits = _Py_bit_length(msd); |
Serhiy Storchaka | b2e64f9 | 2016-11-08 20:34:22 +0200 | [diff] [blame] | 832 | if (SIZE_MAX - msd_bits < result) |
| 833 | goto Overflow; |
| 834 | result += msd_bits; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 835 | } |
| 836 | return result; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 837 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 838 | Overflow: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 839 | PyErr_SetString(PyExc_OverflowError, "int has too many bits " |
| 840 | "to express in a platform size_t"); |
| 841 | return (size_t)-1; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 842 | } |
| 843 | |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 844 | PyObject * |
| 845 | _PyLong_FromByteArray(const unsigned char* bytes, size_t n, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 846 | int little_endian, int is_signed) |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 847 | { |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 848 | const unsigned char* pstartbyte; /* LSB of bytes */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 849 | int incr; /* direction to move pstartbyte */ |
| 850 | const unsigned char* pendbyte; /* MSB of bytes */ |
| 851 | size_t numsignificantbytes; /* number of bytes that matter */ |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 852 | Py_ssize_t ndigits; /* number of Python int digits */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 853 | PyLongObject* v; /* result */ |
| 854 | Py_ssize_t idigit = 0; /* next free index in v->ob_digit */ |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 855 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 856 | if (n == 0) |
| 857 | return PyLong_FromLong(0L); |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 858 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 859 | if (little_endian) { |
| 860 | pstartbyte = bytes; |
| 861 | pendbyte = bytes + n - 1; |
| 862 | incr = 1; |
| 863 | } |
| 864 | else { |
| 865 | pstartbyte = bytes + n - 1; |
| 866 | pendbyte = bytes; |
| 867 | incr = -1; |
| 868 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 869 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 870 | if (is_signed) |
| 871 | is_signed = *pendbyte >= 0x80; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 872 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 873 | /* Compute numsignificantbytes. This consists of finding the most |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 874 | significant byte. Leading 0 bytes are insignificant if the number |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 875 | is positive, and leading 0xff bytes if negative. */ |
| 876 | { |
| 877 | size_t i; |
| 878 | const unsigned char* p = pendbyte; |
| 879 | const int pincr = -incr; /* search MSB to LSB */ |
Martin Panter | eb99570 | 2016-07-28 01:11:04 +0000 | [diff] [blame] | 880 | const unsigned char insignificant = is_signed ? 0xff : 0x00; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 881 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 882 | for (i = 0; i < n; ++i, p += pincr) { |
Martin Panter | eb99570 | 2016-07-28 01:11:04 +0000 | [diff] [blame] | 883 | if (*p != insignificant) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 884 | break; |
| 885 | } |
| 886 | numsignificantbytes = n - i; |
| 887 | /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so |
| 888 | actually has 2 significant bytes. OTOH, 0xff0001 == |
| 889 | -0x00ffff, so we wouldn't *need* to bump it there; but we |
| 890 | do for 0xffff = -0x0001. To be safe without bothering to |
| 891 | check every case, bump it regardless. */ |
| 892 | if (is_signed && numsignificantbytes < n) |
| 893 | ++numsignificantbytes; |
| 894 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 895 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 896 | /* How many Python int digits do we need? We have |
| 897 | 8*numsignificantbytes bits, and each Python int digit has |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 898 | PyLong_SHIFT bits, so it's the ceiling of the quotient. */ |
| 899 | /* catch overflow before it happens */ |
| 900 | if (numsignificantbytes > (PY_SSIZE_T_MAX - PyLong_SHIFT) / 8) { |
| 901 | PyErr_SetString(PyExc_OverflowError, |
| 902 | "byte array too long to convert to int"); |
| 903 | return NULL; |
| 904 | } |
| 905 | ndigits = (numsignificantbytes * 8 + PyLong_SHIFT - 1) / PyLong_SHIFT; |
| 906 | v = _PyLong_New(ndigits); |
| 907 | if (v == NULL) |
| 908 | return NULL; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 909 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 910 | /* Copy the bits over. The tricky parts are computing 2's-comp on |
| 911 | the fly for signed numbers, and dealing with the mismatch between |
| 912 | 8-bit bytes and (probably) 15-bit Python digits.*/ |
| 913 | { |
| 914 | size_t i; |
| 915 | twodigits carry = 1; /* for 2's-comp calculation */ |
| 916 | twodigits accum = 0; /* sliding register */ |
| 917 | unsigned int accumbits = 0; /* number of bits in accum */ |
| 918 | const unsigned char* p = pstartbyte; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 919 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 920 | for (i = 0; i < numsignificantbytes; ++i, p += incr) { |
| 921 | twodigits thisbyte = *p; |
| 922 | /* Compute correction for 2's comp, if needed. */ |
| 923 | if (is_signed) { |
| 924 | thisbyte = (0xff ^ thisbyte) + carry; |
| 925 | carry = thisbyte >> 8; |
| 926 | thisbyte &= 0xff; |
| 927 | } |
| 928 | /* Because we're going LSB to MSB, thisbyte is |
| 929 | more significant than what's already in accum, |
| 930 | so needs to be prepended to accum. */ |
orenmn | 86aa269 | 2017-03-06 10:42:47 +0200 | [diff] [blame] | 931 | accum |= thisbyte << accumbits; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 932 | accumbits += 8; |
| 933 | if (accumbits >= PyLong_SHIFT) { |
| 934 | /* There's enough to fill a Python digit. */ |
| 935 | assert(idigit < ndigits); |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 936 | v->ob_digit[idigit] = (digit)(accum & PyLong_MASK); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 937 | ++idigit; |
| 938 | accum >>= PyLong_SHIFT; |
| 939 | accumbits -= PyLong_SHIFT; |
| 940 | assert(accumbits < PyLong_SHIFT); |
| 941 | } |
| 942 | } |
| 943 | assert(accumbits < PyLong_SHIFT); |
| 944 | if (accumbits) { |
| 945 | assert(idigit < ndigits); |
| 946 | v->ob_digit[idigit] = (digit)accum; |
| 947 | ++idigit; |
| 948 | } |
| 949 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 950 | |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 951 | Py_SET_SIZE(v, is_signed ? -idigit : idigit); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 952 | return (PyObject *)long_normalize(v); |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | int |
| 956 | _PyLong_AsByteArray(PyLongObject* v, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 957 | unsigned char* bytes, size_t n, |
| 958 | int little_endian, int is_signed) |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 959 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 960 | Py_ssize_t i; /* index into v->ob_digit */ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 961 | Py_ssize_t ndigits; /* |v->ob_size| */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 962 | twodigits accum; /* sliding register */ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 963 | unsigned int accumbits; /* # bits in accum */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 964 | int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */ |
| 965 | digit carry; /* for computing 2's-comp */ |
| 966 | size_t j; /* # bytes filled */ |
| 967 | unsigned char* p; /* pointer to next byte in bytes */ |
| 968 | int pincr; /* direction to move p */ |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 969 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 970 | assert(v != NULL && PyLong_Check(v)); |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 971 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 972 | if (Py_SIZE(v) < 0) { |
| 973 | ndigits = -(Py_SIZE(v)); |
| 974 | if (!is_signed) { |
| 975 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 976 | "can't convert negative int to unsigned"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 977 | return -1; |
| 978 | } |
| 979 | do_twos_comp = 1; |
| 980 | } |
| 981 | else { |
| 982 | ndigits = Py_SIZE(v); |
| 983 | do_twos_comp = 0; |
| 984 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 985 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 986 | if (little_endian) { |
| 987 | p = bytes; |
| 988 | pincr = 1; |
| 989 | } |
| 990 | else { |
| 991 | p = bytes + n - 1; |
| 992 | pincr = -1; |
| 993 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 994 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 995 | /* Copy over all the Python digits. |
| 996 | It's crucial that every Python digit except for the MSD contribute |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 997 | 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] | 998 | normalized. */ |
| 999 | assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0); |
| 1000 | j = 0; |
| 1001 | accum = 0; |
| 1002 | accumbits = 0; |
| 1003 | carry = do_twos_comp ? 1 : 0; |
| 1004 | for (i = 0; i < ndigits; ++i) { |
| 1005 | digit thisdigit = v->ob_digit[i]; |
| 1006 | if (do_twos_comp) { |
| 1007 | thisdigit = (thisdigit ^ PyLong_MASK) + carry; |
| 1008 | carry = thisdigit >> PyLong_SHIFT; |
| 1009 | thisdigit &= PyLong_MASK; |
| 1010 | } |
| 1011 | /* Because we're going LSB to MSB, thisdigit is more |
| 1012 | significant than what's already in accum, so needs to be |
| 1013 | prepended to accum. */ |
| 1014 | accum |= (twodigits)thisdigit << accumbits; |
Tim Peters | 8bc84b4 | 2001-06-12 19:17:03 +0000 | [diff] [blame] | 1015 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1016 | /* The most-significant digit may be (probably is) at least |
| 1017 | partly empty. */ |
| 1018 | if (i == ndigits - 1) { |
| 1019 | /* Count # of sign bits -- they needn't be stored, |
| 1020 | * although for signed conversion we need later to |
| 1021 | * make sure at least one sign bit gets stored. */ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 1022 | digit s = do_twos_comp ? thisdigit ^ PyLong_MASK : thisdigit; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1023 | while (s != 0) { |
| 1024 | s >>= 1; |
| 1025 | accumbits++; |
| 1026 | } |
| 1027 | } |
| 1028 | else |
| 1029 | accumbits += PyLong_SHIFT; |
Tim Peters | 8bc84b4 | 2001-06-12 19:17:03 +0000 | [diff] [blame] | 1030 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1031 | /* Store as many bytes as possible. */ |
| 1032 | while (accumbits >= 8) { |
| 1033 | if (j >= n) |
| 1034 | goto Overflow; |
| 1035 | ++j; |
| 1036 | *p = (unsigned char)(accum & 0xff); |
| 1037 | p += pincr; |
| 1038 | accumbits -= 8; |
| 1039 | accum >>= 8; |
| 1040 | } |
| 1041 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 1042 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1043 | /* Store the straggler (if any). */ |
| 1044 | assert(accumbits < 8); |
| 1045 | assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */ |
| 1046 | if (accumbits > 0) { |
| 1047 | if (j >= n) |
| 1048 | goto Overflow; |
| 1049 | ++j; |
| 1050 | if (do_twos_comp) { |
| 1051 | /* Fill leading bits of the byte with sign bits |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1052 | (appropriately pretending that the int had an |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1053 | infinite supply of sign bits). */ |
| 1054 | accum |= (~(twodigits)0) << accumbits; |
| 1055 | } |
| 1056 | *p = (unsigned char)(accum & 0xff); |
| 1057 | p += pincr; |
| 1058 | } |
| 1059 | else if (j == n && n > 0 && is_signed) { |
| 1060 | /* The main loop filled the byte array exactly, so the code |
| 1061 | just above didn't get to ensure there's a sign bit, and the |
| 1062 | loop below wouldn't add one either. Make sure a sign bit |
| 1063 | exists. */ |
| 1064 | unsigned char msb = *(p - pincr); |
| 1065 | int sign_bit_set = msb >= 0x80; |
| 1066 | assert(accumbits == 0); |
| 1067 | if (sign_bit_set == do_twos_comp) |
| 1068 | return 0; |
| 1069 | else |
| 1070 | goto Overflow; |
| 1071 | } |
Tim Peters | 05607ad | 2001-06-13 21:01:27 +0000 | [diff] [blame] | 1072 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1073 | /* Fill remaining bytes with copies of the sign bit. */ |
| 1074 | { |
| 1075 | unsigned char signbyte = do_twos_comp ? 0xffU : 0U; |
| 1076 | for ( ; j < n; ++j, p += pincr) |
| 1077 | *p = signbyte; |
| 1078 | } |
Tim Peters | 05607ad | 2001-06-13 21:01:27 +0000 | [diff] [blame] | 1079 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1080 | return 0; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 1081 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 1082 | Overflow: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1083 | PyErr_SetString(PyExc_OverflowError, "int too big to convert"); |
| 1084 | return -1; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1085 | |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1088 | /* Create a new int object from a C pointer */ |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1089 | |
| 1090 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1091 | PyLong_FromVoidPtr(void *p) |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1092 | { |
Mark Dickinson | 9104479 | 2012-10-18 19:21:43 +0100 | [diff] [blame] | 1093 | #if SIZEOF_VOID_P <= SIZEOF_LONG |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 1094 | return PyLong_FromUnsignedLong((unsigned long)(uintptr_t)p); |
Mark Dickinson | 9104479 | 2012-10-18 19:21:43 +0100 | [diff] [blame] | 1095 | #else |
| 1096 | |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1097 | #if SIZEOF_LONG_LONG < SIZEOF_VOID_P |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1098 | # error "PyLong_FromVoidPtr: sizeof(long long) < sizeof(void*)" |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1099 | #endif |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 1100 | return PyLong_FromUnsignedLongLong((unsigned long long)(uintptr_t)p); |
Mark Dickinson | 9104479 | 2012-10-18 19:21:43 +0100 | [diff] [blame] | 1101 | #endif /* SIZEOF_VOID_P <= SIZEOF_LONG */ |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1102 | |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1105 | /* Get a C pointer from an int object. */ |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1106 | |
| 1107 | void * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1108 | PyLong_AsVoidPtr(PyObject *vv) |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1109 | { |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1110 | #if SIZEOF_VOID_P <= SIZEOF_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1111 | long x; |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1112 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1113 | if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0) |
| 1114 | x = PyLong_AsLong(vv); |
| 1115 | else |
| 1116 | x = PyLong_AsUnsignedLong(vv); |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1117 | #else |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1118 | |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1119 | #if SIZEOF_LONG_LONG < SIZEOF_VOID_P |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1120 | # error "PyLong_AsVoidPtr: sizeof(long long) < sizeof(void*)" |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1121 | #endif |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1122 | long long x; |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1123 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1124 | if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0) |
| 1125 | x = PyLong_AsLongLong(vv); |
| 1126 | else |
| 1127 | x = PyLong_AsUnsignedLongLong(vv); |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1128 | |
| 1129 | #endif /* SIZEOF_VOID_P <= SIZEOF_LONG */ |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1130 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1131 | if (x == -1 && PyErr_Occurred()) |
| 1132 | return NULL; |
| 1133 | return (void *)x; |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1136 | /* Initial long long support by Chris Herborth (chrish@qnx.com), later |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1137 | * rewritten to use the newer PyLong_{As,From}ByteArray API. |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1138 | */ |
| 1139 | |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 1140 | #define PY_ABS_LLONG_MIN (0-(unsigned long long)LLONG_MIN) |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1141 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1142 | /* Create a new int object from a C long long int. */ |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1143 | |
| 1144 | PyObject * |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1145 | PyLong_FromLongLong(long long ival) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1146 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1147 | PyLongObject *v; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1148 | unsigned long long abs_ival; |
| 1149 | unsigned long long t; /* unsigned so >> doesn't propagate sign bit */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1150 | int ndigits = 0; |
| 1151 | int negative = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1152 | |
animalize | 6b51998 | 2019-09-06 14:00:56 +0800 | [diff] [blame] | 1153 | if (IS_SMALL_INT(ival)) { |
Greg Price | 5e63ab0 | 2019-08-24 10:19:37 -0700 | [diff] [blame] | 1154 | return get_small_int((sdigit)ival); |
| 1155 | } |
| 1156 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1157 | if (ival < 0) { |
| 1158 | /* avoid signed overflow on negation; see comments |
| 1159 | in PyLong_FromLong above. */ |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1160 | abs_ival = (unsigned long long)(-1-ival) + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1161 | negative = 1; |
| 1162 | } |
| 1163 | else { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1164 | abs_ival = (unsigned long long)ival; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1165 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1166 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1167 | /* Count the number of Python digits. |
| 1168 | We used to pick 5 ("big enough for anything"), but that's a |
| 1169 | waste of time and space given that 5*15 = 75 bits are rarely |
| 1170 | needed. */ |
| 1171 | t = abs_ival; |
| 1172 | while (t) { |
| 1173 | ++ndigits; |
| 1174 | t >>= PyLong_SHIFT; |
| 1175 | } |
| 1176 | v = _PyLong_New(ndigits); |
| 1177 | if (v != NULL) { |
| 1178 | digit *p = v->ob_digit; |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 1179 | Py_SET_SIZE(v, negative ? -ndigits : ndigits); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1180 | t = abs_ival; |
| 1181 | while (t) { |
| 1182 | *p++ = (digit)(t & PyLong_MASK); |
| 1183 | t >>= PyLong_SHIFT; |
| 1184 | } |
| 1185 | } |
| 1186 | return (PyObject *)v; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1189 | /* 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] | 1190 | |
| 1191 | PyObject * |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1192 | PyLong_FromSsize_t(Py_ssize_t ival) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1193 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1194 | PyLongObject *v; |
| 1195 | size_t abs_ival; |
| 1196 | size_t t; /* unsigned so >> doesn't propagate sign bit */ |
| 1197 | int ndigits = 0; |
| 1198 | int negative = 0; |
Mark Dickinson | 7ab6be2 | 2008-04-15 21:42:42 +0000 | [diff] [blame] | 1199 | |
animalize | 6b51998 | 2019-09-06 14:00:56 +0800 | [diff] [blame] | 1200 | if (IS_SMALL_INT(ival)) { |
Greg Price | 5e63ab0 | 2019-08-24 10:19:37 -0700 | [diff] [blame] | 1201 | return get_small_int((sdigit)ival); |
| 1202 | } |
| 1203 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1204 | if (ival < 0) { |
| 1205 | /* avoid signed overflow when ival = SIZE_T_MIN */ |
| 1206 | abs_ival = (size_t)(-1-ival)+1; |
| 1207 | negative = 1; |
| 1208 | } |
| 1209 | else { |
| 1210 | abs_ival = (size_t)ival; |
| 1211 | } |
Mark Dickinson | 7ab6be2 | 2008-04-15 21:42:42 +0000 | [diff] [blame] | 1212 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1213 | /* Count the number of Python digits. */ |
| 1214 | t = abs_ival; |
| 1215 | while (t) { |
| 1216 | ++ndigits; |
| 1217 | t >>= PyLong_SHIFT; |
| 1218 | } |
| 1219 | v = _PyLong_New(ndigits); |
| 1220 | if (v != NULL) { |
| 1221 | digit *p = v->ob_digit; |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 1222 | Py_SET_SIZE(v, negative ? -ndigits : ndigits); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1223 | t = abs_ival; |
| 1224 | while (t) { |
| 1225 | *p++ = (digit)(t & PyLong_MASK); |
| 1226 | t >>= PyLong_SHIFT; |
| 1227 | } |
| 1228 | } |
| 1229 | return (PyObject *)v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1232 | /* 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] | 1233 | __int__ method. Return -1 and set an error if overflow occurs. */ |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1234 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1235 | long long |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1236 | PyLong_AsLongLong(PyObject *vv) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1237 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1238 | PyLongObject *v; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1239 | long long bytes; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1240 | int res; |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1241 | int do_decref = 0; /* if nb_int was called */ |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1242 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1243 | if (vv == NULL) { |
| 1244 | PyErr_BadInternalCall(); |
| 1245 | return -1; |
| 1246 | } |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1247 | |
| 1248 | if (PyLong_Check(vv)) { |
| 1249 | v = (PyLongObject *)vv; |
| 1250 | } |
| 1251 | else { |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 1252 | v = (PyLongObject *)_PyLong_FromNbIndexOrNbInt(vv); |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1253 | if (v == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1254 | return -1; |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1255 | do_decref = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1256 | } |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1257 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1258 | res = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1259 | switch(Py_SIZE(v)) { |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1260 | case -1: |
| 1261 | bytes = -(sdigit)v->ob_digit[0]; |
| 1262 | break; |
| 1263 | case 0: |
| 1264 | bytes = 0; |
| 1265 | break; |
| 1266 | case 1: |
| 1267 | bytes = v->ob_digit[0]; |
| 1268 | break; |
| 1269 | default: |
| 1270 | res = _PyLong_AsByteArray((PyLongObject *)v, (unsigned char *)&bytes, |
Serhiy Storchaka | c4f3212 | 2013-12-11 21:26:36 +0200 | [diff] [blame] | 1271 | SIZEOF_LONG_LONG, PY_LITTLE_ENDIAN, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1272 | } |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1273 | if (do_decref) { |
| 1274 | Py_DECREF(v); |
| 1275 | } |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1276 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1277 | /* Plan 9 can't handle long long in ? : expressions */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1278 | if (res < 0) |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1279 | return (long long)-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1280 | else |
| 1281 | return bytes; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1284 | /* Get a C unsigned long long int from an int object. |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1285 | Return -1 and set an error if overflow occurs. */ |
| 1286 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1287 | unsigned long long |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1288 | PyLong_AsUnsignedLongLong(PyObject *vv) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1289 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1290 | PyLongObject *v; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1291 | unsigned long long bytes; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1292 | int res; |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1293 | |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 1294 | if (vv == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1295 | PyErr_BadInternalCall(); |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1296 | return (unsigned long long)-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1297 | } |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 1298 | if (!PyLong_Check(vv)) { |
| 1299 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1300 | return (unsigned long long)-1; |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 1301 | } |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1303 | v = (PyLongObject*)vv; |
| 1304 | switch(Py_SIZE(v)) { |
| 1305 | case 0: return 0; |
| 1306 | case 1: return v->ob_digit[0]; |
| 1307 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1308 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 1309 | res = _PyLong_AsByteArray((PyLongObject *)vv, (unsigned char *)&bytes, |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 1310 | SIZEOF_LONG_LONG, PY_LITTLE_ENDIAN, 0); |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1311 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1312 | /* Plan 9 can't handle long long in ? : expressions */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1313 | if (res < 0) |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1314 | return (unsigned long long)res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1315 | else |
| 1316 | return bytes; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1317 | } |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1318 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1319 | /* 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] | 1320 | Returns -1 and sets an error condition if an error occurs. */ |
| 1321 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1322 | static unsigned long long |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1323 | _PyLong_AsUnsignedLongLongMask(PyObject *vv) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1324 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1325 | PyLongObject *v; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1326 | unsigned long long x; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1327 | Py_ssize_t i; |
| 1328 | int sign; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1329 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1330 | if (vv == NULL || !PyLong_Check(vv)) { |
| 1331 | PyErr_BadInternalCall(); |
Zackery Spytz | dc24765 | 2019-06-06 14:39:23 -0600 | [diff] [blame] | 1332 | return (unsigned long long) -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1333 | } |
| 1334 | v = (PyLongObject *)vv; |
| 1335 | switch(Py_SIZE(v)) { |
| 1336 | case 0: return 0; |
| 1337 | case 1: return v->ob_digit[0]; |
| 1338 | } |
| 1339 | i = Py_SIZE(v); |
| 1340 | sign = 1; |
| 1341 | x = 0; |
| 1342 | if (i < 0) { |
| 1343 | sign = -1; |
| 1344 | i = -i; |
| 1345 | } |
| 1346 | while (--i >= 0) { |
| 1347 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 1348 | } |
| 1349 | return x * sign; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1350 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1351 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1352 | unsigned long long |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1353 | PyLong_AsUnsignedLongLongMask(PyObject *op) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1354 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1355 | PyLongObject *lo; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1356 | unsigned long long val; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1357 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1358 | if (op == NULL) { |
| 1359 | PyErr_BadInternalCall(); |
Zackery Spytz | dc24765 | 2019-06-06 14:39:23 -0600 | [diff] [blame] | 1360 | return (unsigned long long)-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1361 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1362 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1363 | if (PyLong_Check(op)) { |
| 1364 | return _PyLong_AsUnsignedLongLongMask(op); |
| 1365 | } |
| 1366 | |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 1367 | lo = (PyLongObject *)_PyLong_FromNbIndexOrNbInt(op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1368 | if (lo == NULL) |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1369 | return (unsigned long long)-1; |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1370 | |
| 1371 | val = _PyLong_AsUnsignedLongLongMask((PyObject *)lo); |
| 1372 | Py_DECREF(lo); |
| 1373 | return val; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1374 | } |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1375 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1376 | /* 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] | 1377 | __int__ method. |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1378 | |
Mark Dickinson | 8d48b43 | 2011-10-23 20:47:14 +0100 | [diff] [blame] | 1379 | On overflow, return -1 and set *overflow to 1 or -1 depending on the sign of |
| 1380 | the result. Otherwise *overflow is 0. |
| 1381 | |
| 1382 | For other errors (e.g., TypeError), return -1 and set an error condition. |
| 1383 | In this case *overflow will be 0. |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1384 | */ |
| 1385 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1386 | long long |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1387 | PyLong_AsLongLongAndOverflow(PyObject *vv, int *overflow) |
| 1388 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1389 | /* This version by Tim Peters */ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1390 | PyLongObject *v; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1391 | unsigned long long x, prev; |
| 1392 | long long res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1393 | Py_ssize_t i; |
| 1394 | int sign; |
| 1395 | int do_decref = 0; /* if nb_int was called */ |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1396 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1397 | *overflow = 0; |
| 1398 | if (vv == NULL) { |
| 1399 | PyErr_BadInternalCall(); |
| 1400 | return -1; |
| 1401 | } |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1402 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1403 | if (PyLong_Check(vv)) { |
| 1404 | v = (PyLongObject *)vv; |
| 1405 | } |
| 1406 | else { |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 1407 | v = (PyLongObject *)_PyLong_FromNbIndexOrNbInt(vv); |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1408 | if (v == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1409 | return -1; |
| 1410 | do_decref = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1411 | } |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1412 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1413 | res = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1414 | i = Py_SIZE(v); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1415 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1416 | switch (i) { |
| 1417 | case -1: |
| 1418 | res = -(sdigit)v->ob_digit[0]; |
| 1419 | break; |
| 1420 | case 0: |
| 1421 | res = 0; |
| 1422 | break; |
| 1423 | case 1: |
| 1424 | res = v->ob_digit[0]; |
| 1425 | break; |
| 1426 | default: |
| 1427 | sign = 1; |
| 1428 | x = 0; |
| 1429 | if (i < 0) { |
| 1430 | sign = -1; |
| 1431 | i = -(i); |
| 1432 | } |
| 1433 | while (--i >= 0) { |
| 1434 | prev = x; |
| 1435 | x = (x << PyLong_SHIFT) + v->ob_digit[i]; |
| 1436 | if ((x >> PyLong_SHIFT) != prev) { |
| 1437 | *overflow = sign; |
| 1438 | goto exit; |
| 1439 | } |
| 1440 | } |
| 1441 | /* Haven't lost any bits, but casting to long requires extra |
| 1442 | * care (see comment above). |
| 1443 | */ |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 1444 | if (x <= (unsigned long long)LLONG_MAX) { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1445 | res = (long long)x * sign; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1446 | } |
| 1447 | else if (sign < 0 && x == PY_ABS_LLONG_MIN) { |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 1448 | res = LLONG_MIN; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1449 | } |
| 1450 | else { |
| 1451 | *overflow = sign; |
| 1452 | /* res is already set to -1 */ |
| 1453 | } |
| 1454 | } |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 1455 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1456 | if (do_decref) { |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1457 | Py_DECREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1458 | } |
| 1459 | return res; |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Serhiy Storchaka | 7cb7bcf | 2018-07-26 13:22:16 +0300 | [diff] [blame] | 1462 | int |
| 1463 | _PyLong_UnsignedShort_Converter(PyObject *obj, void *ptr) |
| 1464 | { |
| 1465 | unsigned long uval; |
| 1466 | |
| 1467 | if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) { |
| 1468 | PyErr_SetString(PyExc_ValueError, "value must be positive"); |
| 1469 | return 0; |
| 1470 | } |
| 1471 | uval = PyLong_AsUnsignedLong(obj); |
| 1472 | if (uval == (unsigned long)-1 && PyErr_Occurred()) |
| 1473 | return 0; |
| 1474 | if (uval > USHRT_MAX) { |
| 1475 | PyErr_SetString(PyExc_OverflowError, |
| 1476 | "Python int too large for C unsigned short"); |
| 1477 | return 0; |
| 1478 | } |
| 1479 | |
| 1480 | *(unsigned short *)ptr = Py_SAFE_DOWNCAST(uval, unsigned long, unsigned short); |
| 1481 | return 1; |
| 1482 | } |
| 1483 | |
| 1484 | int |
| 1485 | _PyLong_UnsignedInt_Converter(PyObject *obj, void *ptr) |
| 1486 | { |
| 1487 | unsigned long uval; |
| 1488 | |
| 1489 | if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) { |
| 1490 | PyErr_SetString(PyExc_ValueError, "value must be positive"); |
| 1491 | return 0; |
| 1492 | } |
| 1493 | uval = PyLong_AsUnsignedLong(obj); |
| 1494 | if (uval == (unsigned long)-1 && PyErr_Occurred()) |
| 1495 | return 0; |
| 1496 | if (uval > UINT_MAX) { |
| 1497 | PyErr_SetString(PyExc_OverflowError, |
| 1498 | "Python int too large for C unsigned int"); |
| 1499 | return 0; |
| 1500 | } |
| 1501 | |
| 1502 | *(unsigned int *)ptr = Py_SAFE_DOWNCAST(uval, unsigned long, unsigned int); |
| 1503 | return 1; |
| 1504 | } |
| 1505 | |
| 1506 | int |
| 1507 | _PyLong_UnsignedLong_Converter(PyObject *obj, void *ptr) |
| 1508 | { |
| 1509 | unsigned long uval; |
| 1510 | |
| 1511 | if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) { |
| 1512 | PyErr_SetString(PyExc_ValueError, "value must be positive"); |
| 1513 | return 0; |
| 1514 | } |
| 1515 | uval = PyLong_AsUnsignedLong(obj); |
| 1516 | if (uval == (unsigned long)-1 && PyErr_Occurred()) |
| 1517 | return 0; |
| 1518 | |
| 1519 | *(unsigned long *)ptr = uval; |
| 1520 | return 1; |
| 1521 | } |
| 1522 | |
| 1523 | int |
| 1524 | _PyLong_UnsignedLongLong_Converter(PyObject *obj, void *ptr) |
| 1525 | { |
| 1526 | unsigned long long uval; |
| 1527 | |
| 1528 | if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) { |
| 1529 | PyErr_SetString(PyExc_ValueError, "value must be positive"); |
| 1530 | return 0; |
| 1531 | } |
| 1532 | uval = PyLong_AsUnsignedLongLong(obj); |
| 1533 | if (uval == (unsigned long long)-1 && PyErr_Occurred()) |
| 1534 | return 0; |
| 1535 | |
| 1536 | *(unsigned long long *)ptr = uval; |
| 1537 | return 1; |
| 1538 | } |
| 1539 | |
| 1540 | int |
| 1541 | _PyLong_Size_t_Converter(PyObject *obj, void *ptr) |
| 1542 | { |
| 1543 | size_t uval; |
| 1544 | |
| 1545 | if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) { |
| 1546 | PyErr_SetString(PyExc_ValueError, "value must be positive"); |
| 1547 | return 0; |
| 1548 | } |
| 1549 | uval = PyLong_AsSize_t(obj); |
| 1550 | if (uval == (size_t)-1 && PyErr_Occurred()) |
| 1551 | return 0; |
| 1552 | |
| 1553 | *(size_t *)ptr = uval; |
| 1554 | return 1; |
| 1555 | } |
| 1556 | |
| 1557 | |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 1558 | #define CHECK_BINOP(v,w) \ |
| 1559 | do { \ |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1560 | if (!PyLong_Check(v) || !PyLong_Check(w)) \ |
| 1561 | Py_RETURN_NOTIMPLEMENTED; \ |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 1562 | } while(0) |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1563 | |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1564 | /* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n] |
| 1565 | * is modified in place, by adding y to it. Carries are propagated as far as |
| 1566 | * x[m-1], and the remaining carry (0 or 1) is returned. |
| 1567 | */ |
| 1568 | static digit |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1569 | 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] | 1570 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1571 | Py_ssize_t i; |
| 1572 | digit carry = 0; |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1573 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1574 | assert(m >= n); |
| 1575 | for (i = 0; i < n; ++i) { |
| 1576 | carry += x[i] + y[i]; |
| 1577 | x[i] = carry & PyLong_MASK; |
| 1578 | carry >>= PyLong_SHIFT; |
| 1579 | assert((carry & 1) == carry); |
| 1580 | } |
| 1581 | for (; carry && i < m; ++i) { |
| 1582 | carry += x[i]; |
| 1583 | x[i] = carry & PyLong_MASK; |
| 1584 | carry >>= PyLong_SHIFT; |
| 1585 | assert((carry & 1) == carry); |
| 1586 | } |
| 1587 | return carry; |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | /* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n] |
| 1591 | * is modified in place, by subtracting y from it. Borrows are propagated as |
| 1592 | * far as x[m-1], and the remaining borrow (0 or 1) is returned. |
| 1593 | */ |
| 1594 | static digit |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1595 | 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] | 1596 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1597 | Py_ssize_t i; |
| 1598 | digit borrow = 0; |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1599 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1600 | assert(m >= n); |
| 1601 | for (i = 0; i < n; ++i) { |
| 1602 | borrow = x[i] - y[i] - borrow; |
| 1603 | x[i] = borrow & PyLong_MASK; |
| 1604 | borrow >>= PyLong_SHIFT; |
| 1605 | borrow &= 1; /* keep only 1 sign bit */ |
| 1606 | } |
| 1607 | for (; borrow && i < m; ++i) { |
| 1608 | borrow = x[i] - borrow; |
| 1609 | x[i] = borrow & PyLong_MASK; |
| 1610 | borrow >>= PyLong_SHIFT; |
| 1611 | borrow &= 1; |
| 1612 | } |
| 1613 | return borrow; |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1614 | } |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1615 | |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 1616 | /* Shift digit vector a[0:m] d bits left, with 0 <= d < PyLong_SHIFT. Put |
| 1617 | * result in z[0:m], and return the d bits shifted out of the top. |
| 1618 | */ |
| 1619 | static digit |
| 1620 | 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] | 1621 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1622 | Py_ssize_t i; |
| 1623 | digit carry = 0; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1624 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1625 | assert(0 <= d && d < PyLong_SHIFT); |
| 1626 | for (i=0; i < m; i++) { |
| 1627 | twodigits acc = (twodigits)a[i] << d | carry; |
| 1628 | z[i] = (digit)acc & PyLong_MASK; |
| 1629 | carry = (digit)(acc >> PyLong_SHIFT); |
| 1630 | } |
| 1631 | return carry; |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | /* Shift digit vector a[0:m] d bits right, with 0 <= d < PyLong_SHIFT. Put |
| 1635 | * result in z[0:m], and return the d bits shifted out of the bottom. |
| 1636 | */ |
| 1637 | static digit |
| 1638 | v_rshift(digit *z, digit *a, Py_ssize_t m, int d) |
| 1639 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1640 | Py_ssize_t i; |
| 1641 | digit carry = 0; |
| 1642 | digit mask = ((digit)1 << d) - 1U; |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 1643 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1644 | assert(0 <= d && d < PyLong_SHIFT); |
| 1645 | for (i=m; i-- > 0;) { |
| 1646 | twodigits acc = (twodigits)carry << PyLong_SHIFT | a[i]; |
| 1647 | carry = (digit)acc & mask; |
| 1648 | z[i] = (digit)(acc >> d); |
| 1649 | } |
| 1650 | return carry; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1653 | /* Divide long pin, w/ size digits, by non-zero digit n, storing quotient |
| 1654 | in pout, and returning the remainder. pin and pout point at the LSD. |
| 1655 | 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] | 1656 | _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] | 1657 | immutable. */ |
| 1658 | |
| 1659 | static digit |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1660 | inplace_divrem1(digit *pout, digit *pin, Py_ssize_t size, digit n) |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1661 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1662 | twodigits rem = 0; |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1663 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1664 | assert(n > 0 && n <= PyLong_MASK); |
| 1665 | pin += size; |
| 1666 | pout += size; |
| 1667 | while (--size >= 0) { |
| 1668 | digit hi; |
| 1669 | rem = (rem << PyLong_SHIFT) | *--pin; |
| 1670 | *--pout = hi = (digit)(rem / n); |
| 1671 | rem -= (twodigits)hi * n; |
| 1672 | } |
| 1673 | return (digit)rem; |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1676 | /* Divide an integer by a digit, returning both the quotient |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1677 | (as function result) and the remainder (through *prem). |
| 1678 | The sign of a is ignored; n should not be zero. */ |
| 1679 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1680 | static PyLongObject * |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1681 | divrem1(PyLongObject *a, digit n, digit *prem) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1682 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 1683 | const Py_ssize_t size = Py_ABS(Py_SIZE(a)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1684 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1685 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1686 | assert(n > 0 && n <= PyLong_MASK); |
| 1687 | z = _PyLong_New(size); |
| 1688 | if (z == NULL) |
| 1689 | return NULL; |
| 1690 | *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n); |
| 1691 | return long_normalize(z); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1694 | /* 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] | 1695 | string. (Return value is non-shared so that callers can modify the |
| 1696 | returned value if necessary.) */ |
| 1697 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1698 | static int |
| 1699 | long_to_decimal_string_internal(PyObject *aa, |
| 1700 | PyObject **p_output, |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1701 | _PyUnicodeWriter *writer, |
| 1702 | _PyBytesWriter *bytes_writer, |
| 1703 | char **bytes_str) |
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 | PyLongObject *scratch, *a; |
Victor Stinner | 1285e5c | 2015-10-14 12:10:20 +0200 | [diff] [blame] | 1706 | PyObject *str = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1707 | Py_ssize_t size, strlen, size_a, i, j; |
| 1708 | digit *pout, *pin, rem, tenpow; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1709 | int negative; |
Mark Dickinson | 4e1de16 | 2016-08-29 17:26:43 +0100 | [diff] [blame] | 1710 | int d; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1711 | enum PyUnicode_Kind kind; |
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 | a = (PyLongObject *)aa; |
| 1714 | if (a == NULL || !PyLong_Check(a)) { |
| 1715 | PyErr_BadInternalCall(); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1716 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1717 | } |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 1718 | size_a = Py_ABS(Py_SIZE(a)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1719 | negative = Py_SIZE(a) < 0; |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1720 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1721 | /* quick and dirty upper bound for the number of digits |
| 1722 | required to express a in base _PyLong_DECIMAL_BASE: |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1723 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1724 | #digits = 1 + floor(log2(a) / log2(_PyLong_DECIMAL_BASE)) |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1725 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1726 | But log2(a) < size_a * PyLong_SHIFT, and |
| 1727 | log2(_PyLong_DECIMAL_BASE) = log2(10) * _PyLong_DECIMAL_SHIFT |
Mark Dickinson | 4e1de16 | 2016-08-29 17:26:43 +0100 | [diff] [blame] | 1728 | > 3.3 * _PyLong_DECIMAL_SHIFT |
| 1729 | |
| 1730 | size_a * PyLong_SHIFT / (3.3 * _PyLong_DECIMAL_SHIFT) = |
| 1731 | size_a + size_a / d < size_a + size_a / floor(d), |
| 1732 | where d = (3.3 * _PyLong_DECIMAL_SHIFT) / |
| 1733 | (PyLong_SHIFT - 3.3 * _PyLong_DECIMAL_SHIFT) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1734 | */ |
Mark Dickinson | 4e1de16 | 2016-08-29 17:26:43 +0100 | [diff] [blame] | 1735 | d = (33 * _PyLong_DECIMAL_SHIFT) / |
| 1736 | (10 * PyLong_SHIFT - 33 * _PyLong_DECIMAL_SHIFT); |
| 1737 | assert(size_a < PY_SSIZE_T_MAX/2); |
| 1738 | size = 1 + size_a + size_a / d; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1739 | scratch = _PyLong_New(size); |
| 1740 | if (scratch == NULL) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1741 | return -1; |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1742 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1743 | /* convert array of base _PyLong_BASE digits in pin to an array of |
| 1744 | base _PyLong_DECIMAL_BASE digits in pout, following Knuth (TAOCP, |
| 1745 | Volume 2 (3rd edn), section 4.4, Method 1b). */ |
| 1746 | pin = a->ob_digit; |
| 1747 | pout = scratch->ob_digit; |
| 1748 | size = 0; |
| 1749 | for (i = size_a; --i >= 0; ) { |
| 1750 | digit hi = pin[i]; |
| 1751 | for (j = 0; j < size; j++) { |
| 1752 | twodigits z = (twodigits)pout[j] << PyLong_SHIFT | hi; |
| 1753 | hi = (digit)(z / _PyLong_DECIMAL_BASE); |
| 1754 | pout[j] = (digit)(z - (twodigits)hi * |
| 1755 | _PyLong_DECIMAL_BASE); |
| 1756 | } |
| 1757 | while (hi) { |
| 1758 | pout[size++] = hi % _PyLong_DECIMAL_BASE; |
| 1759 | hi /= _PyLong_DECIMAL_BASE; |
| 1760 | } |
| 1761 | /* check for keyboard interrupt */ |
| 1762 | SIGCHECK({ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 1763 | Py_DECREF(scratch); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1764 | return -1; |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 1765 | }); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1766 | } |
| 1767 | /* pout should have at least one digit, so that the case when a = 0 |
| 1768 | works correctly */ |
| 1769 | if (size == 0) |
| 1770 | pout[size++] = 0; |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1771 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1772 | /* calculate exact length of output string, and allocate */ |
| 1773 | strlen = negative + 1 + (size - 1) * _PyLong_DECIMAL_SHIFT; |
| 1774 | tenpow = 10; |
| 1775 | rem = pout[size-1]; |
| 1776 | while (rem >= tenpow) { |
| 1777 | tenpow *= 10; |
| 1778 | strlen++; |
| 1779 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1780 | if (writer) { |
Christian Heimes | 110ac16 | 2012-09-10 02:51:27 +0200 | [diff] [blame] | 1781 | if (_PyUnicodeWriter_Prepare(writer, strlen, '9') == -1) { |
| 1782 | Py_DECREF(scratch); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1783 | return -1; |
Christian Heimes | 110ac16 | 2012-09-10 02:51:27 +0200 | [diff] [blame] | 1784 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1785 | kind = writer->kind; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1786 | } |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1787 | else if (bytes_writer) { |
| 1788 | *bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, strlen); |
| 1789 | if (*bytes_str == NULL) { |
| 1790 | Py_DECREF(scratch); |
| 1791 | return -1; |
| 1792 | } |
| 1793 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1794 | else { |
| 1795 | str = PyUnicode_New(strlen, '9'); |
| 1796 | if (str == NULL) { |
| 1797 | Py_DECREF(scratch); |
| 1798 | return -1; |
| 1799 | } |
| 1800 | kind = PyUnicode_KIND(str); |
| 1801 | } |
| 1802 | |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1803 | #define WRITE_DIGITS(p) \ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1804 | do { \ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1805 | /* pout[0] through pout[size-2] contribute exactly \ |
| 1806 | _PyLong_DECIMAL_SHIFT digits each */ \ |
| 1807 | for (i=0; i < size - 1; i++) { \ |
| 1808 | rem = pout[i]; \ |
| 1809 | for (j = 0; j < _PyLong_DECIMAL_SHIFT; j++) { \ |
| 1810 | *--p = '0' + rem % 10; \ |
| 1811 | rem /= 10; \ |
| 1812 | } \ |
| 1813 | } \ |
| 1814 | /* pout[size-1]: always produce at least one decimal digit */ \ |
| 1815 | rem = pout[i]; \ |
| 1816 | do { \ |
| 1817 | *--p = '0' + rem % 10; \ |
| 1818 | rem /= 10; \ |
| 1819 | } while (rem != 0); \ |
| 1820 | \ |
| 1821 | /* and sign */ \ |
| 1822 | if (negative) \ |
| 1823 | *--p = '-'; \ |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1824 | } while (0) |
| 1825 | |
| 1826 | #define WRITE_UNICODE_DIGITS(TYPE) \ |
| 1827 | do { \ |
| 1828 | if (writer) \ |
| 1829 | p = (TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos + strlen; \ |
| 1830 | else \ |
| 1831 | p = (TYPE*)PyUnicode_DATA(str) + strlen; \ |
| 1832 | \ |
| 1833 | WRITE_DIGITS(p); \ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1834 | \ |
| 1835 | /* check we've counted correctly */ \ |
| 1836 | if (writer) \ |
| 1837 | assert(p == ((TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos)); \ |
| 1838 | else \ |
| 1839 | assert(p == (TYPE*)PyUnicode_DATA(str)); \ |
| 1840 | } while (0) |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1841 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1842 | /* fill the string right-to-left */ |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1843 | if (bytes_writer) { |
| 1844 | char *p = *bytes_str + strlen; |
| 1845 | WRITE_DIGITS(p); |
| 1846 | assert(p == *bytes_str); |
| 1847 | } |
| 1848 | else if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1849 | Py_UCS1 *p; |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1850 | WRITE_UNICODE_DIGITS(Py_UCS1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1851 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1852 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 1853 | Py_UCS2 *p; |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1854 | WRITE_UNICODE_DIGITS(Py_UCS2); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1855 | } |
| 1856 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1857 | Py_UCS4 *p; |
Victor Stinner | e577ab3 | 2012-05-29 18:51:10 +0200 | [diff] [blame] | 1858 | assert (kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1859 | WRITE_UNICODE_DIGITS(Py_UCS4); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1860 | } |
| 1861 | #undef WRITE_DIGITS |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1862 | #undef WRITE_UNICODE_DIGITS |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1863 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1864 | Py_DECREF(scratch); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1865 | if (writer) { |
| 1866 | writer->pos += strlen; |
| 1867 | } |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1868 | else if (bytes_writer) { |
| 1869 | (*bytes_str) += strlen; |
| 1870 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1871 | else { |
| 1872 | assert(_PyUnicode_CheckConsistency(str, 1)); |
| 1873 | *p_output = (PyObject *)str; |
| 1874 | } |
| 1875 | return 0; |
| 1876 | } |
| 1877 | |
| 1878 | static PyObject * |
| 1879 | long_to_decimal_string(PyObject *aa) |
| 1880 | { |
| 1881 | PyObject *v; |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1882 | if (long_to_decimal_string_internal(aa, &v, NULL, NULL, NULL) == -1) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1883 | return NULL; |
| 1884 | return v; |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1887 | /* Convert an int object to a string, using a given conversion base, |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1888 | which should be one of 2, 8 or 16. Return a string object. |
| 1889 | If base is 2, 8 or 16, add the proper prefix '0b', '0o' or '0x' |
| 1890 | if alternate is nonzero. */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1891 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1892 | static int |
| 1893 | long_format_binary(PyObject *aa, int base, int alternate, |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1894 | PyObject **p_output, _PyUnicodeWriter *writer, |
| 1895 | _PyBytesWriter *bytes_writer, char **bytes_str) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1896 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1897 | PyLongObject *a = (PyLongObject *)aa; |
Victor Stinner | 1285e5c | 2015-10-14 12:10:20 +0200 | [diff] [blame] | 1898 | PyObject *v = NULL; |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1899 | Py_ssize_t sz; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1900 | Py_ssize_t size_a; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1901 | enum PyUnicode_Kind kind; |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1902 | int negative; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1903 | int bits; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1904 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1905 | assert(base == 2 || base == 8 || base == 16); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1906 | if (a == NULL || !PyLong_Check(a)) { |
| 1907 | PyErr_BadInternalCall(); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1908 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1909 | } |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 1910 | size_a = Py_ABS(Py_SIZE(a)); |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1911 | negative = Py_SIZE(a) < 0; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1912 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1913 | /* Compute a rough upper bound for the length of the string */ |
| 1914 | switch (base) { |
| 1915 | case 16: |
| 1916 | bits = 4; |
| 1917 | break; |
| 1918 | case 8: |
| 1919 | bits = 3; |
| 1920 | break; |
| 1921 | case 2: |
| 1922 | bits = 1; |
| 1923 | break; |
| 1924 | default: |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 1925 | Py_UNREACHABLE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1926 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1927 | |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1928 | /* Compute exact length 'sz' of output string. */ |
| 1929 | if (size_a == 0) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1930 | sz = 1; |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1931 | } |
| 1932 | else { |
| 1933 | Py_ssize_t size_a_in_bits; |
| 1934 | /* Ensure overflow doesn't occur during computation of sz. */ |
| 1935 | if (size_a > (PY_SSIZE_T_MAX - 3) / PyLong_SHIFT) { |
| 1936 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 02515f7 | 2013-08-03 12:08:22 +0100 | [diff] [blame] | 1937 | "int too large to format"); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1938 | return -1; |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1939 | } |
| 1940 | size_a_in_bits = (size_a - 1) * PyLong_SHIFT + |
Niklas Fiekas | c5b7900 | 2020-01-16 15:09:19 +0100 | [diff] [blame] | 1941 | _Py_bit_length(a->ob_digit[size_a - 1]); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1942 | /* Allow 1 character for a '-' sign. */ |
| 1943 | sz = negative + (size_a_in_bits + (bits - 1)) / bits; |
| 1944 | } |
| 1945 | if (alternate) { |
| 1946 | /* 2 characters for prefix */ |
| 1947 | sz += 2; |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1948 | } |
| 1949 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1950 | if (writer) { |
| 1951 | if (_PyUnicodeWriter_Prepare(writer, sz, 'x') == -1) |
| 1952 | return -1; |
| 1953 | kind = writer->kind; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1954 | } |
Victor Stinner | 199c9a6 | 2015-10-14 09:47:23 +0200 | [diff] [blame] | 1955 | else if (bytes_writer) { |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1956 | *bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, sz); |
| 1957 | if (*bytes_str == NULL) |
| 1958 | return -1; |
| 1959 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1960 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1961 | v = PyUnicode_New(sz, 'x'); |
| 1962 | if (v == NULL) |
| 1963 | return -1; |
| 1964 | kind = PyUnicode_KIND(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1965 | } |
Mark Dickinson | 8accd6b | 2009-09-17 19:39:12 +0000 | [diff] [blame] | 1966 | |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1967 | #define WRITE_DIGITS(p) \ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1968 | do { \ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1969 | if (size_a == 0) { \ |
| 1970 | *--p = '0'; \ |
| 1971 | } \ |
| 1972 | else { \ |
| 1973 | /* JRH: special case for power-of-2 bases */ \ |
| 1974 | twodigits accum = 0; \ |
| 1975 | int accumbits = 0; /* # of bits in accum */ \ |
| 1976 | Py_ssize_t i; \ |
| 1977 | for (i = 0; i < size_a; ++i) { \ |
| 1978 | accum |= (twodigits)a->ob_digit[i] << accumbits; \ |
| 1979 | accumbits += PyLong_SHIFT; \ |
| 1980 | assert(accumbits >= bits); \ |
| 1981 | do { \ |
| 1982 | char cdigit; \ |
| 1983 | cdigit = (char)(accum & (base - 1)); \ |
| 1984 | cdigit += (cdigit < 10) ? '0' : 'a'-10; \ |
| 1985 | *--p = cdigit; \ |
| 1986 | accumbits -= bits; \ |
| 1987 | accum >>= bits; \ |
| 1988 | } while (i < size_a-1 ? accumbits >= bits : accum > 0); \ |
| 1989 | } \ |
| 1990 | } \ |
| 1991 | \ |
| 1992 | if (alternate) { \ |
| 1993 | if (base == 16) \ |
| 1994 | *--p = 'x'; \ |
| 1995 | else if (base == 8) \ |
| 1996 | *--p = 'o'; \ |
| 1997 | else /* (base == 2) */ \ |
| 1998 | *--p = 'b'; \ |
| 1999 | *--p = '0'; \ |
| 2000 | } \ |
| 2001 | if (negative) \ |
| 2002 | *--p = '-'; \ |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 2003 | } while (0) |
| 2004 | |
| 2005 | #define WRITE_UNICODE_DIGITS(TYPE) \ |
| 2006 | do { \ |
| 2007 | if (writer) \ |
| 2008 | p = (TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos + sz; \ |
| 2009 | else \ |
| 2010 | p = (TYPE*)PyUnicode_DATA(v) + sz; \ |
| 2011 | \ |
| 2012 | WRITE_DIGITS(p); \ |
| 2013 | \ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2014 | if (writer) \ |
| 2015 | assert(p == ((TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos)); \ |
| 2016 | else \ |
| 2017 | assert(p == (TYPE*)PyUnicode_DATA(v)); \ |
| 2018 | } while (0) |
| 2019 | |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 2020 | if (bytes_writer) { |
| 2021 | char *p = *bytes_str + sz; |
| 2022 | WRITE_DIGITS(p); |
| 2023 | assert(p == *bytes_str); |
| 2024 | } |
| 2025 | else if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2026 | Py_UCS1 *p; |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 2027 | WRITE_UNICODE_DIGITS(Py_UCS1); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2028 | } |
| 2029 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2030 | Py_UCS2 *p; |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 2031 | WRITE_UNICODE_DIGITS(Py_UCS2); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2032 | } |
| 2033 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2034 | Py_UCS4 *p; |
Victor Stinner | e577ab3 | 2012-05-29 18:51:10 +0200 | [diff] [blame] | 2035 | assert (kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 2036 | WRITE_UNICODE_DIGITS(Py_UCS4); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2037 | } |
| 2038 | #undef WRITE_DIGITS |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 2039 | #undef WRITE_UNICODE_DIGITS |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2040 | |
| 2041 | if (writer) { |
| 2042 | writer->pos += sz; |
| 2043 | } |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 2044 | else if (bytes_writer) { |
| 2045 | (*bytes_str) += sz; |
| 2046 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2047 | else { |
| 2048 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 2049 | *p_output = v; |
| 2050 | } |
| 2051 | return 0; |
| 2052 | } |
| 2053 | |
| 2054 | PyObject * |
| 2055 | _PyLong_Format(PyObject *obj, int base) |
| 2056 | { |
| 2057 | PyObject *str; |
| 2058 | int err; |
| 2059 | if (base == 10) |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 2060 | err = long_to_decimal_string_internal(obj, &str, NULL, NULL, NULL); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2061 | else |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 2062 | err = long_format_binary(obj, base, 1, &str, NULL, NULL, NULL); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2063 | if (err == -1) |
| 2064 | return NULL; |
| 2065 | return str; |
| 2066 | } |
| 2067 | |
| 2068 | int |
| 2069 | _PyLong_FormatWriter(_PyUnicodeWriter *writer, |
| 2070 | PyObject *obj, |
| 2071 | int base, int alternate) |
| 2072 | { |
| 2073 | if (base == 10) |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 2074 | return long_to_decimal_string_internal(obj, NULL, writer, |
| 2075 | NULL, NULL); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2076 | else |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 2077 | return long_format_binary(obj, base, alternate, NULL, writer, |
| 2078 | NULL, NULL); |
| 2079 | } |
| 2080 | |
| 2081 | char* |
| 2082 | _PyLong_FormatBytesWriter(_PyBytesWriter *writer, char *str, |
| 2083 | PyObject *obj, |
| 2084 | int base, int alternate) |
| 2085 | { |
| 2086 | char *str2; |
| 2087 | int res; |
| 2088 | str2 = str; |
| 2089 | if (base == 10) |
| 2090 | res = long_to_decimal_string_internal(obj, NULL, NULL, |
| 2091 | writer, &str2); |
| 2092 | else |
| 2093 | res = long_format_binary(obj, base, alternate, NULL, NULL, |
| 2094 | writer, &str2); |
| 2095 | if (res < 0) |
| 2096 | return NULL; |
| 2097 | assert(str2 != NULL); |
| 2098 | return str2; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2099 | } |
| 2100 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2101 | /* Table of digit values for 8-bit string -> integer conversion. |
| 2102 | * '0' maps to 0, ..., '9' maps to 9. |
| 2103 | * 'a' and 'A' map to 10, ..., 'z' and 'Z' map to 35. |
| 2104 | * All other indices map to 37. |
| 2105 | * 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] | 2106 | * base B digit iff _PyLong_DigitValue[Py_CHARPyLong_MASK(c)] < B. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2107 | */ |
Raymond Hettinger | 3563153 | 2009-01-09 03:58:09 +0000 | [diff] [blame] | 2108 | unsigned char _PyLong_DigitValue[256] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 37, 37, 37, 37, 37, 37, |
| 2113 | 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
| 2114 | 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37, |
| 2115 | 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
| 2116 | 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37, |
| 2117 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2118 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2119 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2120 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2121 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2122 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2123 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2124 | 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] | 2125 | }; |
| 2126 | |
| 2127 | /* *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] | 2128 | * 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] | 2129 | * non-digit (which may be *str!). A normalized int is returned. |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 2130 | * The point to this routine is that it takes time linear in the number of |
| 2131 | * string characters. |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2132 | * |
| 2133 | * Return values: |
| 2134 | * -1 on syntax error (exception needs to be set, *res is untouched) |
| 2135 | * 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] | 2136 | */ |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2137 | static int |
| 2138 | long_from_binary_base(const char **str, int base, PyLongObject **res) |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 2139 | { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 2140 | const char *p = *str; |
| 2141 | const char *start = p; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2142 | char prev = 0; |
Serhiy Storchaka | 29ba688 | 2017-12-03 22:16:21 +0200 | [diff] [blame] | 2143 | Py_ssize_t digits = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2144 | int bits_per_char; |
| 2145 | Py_ssize_t n; |
| 2146 | PyLongObject *z; |
| 2147 | twodigits accum; |
| 2148 | int bits_in_accum; |
| 2149 | digit *pdigit; |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 2150 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2151 | assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0); |
| 2152 | n = base; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2153 | for (bits_per_char = -1; n; ++bits_per_char) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2154 | n >>= 1; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2155 | } |
| 2156 | /* count digits and set p to end-of-string */ |
| 2157 | while (_PyLong_DigitValue[Py_CHARMASK(*p)] < base || *p == '_') { |
| 2158 | if (*p == '_') { |
| 2159 | if (prev == '_') { |
| 2160 | *str = p - 1; |
| 2161 | return -1; |
| 2162 | } |
| 2163 | } else { |
| 2164 | ++digits; |
| 2165 | } |
| 2166 | prev = *p; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2167 | ++p; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2168 | } |
| 2169 | if (prev == '_') { |
| 2170 | /* Trailing underscore not allowed. */ |
| 2171 | *str = p - 1; |
| 2172 | return -1; |
| 2173 | } |
| 2174 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2175 | *str = p; |
Serhiy Storchaka | 85c0b89 | 2017-10-03 14:13:44 +0300 | [diff] [blame] | 2176 | /* n <- the number of Python digits needed, |
| 2177 | = ceiling((digits * bits_per_char) / PyLong_SHIFT). */ |
| 2178 | if (digits > (PY_SSIZE_T_MAX - (PyLong_SHIFT - 1)) / bits_per_char) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2179 | PyErr_SetString(PyExc_ValueError, |
| 2180 | "int string too large to convert"); |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2181 | *res = NULL; |
| 2182 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2183 | } |
Serhiy Storchaka | 85c0b89 | 2017-10-03 14:13:44 +0300 | [diff] [blame] | 2184 | n = (digits * bits_per_char + PyLong_SHIFT - 1) / PyLong_SHIFT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2185 | z = _PyLong_New(n); |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2186 | if (z == NULL) { |
| 2187 | *res = NULL; |
| 2188 | return 0; |
| 2189 | } |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2190 | /* Read string from right, and fill in int from left; i.e., |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2191 | * from least to most significant in both. |
| 2192 | */ |
| 2193 | accum = 0; |
| 2194 | bits_in_accum = 0; |
| 2195 | pdigit = z->ob_digit; |
| 2196 | while (--p >= start) { |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2197 | int k; |
| 2198 | if (*p == '_') { |
| 2199 | continue; |
| 2200 | } |
| 2201 | k = (int)_PyLong_DigitValue[Py_CHARMASK(*p)]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2202 | assert(k >= 0 && k < base); |
| 2203 | accum |= (twodigits)k << bits_in_accum; |
| 2204 | bits_in_accum += bits_per_char; |
| 2205 | if (bits_in_accum >= PyLong_SHIFT) { |
| 2206 | *pdigit++ = (digit)(accum & PyLong_MASK); |
| 2207 | assert(pdigit - z->ob_digit <= n); |
| 2208 | accum >>= PyLong_SHIFT; |
| 2209 | bits_in_accum -= PyLong_SHIFT; |
| 2210 | assert(bits_in_accum < PyLong_SHIFT); |
| 2211 | } |
| 2212 | } |
| 2213 | if (bits_in_accum) { |
| 2214 | assert(bits_in_accum <= PyLong_SHIFT); |
| 2215 | *pdigit++ = (digit)accum; |
| 2216 | assert(pdigit - z->ob_digit <= n); |
| 2217 | } |
| 2218 | while (pdigit - z->ob_digit < n) |
| 2219 | *pdigit++ = 0; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2220 | *res = long_normalize(z); |
| 2221 | return 0; |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 2222 | } |
| 2223 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2224 | /* Parses an int from a bytestring. Leading and trailing whitespace will be |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2225 | * ignored. |
| 2226 | * |
| 2227 | * If successful, a PyLong object will be returned and 'pend' will be pointing |
| 2228 | * to the first unused byte unless it's NULL. |
| 2229 | * |
| 2230 | * If unsuccessful, NULL will be returned. |
| 2231 | */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2232 | PyObject * |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 2233 | PyLong_FromString(const char *str, char **pend, int base) |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 2234 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2235 | int sign = 1, error_if_nonzero = 0; |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 2236 | const char *start, *orig_str = str; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2237 | PyLongObject *z = NULL; |
| 2238 | PyObject *strobj; |
| 2239 | Py_ssize_t slen; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2240 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2241 | if ((base != 0 && base < 2) || base > 36) { |
| 2242 | PyErr_SetString(PyExc_ValueError, |
| 2243 | "int() arg 2 must be >= 2 and <= 36"); |
| 2244 | return NULL; |
| 2245 | } |
Jordon Xu | 2ec7010 | 2019-09-11 00:04:08 +0800 | [diff] [blame] | 2246 | while (*str != '\0' && Py_ISSPACE(*str)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2247 | str++; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2248 | } |
| 2249 | if (*str == '+') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2250 | ++str; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2251 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2252 | else if (*str == '-') { |
| 2253 | ++str; |
| 2254 | sign = -1; |
| 2255 | } |
| 2256 | if (base == 0) { |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2257 | if (str[0] != '0') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2258 | base = 10; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2259 | } |
| 2260 | else if (str[1] == 'x' || str[1] == 'X') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2261 | base = 16; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2262 | } |
| 2263 | else if (str[1] == 'o' || str[1] == 'O') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2264 | base = 8; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2265 | } |
| 2266 | else if (str[1] == 'b' || str[1] == 'B') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2267 | base = 2; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2268 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2269 | else { |
| 2270 | /* "old" (C-style) octal literal, now invalid. |
| 2271 | it might still be zero though */ |
| 2272 | error_if_nonzero = 1; |
| 2273 | base = 10; |
| 2274 | } |
| 2275 | } |
| 2276 | if (str[0] == '0' && |
| 2277 | ((base == 16 && (str[1] == 'x' || str[1] == 'X')) || |
| 2278 | (base == 8 && (str[1] == 'o' || str[1] == 'O')) || |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2279 | (base == 2 && (str[1] == 'b' || str[1] == 'B')))) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2280 | str += 2; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2281 | /* One underscore allowed here. */ |
| 2282 | if (*str == '_') { |
| 2283 | ++str; |
| 2284 | } |
| 2285 | } |
| 2286 | if (str[0] == '_') { |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 2287 | /* May not start with underscores. */ |
| 2288 | goto onError; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2289 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2290 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2291 | start = str; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2292 | if ((base & (base - 1)) == 0) { |
| 2293 | int res = long_from_binary_base(&str, base, &z); |
| 2294 | if (res < 0) { |
| 2295 | /* Syntax error. */ |
| 2296 | goto onError; |
| 2297 | } |
| 2298 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2299 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2300 | /*** |
| 2301 | Binary bases can be converted in time linear in the number of digits, because |
| 2302 | Python's representation base is binary. Other bases (including decimal!) use |
| 2303 | the simple quadratic-time algorithm below, complicated by some speed tricks. |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2304 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2305 | First some math: the largest integer that can be expressed in N base-B digits |
| 2306 | is B**N-1. Consequently, if we have an N-digit input in base B, the worst- |
| 2307 | case number of Python digits needed to hold it is the smallest integer n s.t. |
| 2308 | |
| 2309 | BASE**n-1 >= B**N-1 [or, adding 1 to both sides] |
| 2310 | BASE**n >= B**N [taking logs to base BASE] |
| 2311 | n >= log(B**N)/log(BASE) = N * log(B)/log(BASE) |
| 2312 | |
| 2313 | 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] | 2314 | 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] | 2315 | and the result is computed into it. |
| 2316 | |
| 2317 | The input string is actually treated as being in base base**i (i.e., i digits |
| 2318 | are processed at a time), where two more static arrays hold: |
| 2319 | |
| 2320 | convwidth_base[base] = the largest integer i such that base**i <= BASE |
| 2321 | convmultmax_base[base] = base ** convwidth_base[base] |
| 2322 | |
| 2323 | The first of these is the largest i such that i consecutive input digits |
| 2324 | must fit in a single Python digit. The second is effectively the input |
| 2325 | base we're really using. |
| 2326 | |
| 2327 | Viewing the input as a sequence <c0, c1, ..., c_n-1> of digits in base |
| 2328 | convmultmax_base[base], the result is "simply" |
| 2329 | |
| 2330 | (((c0*B + c1)*B + c2)*B + c3)*B + ... ))) + c_n-1 |
| 2331 | |
| 2332 | where B = convmultmax_base[base]. |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2333 | |
| 2334 | Error analysis: as above, the number of Python digits `n` needed is worst- |
| 2335 | case |
| 2336 | |
| 2337 | n >= N * log(B)/log(BASE) |
| 2338 | |
| 2339 | where `N` is the number of input digits in base `B`. This is computed via |
| 2340 | |
| 2341 | size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1; |
| 2342 | |
| 2343 | below. Two numeric concerns are how much space this can waste, and whether |
| 2344 | the computed result can be too small. To be concrete, assume BASE = 2**15, |
| 2345 | which is the default (and it's unlikely anyone changes that). |
| 2346 | |
| 2347 | Waste isn't a problem: provided the first input digit isn't 0, the difference |
| 2348 | between the worst-case input with N digits and the smallest input with N |
| 2349 | digits is about a factor of B, but B is small compared to BASE so at most |
| 2350 | one allocated Python digit can remain unused on that count. If |
| 2351 | N*log(B)/log(BASE) is mathematically an exact integer, then truncating that |
| 2352 | and adding 1 returns a result 1 larger than necessary. However, that can't |
| 2353 | happen: whenever B is a power of 2, long_from_binary_base() is called |
| 2354 | instead, and it's impossible for B**i to be an integer power of 2**15 when |
| 2355 | B is not a power of 2 (i.e., it's impossible for N*log(B)/log(BASE) to be |
| 2356 | an exact integer when B is not a power of 2, since B**i has a prime factor |
| 2357 | other than 2 in that case, but (2**15)**j's only prime factor is 2). |
| 2358 | |
| 2359 | The computed result can be too small if the true value of N*log(B)/log(BASE) |
| 2360 | is a little bit larger than an exact integer, but due to roundoff errors (in |
| 2361 | computing log(B), log(BASE), their quotient, and/or multiplying that by N) |
| 2362 | yields a numeric result a little less than that integer. Unfortunately, "how |
| 2363 | close can a transcendental function get to an integer over some range?" |
| 2364 | questions are generally theoretically intractable. Computer analysis via |
| 2365 | continued fractions is practical: expand log(B)/log(BASE) via continued |
| 2366 | fractions, giving a sequence i/j of "the best" rational approximations. Then |
| 2367 | j*log(B)/log(BASE) is approximately equal to (the integer) i. This shows that |
| 2368 | we can get very close to being in trouble, but very rarely. For example, |
| 2369 | 76573 is a denominator in one of the continued-fraction approximations to |
| 2370 | log(10)/log(2**15), and indeed: |
| 2371 | |
| 2372 | >>> log(10)/log(2**15)*76573 |
| 2373 | 16958.000000654003 |
| 2374 | |
| 2375 | is very close to an integer. If we were working with IEEE single-precision, |
| 2376 | rounding errors could kill us. Finding worst cases in IEEE double-precision |
| 2377 | requires better-than-double-precision log() functions, and Tim didn't bother. |
| 2378 | 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] | 2379 | 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] | 2380 | This should happen extremely rarely, and in fact I don't have a test case |
| 2381 | that triggers it(!). Instead the code was tested by artificially allocating |
| 2382 | just 1 digit at the start, so that the copying code was exercised for every |
| 2383 | digit beyond the first. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2384 | ***/ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2385 | twodigits c; /* current input character */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2386 | Py_ssize_t size_z; |
Serhiy Storchaka | 29ba688 | 2017-12-03 22:16:21 +0200 | [diff] [blame] | 2387 | Py_ssize_t digits = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2388 | int i; |
| 2389 | int convwidth; |
| 2390 | twodigits convmultmax, convmult; |
| 2391 | digit *pz, *pzstop; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2392 | const char *scan, *lastdigit; |
| 2393 | char prev = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2394 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2395 | static double log_base_BASE[37] = {0.0e0,}; |
| 2396 | static int convwidth_base[37] = {0,}; |
| 2397 | static twodigits convmultmax_base[37] = {0,}; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2398 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2399 | if (log_base_BASE[base] == 0.0) { |
| 2400 | twodigits convmax = base; |
| 2401 | int i = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2402 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2403 | log_base_BASE[base] = (log((double)base) / |
| 2404 | log((double)PyLong_BASE)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2405 | for (;;) { |
| 2406 | twodigits next = convmax * base; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2407 | if (next > PyLong_BASE) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2408 | break; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2409 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2410 | convmax = next; |
| 2411 | ++i; |
| 2412 | } |
| 2413 | convmultmax_base[base] = convmax; |
| 2414 | assert(i > 0); |
| 2415 | convwidth_base[base] = i; |
| 2416 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2417 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2418 | /* Find length of the string of numeric characters. */ |
| 2419 | scan = str; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2420 | lastdigit = str; |
| 2421 | |
| 2422 | while (_PyLong_DigitValue[Py_CHARMASK(*scan)] < base || *scan == '_') { |
| 2423 | if (*scan == '_') { |
| 2424 | if (prev == '_') { |
| 2425 | /* Only one underscore allowed. */ |
| 2426 | str = lastdigit + 1; |
| 2427 | goto onError; |
| 2428 | } |
| 2429 | } |
| 2430 | else { |
| 2431 | ++digits; |
| 2432 | lastdigit = scan; |
| 2433 | } |
| 2434 | prev = *scan; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2435 | ++scan; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2436 | } |
| 2437 | if (prev == '_') { |
| 2438 | /* Trailing underscore not allowed. */ |
| 2439 | /* Set error pointer to first underscore. */ |
| 2440 | str = lastdigit + 1; |
| 2441 | goto onError; |
| 2442 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2443 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2444 | /* Create an int object that can contain the largest possible |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2445 | * integer with this base and length. Note that there's no |
| 2446 | * need to initialize z->ob_digit -- no slot is read up before |
| 2447 | * being stored into. |
| 2448 | */ |
Victor Stinner | dd431b3 | 2017-12-08 00:06:55 +0100 | [diff] [blame] | 2449 | double fsize_z = (double)digits * log_base_BASE[base] + 1.0; |
| 2450 | if (fsize_z > (double)MAX_LONG_DIGITS) { |
Serhiy Storchaka | 29ba688 | 2017-12-03 22:16:21 +0200 | [diff] [blame] | 2451 | /* The same exception as in _PyLong_New(). */ |
| 2452 | PyErr_SetString(PyExc_OverflowError, |
| 2453 | "too many digits in integer"); |
| 2454 | return NULL; |
| 2455 | } |
| 2456 | size_z = (Py_ssize_t)fsize_z; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2457 | /* Uncomment next line to test exceedingly rare copy code */ |
| 2458 | /* size_z = 1; */ |
| 2459 | assert(size_z > 0); |
| 2460 | z = _PyLong_New(size_z); |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2461 | if (z == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2462 | return NULL; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2463 | } |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 2464 | Py_SET_SIZE(z, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2465 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2466 | /* `convwidth` consecutive input digits are treated as a single |
| 2467 | * digit in base `convmultmax`. |
| 2468 | */ |
| 2469 | convwidth = convwidth_base[base]; |
| 2470 | convmultmax = convmultmax_base[base]; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2471 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2472 | /* Work ;-) */ |
| 2473 | while (str < scan) { |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2474 | if (*str == '_') { |
| 2475 | str++; |
| 2476 | continue; |
| 2477 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2478 | /* grab up to convwidth digits from the input string */ |
| 2479 | c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)]; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2480 | for (i = 1; i < convwidth && str != scan; ++str) { |
| 2481 | if (*str == '_') { |
| 2482 | continue; |
| 2483 | } |
| 2484 | i++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2485 | c = (twodigits)(c * base + |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2486 | (int)_PyLong_DigitValue[Py_CHARMASK(*str)]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2487 | assert(c < PyLong_BASE); |
| 2488 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2489 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2490 | convmult = convmultmax; |
| 2491 | /* Calculate the shift only if we couldn't get |
| 2492 | * convwidth digits. |
| 2493 | */ |
| 2494 | if (i != convwidth) { |
| 2495 | convmult = base; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2496 | for ( ; i > 1; --i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2497 | convmult *= base; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2498 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2499 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2500 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2501 | /* Multiply z by convmult, and add c. */ |
| 2502 | pz = z->ob_digit; |
| 2503 | pzstop = pz + Py_SIZE(z); |
| 2504 | for (; pz < pzstop; ++pz) { |
| 2505 | c += (twodigits)*pz * convmult; |
| 2506 | *pz = (digit)(c & PyLong_MASK); |
| 2507 | c >>= PyLong_SHIFT; |
| 2508 | } |
| 2509 | /* carry off the current end? */ |
| 2510 | if (c) { |
| 2511 | assert(c < PyLong_BASE); |
| 2512 | if (Py_SIZE(z) < size_z) { |
| 2513 | *pz = (digit)c; |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 2514 | Py_SET_SIZE(z, Py_SIZE(z) + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2515 | } |
| 2516 | else { |
| 2517 | PyLongObject *tmp; |
| 2518 | /* Extremely rare. Get more space. */ |
| 2519 | assert(Py_SIZE(z) == size_z); |
| 2520 | tmp = _PyLong_New(size_z + 1); |
| 2521 | if (tmp == NULL) { |
| 2522 | Py_DECREF(z); |
| 2523 | return NULL; |
| 2524 | } |
| 2525 | memcpy(tmp->ob_digit, |
| 2526 | z->ob_digit, |
| 2527 | sizeof(digit) * size_z); |
| 2528 | Py_DECREF(z); |
| 2529 | z = tmp; |
| 2530 | z->ob_digit[size_z] = (digit)c; |
| 2531 | ++size_z; |
| 2532 | } |
| 2533 | } |
| 2534 | } |
| 2535 | } |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2536 | if (z == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2537 | return NULL; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2538 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2539 | if (error_if_nonzero) { |
| 2540 | /* reset the base to 0, else the exception message |
| 2541 | doesn't make too much sense */ |
| 2542 | base = 0; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2543 | if (Py_SIZE(z) != 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2544 | goto onError; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2545 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2546 | /* there might still be other problems, therefore base |
| 2547 | remains zero here for the same reason */ |
| 2548 | } |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2549 | if (str == start) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2550 | goto onError; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2551 | } |
| 2552 | if (sign < 0) { |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 2553 | Py_SET_SIZE(z, -(Py_SIZE(z))); |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2554 | } |
Jordon Xu | 2ec7010 | 2019-09-11 00:04:08 +0800 | [diff] [blame] | 2555 | while (*str && Py_ISSPACE(*str)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2556 | str++; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2557 | } |
| 2558 | if (*str != '\0') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2559 | goto onError; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2560 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2561 | long_normalize(z); |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2562 | z = maybe_small_long(z); |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2563 | if (z == NULL) { |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2564 | return NULL; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2565 | } |
| 2566 | if (pend != NULL) { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 2567 | *pend = (char *)str; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2568 | } |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2569 | return (PyObject *) z; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2570 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2571 | onError: |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2572 | if (pend != NULL) { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 2573 | *pend = (char *)str; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2574 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2575 | Py_XDECREF(z); |
| 2576 | slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200; |
| 2577 | strobj = PyUnicode_FromStringAndSize(orig_str, slen); |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2578 | if (strobj == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2579 | return NULL; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2580 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2581 | PyErr_Format(PyExc_ValueError, |
Serhiy Storchaka | 579ddc2 | 2013-08-03 21:14:05 +0300 | [diff] [blame] | 2582 | "invalid literal for int() with base %d: %.200R", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2583 | base, strobj); |
| 2584 | Py_DECREF(strobj); |
| 2585 | return NULL; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2586 | } |
| 2587 | |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2588 | /* Since PyLong_FromString doesn't have a length parameter, |
| 2589 | * check here for possible NULs in the string. |
| 2590 | * |
| 2591 | * Reports an invalid literal as a bytes object. |
| 2592 | */ |
| 2593 | PyObject * |
| 2594 | _PyLong_FromBytes(const char *s, Py_ssize_t len, int base) |
| 2595 | { |
| 2596 | PyObject *result, *strobj; |
| 2597 | char *end = NULL; |
| 2598 | |
Serhiy Storchaka | 20b39b2 | 2014-09-28 11:27:24 +0300 | [diff] [blame] | 2599 | result = PyLong_FromString(s, &end, base); |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2600 | if (end == NULL || (result != NULL && end == s + len)) |
| 2601 | return result; |
| 2602 | Py_XDECREF(result); |
| 2603 | strobj = PyBytes_FromStringAndSize(s, Py_MIN(len, 200)); |
| 2604 | if (strobj != NULL) { |
| 2605 | PyErr_Format(PyExc_ValueError, |
Serhiy Storchaka | 579ddc2 | 2013-08-03 21:14:05 +0300 | [diff] [blame] | 2606 | "invalid literal for int() with base %d: %.200R", |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2607 | base, strobj); |
| 2608 | Py_DECREF(strobj); |
| 2609 | } |
| 2610 | return NULL; |
| 2611 | } |
| 2612 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2613 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2614 | PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2615 | { |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 2616 | PyObject *v, *unicode = PyUnicode_FromWideChar(u, length); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2617 | if (unicode == NULL) |
| 2618 | return NULL; |
| 2619 | v = PyLong_FromUnicodeObject(unicode, base); |
| 2620 | Py_DECREF(unicode); |
| 2621 | return v; |
| 2622 | } |
| 2623 | |
| 2624 | PyObject * |
| 2625 | PyLong_FromUnicodeObject(PyObject *u, int base) |
| 2626 | { |
Serhiy Storchaka | 579ddc2 | 2013-08-03 21:14:05 +0300 | [diff] [blame] | 2627 | PyObject *result, *asciidig; |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 2628 | const char *buffer; |
| 2629 | char *end = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2630 | Py_ssize_t buflen; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2631 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2632 | asciidig = _PyUnicode_TransformDecimalAndSpaceToASCII(u); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 2633 | if (asciidig == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2634 | return NULL; |
Serhiy Storchaka | 9b6c60c | 2017-11-13 21:23:48 +0200 | [diff] [blame] | 2635 | assert(PyUnicode_IS_ASCII(asciidig)); |
| 2636 | /* Simply get a pointer to existing ASCII characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2637 | buffer = PyUnicode_AsUTF8AndSize(asciidig, &buflen); |
Serhiy Storchaka | 9b6c60c | 2017-11-13 21:23:48 +0200 | [diff] [blame] | 2638 | assert(buffer != NULL); |
| 2639 | |
| 2640 | result = PyLong_FromString(buffer, &end, base); |
| 2641 | if (end == NULL || (result != NULL && end == buffer + buflen)) { |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 2642 | Py_DECREF(asciidig); |
Serhiy Storchaka | 9b6c60c | 2017-11-13 21:23:48 +0200 | [diff] [blame] | 2643 | return result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2644 | } |
Serhiy Storchaka | 9b6c60c | 2017-11-13 21:23:48 +0200 | [diff] [blame] | 2645 | Py_DECREF(asciidig); |
| 2646 | Py_XDECREF(result); |
Serhiy Storchaka | 579ddc2 | 2013-08-03 21:14:05 +0300 | [diff] [blame] | 2647 | PyErr_Format(PyExc_ValueError, |
| 2648 | "invalid literal for int() with base %d: %.200R", |
| 2649 | base, u); |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2650 | return NULL; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2651 | } |
| 2652 | |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2653 | /* forward */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2654 | static PyLongObject *x_divrem |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2655 | (PyLongObject *, PyLongObject *, PyLongObject **); |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 2656 | static PyObject *long_long(PyObject *v); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2657 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2658 | /* Int division with remainder, top-level routine */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2659 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2660 | static int |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2661 | long_divrem(PyLongObject *a, PyLongObject *b, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2662 | PyLongObject **pdiv, PyLongObject **prem) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2663 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 2664 | 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] | 2665 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2666 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2667 | if (size_b == 0) { |
| 2668 | PyErr_SetString(PyExc_ZeroDivisionError, |
| 2669 | "integer division or modulo by zero"); |
| 2670 | return -1; |
| 2671 | } |
| 2672 | if (size_a < size_b || |
| 2673 | (size_a == size_b && |
| 2674 | a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) { |
| 2675 | /* |a| < |b|. */ |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 2676 | *prem = (PyLongObject *)long_long((PyObject *)a); |
Mark Dickinson | b820d7f | 2016-08-22 12:24:46 +0100 | [diff] [blame] | 2677 | if (*prem == NULL) { |
Mark Dickinson | b820d7f | 2016-08-22 12:24:46 +0100 | [diff] [blame] | 2678 | return -1; |
| 2679 | } |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 2680 | Py_INCREF(_PyLong_Zero); |
| 2681 | *pdiv = (PyLongObject*)_PyLong_Zero; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2682 | return 0; |
| 2683 | } |
| 2684 | if (size_b == 1) { |
| 2685 | digit rem = 0; |
| 2686 | z = divrem1(a, b->ob_digit[0], &rem); |
| 2687 | if (z == NULL) |
| 2688 | return -1; |
| 2689 | *prem = (PyLongObject *) PyLong_FromLong((long)rem); |
| 2690 | if (*prem == NULL) { |
| 2691 | Py_DECREF(z); |
| 2692 | return -1; |
| 2693 | } |
| 2694 | } |
| 2695 | else { |
| 2696 | z = x_divrem(a, b, prem); |
| 2697 | if (z == NULL) |
| 2698 | return -1; |
| 2699 | } |
| 2700 | /* Set the signs. |
| 2701 | The quotient z has the sign of a*b; |
| 2702 | the remainder r has the sign of a, |
| 2703 | so a = b*z + r. */ |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 2704 | if ((Py_SIZE(a) < 0) != (Py_SIZE(b) < 0)) { |
| 2705 | _PyLong_Negate(&z); |
| 2706 | if (z == NULL) { |
| 2707 | Py_CLEAR(*prem); |
| 2708 | return -1; |
| 2709 | } |
| 2710 | } |
| 2711 | if (Py_SIZE(a) < 0 && Py_SIZE(*prem) != 0) { |
| 2712 | _PyLong_Negate(prem); |
| 2713 | if (*prem == NULL) { |
| 2714 | Py_DECREF(z); |
| 2715 | Py_CLEAR(*prem); |
| 2716 | return -1; |
| 2717 | } |
| 2718 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2719 | *pdiv = maybe_small_long(z); |
| 2720 | return 0; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2721 | } |
| 2722 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2723 | /* Unsigned int division with remainder -- the algorithm. The arguments v1 |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 2724 | 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] | 2725 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2726 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2727 | x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2728 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2729 | PyLongObject *v, *w, *a; |
| 2730 | Py_ssize_t i, k, size_v, size_w; |
| 2731 | int d; |
| 2732 | digit wm1, wm2, carry, q, r, vtop, *v0, *vk, *w0, *ak; |
| 2733 | twodigits vv; |
| 2734 | sdigit zhi; |
| 2735 | stwodigits z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2736 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2737 | /* We follow Knuth [The Art of Computer Programming, Vol. 2 (3rd |
| 2738 | edn.), section 4.3.1, Algorithm D], except that we don't explicitly |
| 2739 | handle the special case when the initial estimate q for a quotient |
| 2740 | digit is >= PyLong_BASE: the max value for q is PyLong_BASE+1, and |
| 2741 | that won't overflow a digit. */ |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 2742 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2743 | /* allocate space; w will also be used to hold the final remainder */ |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 2744 | size_v = Py_ABS(Py_SIZE(v1)); |
| 2745 | size_w = Py_ABS(Py_SIZE(w1)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2746 | assert(size_v >= size_w && size_w >= 2); /* Assert checks by div() */ |
| 2747 | v = _PyLong_New(size_v+1); |
| 2748 | if (v == NULL) { |
| 2749 | *prem = NULL; |
| 2750 | return NULL; |
| 2751 | } |
| 2752 | w = _PyLong_New(size_w); |
| 2753 | if (w == NULL) { |
| 2754 | Py_DECREF(v); |
| 2755 | *prem = NULL; |
| 2756 | return NULL; |
| 2757 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2758 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2759 | /* normalize: shift w1 left so that its top digit is >= PyLong_BASE/2. |
| 2760 | 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] | 2761 | d = PyLong_SHIFT - _Py_bit_length(w1->ob_digit[size_w-1]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2762 | carry = v_lshift(w->ob_digit, w1->ob_digit, size_w, d); |
| 2763 | assert(carry == 0); |
| 2764 | carry = v_lshift(v->ob_digit, v1->ob_digit, size_v, d); |
| 2765 | if (carry != 0 || v->ob_digit[size_v-1] >= w->ob_digit[size_w-1]) { |
| 2766 | v->ob_digit[size_v] = carry; |
| 2767 | size_v++; |
| 2768 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2769 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2770 | /* Now v->ob_digit[size_v-1] < w->ob_digit[size_w-1], so quotient has |
| 2771 | at most (and usually exactly) k = size_v - size_w digits. */ |
| 2772 | k = size_v - size_w; |
| 2773 | assert(k >= 0); |
| 2774 | a = _PyLong_New(k); |
| 2775 | if (a == NULL) { |
| 2776 | Py_DECREF(w); |
| 2777 | Py_DECREF(v); |
| 2778 | *prem = NULL; |
| 2779 | return NULL; |
| 2780 | } |
| 2781 | v0 = v->ob_digit; |
| 2782 | w0 = w->ob_digit; |
| 2783 | wm1 = w0[size_w-1]; |
| 2784 | wm2 = w0[size_w-2]; |
| 2785 | for (vk = v0+k, ak = a->ob_digit + k; vk-- > v0;) { |
| 2786 | /* inner loop: divide vk[0:size_w+1] by w0[0:size_w], giving |
| 2787 | single-digit quotient q, remainder in vk[0:size_w]. */ |
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 | SIGCHECK({ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2790 | Py_DECREF(a); |
| 2791 | Py_DECREF(w); |
| 2792 | Py_DECREF(v); |
| 2793 | *prem = NULL; |
| 2794 | return NULL; |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 2795 | }); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2796 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2797 | /* estimate quotient digit q; may overestimate by 1 (rare) */ |
| 2798 | vtop = vk[size_w]; |
| 2799 | assert(vtop <= wm1); |
| 2800 | vv = ((twodigits)vtop << PyLong_SHIFT) | vk[size_w-1]; |
| 2801 | q = (digit)(vv / wm1); |
| 2802 | r = (digit)(vv - (twodigits)wm1 * q); /* r = vv % wm1 */ |
| 2803 | while ((twodigits)wm2 * q > (((twodigits)r << PyLong_SHIFT) |
| 2804 | | vk[size_w-2])) { |
| 2805 | --q; |
| 2806 | r += wm1; |
| 2807 | if (r >= PyLong_BASE) |
| 2808 | break; |
| 2809 | } |
| 2810 | assert(q <= PyLong_BASE); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2811 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2812 | /* subtract q*w0[0:size_w] from vk[0:size_w+1] */ |
| 2813 | zhi = 0; |
| 2814 | for (i = 0; i < size_w; ++i) { |
| 2815 | /* invariants: -PyLong_BASE <= -q <= zhi <= 0; |
| 2816 | -PyLong_BASE * q <= z < PyLong_BASE */ |
| 2817 | z = (sdigit)vk[i] + zhi - |
| 2818 | (stwodigits)q * (stwodigits)w0[i]; |
| 2819 | vk[i] = (digit)z & PyLong_MASK; |
| 2820 | zhi = (sdigit)Py_ARITHMETIC_RIGHT_SHIFT(stwodigits, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2821 | z, PyLong_SHIFT); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2822 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2823 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2824 | /* add w back if q was too large (this branch taken rarely) */ |
| 2825 | assert((sdigit)vtop + zhi == -1 || (sdigit)vtop + zhi == 0); |
| 2826 | if ((sdigit)vtop + zhi < 0) { |
| 2827 | carry = 0; |
| 2828 | for (i = 0; i < size_w; ++i) { |
| 2829 | carry += vk[i] + w0[i]; |
| 2830 | vk[i] = carry & PyLong_MASK; |
| 2831 | carry >>= PyLong_SHIFT; |
| 2832 | } |
| 2833 | --q; |
| 2834 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2835 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2836 | /* store quotient digit */ |
| 2837 | assert(q < PyLong_BASE); |
| 2838 | *--ak = q; |
| 2839 | } |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 2840 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2841 | /* unshift remainder; we reuse w to store the result */ |
| 2842 | carry = v_rshift(w0, v0, size_w, d); |
| 2843 | assert(carry==0); |
| 2844 | Py_DECREF(v); |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 2845 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2846 | *prem = long_normalize(w); |
| 2847 | return long_normalize(a); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2848 | } |
| 2849 | |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2850 | /* For a nonzero PyLong a, express a in the form x * 2**e, with 0.5 <= |
| 2851 | abs(x) < 1.0 and e >= 0; return x and put e in *e. Here x is |
| 2852 | rounded to DBL_MANT_DIG significant bits using round-half-to-even. |
| 2853 | If a == 0, return 0.0 and set *e = 0. If the resulting exponent |
| 2854 | e is larger than PY_SSIZE_T_MAX, raise OverflowError and return |
| 2855 | -1.0. */ |
| 2856 | |
| 2857 | /* attempt to define 2.0**DBL_MANT_DIG as a compile-time constant */ |
| 2858 | #if DBL_MANT_DIG == 53 |
| 2859 | #define EXP2_DBL_MANT_DIG 9007199254740992.0 |
| 2860 | #else |
| 2861 | #define EXP2_DBL_MANT_DIG (ldexp(1.0, DBL_MANT_DIG)) |
| 2862 | #endif |
| 2863 | |
| 2864 | double |
| 2865 | _PyLong_Frexp(PyLongObject *a, Py_ssize_t *e) |
| 2866 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2867 | Py_ssize_t a_size, a_bits, shift_digits, shift_bits, x_size; |
| 2868 | /* See below for why x_digits is always large enough. */ |
Dong-hee Na | b88cd58 | 2020-05-04 22:32:42 +0900 | [diff] [blame] | 2869 | digit rem; |
| 2870 | digit x_digits[2 + (DBL_MANT_DIG + 1) / PyLong_SHIFT] = {0,}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2871 | double dx; |
| 2872 | /* Correction term for round-half-to-even rounding. For a digit x, |
| 2873 | "x + half_even_correction[x & 7]" gives x rounded to the nearest |
| 2874 | multiple of 4, rounding ties to a multiple of 8. */ |
| 2875 | 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] | 2876 | |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 2877 | a_size = Py_ABS(Py_SIZE(a)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2878 | if (a_size == 0) { |
| 2879 | /* Special case for 0: significand 0.0, exponent 0. */ |
| 2880 | *e = 0; |
| 2881 | return 0.0; |
| 2882 | } |
Niklas Fiekas | c5b7900 | 2020-01-16 15:09:19 +0100 | [diff] [blame] | 2883 | a_bits = _Py_bit_length(a->ob_digit[a_size-1]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2884 | /* The following is an overflow-free version of the check |
| 2885 | "if ((a_size - 1) * PyLong_SHIFT + a_bits > PY_SSIZE_T_MAX) ..." */ |
| 2886 | if (a_size >= (PY_SSIZE_T_MAX - 1) / PyLong_SHIFT + 1 && |
| 2887 | (a_size > (PY_SSIZE_T_MAX - 1) / PyLong_SHIFT + 1 || |
| 2888 | a_bits > (PY_SSIZE_T_MAX - 1) % PyLong_SHIFT + 1)) |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2889 | goto overflow; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2890 | a_bits = (a_size - 1) * PyLong_SHIFT + a_bits; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2891 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2892 | /* Shift the first DBL_MANT_DIG + 2 bits of a into x_digits[0:x_size] |
| 2893 | (shifting left if a_bits <= DBL_MANT_DIG + 2). |
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 | Number of digits needed for result: write // for floor division. |
| 2896 | Then if shifting left, we end up using |
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 | 1 + a_size + (DBL_MANT_DIG + 2 - a_bits) // PyLong_SHIFT |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2899 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2900 | digits. If shifting right, we use |
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 | a_size - (a_bits - DBL_MANT_DIG - 2) // PyLong_SHIFT |
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 | digits. Using a_size = 1 + (a_bits - 1) // PyLong_SHIFT along with |
| 2905 | the inequalities |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2906 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2907 | m // PyLong_SHIFT + n // PyLong_SHIFT <= (m + n) // PyLong_SHIFT |
| 2908 | m // PyLong_SHIFT - n // PyLong_SHIFT <= |
| 2909 | 1 + (m - n - 1) // PyLong_SHIFT, |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2910 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2911 | 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] | 2912 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2913 | x_size <= 2 + (DBL_MANT_DIG + 1) // PyLong_SHIFT |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2914 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2915 | in both cases. |
| 2916 | */ |
| 2917 | if (a_bits <= DBL_MANT_DIG + 2) { |
| 2918 | shift_digits = (DBL_MANT_DIG + 2 - a_bits) / PyLong_SHIFT; |
| 2919 | shift_bits = (DBL_MANT_DIG + 2 - a_bits) % PyLong_SHIFT; |
Dong-hee Na | b88cd58 | 2020-05-04 22:32:42 +0900 | [diff] [blame] | 2920 | x_size = shift_digits; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2921 | rem = v_lshift(x_digits + x_size, a->ob_digit, a_size, |
| 2922 | (int)shift_bits); |
| 2923 | x_size += a_size; |
| 2924 | x_digits[x_size++] = rem; |
| 2925 | } |
| 2926 | else { |
| 2927 | shift_digits = (a_bits - DBL_MANT_DIG - 2) / PyLong_SHIFT; |
| 2928 | shift_bits = (a_bits - DBL_MANT_DIG - 2) % PyLong_SHIFT; |
| 2929 | rem = v_rshift(x_digits, a->ob_digit + shift_digits, |
| 2930 | a_size - shift_digits, (int)shift_bits); |
| 2931 | x_size = a_size - shift_digits; |
| 2932 | /* For correct rounding below, we need the least significant |
| 2933 | bit of x to be 'sticky' for this shift: if any of the bits |
| 2934 | shifted out was nonzero, we set the least significant bit |
| 2935 | of x. */ |
| 2936 | if (rem) |
| 2937 | x_digits[0] |= 1; |
| 2938 | else |
| 2939 | while (shift_digits > 0) |
| 2940 | if (a->ob_digit[--shift_digits]) { |
| 2941 | x_digits[0] |= 1; |
| 2942 | break; |
| 2943 | } |
| 2944 | } |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2945 | 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] | 2946 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2947 | /* Round, and convert to double. */ |
| 2948 | x_digits[0] += half_even_correction[x_digits[0] & 7]; |
| 2949 | dx = x_digits[--x_size]; |
| 2950 | while (x_size > 0) |
| 2951 | dx = dx * PyLong_BASE + x_digits[--x_size]; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2952 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2953 | /* Rescale; make correction if result is 1.0. */ |
| 2954 | dx /= 4.0 * EXP2_DBL_MANT_DIG; |
| 2955 | if (dx == 1.0) { |
| 2956 | if (a_bits == PY_SSIZE_T_MAX) |
| 2957 | goto overflow; |
| 2958 | dx = 0.5; |
| 2959 | a_bits += 1; |
| 2960 | } |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2961 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2962 | *e = a_bits; |
| 2963 | return Py_SIZE(a) < 0 ? -dx : dx; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2964 | |
| 2965 | overflow: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2966 | /* exponent > PY_SSIZE_T_MAX */ |
| 2967 | PyErr_SetString(PyExc_OverflowError, |
| 2968 | "huge integer: number of bits overflows a Py_ssize_t"); |
| 2969 | *e = 0; |
| 2970 | return -1.0; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2971 | } |
| 2972 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2973 | /* 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] | 2974 | using the round-half-to-even rule in the case of a tie. */ |
| 2975 | |
| 2976 | double |
| 2977 | PyLong_AsDouble(PyObject *v) |
| 2978 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2979 | Py_ssize_t exponent; |
| 2980 | double x; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2981 | |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 2982 | if (v == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2983 | PyErr_BadInternalCall(); |
| 2984 | return -1.0; |
| 2985 | } |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 2986 | if (!PyLong_Check(v)) { |
| 2987 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 2988 | return -1.0; |
| 2989 | } |
Yury Selivanov | 186c30b | 2016-02-05 19:40:01 -0500 | [diff] [blame] | 2990 | if (Py_ABS(Py_SIZE(v)) <= 1) { |
Yury Selivanov | a0fcaca | 2016-02-06 12:21:33 -0500 | [diff] [blame] | 2991 | /* Fast path; single digit long (31 bits) will cast safely |
Mark Dickinson | 1dc3c89 | 2016-08-21 10:33:36 +0100 | [diff] [blame] | 2992 | to double. This improves performance of FP/long operations |
| 2993 | by 20%. |
Yury Selivanov | 186c30b | 2016-02-05 19:40:01 -0500 | [diff] [blame] | 2994 | */ |
| 2995 | return (double)MEDIUM_VALUE((PyLongObject *)v); |
| 2996 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2997 | x = _PyLong_Frexp((PyLongObject *)v, &exponent); |
| 2998 | if ((x == -1.0 && PyErr_Occurred()) || exponent > DBL_MAX_EXP) { |
| 2999 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 02515f7 | 2013-08-03 12:08:22 +0100 | [diff] [blame] | 3000 | "int too large to convert to float"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3001 | return -1.0; |
| 3002 | } |
| 3003 | return ldexp(x, (int)exponent); |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 3004 | } |
| 3005 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3006 | /* Methods */ |
| 3007 | |
HongWeipeng | 42acb7b | 2019-09-18 23:10:15 +0800 | [diff] [blame] | 3008 | /* if a < b, return a negative number |
| 3009 | if a == b, return 0 |
| 3010 | if a > b, return a positive number */ |
| 3011 | |
| 3012 | static Py_ssize_t |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3013 | long_compare(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3014 | { |
HongWeipeng | 42acb7b | 2019-09-18 23:10:15 +0800 | [diff] [blame] | 3015 | Py_ssize_t sign = Py_SIZE(a) - Py_SIZE(b); |
| 3016 | if (sign == 0) { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3017 | Py_ssize_t i = Py_ABS(Py_SIZE(a)); |
HongWeipeng | 42acb7b | 2019-09-18 23:10:15 +0800 | [diff] [blame] | 3018 | sdigit diff = 0; |
| 3019 | while (--i >= 0) { |
| 3020 | diff = (sdigit) a->ob_digit[i] - (sdigit) b->ob_digit[i]; |
| 3021 | if (diff) { |
| 3022 | break; |
| 3023 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3024 | } |
HongWeipeng | 42acb7b | 2019-09-18 23:10:15 +0800 | [diff] [blame] | 3025 | sign = Py_SIZE(a) < 0 ? -diff : diff; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3026 | } |
HongWeipeng | 42acb7b | 2019-09-18 23:10:15 +0800 | [diff] [blame] | 3027 | return sign; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3028 | } |
| 3029 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3030 | static PyObject * |
| 3031 | long_richcompare(PyObject *self, PyObject *other, int op) |
| 3032 | { |
HongWeipeng | 42acb7b | 2019-09-18 23:10:15 +0800 | [diff] [blame] | 3033 | Py_ssize_t result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3034 | CHECK_BINOP(self, other); |
| 3035 | if (self == other) |
| 3036 | result = 0; |
| 3037 | else |
| 3038 | result = long_compare((PyLongObject*)self, (PyLongObject*)other); |
stratakis | e8b1965 | 2017-11-02 11:32:54 +0100 | [diff] [blame] | 3039 | Py_RETURN_RICHCOMPARE(result, 0, op); |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3040 | } |
| 3041 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3042 | static Py_hash_t |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3043 | long_hash(PyLongObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 3044 | { |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 3045 | Py_uhash_t x; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3046 | Py_ssize_t i; |
| 3047 | int sign; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 3048 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3049 | i = Py_SIZE(v); |
| 3050 | switch(i) { |
| 3051 | case -1: return v->ob_digit[0]==1 ? -2 : -(sdigit)v->ob_digit[0]; |
| 3052 | case 0: return 0; |
| 3053 | case 1: return v->ob_digit[0]; |
| 3054 | } |
| 3055 | sign = 1; |
| 3056 | x = 0; |
| 3057 | if (i < 0) { |
| 3058 | sign = -1; |
| 3059 | i = -(i); |
| 3060 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3061 | while (--i >= 0) { |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 3062 | /* Here x is a quantity in the range [0, _PyHASH_MODULUS); we |
| 3063 | want to compute x * 2**PyLong_SHIFT + v->ob_digit[i] modulo |
| 3064 | _PyHASH_MODULUS. |
| 3065 | |
| 3066 | The computation of x * 2**PyLong_SHIFT % _PyHASH_MODULUS |
| 3067 | amounts to a rotation of the bits of x. To see this, write |
| 3068 | |
| 3069 | x * 2**PyLong_SHIFT = y * 2**_PyHASH_BITS + z |
| 3070 | |
| 3071 | where y = x >> (_PyHASH_BITS - PyLong_SHIFT) gives the top |
| 3072 | PyLong_SHIFT bits of x (those that are shifted out of the |
| 3073 | original _PyHASH_BITS bits, and z = (x << PyLong_SHIFT) & |
| 3074 | _PyHASH_MODULUS gives the bottom _PyHASH_BITS - PyLong_SHIFT |
| 3075 | bits of x, shifted up. Then since 2**_PyHASH_BITS is |
| 3076 | congruent to 1 modulo _PyHASH_MODULUS, y*2**_PyHASH_BITS is |
| 3077 | congruent to y modulo _PyHASH_MODULUS. So |
| 3078 | |
| 3079 | x * 2**PyLong_SHIFT = y + z (mod _PyHASH_MODULUS). |
| 3080 | |
| 3081 | The right-hand side is just the result of rotating the |
| 3082 | _PyHASH_BITS bits of x left by PyLong_SHIFT places; since |
| 3083 | not all _PyHASH_BITS bits of x are 1s, the same is true |
| 3084 | after rotation, so 0 <= y+z < _PyHASH_MODULUS and y + z is |
| 3085 | the reduction of x*2**PyLong_SHIFT modulo |
| 3086 | _PyHASH_MODULUS. */ |
| 3087 | x = ((x << PyLong_SHIFT) & _PyHASH_MODULUS) | |
| 3088 | (x >> (_PyHASH_BITS - PyLong_SHIFT)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3089 | x += v->ob_digit[i]; |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 3090 | if (x >= _PyHASH_MODULUS) |
| 3091 | x -= _PyHASH_MODULUS; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3092 | } |
| 3093 | x = x * sign; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 3094 | if (x == (Py_uhash_t)-1) |
| 3095 | x = (Py_uhash_t)-2; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3096 | return (Py_hash_t)x; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 3097 | } |
| 3098 | |
| 3099 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 3100 | /* Add the absolute values of two integers. */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3101 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3102 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3103 | x_add(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3104 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3105 | 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] | 3106 | PyLongObject *z; |
| 3107 | Py_ssize_t i; |
| 3108 | digit carry = 0; |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 3109 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3110 | /* Ensure a is the larger of the two: */ |
| 3111 | if (size_a < size_b) { |
| 3112 | { PyLongObject *temp = a; a = b; b = temp; } |
| 3113 | { Py_ssize_t size_temp = size_a; |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3114 | size_a = size_b; |
| 3115 | size_b = size_temp; } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3116 | } |
| 3117 | z = _PyLong_New(size_a+1); |
| 3118 | if (z == NULL) |
| 3119 | return NULL; |
| 3120 | for (i = 0; i < size_b; ++i) { |
| 3121 | carry += a->ob_digit[i] + b->ob_digit[i]; |
| 3122 | z->ob_digit[i] = carry & PyLong_MASK; |
| 3123 | carry >>= PyLong_SHIFT; |
| 3124 | } |
| 3125 | for (; i < size_a; ++i) { |
| 3126 | carry += a->ob_digit[i]; |
| 3127 | z->ob_digit[i] = carry & PyLong_MASK; |
| 3128 | carry >>= PyLong_SHIFT; |
| 3129 | } |
| 3130 | z->ob_digit[i] = carry; |
| 3131 | return long_normalize(z); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3132 | } |
| 3133 | |
| 3134 | /* Subtract the absolute values of two integers. */ |
| 3135 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3136 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3137 | x_sub(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3138 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3139 | 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] | 3140 | PyLongObject *z; |
| 3141 | Py_ssize_t i; |
| 3142 | int sign = 1; |
| 3143 | digit borrow = 0; |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 3144 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3145 | /* Ensure a is the larger of the two: */ |
| 3146 | if (size_a < size_b) { |
| 3147 | sign = -1; |
| 3148 | { PyLongObject *temp = a; a = b; b = temp; } |
| 3149 | { Py_ssize_t size_temp = size_a; |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3150 | size_a = size_b; |
| 3151 | size_b = size_temp; } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3152 | } |
| 3153 | else if (size_a == size_b) { |
| 3154 | /* Find highest digit where a and b differ: */ |
| 3155 | i = size_a; |
| 3156 | while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) |
| 3157 | ; |
| 3158 | if (i < 0) |
| 3159 | return (PyLongObject *)PyLong_FromLong(0); |
| 3160 | if (a->ob_digit[i] < b->ob_digit[i]) { |
| 3161 | sign = -1; |
| 3162 | { PyLongObject *temp = a; a = b; b = temp; } |
| 3163 | } |
| 3164 | size_a = size_b = i+1; |
| 3165 | } |
| 3166 | z = _PyLong_New(size_a); |
| 3167 | if (z == NULL) |
| 3168 | return NULL; |
| 3169 | for (i = 0; i < size_b; ++i) { |
| 3170 | /* The following assumes unsigned arithmetic |
| 3171 | works module 2**N for some N>PyLong_SHIFT. */ |
| 3172 | borrow = a->ob_digit[i] - b->ob_digit[i] - borrow; |
| 3173 | z->ob_digit[i] = borrow & PyLong_MASK; |
| 3174 | borrow >>= PyLong_SHIFT; |
| 3175 | borrow &= 1; /* Keep only one sign bit */ |
| 3176 | } |
| 3177 | for (; i < size_a; ++i) { |
| 3178 | borrow = a->ob_digit[i] - borrow; |
| 3179 | z->ob_digit[i] = borrow & PyLong_MASK; |
| 3180 | borrow >>= PyLong_SHIFT; |
| 3181 | borrow &= 1; /* Keep only one sign bit */ |
| 3182 | } |
| 3183 | assert(borrow == 0); |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 3184 | if (sign < 0) { |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 3185 | Py_SET_SIZE(z, -Py_SIZE(z)); |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 3186 | } |
HongWeipeng | 036fe85 | 2019-11-26 15:54:49 +0800 | [diff] [blame] | 3187 | return maybe_small_long(long_normalize(z)); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3188 | } |
| 3189 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3190 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3191 | long_add(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3192 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3193 | PyLongObject *z; |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 3194 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3195 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3196 | |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3197 | 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] | 3198 | return PyLong_FromLong(MEDIUM_VALUE(a) + MEDIUM_VALUE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3199 | } |
| 3200 | if (Py_SIZE(a) < 0) { |
| 3201 | if (Py_SIZE(b) < 0) { |
| 3202 | z = x_add(a, b); |
Serhiy Storchaka | e63e5d6 | 2016-06-04 00:06:45 +0300 | [diff] [blame] | 3203 | if (z != NULL) { |
| 3204 | /* x_add received at least one multiple-digit int, |
| 3205 | and thus z must be a multiple-digit int. |
| 3206 | That also means z is not an element of |
| 3207 | small_ints, so negating it in-place is safe. */ |
| 3208 | assert(Py_REFCNT(z) == 1); |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 3209 | Py_SET_SIZE(z, -(Py_SIZE(z))); |
Serhiy Storchaka | e63e5d6 | 2016-06-04 00:06:45 +0300 | [diff] [blame] | 3210 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3211 | } |
| 3212 | else |
| 3213 | z = x_sub(b, a); |
| 3214 | } |
| 3215 | else { |
| 3216 | if (Py_SIZE(b) < 0) |
| 3217 | z = x_sub(a, b); |
| 3218 | else |
| 3219 | z = x_add(a, b); |
| 3220 | } |
| 3221 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3222 | } |
| 3223 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3224 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3225 | long_sub(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3226 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3227 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3228 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3229 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3230 | |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3231 | 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] | 3232 | return PyLong_FromLong(MEDIUM_VALUE(a) - MEDIUM_VALUE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3233 | } |
| 3234 | if (Py_SIZE(a) < 0) { |
HongWeipeng | 036fe85 | 2019-11-26 15:54:49 +0800 | [diff] [blame] | 3235 | if (Py_SIZE(b) < 0) { |
| 3236 | z = x_sub(b, a); |
| 3237 | } |
| 3238 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3239 | z = x_add(a, b); |
HongWeipeng | 036fe85 | 2019-11-26 15:54:49 +0800 | [diff] [blame] | 3240 | if (z != NULL) { |
| 3241 | assert(Py_SIZE(z) == 0 || Py_REFCNT(z) == 1); |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 3242 | Py_SET_SIZE(z, -(Py_SIZE(z))); |
HongWeipeng | 036fe85 | 2019-11-26 15:54:49 +0800 | [diff] [blame] | 3243 | } |
Serhiy Storchaka | e63e5d6 | 2016-06-04 00:06:45 +0300 | [diff] [blame] | 3244 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3245 | } |
| 3246 | else { |
| 3247 | if (Py_SIZE(b) < 0) |
| 3248 | z = x_add(a, b); |
| 3249 | else |
| 3250 | z = x_sub(a, b); |
| 3251 | } |
| 3252 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3253 | } |
| 3254 | |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3255 | /* Grade school multiplication, ignoring the signs. |
| 3256 | * Returns the absolute value of the product, or NULL if error. |
| 3257 | */ |
| 3258 | static PyLongObject * |
| 3259 | x_mul(PyLongObject *a, PyLongObject *b) |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3260 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3261 | PyLongObject *z; |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3262 | Py_ssize_t size_a = Py_ABS(Py_SIZE(a)); |
| 3263 | Py_ssize_t size_b = Py_ABS(Py_SIZE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3264 | Py_ssize_t i; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3265 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3266 | z = _PyLong_New(size_a + size_b); |
| 3267 | if (z == NULL) |
| 3268 | return NULL; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3269 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3270 | memset(z->ob_digit, 0, Py_SIZE(z) * sizeof(digit)); |
| 3271 | if (a == b) { |
| 3272 | /* Efficient squaring per HAC, Algorithm 14.16: |
| 3273 | * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf |
| 3274 | * Gives slightly less than a 2x speedup when a == b, |
| 3275 | * via exploiting that each entry in the multiplication |
| 3276 | * pyramid appears twice (except for the size_a squares). |
| 3277 | */ |
| 3278 | for (i = 0; i < size_a; ++i) { |
| 3279 | twodigits carry; |
| 3280 | twodigits f = a->ob_digit[i]; |
| 3281 | digit *pz = z->ob_digit + (i << 1); |
| 3282 | digit *pa = a->ob_digit + i + 1; |
| 3283 | digit *paend = a->ob_digit + size_a; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3285 | SIGCHECK({ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3286 | Py_DECREF(z); |
| 3287 | return NULL; |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 3288 | }); |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 3289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3290 | carry = *pz + f * f; |
| 3291 | *pz++ = (digit)(carry & PyLong_MASK); |
| 3292 | carry >>= PyLong_SHIFT; |
| 3293 | assert(carry <= PyLong_MASK); |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 3294 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3295 | /* Now f is added in twice in each column of the |
| 3296 | * pyramid it appears. Same as adding f<<1 once. |
| 3297 | */ |
| 3298 | f <<= 1; |
| 3299 | while (pa < paend) { |
| 3300 | carry += *pz + *pa++ * f; |
| 3301 | *pz++ = (digit)(carry & PyLong_MASK); |
| 3302 | carry >>= PyLong_SHIFT; |
| 3303 | assert(carry <= (PyLong_MASK << 1)); |
| 3304 | } |
| 3305 | if (carry) { |
| 3306 | carry += *pz; |
| 3307 | *pz++ = (digit)(carry & PyLong_MASK); |
| 3308 | carry >>= PyLong_SHIFT; |
| 3309 | } |
| 3310 | if (carry) |
| 3311 | *pz += (digit)(carry & PyLong_MASK); |
| 3312 | assert((carry >> PyLong_SHIFT) == 0); |
| 3313 | } |
| 3314 | } |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 3315 | else { /* a is not the same as b -- gradeschool int mult */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3316 | for (i = 0; i < size_a; ++i) { |
| 3317 | twodigits carry = 0; |
| 3318 | twodigits f = a->ob_digit[i]; |
| 3319 | digit *pz = z->ob_digit + i; |
| 3320 | digit *pb = b->ob_digit; |
| 3321 | digit *pbend = b->ob_digit + size_b; |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 3322 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3323 | SIGCHECK({ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3324 | Py_DECREF(z); |
| 3325 | return NULL; |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 3326 | }); |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 3327 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3328 | while (pb < pbend) { |
| 3329 | carry += *pz + *pb++ * f; |
| 3330 | *pz++ = (digit)(carry & PyLong_MASK); |
| 3331 | carry >>= PyLong_SHIFT; |
| 3332 | assert(carry <= PyLong_MASK); |
| 3333 | } |
| 3334 | if (carry) |
| 3335 | *pz += (digit)(carry & PyLong_MASK); |
| 3336 | assert((carry >> PyLong_SHIFT) == 0); |
| 3337 | } |
| 3338 | } |
| 3339 | return long_normalize(z); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3340 | } |
| 3341 | |
| 3342 | /* A helper for Karatsuba multiplication (k_mul). |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 3343 | Takes an int "n" and an integer "size" representing the place to |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3344 | split, and sets low and high such that abs(n) == (high << size) + low, |
| 3345 | viewing the shift as being by digits. The sign bit is ignored, and |
| 3346 | the return values are >= 0. |
| 3347 | Returns 0 on success, -1 on failure. |
| 3348 | */ |
| 3349 | static int |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3350 | kmul_split(PyLongObject *n, |
| 3351 | Py_ssize_t size, |
| 3352 | PyLongObject **high, |
| 3353 | PyLongObject **low) |
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 | PyLongObject *hi, *lo; |
| 3356 | Py_ssize_t size_lo, size_hi; |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3357 | const Py_ssize_t size_n = Py_ABS(Py_SIZE(n)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3358 | |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 3359 | size_lo = Py_MIN(size_n, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3360 | size_hi = size_n - size_lo; |
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 | if ((hi = _PyLong_New(size_hi)) == NULL) |
| 3363 | return -1; |
| 3364 | if ((lo = _PyLong_New(size_lo)) == NULL) { |
| 3365 | Py_DECREF(hi); |
| 3366 | return -1; |
| 3367 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3368 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3369 | memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit)); |
| 3370 | 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] | 3371 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3372 | *high = long_normalize(hi); |
| 3373 | *low = long_normalize(lo); |
| 3374 | return 0; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3375 | } |
| 3376 | |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3377 | static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b); |
| 3378 | |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3379 | /* Karatsuba multiplication. Ignores the input signs, and returns the |
| 3380 | * absolute value of the product (or NULL if error). |
| 3381 | * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295). |
| 3382 | */ |
| 3383 | static PyLongObject * |
| 3384 | k_mul(PyLongObject *a, PyLongObject *b) |
| 3385 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3386 | Py_ssize_t asize = Py_ABS(Py_SIZE(a)); |
| 3387 | Py_ssize_t bsize = Py_ABS(Py_SIZE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3388 | PyLongObject *ah = NULL; |
| 3389 | PyLongObject *al = NULL; |
| 3390 | PyLongObject *bh = NULL; |
| 3391 | PyLongObject *bl = NULL; |
| 3392 | PyLongObject *ret = NULL; |
| 3393 | PyLongObject *t1, *t2, *t3; |
| 3394 | Py_ssize_t shift; /* the number of digits we split off */ |
| 3395 | Py_ssize_t i; |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 3396 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3397 | /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl |
| 3398 | * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl |
| 3399 | * Then the original product is |
| 3400 | * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl |
| 3401 | * By picking X to be a power of 2, "*X" is just shifting, and it's |
| 3402 | * been reduced to 3 multiplies on numbers half the size. |
| 3403 | */ |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3404 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3405 | /* We want to split based on the larger number; fiddle so that b |
| 3406 | * is largest. |
| 3407 | */ |
| 3408 | if (asize > bsize) { |
| 3409 | t1 = a; |
| 3410 | a = b; |
| 3411 | b = t1; |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 3412 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3413 | i = asize; |
| 3414 | asize = bsize; |
| 3415 | bsize = i; |
| 3416 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3417 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3418 | /* Use gradeschool math when either number is too small. */ |
| 3419 | i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF; |
| 3420 | if (asize <= i) { |
| 3421 | if (asize == 0) |
| 3422 | return (PyLongObject *)PyLong_FromLong(0); |
| 3423 | else |
| 3424 | return x_mul(a, b); |
| 3425 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3426 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3427 | /* If a is small compared to b, splitting on b gives a degenerate |
| 3428 | * case with ah==0, and Karatsuba may be (even much) less efficient |
| 3429 | * than "grade school" then. However, we can still win, by viewing |
| 3430 | * b as a string of "big digits", each of width a->ob_size. That |
| 3431 | * leads to a sequence of balanced calls to k_mul. |
| 3432 | */ |
| 3433 | if (2 * asize <= bsize) |
| 3434 | return k_lopsided_mul(a, b); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3435 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3436 | /* Split a & b into hi & lo pieces. */ |
| 3437 | shift = bsize >> 1; |
| 3438 | if (kmul_split(a, shift, &ah, &al) < 0) goto fail; |
| 3439 | assert(Py_SIZE(ah) > 0); /* the split isn't degenerate */ |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3440 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3441 | if (a == b) { |
| 3442 | bh = ah; |
| 3443 | bl = al; |
| 3444 | Py_INCREF(bh); |
| 3445 | Py_INCREF(bl); |
| 3446 | } |
| 3447 | else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3448 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3449 | /* The plan: |
| 3450 | * 1. Allocate result space (asize + bsize digits: that's always |
| 3451 | * enough). |
| 3452 | * 2. Compute ah*bh, and copy into result at 2*shift. |
| 3453 | * 3. Compute al*bl, and copy into result at 0. Note that this |
| 3454 | * can't overlap with #2. |
| 3455 | * 4. Subtract al*bl from the result, starting at shift. This may |
| 3456 | * underflow (borrow out of the high digit), but we don't care: |
| 3457 | * we're effectively doing unsigned arithmetic mod |
| 3458 | * BASE**(sizea + sizeb), and so long as the *final* result fits, |
| 3459 | * borrows and carries out of the high digit can be ignored. |
| 3460 | * 5. Subtract ah*bh from the result, starting at shift. |
| 3461 | * 6. Compute (ah+al)*(bh+bl), and add it into the result starting |
| 3462 | * at shift. |
| 3463 | */ |
Tim Peters | d64c1de | 2002-08-12 17:36:03 +0000 | [diff] [blame] | 3464 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3465 | /* 1. Allocate result space. */ |
| 3466 | ret = _PyLong_New(asize + bsize); |
| 3467 | if (ret == NULL) goto fail; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3468 | #ifdef Py_DEBUG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3469 | /* Fill with trash, to catch reference to uninitialized digits. */ |
| 3470 | memset(ret->ob_digit, 0xDF, Py_SIZE(ret) * sizeof(digit)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3471 | #endif |
Tim Peters | 44121a6 | 2002-08-12 06:17:58 +0000 | [diff] [blame] | 3472 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3473 | /* 2. t1 <- ah*bh, and copy into high digits of result. */ |
| 3474 | if ((t1 = k_mul(ah, bh)) == NULL) goto fail; |
| 3475 | assert(Py_SIZE(t1) >= 0); |
| 3476 | assert(2*shift + Py_SIZE(t1) <= Py_SIZE(ret)); |
| 3477 | memcpy(ret->ob_digit + 2*shift, t1->ob_digit, |
| 3478 | Py_SIZE(t1) * sizeof(digit)); |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 3479 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3480 | /* Zero-out the digits higher than the ah*bh copy. */ |
| 3481 | i = Py_SIZE(ret) - 2*shift - Py_SIZE(t1); |
| 3482 | if (i) |
| 3483 | memset(ret->ob_digit + 2*shift + Py_SIZE(t1), 0, |
| 3484 | i * sizeof(digit)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3485 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3486 | /* 3. t2 <- al*bl, and copy into the low digits. */ |
| 3487 | if ((t2 = k_mul(al, bl)) == NULL) { |
| 3488 | Py_DECREF(t1); |
| 3489 | goto fail; |
| 3490 | } |
| 3491 | assert(Py_SIZE(t2) >= 0); |
| 3492 | assert(Py_SIZE(t2) <= 2*shift); /* no overlap with high digits */ |
| 3493 | memcpy(ret->ob_digit, t2->ob_digit, Py_SIZE(t2) * sizeof(digit)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3494 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3495 | /* Zero out remaining digits. */ |
| 3496 | i = 2*shift - Py_SIZE(t2); /* number of uninitialized digits */ |
| 3497 | if (i) |
| 3498 | memset(ret->ob_digit + Py_SIZE(t2), 0, i * sizeof(digit)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3499 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3500 | /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first |
| 3501 | * because it's fresher in cache. |
| 3502 | */ |
| 3503 | i = Py_SIZE(ret) - shift; /* # digits after shift */ |
| 3504 | (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, Py_SIZE(t2)); |
| 3505 | Py_DECREF(t2); |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 3506 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3507 | (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, Py_SIZE(t1)); |
| 3508 | Py_DECREF(t1); |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 3509 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3510 | /* 6. t3 <- (ah+al)(bh+bl), and add into result. */ |
| 3511 | if ((t1 = x_add(ah, al)) == NULL) goto fail; |
| 3512 | Py_DECREF(ah); |
| 3513 | Py_DECREF(al); |
| 3514 | ah = al = NULL; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3515 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3516 | if (a == b) { |
| 3517 | t2 = t1; |
| 3518 | Py_INCREF(t2); |
| 3519 | } |
| 3520 | else if ((t2 = x_add(bh, bl)) == NULL) { |
| 3521 | Py_DECREF(t1); |
| 3522 | goto fail; |
| 3523 | } |
| 3524 | Py_DECREF(bh); |
| 3525 | Py_DECREF(bl); |
| 3526 | bh = bl = NULL; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3527 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3528 | t3 = k_mul(t1, t2); |
| 3529 | Py_DECREF(t1); |
| 3530 | Py_DECREF(t2); |
| 3531 | if (t3 == NULL) goto fail; |
| 3532 | assert(Py_SIZE(t3) >= 0); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3533 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3534 | /* Add t3. It's not obvious why we can't run out of room here. |
| 3535 | * See the (*) comment after this function. |
| 3536 | */ |
| 3537 | (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, Py_SIZE(t3)); |
| 3538 | Py_DECREF(t3); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3539 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3540 | return long_normalize(ret); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3541 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3542 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3543 | Py_XDECREF(ret); |
| 3544 | Py_XDECREF(ah); |
| 3545 | Py_XDECREF(al); |
| 3546 | Py_XDECREF(bh); |
| 3547 | Py_XDECREF(bl); |
| 3548 | return NULL; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3549 | } |
| 3550 | |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3551 | /* (*) Why adding t3 can't "run out of room" above. |
| 3552 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3553 | Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts |
| 3554 | to start with: |
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 | 1. For any integer i, i = c(i/2) + f(i/2). In particular, |
| 3557 | bsize = c(bsize/2) + f(bsize/2). |
| 3558 | 2. shift = f(bsize/2) |
| 3559 | 3. asize <= bsize |
| 3560 | 4. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this |
| 3561 | routine, so asize > bsize/2 >= f(bsize/2) in this routine. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3562 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3563 | We allocated asize + bsize result digits, and add t3 into them at an offset |
| 3564 | of shift. This leaves asize+bsize-shift allocated digit positions for t3 |
| 3565 | to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) = |
| 3566 | asize + c(bsize/2) available digit positions. |
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 | bh has c(bsize/2) digits, and bl at most f(size/2) digits. So bh+hl has |
| 3569 | at most c(bsize/2) digits + 1 bit. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3570 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3571 | If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2) |
| 3572 | digits, and al has at most f(bsize/2) digits in any case. So ah+al has at |
| 3573 | most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3574 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3575 | The product (ah+al)*(bh+bl) therefore has at most |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3576 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3577 | 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] | 3578 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3579 | and we have asize + c(bsize/2) available digit positions. We need to show |
| 3580 | this is always enough. An instance of c(bsize/2) cancels out in both, so |
| 3581 | the question reduces to whether asize digits is enough to hold |
| 3582 | (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits. If asize < bsize, |
| 3583 | then we're asking whether asize digits >= f(bsize/2) digits + 2 bits. By #4, |
| 3584 | 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] | 3585 | 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] | 3586 | 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] | 3587 | c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits |
| 3588 | is enough to hold 2 bits. This is so if bsize >= 2, which holds because |
| 3589 | bsize >= KARATSUBA_CUTOFF >= 2. |
Tim Peters | 8e966ee | 2002-08-14 16:36:23 +0000 | [diff] [blame] | 3590 | |
Tim Peters | 48d52c0 | 2002-08-14 17:07:32 +0000 | [diff] [blame] | 3591 | Note that since there's always enough room for (ah+al)*(bh+bl), and that's |
| 3592 | clearly >= each of ah*bh and al*bl, there's always enough room to subtract |
| 3593 | ah*bh and al*bl too. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3594 | */ |
| 3595 | |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3596 | /* b has at least twice the digits of a, and a is big enough that Karatsuba |
| 3597 | * would pay off *if* the inputs had balanced sizes. View b as a sequence |
| 3598 | * of slices, each with a->ob_size digits, and multiply the slices by a, |
| 3599 | * one at a time. This gives k_mul balanced inputs to work with, and is |
| 3600 | * also cache-friendly (we compute one double-width slice of the result |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 3601 | * at a time, then move on, never backtracking except for the helpful |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3602 | * single-width slice overlap between successive partial sums). |
| 3603 | */ |
| 3604 | static PyLongObject * |
| 3605 | k_lopsided_mul(PyLongObject *a, PyLongObject *b) |
| 3606 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3607 | const Py_ssize_t asize = Py_ABS(Py_SIZE(a)); |
| 3608 | Py_ssize_t bsize = Py_ABS(Py_SIZE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3609 | Py_ssize_t nbdone; /* # of b digits already multiplied */ |
| 3610 | PyLongObject *ret; |
| 3611 | PyLongObject *bslice = NULL; |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3612 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3613 | assert(asize > KARATSUBA_CUTOFF); |
| 3614 | assert(2 * asize <= bsize); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3615 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3616 | /* Allocate result space, and zero it out. */ |
| 3617 | ret = _PyLong_New(asize + bsize); |
| 3618 | if (ret == NULL) |
| 3619 | return NULL; |
| 3620 | memset(ret->ob_digit, 0, Py_SIZE(ret) * sizeof(digit)); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3621 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3622 | /* Successive slices of b are copied into bslice. */ |
| 3623 | bslice = _PyLong_New(asize); |
| 3624 | if (bslice == NULL) |
| 3625 | goto fail; |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3626 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3627 | nbdone = 0; |
| 3628 | while (bsize > 0) { |
| 3629 | PyLongObject *product; |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 3630 | const Py_ssize_t nbtouse = Py_MIN(bsize, asize); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3631 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3632 | /* Multiply the next slice of b by a. */ |
| 3633 | memcpy(bslice->ob_digit, b->ob_digit + nbdone, |
| 3634 | nbtouse * sizeof(digit)); |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 3635 | Py_SET_SIZE(bslice, nbtouse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3636 | product = k_mul(a, bslice); |
| 3637 | if (product == NULL) |
| 3638 | goto fail; |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3639 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3640 | /* Add into result. */ |
| 3641 | (void)v_iadd(ret->ob_digit + nbdone, Py_SIZE(ret) - nbdone, |
| 3642 | product->ob_digit, Py_SIZE(product)); |
| 3643 | Py_DECREF(product); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3644 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3645 | bsize -= nbtouse; |
| 3646 | nbdone += nbtouse; |
| 3647 | } |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3648 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3649 | Py_DECREF(bslice); |
| 3650 | return long_normalize(ret); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3651 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3652 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3653 | Py_DECREF(ret); |
| 3654 | Py_XDECREF(bslice); |
| 3655 | return NULL; |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3656 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3657 | |
| 3658 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3659 | long_mul(PyLongObject *a, PyLongObject *b) |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3660 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3661 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3662 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3663 | CHECK_BINOP(a, b); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3664 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3665 | /* fast path for single-digit multiplication */ |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3666 | 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] | 3667 | stwodigits v = (stwodigits)(MEDIUM_VALUE(a)) * MEDIUM_VALUE(b); |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 3668 | return PyLong_FromLongLong((long long)v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3669 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3670 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3671 | z = k_mul(a, b); |
| 3672 | /* Negate if exactly one of the inputs is negative. */ |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 3673 | if (((Py_SIZE(a) ^ Py_SIZE(b)) < 0) && z) { |
| 3674 | _PyLong_Negate(&z); |
| 3675 | if (z == NULL) |
| 3676 | return NULL; |
| 3677 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3678 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3679 | } |
| 3680 | |
Yury Selivanov | e0b2309 | 2016-02-11 10:26:27 -0500 | [diff] [blame] | 3681 | /* Fast modulo division for single-digit longs. */ |
| 3682 | static PyObject * |
| 3683 | fast_mod(PyLongObject *a, PyLongObject *b) |
| 3684 | { |
| 3685 | sdigit left = a->ob_digit[0]; |
| 3686 | sdigit right = b->ob_digit[0]; |
| 3687 | sdigit mod; |
| 3688 | |
| 3689 | assert(Py_ABS(Py_SIZE(a)) == 1); |
| 3690 | assert(Py_ABS(Py_SIZE(b)) == 1); |
| 3691 | |
| 3692 | if (Py_SIZE(a) == Py_SIZE(b)) { |
| 3693 | /* 'a' and 'b' have the same sign. */ |
| 3694 | mod = left % right; |
| 3695 | } |
| 3696 | else { |
| 3697 | /* Either 'a' or 'b' is negative. */ |
| 3698 | mod = right - 1 - (left - 1) % right; |
| 3699 | } |
| 3700 | |
Victor Stinner | f963c13 | 2016-03-23 18:36:54 +0100 | [diff] [blame] | 3701 | return PyLong_FromLong(mod * (sdigit)Py_SIZE(b)); |
Yury Selivanov | e0b2309 | 2016-02-11 10:26:27 -0500 | [diff] [blame] | 3702 | } |
| 3703 | |
| 3704 | /* Fast floor division for single-digit longs. */ |
| 3705 | static PyObject * |
| 3706 | fast_floor_div(PyLongObject *a, PyLongObject *b) |
| 3707 | { |
| 3708 | sdigit left = a->ob_digit[0]; |
| 3709 | sdigit right = b->ob_digit[0]; |
| 3710 | sdigit div; |
| 3711 | |
| 3712 | assert(Py_ABS(Py_SIZE(a)) == 1); |
| 3713 | assert(Py_ABS(Py_SIZE(b)) == 1); |
| 3714 | |
| 3715 | if (Py_SIZE(a) == Py_SIZE(b)) { |
| 3716 | /* 'a' and 'b' have the same sign. */ |
| 3717 | div = left / right; |
| 3718 | } |
| 3719 | else { |
| 3720 | /* Either 'a' or 'b' is negative. */ |
| 3721 | div = -1 - (left - 1) / right; |
| 3722 | } |
| 3723 | |
| 3724 | return PyLong_FromLong(div); |
| 3725 | } |
| 3726 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3727 | /* The / and % operators are now defined in terms of divmod(). |
| 3728 | The expression a mod b has the value a - b*floor(a/b). |
| 3729 | The long_divrem function gives the remainder after division of |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3730 | |a| by |b|, with the sign of a. This is also expressed |
| 3731 | as a - b*trunc(a/b), if trunc truncates towards zero. |
| 3732 | Some examples: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3733 | a b a rem b a mod b |
| 3734 | 13 10 3 3 |
| 3735 | -13 10 -3 7 |
| 3736 | 13 -10 3 -7 |
| 3737 | -13 -10 -3 -3 |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3738 | 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] | 3739 | have different signs. We then subtract one from the 'div' |
| 3740 | part of the outcome to keep the invariant intact. */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3741 | |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3742 | /* Compute |
| 3743 | * *pdiv, *pmod = divmod(v, w) |
| 3744 | * NULL can be passed for pdiv or pmod, in which case that part of |
| 3745 | * the result is simply thrown away. The caller owns a reference to |
| 3746 | * each of these it requests (does not pass NULL for). |
| 3747 | */ |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3748 | static int |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3749 | l_divmod(PyLongObject *v, PyLongObject *w, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3750 | PyLongObject **pdiv, PyLongObject **pmod) |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3751 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3752 | PyLongObject *div, *mod; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3753 | |
Yury Selivanov | e0b2309 | 2016-02-11 10:26:27 -0500 | [diff] [blame] | 3754 | if (Py_ABS(Py_SIZE(v)) == 1 && Py_ABS(Py_SIZE(w)) == 1) { |
| 3755 | /* Fast path for single-digit longs */ |
| 3756 | div = NULL; |
| 3757 | if (pdiv != NULL) { |
| 3758 | div = (PyLongObject *)fast_floor_div(v, w); |
| 3759 | if (div == NULL) { |
| 3760 | return -1; |
| 3761 | } |
| 3762 | } |
| 3763 | if (pmod != NULL) { |
| 3764 | mod = (PyLongObject *)fast_mod(v, w); |
| 3765 | if (mod == NULL) { |
| 3766 | Py_XDECREF(div); |
| 3767 | return -1; |
| 3768 | } |
| 3769 | *pmod = mod; |
| 3770 | } |
| 3771 | if (pdiv != NULL) { |
| 3772 | /* We only want to set `*pdiv` when `*pmod` is |
| 3773 | set successfully. */ |
| 3774 | *pdiv = div; |
| 3775 | } |
| 3776 | return 0; |
| 3777 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3778 | if (long_divrem(v, w, &div, &mod) < 0) |
| 3779 | return -1; |
| 3780 | if ((Py_SIZE(mod) < 0 && Py_SIZE(w) > 0) || |
| 3781 | (Py_SIZE(mod) > 0 && Py_SIZE(w) < 0)) { |
| 3782 | PyLongObject *temp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3783 | temp = (PyLongObject *) long_add(mod, w); |
| 3784 | Py_DECREF(mod); |
| 3785 | mod = temp; |
| 3786 | if (mod == NULL) { |
| 3787 | Py_DECREF(div); |
| 3788 | return -1; |
| 3789 | } |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 3790 | temp = (PyLongObject *) long_sub(div, (PyLongObject *)_PyLong_One); |
| 3791 | if (temp == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3792 | Py_DECREF(mod); |
| 3793 | Py_DECREF(div); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3794 | return -1; |
| 3795 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3796 | Py_DECREF(div); |
| 3797 | div = temp; |
| 3798 | } |
| 3799 | if (pdiv != NULL) |
| 3800 | *pdiv = div; |
| 3801 | else |
| 3802 | Py_DECREF(div); |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3803 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3804 | if (pmod != NULL) |
| 3805 | *pmod = mod; |
| 3806 | else |
| 3807 | Py_DECREF(mod); |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3808 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3809 | return 0; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3810 | } |
| 3811 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3812 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3813 | long_div(PyObject *a, PyObject *b) |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3814 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3815 | PyLongObject *div; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3816 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3817 | CHECK_BINOP(a, b); |
Yury Selivanov | e0b2309 | 2016-02-11 10:26:27 -0500 | [diff] [blame] | 3818 | |
| 3819 | if (Py_ABS(Py_SIZE(a)) == 1 && Py_ABS(Py_SIZE(b)) == 1) { |
| 3820 | return fast_floor_div((PyLongObject*)a, (PyLongObject*)b); |
| 3821 | } |
| 3822 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3823 | if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, NULL) < 0) |
| 3824 | div = NULL; |
| 3825 | return (PyObject *)div; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3826 | } |
| 3827 | |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3828 | /* PyLong/PyLong -> float, with correctly rounded result. */ |
| 3829 | |
| 3830 | #define MANT_DIG_DIGITS (DBL_MANT_DIG / PyLong_SHIFT) |
| 3831 | #define MANT_DIG_BITS (DBL_MANT_DIG % PyLong_SHIFT) |
| 3832 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3833 | static PyObject * |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3834 | long_true_divide(PyObject *v, PyObject *w) |
Tim Peters | 20dab9f | 2001-09-04 05:31:47 +0000 | [diff] [blame] | 3835 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3836 | PyLongObject *a, *b, *x; |
| 3837 | Py_ssize_t a_size, b_size, shift, extra_bits, diff, x_size, x_bits; |
| 3838 | digit mask, low; |
| 3839 | int inexact, negate, a_is_small, b_is_small; |
| 3840 | double dx, result; |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 3841 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3842 | CHECK_BINOP(v, w); |
| 3843 | a = (PyLongObject *)v; |
| 3844 | b = (PyLongObject *)w; |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 3845 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3846 | /* |
| 3847 | Method in a nutshell: |
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 | 0. reduce to case a, b > 0; filter out obvious underflow/overflow |
| 3850 | 1. choose a suitable integer 'shift' |
| 3851 | 2. use integer arithmetic to compute x = floor(2**-shift*a/b) |
| 3852 | 3. adjust x for correct rounding |
| 3853 | 4. convert x to a double dx with the same value |
| 3854 | 5. return ldexp(dx, shift). |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3855 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3856 | In more detail: |
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 | 0. For any a, a/0 raises ZeroDivisionError; for nonzero b, 0/b |
| 3859 | returns either 0.0 or -0.0, depending on the sign of b. For a and |
| 3860 | b both nonzero, ignore signs of a and b, and add the sign back in |
| 3861 | at the end. Now write a_bits and b_bits for the bit lengths of a |
| 3862 | and b respectively (that is, a_bits = 1 + floor(log_2(a)); likewise |
| 3863 | for b). Then |
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 | 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] | 3866 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3867 | So if a_bits - b_bits > DBL_MAX_EXP then a/b > 2**DBL_MAX_EXP and |
| 3868 | so overflows. Similarly, if a_bits - b_bits < DBL_MIN_EXP - |
| 3869 | DBL_MANT_DIG - 1 then a/b underflows to 0. With these cases out of |
| 3870 | the way, we can assume that |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3871 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3872 | 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] | 3873 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3874 | 1. The integer 'shift' is chosen so that x has the right number of |
| 3875 | bits for a double, plus two or three extra bits that will be used |
| 3876 | in the rounding decisions. Writing a_bits and b_bits for the |
| 3877 | number of significant bits in a and b respectively, a |
| 3878 | straightforward formula for shift 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 = a_bits - b_bits - 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 | This is fine in the usual case, but if a/b is smaller than the |
| 3883 | smallest normal float then it can lead to double rounding on an |
| 3884 | IEEE 754 platform, giving incorrectly rounded results. So we |
| 3885 | adjust the formula slightly. The actual formula used is: |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3886 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3887 | 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] | 3888 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3889 | 2. The quantity x is computed by first shifting a (left -shift bits |
| 3890 | if shift <= 0, right shift bits if shift > 0) and then dividing by |
| 3891 | b. For both the shift and the division, we keep track of whether |
| 3892 | the result is inexact, in a flag 'inexact'; this information is |
| 3893 | needed at the rounding stage. |
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 | With the choice of shift above, together with our assumption that |
| 3896 | a_bits - b_bits >= DBL_MIN_EXP - DBL_MANT_DIG - 1, it follows |
| 3897 | that x >= 1. |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3898 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3899 | 3. Now x * 2**shift <= a/b < (x+1) * 2**shift. We want to replace |
| 3900 | this with an exactly representable float of the form |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3901 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3902 | round(x/2**extra_bits) * 2**(extra_bits+shift). |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3903 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3904 | For float representability, we need x/2**extra_bits < |
| 3905 | 2**DBL_MANT_DIG and extra_bits + shift >= DBL_MIN_EXP - |
| 3906 | DBL_MANT_DIG. This translates to the condition: |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3907 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3908 | extra_bits >= MAX(x_bits, DBL_MIN_EXP - shift) - DBL_MANT_DIG |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3909 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3910 | To round, we just modify the bottom digit of x in-place; this can |
| 3911 | end up giving a digit with value > PyLONG_MASK, but that's not a |
| 3912 | problem since digits can hold values up to 2*PyLONG_MASK+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 | With the original choices for shift above, extra_bits will always |
| 3915 | be 2 or 3. Then rounding under the round-half-to-even rule, we |
| 3916 | round up iff the most significant of the extra bits is 1, and |
| 3917 | either: (a) the computation of x in step 2 had an inexact result, |
| 3918 | or (b) at least one other of the extra bits is 1, or (c) the least |
| 3919 | significant bit of x (above those to be rounded) is 1. |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3920 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3921 | 4. Conversion to a double is straightforward; all floating-point |
| 3922 | operations involved in the conversion are exact, so there's no |
| 3923 | danger of rounding errors. |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3924 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3925 | 5. Use ldexp(x, shift) to compute x*2**shift, the final result. |
| 3926 | The result will always be exactly representable as a double, except |
| 3927 | in the case that it overflows. To avoid dependence on the exact |
| 3928 | behaviour of ldexp on overflow, we check for overflow before |
| 3929 | applying ldexp. The result of ldexp is adjusted for sign before |
| 3930 | returning. |
| 3931 | */ |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3932 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3933 | /* Reduce to case where a and b are both positive. */ |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3934 | a_size = Py_ABS(Py_SIZE(a)); |
| 3935 | b_size = Py_ABS(Py_SIZE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3936 | negate = (Py_SIZE(a) < 0) ^ (Py_SIZE(b) < 0); |
| 3937 | if (b_size == 0) { |
| 3938 | PyErr_SetString(PyExc_ZeroDivisionError, |
| 3939 | "division by zero"); |
| 3940 | goto error; |
| 3941 | } |
| 3942 | if (a_size == 0) |
| 3943 | goto underflow_or_zero; |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3944 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3945 | /* Fast path for a and b small (exactly representable in a double). |
| 3946 | Relies on floating-point division being correctly rounded; results |
| 3947 | may be subject to double rounding on x86 machines that operate with |
| 3948 | the x87 FPU set to 64-bit precision. */ |
| 3949 | a_is_small = a_size <= MANT_DIG_DIGITS || |
| 3950 | (a_size == MANT_DIG_DIGITS+1 && |
| 3951 | a->ob_digit[MANT_DIG_DIGITS] >> MANT_DIG_BITS == 0); |
| 3952 | b_is_small = b_size <= MANT_DIG_DIGITS || |
| 3953 | (b_size == MANT_DIG_DIGITS+1 && |
| 3954 | b->ob_digit[MANT_DIG_DIGITS] >> MANT_DIG_BITS == 0); |
| 3955 | if (a_is_small && b_is_small) { |
| 3956 | double da, db; |
| 3957 | da = a->ob_digit[--a_size]; |
| 3958 | while (a_size > 0) |
| 3959 | da = da * PyLong_BASE + a->ob_digit[--a_size]; |
| 3960 | db = b->ob_digit[--b_size]; |
| 3961 | while (b_size > 0) |
| 3962 | db = db * PyLong_BASE + b->ob_digit[--b_size]; |
| 3963 | result = da / db; |
| 3964 | goto success; |
| 3965 | } |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 3966 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3967 | /* Catch obvious cases of underflow and overflow */ |
| 3968 | diff = a_size - b_size; |
| 3969 | if (diff > PY_SSIZE_T_MAX/PyLong_SHIFT - 1) |
| 3970 | /* Extreme overflow */ |
| 3971 | goto overflow; |
| 3972 | else if (diff < 1 - PY_SSIZE_T_MAX/PyLong_SHIFT) |
| 3973 | /* Extreme underflow */ |
| 3974 | goto underflow_or_zero; |
| 3975 | /* Next line is now safe from overflowing a Py_ssize_t */ |
Niklas Fiekas | c5b7900 | 2020-01-16 15:09:19 +0100 | [diff] [blame] | 3976 | diff = diff * PyLong_SHIFT + _Py_bit_length(a->ob_digit[a_size - 1]) - |
| 3977 | _Py_bit_length(b->ob_digit[b_size - 1]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3978 | /* Now diff = a_bits - b_bits. */ |
| 3979 | if (diff > DBL_MAX_EXP) |
| 3980 | goto overflow; |
| 3981 | else if (diff < DBL_MIN_EXP - DBL_MANT_DIG - 1) |
| 3982 | goto underflow_or_zero; |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 3983 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3984 | /* Choose value for shift; see comments for step 1 above. */ |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 3985 | shift = Py_MAX(diff, DBL_MIN_EXP) - DBL_MANT_DIG - 2; |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3986 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3987 | inexact = 0; |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3988 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3989 | /* x = abs(a * 2**-shift) */ |
| 3990 | if (shift <= 0) { |
| 3991 | Py_ssize_t i, shift_digits = -shift / PyLong_SHIFT; |
| 3992 | digit rem; |
| 3993 | /* x = a << -shift */ |
| 3994 | if (a_size >= PY_SSIZE_T_MAX - 1 - shift_digits) { |
| 3995 | /* In practice, it's probably impossible to end up |
| 3996 | here. Both a and b would have to be enormous, |
| 3997 | using close to SIZE_T_MAX bytes of memory each. */ |
| 3998 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3999 | "intermediate overflow during division"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4000 | goto error; |
| 4001 | } |
| 4002 | x = _PyLong_New(a_size + shift_digits + 1); |
| 4003 | if (x == NULL) |
| 4004 | goto error; |
| 4005 | for (i = 0; i < shift_digits; i++) |
| 4006 | x->ob_digit[i] = 0; |
| 4007 | rem = v_lshift(x->ob_digit + shift_digits, a->ob_digit, |
| 4008 | a_size, -shift % PyLong_SHIFT); |
| 4009 | x->ob_digit[a_size + shift_digits] = rem; |
| 4010 | } |
| 4011 | else { |
| 4012 | Py_ssize_t shift_digits = shift / PyLong_SHIFT; |
| 4013 | digit rem; |
| 4014 | /* x = a >> shift */ |
| 4015 | assert(a_size >= shift_digits); |
| 4016 | x = _PyLong_New(a_size - shift_digits); |
| 4017 | if (x == NULL) |
| 4018 | goto error; |
| 4019 | rem = v_rshift(x->ob_digit, a->ob_digit + shift_digits, |
| 4020 | a_size - shift_digits, shift % PyLong_SHIFT); |
| 4021 | /* set inexact if any of the bits shifted out is nonzero */ |
| 4022 | if (rem) |
| 4023 | inexact = 1; |
| 4024 | while (!inexact && shift_digits > 0) |
| 4025 | if (a->ob_digit[--shift_digits]) |
| 4026 | inexact = 1; |
| 4027 | } |
| 4028 | long_normalize(x); |
| 4029 | x_size = Py_SIZE(x); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 4030 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4031 | /* x //= b. If the remainder is nonzero, set inexact. We own the only |
| 4032 | reference to x, so it's safe to modify it in-place. */ |
| 4033 | if (b_size == 1) { |
| 4034 | digit rem = inplace_divrem1(x->ob_digit, x->ob_digit, x_size, |
| 4035 | b->ob_digit[0]); |
| 4036 | long_normalize(x); |
| 4037 | if (rem) |
| 4038 | inexact = 1; |
| 4039 | } |
| 4040 | else { |
| 4041 | PyLongObject *div, *rem; |
| 4042 | div = x_divrem(x, b, &rem); |
| 4043 | Py_DECREF(x); |
| 4044 | x = div; |
| 4045 | if (x == NULL) |
| 4046 | goto error; |
| 4047 | if (Py_SIZE(rem)) |
| 4048 | inexact = 1; |
| 4049 | Py_DECREF(rem); |
| 4050 | } |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 4051 | x_size = Py_ABS(Py_SIZE(x)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4052 | assert(x_size > 0); /* result of division is never zero */ |
Niklas Fiekas | c5b7900 | 2020-01-16 15:09:19 +0100 | [diff] [blame] | 4053 | 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] | 4054 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4055 | /* The number of extra bits that have to be rounded away. */ |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 4056 | 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] | 4057 | assert(extra_bits == 2 || extra_bits == 3); |
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 | /* Round by directly modifying the low digit of x. */ |
| 4060 | mask = (digit)1 << (extra_bits - 1); |
| 4061 | low = x->ob_digit[0] | inexact; |
Mark Dickinson | dc590a4 | 2016-08-21 10:23:23 +0100 | [diff] [blame] | 4062 | if ((low & mask) && (low & (3U*mask-1U))) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4063 | low += mask; |
Mark Dickinson | dc590a4 | 2016-08-21 10:23:23 +0100 | [diff] [blame] | 4064 | x->ob_digit[0] = low & ~(2U*mask-1U); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 4065 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4066 | /* Convert x to a double dx; the conversion is exact. */ |
| 4067 | dx = x->ob_digit[--x_size]; |
| 4068 | while (x_size > 0) |
| 4069 | dx = dx * PyLong_BASE + x->ob_digit[--x_size]; |
| 4070 | Py_DECREF(x); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 4071 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4072 | /* Check whether ldexp result will overflow a double. */ |
| 4073 | if (shift + x_bits >= DBL_MAX_EXP && |
| 4074 | (shift + x_bits > DBL_MAX_EXP || dx == ldexp(1.0, (int)x_bits))) |
| 4075 | goto overflow; |
| 4076 | result = ldexp(dx, (int)shift); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 4077 | |
| 4078 | success: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4079 | return PyFloat_FromDouble(negate ? -result : result); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 4080 | |
| 4081 | underflow_or_zero: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4082 | return PyFloat_FromDouble(negate ? -0.0 : 0.0); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 4083 | |
| 4084 | overflow: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4085 | PyErr_SetString(PyExc_OverflowError, |
| 4086 | "integer division result too large for a float"); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 4087 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4088 | return NULL; |
Tim Peters | 20dab9f | 2001-09-04 05:31:47 +0000 | [diff] [blame] | 4089 | } |
| 4090 | |
| 4091 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 4092 | long_mod(PyObject *a, PyObject *b) |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 4093 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4094 | PyLongObject *mod; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 4095 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4096 | CHECK_BINOP(a, b); |
| 4097 | |
Yury Selivanov | e0b2309 | 2016-02-11 10:26:27 -0500 | [diff] [blame] | 4098 | if (Py_ABS(Py_SIZE(a)) == 1 && Py_ABS(Py_SIZE(b)) == 1) { |
| 4099 | return fast_mod((PyLongObject*)a, (PyLongObject*)b); |
| 4100 | } |
| 4101 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4102 | if (l_divmod((PyLongObject*)a, (PyLongObject*)b, NULL, &mod) < 0) |
| 4103 | mod = NULL; |
| 4104 | return (PyObject *)mod; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 4105 | } |
| 4106 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4107 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 4108 | long_divmod(PyObject *a, PyObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4109 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4110 | PyLongObject *div, *mod; |
| 4111 | PyObject *z; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 4112 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4113 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 4114 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4115 | if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, &mod) < 0) { |
| 4116 | return NULL; |
| 4117 | } |
| 4118 | z = PyTuple_New(2); |
| 4119 | if (z != NULL) { |
Sergey Fedoseev | ea6207d | 2019-02-21 15:01:11 +0500 | [diff] [blame] | 4120 | PyTuple_SET_ITEM(z, 0, (PyObject *) div); |
| 4121 | PyTuple_SET_ITEM(z, 1, (PyObject *) mod); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4122 | } |
| 4123 | else { |
| 4124 | Py_DECREF(div); |
| 4125 | Py_DECREF(mod); |
| 4126 | } |
| 4127 | return z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4128 | } |
| 4129 | |
Mark Dickinson | c529967 | 2019-06-02 10:24:06 +0100 | [diff] [blame] | 4130 | |
| 4131 | /* Compute an inverse to a modulo n, or raise ValueError if a is not |
| 4132 | invertible modulo n. Assumes n is positive. The inverse returned |
| 4133 | is whatever falls out of the extended Euclidean algorithm: it may |
| 4134 | be either positive or negative, but will be smaller than n in |
| 4135 | absolute value. |
| 4136 | |
| 4137 | Pure Python equivalent for long_invmod: |
| 4138 | |
| 4139 | def invmod(a, n): |
| 4140 | b, c = 1, 0 |
| 4141 | while n: |
| 4142 | q, r = divmod(a, n) |
| 4143 | a, b, c, n = n, c, b - q*c, r |
| 4144 | |
| 4145 | # at this point a is the gcd of the original inputs |
| 4146 | if a == 1: |
| 4147 | return b |
| 4148 | raise ValueError("Not invertible") |
| 4149 | */ |
| 4150 | |
| 4151 | static PyLongObject * |
| 4152 | long_invmod(PyLongObject *a, PyLongObject *n) |
| 4153 | { |
| 4154 | PyLongObject *b, *c; |
| 4155 | |
| 4156 | /* Should only ever be called for positive n */ |
| 4157 | assert(Py_SIZE(n) > 0); |
| 4158 | |
| 4159 | b = (PyLongObject *)PyLong_FromLong(1L); |
| 4160 | if (b == NULL) { |
| 4161 | return NULL; |
| 4162 | } |
| 4163 | c = (PyLongObject *)PyLong_FromLong(0L); |
| 4164 | if (c == NULL) { |
| 4165 | Py_DECREF(b); |
| 4166 | return NULL; |
| 4167 | } |
| 4168 | Py_INCREF(a); |
| 4169 | Py_INCREF(n); |
| 4170 | |
| 4171 | /* references now owned: a, b, c, n */ |
| 4172 | while (Py_SIZE(n) != 0) { |
| 4173 | PyLongObject *q, *r, *s, *t; |
| 4174 | |
| 4175 | if (l_divmod(a, n, &q, &r) == -1) { |
| 4176 | goto Error; |
| 4177 | } |
| 4178 | Py_DECREF(a); |
| 4179 | a = n; |
| 4180 | n = r; |
| 4181 | t = (PyLongObject *)long_mul(q, c); |
| 4182 | Py_DECREF(q); |
| 4183 | if (t == NULL) { |
| 4184 | goto Error; |
| 4185 | } |
| 4186 | s = (PyLongObject *)long_sub(b, t); |
| 4187 | Py_DECREF(t); |
| 4188 | if (s == NULL) { |
| 4189 | goto Error; |
| 4190 | } |
| 4191 | Py_DECREF(b); |
| 4192 | b = c; |
| 4193 | c = s; |
| 4194 | } |
| 4195 | /* references now owned: a, b, c, n */ |
| 4196 | |
| 4197 | Py_DECREF(c); |
| 4198 | Py_DECREF(n); |
Petr Viktorin | 1e375c6 | 2019-06-03 02:28:29 +0200 | [diff] [blame] | 4199 | if (long_compare(a, (PyLongObject *)_PyLong_One)) { |
Mark Dickinson | c529967 | 2019-06-02 10:24:06 +0100 | [diff] [blame] | 4200 | /* a != 1; we don't have an inverse. */ |
| 4201 | Py_DECREF(a); |
| 4202 | Py_DECREF(b); |
| 4203 | PyErr_SetString(PyExc_ValueError, |
| 4204 | "base is not invertible for the given modulus"); |
| 4205 | return NULL; |
| 4206 | } |
| 4207 | else { |
| 4208 | /* a == 1; b gives an inverse modulo n */ |
| 4209 | Py_DECREF(a); |
| 4210 | return b; |
| 4211 | } |
| 4212 | |
| 4213 | Error: |
| 4214 | Py_DECREF(a); |
| 4215 | Py_DECREF(b); |
| 4216 | Py_DECREF(c); |
| 4217 | Py_DECREF(n); |
| 4218 | return NULL; |
| 4219 | } |
| 4220 | |
| 4221 | |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4222 | /* pow(v, w, x) */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4223 | static PyObject * |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 4224 | long_pow(PyObject *v, PyObject *w, PyObject *x) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4225 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4226 | PyLongObject *a, *b, *c; /* a,b,c = v,w,x */ |
| 4227 | int negativeOutput = 0; /* if x<0 return negative output */ |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 4228 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4229 | PyLongObject *z = NULL; /* accumulated result */ |
| 4230 | Py_ssize_t i, j, k; /* counters */ |
| 4231 | PyLongObject *temp = NULL; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4232 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4233 | /* 5-ary values. If the exponent is large enough, table is |
| 4234 | * precomputed so that table[i] == a**i % c for i in range(32). |
| 4235 | */ |
| 4236 | PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 4237 | 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] | 4238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4239 | /* a, b, c = v, w, x */ |
| 4240 | CHECK_BINOP(v, w); |
| 4241 | a = (PyLongObject*)v; Py_INCREF(a); |
| 4242 | b = (PyLongObject*)w; Py_INCREF(b); |
| 4243 | if (PyLong_Check(x)) { |
| 4244 | c = (PyLongObject *)x; |
| 4245 | Py_INCREF(x); |
| 4246 | } |
| 4247 | else if (x == Py_None) |
| 4248 | c = NULL; |
| 4249 | else { |
| 4250 | Py_DECREF(a); |
| 4251 | Py_DECREF(b); |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 4252 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4253 | } |
Tim Peters | 4c483c4 | 2001-09-05 06:24:58 +0000 | [diff] [blame] | 4254 | |
Mark Dickinson | c529967 | 2019-06-02 10:24:06 +0100 | [diff] [blame] | 4255 | if (Py_SIZE(b) < 0 && c == NULL) { |
| 4256 | /* if exponent is negative and there's no modulus: |
| 4257 | return a float. This works because we know |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4258 | that this calls float_pow() which converts its |
| 4259 | arguments to double. */ |
Mark Dickinson | c529967 | 2019-06-02 10:24:06 +0100 | [diff] [blame] | 4260 | Py_DECREF(a); |
| 4261 | Py_DECREF(b); |
| 4262 | return PyFloat_Type.tp_as_number->nb_power(v, w, x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4263 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4264 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4265 | if (c) { |
| 4266 | /* if modulus == 0: |
| 4267 | raise ValueError() */ |
| 4268 | if (Py_SIZE(c) == 0) { |
| 4269 | PyErr_SetString(PyExc_ValueError, |
| 4270 | "pow() 3rd argument cannot be 0"); |
| 4271 | goto Error; |
| 4272 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4273 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4274 | /* if modulus < 0: |
| 4275 | negativeOutput = True |
| 4276 | modulus = -modulus */ |
| 4277 | if (Py_SIZE(c) < 0) { |
| 4278 | negativeOutput = 1; |
| 4279 | temp = (PyLongObject *)_PyLong_Copy(c); |
| 4280 | if (temp == NULL) |
| 4281 | goto Error; |
| 4282 | Py_DECREF(c); |
| 4283 | c = temp; |
| 4284 | temp = NULL; |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 4285 | _PyLong_Negate(&c); |
| 4286 | if (c == NULL) |
| 4287 | goto Error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4288 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4290 | /* if modulus == 1: |
| 4291 | return 0 */ |
| 4292 | if ((Py_SIZE(c) == 1) && (c->ob_digit[0] == 1)) { |
| 4293 | z = (PyLongObject *)PyLong_FromLong(0L); |
| 4294 | goto Done; |
| 4295 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4296 | |
Mark Dickinson | c529967 | 2019-06-02 10:24:06 +0100 | [diff] [blame] | 4297 | /* if exponent is negative, negate the exponent and |
| 4298 | replace the base with a modular inverse */ |
| 4299 | if (Py_SIZE(b) < 0) { |
| 4300 | temp = (PyLongObject *)_PyLong_Copy(b); |
| 4301 | if (temp == NULL) |
| 4302 | goto Error; |
| 4303 | Py_DECREF(b); |
| 4304 | b = temp; |
| 4305 | temp = NULL; |
| 4306 | _PyLong_Negate(&b); |
| 4307 | if (b == NULL) |
| 4308 | goto Error; |
| 4309 | |
| 4310 | temp = long_invmod(a, c); |
| 4311 | if (temp == NULL) |
| 4312 | goto Error; |
| 4313 | Py_DECREF(a); |
| 4314 | a = temp; |
| 4315 | } |
| 4316 | |
Tim Peters | 81a9315 | 2013-10-05 16:53:52 -0500 | [diff] [blame] | 4317 | /* Reduce base by modulus in some cases: |
| 4318 | 1. If base < 0. Forcing the base non-negative makes things easier. |
| 4319 | 2. If base is obviously larger than the modulus. The "small |
| 4320 | exponent" case later can multiply directly by base repeatedly, |
| 4321 | while the "large exponent" case multiplies directly by base 31 |
| 4322 | times. It can be unboundedly faster to multiply by |
| 4323 | base % modulus instead. |
| 4324 | We could _always_ do this reduction, but l_divmod() isn't cheap, |
| 4325 | so we only do it when it buys something. */ |
| 4326 | if (Py_SIZE(a) < 0 || Py_SIZE(a) > Py_SIZE(c)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4327 | if (l_divmod(a, c, NULL, &temp) < 0) |
| 4328 | goto Error; |
| 4329 | Py_DECREF(a); |
| 4330 | a = temp; |
| 4331 | temp = NULL; |
| 4332 | } |
| 4333 | } |
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 | /* At this point a, b, and c are guaranteed non-negative UNLESS |
| 4336 | c is NULL, in which case a may be negative. */ |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4337 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4338 | z = (PyLongObject *)PyLong_FromLong(1L); |
| 4339 | if (z == NULL) |
| 4340 | goto Error; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4341 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4342 | /* Perform a modular reduction, X = X % c, but leave X alone if c |
| 4343 | * is NULL. |
| 4344 | */ |
| 4345 | #define REDUCE(X) \ |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4346 | do { \ |
| 4347 | if (c != NULL) { \ |
| 4348 | if (l_divmod(X, c, NULL, &temp) < 0) \ |
| 4349 | goto Error; \ |
| 4350 | Py_XDECREF(X); \ |
| 4351 | X = temp; \ |
| 4352 | temp = NULL; \ |
| 4353 | } \ |
| 4354 | } while(0) |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4355 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4356 | /* Multiply two values, then reduce the result: |
| 4357 | result = X*Y % c. If c is NULL, skip the mod. */ |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4358 | #define MULT(X, Y, result) \ |
| 4359 | do { \ |
| 4360 | temp = (PyLongObject *)long_mul(X, Y); \ |
| 4361 | if (temp == NULL) \ |
| 4362 | goto Error; \ |
| 4363 | Py_XDECREF(result); \ |
| 4364 | result = temp; \ |
| 4365 | temp = NULL; \ |
| 4366 | REDUCE(result); \ |
| 4367 | } while(0) |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4368 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4369 | if (Py_SIZE(b) <= FIVEARY_CUTOFF) { |
| 4370 | /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */ |
| 4371 | /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */ |
| 4372 | for (i = Py_SIZE(b) - 1; i >= 0; --i) { |
| 4373 | digit bi = b->ob_digit[i]; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4374 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4375 | for (j = (digit)1 << (PyLong_SHIFT-1); j != 0; j >>= 1) { |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4376 | MULT(z, z, z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4377 | if (bi & j) |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4378 | MULT(z, a, z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4379 | } |
| 4380 | } |
| 4381 | } |
| 4382 | else { |
| 4383 | /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */ |
| 4384 | Py_INCREF(z); /* still holds 1L */ |
| 4385 | table[0] = z; |
| 4386 | for (i = 1; i < 32; ++i) |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4387 | MULT(table[i-1], a, table[i]); |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4388 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4389 | for (i = Py_SIZE(b) - 1; i >= 0; --i) { |
| 4390 | const digit bi = b->ob_digit[i]; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4391 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4392 | for (j = PyLong_SHIFT - 5; j >= 0; j -= 5) { |
| 4393 | const int index = (bi >> j) & 0x1f; |
| 4394 | for (k = 0; k < 5; ++k) |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4395 | MULT(z, z, z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4396 | if (index) |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4397 | MULT(z, table[index], z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4398 | } |
| 4399 | } |
| 4400 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4401 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4402 | if (negativeOutput && (Py_SIZE(z) != 0)) { |
| 4403 | temp = (PyLongObject *)long_sub(z, c); |
| 4404 | if (temp == NULL) |
| 4405 | goto Error; |
| 4406 | Py_DECREF(z); |
| 4407 | z = temp; |
| 4408 | temp = NULL; |
| 4409 | } |
| 4410 | goto Done; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4411 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4412 | Error: |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 4413 | Py_CLEAR(z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4414 | /* fall through */ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4415 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4416 | if (Py_SIZE(b) > FIVEARY_CUTOFF) { |
| 4417 | for (i = 0; i < 32; ++i) |
| 4418 | Py_XDECREF(table[i]); |
| 4419 | } |
| 4420 | Py_DECREF(a); |
| 4421 | Py_DECREF(b); |
| 4422 | Py_XDECREF(c); |
| 4423 | Py_XDECREF(temp); |
| 4424 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4425 | } |
| 4426 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4427 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 4428 | long_invert(PyLongObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4429 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4430 | /* Implement ~x as -(x+1) */ |
| 4431 | PyLongObject *x; |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 4432 | if (Py_ABS(Py_SIZE(v)) <=1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4433 | return PyLong_FromLong(-(MEDIUM_VALUE(v)+1)); |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 4434 | x = (PyLongObject *) long_add(v, (PyLongObject *)_PyLong_One); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4435 | if (x == NULL) |
| 4436 | return NULL; |
Mark Dickinson | 583c6e8 | 2016-08-29 16:40:29 +0100 | [diff] [blame] | 4437 | _PyLong_Negate(&x); |
| 4438 | /* No need for maybe_small_long here, since any small |
| 4439 | longs will have been caught in the Py_SIZE <= 1 fast path. */ |
| 4440 | return (PyObject *)x; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4441 | } |
| 4442 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4443 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 4444 | long_neg(PyLongObject *v) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4445 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4446 | PyLongObject *z; |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 4447 | if (Py_ABS(Py_SIZE(v)) <= 1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4448 | return PyLong_FromLong(-MEDIUM_VALUE(v)); |
| 4449 | z = (PyLongObject *)_PyLong_Copy(v); |
| 4450 | if (z != NULL) |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 4451 | Py_SET_SIZE(z, -(Py_SIZE(v))); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4452 | return (PyObject *)z; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4453 | } |
| 4454 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4455 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 4456 | long_abs(PyLongObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4457 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4458 | if (Py_SIZE(v) < 0) |
| 4459 | return long_neg(v); |
| 4460 | else |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 4461 | return long_long((PyObject *)v); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4462 | } |
| 4463 | |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 4464 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 4465 | long_bool(PyLongObject *v) |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 4466 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4467 | return Py_SIZE(v) != 0; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4468 | } |
| 4469 | |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4470 | /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */ |
| 4471 | static int |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4472 | divmod_shift(PyObject *shiftby, Py_ssize_t *wordshift, digit *remshift) |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4473 | { |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4474 | assert(PyLong_Check(shiftby)); |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4475 | assert(Py_SIZE(shiftby) >= 0); |
| 4476 | Py_ssize_t lshiftby = PyLong_AsSsize_t((PyObject *)shiftby); |
| 4477 | if (lshiftby >= 0) { |
| 4478 | *wordshift = lshiftby / PyLong_SHIFT; |
| 4479 | *remshift = lshiftby % PyLong_SHIFT; |
| 4480 | return 0; |
| 4481 | } |
| 4482 | /* PyLong_Check(shiftby) is true and Py_SIZE(shiftby) >= 0, so it must |
| 4483 | be that PyLong_AsSsize_t raised an OverflowError. */ |
| 4484 | assert(PyErr_ExceptionMatches(PyExc_OverflowError)); |
| 4485 | PyErr_Clear(); |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4486 | PyLongObject *wordshift_obj = divrem1((PyLongObject *)shiftby, PyLong_SHIFT, remshift); |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4487 | if (wordshift_obj == NULL) { |
| 4488 | return -1; |
| 4489 | } |
| 4490 | *wordshift = PyLong_AsSsize_t((PyObject *)wordshift_obj); |
| 4491 | Py_DECREF(wordshift_obj); |
| 4492 | if (*wordshift >= 0 && *wordshift < PY_SSIZE_T_MAX / (Py_ssize_t)sizeof(digit)) { |
| 4493 | return 0; |
| 4494 | } |
| 4495 | PyErr_Clear(); |
| 4496 | /* Clip the value. With such large wordshift the right shift |
| 4497 | returns 0 and the left shift raises an error in _PyLong_New(). */ |
| 4498 | *wordshift = PY_SSIZE_T_MAX / sizeof(digit); |
| 4499 | *remshift = 0; |
| 4500 | return 0; |
| 4501 | } |
| 4502 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4503 | static PyObject * |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4504 | long_rshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4505 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4506 | PyLongObject *z = NULL; |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4507 | Py_ssize_t newsize, hishift, i, j; |
| 4508 | digit lomask, himask; |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4509 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4510 | if (Py_SIZE(a) < 0) { |
| 4511 | /* Right shifting negative numbers is harder */ |
| 4512 | PyLongObject *a1, *a2; |
| 4513 | a1 = (PyLongObject *) long_invert(a); |
| 4514 | if (a1 == NULL) |
Mark Dickinson | 92ca535 | 2016-09-17 17:50:50 +0100 | [diff] [blame] | 4515 | return NULL; |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4516 | a2 = (PyLongObject *) long_rshift1(a1, wordshift, remshift); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4517 | Py_DECREF(a1); |
| 4518 | if (a2 == NULL) |
Mark Dickinson | 92ca535 | 2016-09-17 17:50:50 +0100 | [diff] [blame] | 4519 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4520 | z = (PyLongObject *) long_invert(a2); |
| 4521 | Py_DECREF(a2); |
| 4522 | } |
| 4523 | else { |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4524 | newsize = Py_SIZE(a) - wordshift; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4525 | if (newsize <= 0) |
| 4526 | return PyLong_FromLong(0); |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4527 | hishift = PyLong_SHIFT - remshift; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4528 | lomask = ((digit)1 << hishift) - 1; |
| 4529 | himask = PyLong_MASK ^ lomask; |
| 4530 | z = _PyLong_New(newsize); |
| 4531 | if (z == NULL) |
Mark Dickinson | 92ca535 | 2016-09-17 17:50:50 +0100 | [diff] [blame] | 4532 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4533 | for (i = 0, j = wordshift; i < newsize; i++, j++) { |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4534 | z->ob_digit[i] = (a->ob_digit[j] >> remshift) & lomask; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4535 | if (i+1 < newsize) |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4536 | z->ob_digit[i] |= (a->ob_digit[j+1] << hishift) & himask; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4537 | } |
Mark Dickinson | 92ca535 | 2016-09-17 17:50:50 +0100 | [diff] [blame] | 4538 | z = maybe_small_long(long_normalize(z)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4539 | } |
Mark Dickinson | 92ca535 | 2016-09-17 17:50:50 +0100 | [diff] [blame] | 4540 | return (PyObject *)z; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4541 | } |
| 4542 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4543 | static PyObject * |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4544 | long_rshift(PyObject *a, PyObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4545 | { |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4546 | Py_ssize_t wordshift; |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4547 | digit remshift; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4548 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4549 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 4550 | |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4551 | if (Py_SIZE(b) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4552 | PyErr_SetString(PyExc_ValueError, "negative shift count"); |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 4553 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4554 | } |
Mark Dickinson | 82a9527 | 2016-08-29 19:27:06 +0100 | [diff] [blame] | 4555 | if (Py_SIZE(a) == 0) { |
| 4556 | return PyLong_FromLong(0); |
| 4557 | } |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4558 | if (divmod_shift(b, &wordshift, &remshift) < 0) |
| 4559 | return NULL; |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4560 | return long_rshift1((PyLongObject *)a, wordshift, remshift); |
| 4561 | } |
| 4562 | |
| 4563 | /* Return a >> shiftby. */ |
| 4564 | PyObject * |
| 4565 | _PyLong_Rshift(PyObject *a, size_t shiftby) |
| 4566 | { |
| 4567 | Py_ssize_t wordshift; |
| 4568 | digit remshift; |
| 4569 | |
| 4570 | assert(PyLong_Check(a)); |
| 4571 | if (Py_SIZE(a) == 0) { |
| 4572 | return PyLong_FromLong(0); |
| 4573 | } |
| 4574 | wordshift = shiftby / PyLong_SHIFT; |
| 4575 | remshift = shiftby % PyLong_SHIFT; |
| 4576 | return long_rshift1((PyLongObject *)a, wordshift, remshift); |
| 4577 | } |
| 4578 | |
| 4579 | static PyObject * |
| 4580 | long_lshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift) |
| 4581 | { |
| 4582 | /* This version due to Tim Peters */ |
| 4583 | PyLongObject *z = NULL; |
| 4584 | Py_ssize_t oldsize, newsize, i, j; |
| 4585 | twodigits accum; |
| 4586 | |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 4587 | oldsize = Py_ABS(Py_SIZE(a)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4588 | newsize = oldsize + wordshift; |
| 4589 | if (remshift) |
| 4590 | ++newsize; |
| 4591 | z = _PyLong_New(newsize); |
| 4592 | if (z == NULL) |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 4593 | return NULL; |
| 4594 | if (Py_SIZE(a) < 0) { |
| 4595 | assert(Py_REFCNT(z) == 1); |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 4596 | Py_SET_SIZE(z, -Py_SIZE(z)); |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 4597 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4598 | for (i = 0; i < wordshift; i++) |
| 4599 | z->ob_digit[i] = 0; |
| 4600 | accum = 0; |
| 4601 | for (i = wordshift, j = 0; j < oldsize; i++, j++) { |
| 4602 | accum |= (twodigits)a->ob_digit[j] << remshift; |
| 4603 | z->ob_digit[i] = (digit)(accum & PyLong_MASK); |
| 4604 | accum >>= PyLong_SHIFT; |
| 4605 | } |
| 4606 | if (remshift) |
| 4607 | z->ob_digit[newsize-1] = (digit)accum; |
| 4608 | else |
| 4609 | assert(!accum); |
| 4610 | z = long_normalize(z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4611 | return (PyObject *) maybe_small_long(z); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4612 | } |
| 4613 | |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4614 | static PyObject * |
| 4615 | long_lshift(PyObject *a, PyObject *b) |
| 4616 | { |
| 4617 | Py_ssize_t wordshift; |
| 4618 | digit remshift; |
| 4619 | |
| 4620 | CHECK_BINOP(a, b); |
| 4621 | |
| 4622 | if (Py_SIZE(b) < 0) { |
| 4623 | PyErr_SetString(PyExc_ValueError, "negative shift count"); |
| 4624 | return NULL; |
| 4625 | } |
| 4626 | if (Py_SIZE(a) == 0) { |
| 4627 | return PyLong_FromLong(0); |
| 4628 | } |
| 4629 | if (divmod_shift(b, &wordshift, &remshift) < 0) |
| 4630 | return NULL; |
| 4631 | return long_lshift1((PyLongObject *)a, wordshift, remshift); |
| 4632 | } |
| 4633 | |
| 4634 | /* Return a << shiftby. */ |
| 4635 | PyObject * |
| 4636 | _PyLong_Lshift(PyObject *a, size_t shiftby) |
| 4637 | { |
| 4638 | Py_ssize_t wordshift; |
| 4639 | digit remshift; |
| 4640 | |
| 4641 | assert(PyLong_Check(a)); |
| 4642 | if (Py_SIZE(a) == 0) { |
| 4643 | return PyLong_FromLong(0); |
| 4644 | } |
| 4645 | wordshift = shiftby / PyLong_SHIFT; |
| 4646 | remshift = shiftby % PyLong_SHIFT; |
| 4647 | return long_lshift1((PyLongObject *)a, wordshift, remshift); |
| 4648 | } |
| 4649 | |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4650 | /* Compute two's complement of digit vector a[0:m], writing result to |
| 4651 | z[0:m]. The digit vector a need not be normalized, but should not |
| 4652 | be entirely zero. a and z may point to the same digit vector. */ |
| 4653 | |
| 4654 | static void |
| 4655 | v_complement(digit *z, digit *a, Py_ssize_t m) |
| 4656 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4657 | Py_ssize_t i; |
| 4658 | digit carry = 1; |
| 4659 | for (i = 0; i < m; ++i) { |
| 4660 | carry += a[i] ^ PyLong_MASK; |
| 4661 | z[i] = carry & PyLong_MASK; |
| 4662 | carry >>= PyLong_SHIFT; |
| 4663 | } |
| 4664 | assert(carry == 0); |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4665 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 4666 | |
| 4667 | /* Bitwise and/xor/or operations */ |
| 4668 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4669 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 4670 | long_bitwise(PyLongObject *a, |
Benjamin Peterson | 513762f | 2012-12-26 16:43:33 -0600 | [diff] [blame] | 4671 | char op, /* '&', '|', '^' */ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4672 | PyLongObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4673 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4674 | int nega, negb, negz; |
| 4675 | Py_ssize_t size_a, size_b, size_z, i; |
| 4676 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4677 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4678 | /* Bitwise operations for negative numbers operate as though |
| 4679 | on a two's complement representation. So convert arguments |
| 4680 | from sign-magnitude to two's complement, and convert the |
| 4681 | result back to sign-magnitude at the end. */ |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4682 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4683 | /* If a is negative, replace it by its two's complement. */ |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 4684 | size_a = Py_ABS(Py_SIZE(a)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4685 | nega = Py_SIZE(a) < 0; |
| 4686 | if (nega) { |
| 4687 | z = _PyLong_New(size_a); |
| 4688 | if (z == NULL) |
| 4689 | return NULL; |
| 4690 | v_complement(z->ob_digit, a->ob_digit, size_a); |
| 4691 | a = z; |
| 4692 | } |
| 4693 | else |
| 4694 | /* Keep reference count consistent. */ |
| 4695 | Py_INCREF(a); |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4696 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4697 | /* Same for b. */ |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 4698 | size_b = Py_ABS(Py_SIZE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4699 | negb = Py_SIZE(b) < 0; |
| 4700 | if (negb) { |
| 4701 | z = _PyLong_New(size_b); |
| 4702 | if (z == NULL) { |
| 4703 | Py_DECREF(a); |
| 4704 | return NULL; |
| 4705 | } |
| 4706 | v_complement(z->ob_digit, b->ob_digit, size_b); |
| 4707 | b = z; |
| 4708 | } |
| 4709 | else |
| 4710 | Py_INCREF(b); |
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 | /* Swap a and b if necessary to ensure size_a >= size_b. */ |
| 4713 | if (size_a < size_b) { |
| 4714 | z = a; a = b; b = z; |
| 4715 | size_z = size_a; size_a = size_b; size_b = size_z; |
| 4716 | negz = nega; nega = negb; negb = negz; |
| 4717 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4718 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4719 | /* JRH: The original logic here was to allocate the result value (z) |
| 4720 | as the longer of the two operands. However, there are some cases |
| 4721 | where the result is guaranteed to be shorter than that: AND of two |
| 4722 | positives, OR of two negatives: use the shorter number. AND with |
| 4723 | mixed signs: use the positive number. OR with mixed signs: use the |
| 4724 | negative number. |
| 4725 | */ |
| 4726 | switch (op) { |
| 4727 | case '^': |
| 4728 | negz = nega ^ negb; |
| 4729 | size_z = size_a; |
| 4730 | break; |
| 4731 | case '&': |
| 4732 | negz = nega & negb; |
| 4733 | size_z = negb ? size_a : size_b; |
| 4734 | break; |
| 4735 | case '|': |
| 4736 | negz = nega | negb; |
| 4737 | size_z = negb ? size_b : size_a; |
| 4738 | break; |
| 4739 | default: |
stratakis | a10d426 | 2019-03-18 18:59:20 +0100 | [diff] [blame] | 4740 | Py_UNREACHABLE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4741 | } |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 4742 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4743 | /* We allow an extra digit if z is negative, to make sure that |
| 4744 | the final two's complement of z doesn't overflow. */ |
| 4745 | z = _PyLong_New(size_z + negz); |
| 4746 | if (z == NULL) { |
| 4747 | Py_DECREF(a); |
| 4748 | Py_DECREF(b); |
| 4749 | return NULL; |
| 4750 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4751 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4752 | /* Compute digits for overlap of a and b. */ |
| 4753 | switch(op) { |
| 4754 | case '&': |
| 4755 | for (i = 0; i < size_b; ++i) |
| 4756 | z->ob_digit[i] = a->ob_digit[i] & b->ob_digit[i]; |
| 4757 | break; |
| 4758 | case '|': |
| 4759 | for (i = 0; i < size_b; ++i) |
| 4760 | z->ob_digit[i] = a->ob_digit[i] | b->ob_digit[i]; |
| 4761 | break; |
| 4762 | case '^': |
| 4763 | for (i = 0; i < size_b; ++i) |
| 4764 | z->ob_digit[i] = a->ob_digit[i] ^ b->ob_digit[i]; |
| 4765 | break; |
| 4766 | default: |
stratakis | a10d426 | 2019-03-18 18:59:20 +0100 | [diff] [blame] | 4767 | Py_UNREACHABLE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4768 | } |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4769 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4770 | /* Copy any remaining digits of a, inverting if necessary. */ |
| 4771 | if (op == '^' && negb) |
| 4772 | for (; i < size_z; ++i) |
| 4773 | z->ob_digit[i] = a->ob_digit[i] ^ PyLong_MASK; |
| 4774 | else if (i < size_z) |
| 4775 | memcpy(&z->ob_digit[i], &a->ob_digit[i], |
| 4776 | (size_z-i)*sizeof(digit)); |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4777 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4778 | /* Complement result if negative. */ |
| 4779 | if (negz) { |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 4780 | Py_SET_SIZE(z, -(Py_SIZE(z))); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4781 | z->ob_digit[size_z] = PyLong_MASK; |
| 4782 | v_complement(z->ob_digit, z->ob_digit, size_z+1); |
| 4783 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4784 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4785 | Py_DECREF(a); |
| 4786 | Py_DECREF(b); |
| 4787 | return (PyObject *)maybe_small_long(long_normalize(z)); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4788 | } |
| 4789 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4790 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 4791 | long_and(PyObject *a, PyObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4792 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4793 | PyObject *c; |
| 4794 | CHECK_BINOP(a, b); |
| 4795 | c = long_bitwise((PyLongObject*)a, '&', (PyLongObject*)b); |
| 4796 | return c; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4797 | } |
| 4798 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4799 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 4800 | long_xor(PyObject *a, PyObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4801 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4802 | PyObject *c; |
| 4803 | CHECK_BINOP(a, b); |
| 4804 | c = long_bitwise((PyLongObject*)a, '^', (PyLongObject*)b); |
| 4805 | return c; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4806 | } |
| 4807 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4808 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 4809 | long_or(PyObject *a, PyObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4810 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4811 | PyObject *c; |
| 4812 | CHECK_BINOP(a, b); |
| 4813 | c = long_bitwise((PyLongObject*)a, '|', (PyLongObject*)b); |
| 4814 | return c; |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 4815 | } |
| 4816 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4817 | static PyObject * |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 4818 | long_long(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 4819 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4820 | if (PyLong_CheckExact(v)) |
| 4821 | Py_INCREF(v); |
| 4822 | else |
| 4823 | v = _PyLong_Copy((PyLongObject *)v); |
| 4824 | return v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 4825 | } |
| 4826 | |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 4827 | PyObject * |
| 4828 | _PyLong_GCD(PyObject *aarg, PyObject *barg) |
| 4829 | { |
| 4830 | PyLongObject *a, *b, *c = NULL, *d = NULL, *r; |
| 4831 | stwodigits x, y, q, s, t, c_carry, d_carry; |
| 4832 | stwodigits A, B, C, D, T; |
| 4833 | int nbits, k; |
| 4834 | Py_ssize_t size_a, size_b, alloc_a, alloc_b; |
| 4835 | digit *a_digit, *b_digit, *c_digit, *d_digit, *a_end, *b_end; |
| 4836 | |
| 4837 | a = (PyLongObject *)aarg; |
| 4838 | b = (PyLongObject *)barg; |
| 4839 | size_a = Py_SIZE(a); |
| 4840 | size_b = Py_SIZE(b); |
| 4841 | if (-2 <= size_a && size_a <= 2 && -2 <= size_b && size_b <= 2) { |
| 4842 | Py_INCREF(a); |
| 4843 | Py_INCREF(b); |
| 4844 | goto simple; |
| 4845 | } |
| 4846 | |
| 4847 | /* Initial reduction: make sure that 0 <= b <= a. */ |
| 4848 | a = (PyLongObject *)long_abs(a); |
| 4849 | if (a == NULL) |
| 4850 | return NULL; |
| 4851 | b = (PyLongObject *)long_abs(b); |
| 4852 | if (b == NULL) { |
| 4853 | Py_DECREF(a); |
| 4854 | return NULL; |
| 4855 | } |
| 4856 | if (long_compare(a, b) < 0) { |
| 4857 | r = a; |
| 4858 | a = b; |
| 4859 | b = r; |
| 4860 | } |
| 4861 | /* We now own references to a and b */ |
| 4862 | |
| 4863 | alloc_a = Py_SIZE(a); |
| 4864 | alloc_b = Py_SIZE(b); |
| 4865 | /* reduce until a fits into 2 digits */ |
| 4866 | while ((size_a = Py_SIZE(a)) > 2) { |
Niklas Fiekas | c5b7900 | 2020-01-16 15:09:19 +0100 | [diff] [blame] | 4867 | nbits = _Py_bit_length(a->ob_digit[size_a-1]); |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 4868 | /* extract top 2*PyLong_SHIFT bits of a into x, along with |
| 4869 | corresponding bits of b into y */ |
| 4870 | size_b = Py_SIZE(b); |
| 4871 | assert(size_b <= size_a); |
| 4872 | if (size_b == 0) { |
| 4873 | if (size_a < alloc_a) { |
| 4874 | r = (PyLongObject *)_PyLong_Copy(a); |
| 4875 | Py_DECREF(a); |
| 4876 | } |
| 4877 | else |
| 4878 | r = a; |
| 4879 | Py_DECREF(b); |
| 4880 | Py_XDECREF(c); |
| 4881 | Py_XDECREF(d); |
| 4882 | return (PyObject *)r; |
| 4883 | } |
| 4884 | x = (((twodigits)a->ob_digit[size_a-1] << (2*PyLong_SHIFT-nbits)) | |
| 4885 | ((twodigits)a->ob_digit[size_a-2] << (PyLong_SHIFT-nbits)) | |
| 4886 | (a->ob_digit[size_a-3] >> nbits)); |
| 4887 | |
| 4888 | y = ((size_b >= size_a - 2 ? b->ob_digit[size_a-3] >> nbits : 0) | |
| 4889 | (size_b >= size_a - 1 ? (twodigits)b->ob_digit[size_a-2] << (PyLong_SHIFT-nbits) : 0) | |
| 4890 | (size_b >= size_a ? (twodigits)b->ob_digit[size_a-1] << (2*PyLong_SHIFT-nbits) : 0)); |
| 4891 | |
| 4892 | /* inner loop of Lehmer's algorithm; A, B, C, D never grow |
| 4893 | larger than PyLong_MASK during the algorithm. */ |
| 4894 | A = 1; B = 0; C = 0; D = 1; |
| 4895 | for (k=0;; k++) { |
| 4896 | if (y-C == 0) |
| 4897 | break; |
| 4898 | q = (x+(A-1))/(y-C); |
| 4899 | s = B+q*D; |
| 4900 | t = x-q*y; |
| 4901 | if (s > t) |
| 4902 | break; |
| 4903 | x = y; y = t; |
| 4904 | t = A+q*C; A = D; B = C; C = s; D = t; |
| 4905 | } |
| 4906 | |
| 4907 | if (k == 0) { |
| 4908 | /* no progress; do a Euclidean step */ |
| 4909 | if (l_divmod(a, b, NULL, &r) < 0) |
| 4910 | goto error; |
| 4911 | Py_DECREF(a); |
| 4912 | a = b; |
| 4913 | b = r; |
| 4914 | alloc_a = alloc_b; |
| 4915 | alloc_b = Py_SIZE(b); |
| 4916 | continue; |
| 4917 | } |
| 4918 | |
| 4919 | /* |
| 4920 | a, b = A*b-B*a, D*a-C*b if k is odd |
| 4921 | a, b = A*a-B*b, D*b-C*a if k is even |
| 4922 | */ |
| 4923 | if (k&1) { |
| 4924 | T = -A; A = -B; B = T; |
| 4925 | T = -C; C = -D; D = T; |
| 4926 | } |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 4927 | if (c != NULL) { |
| 4928 | Py_SET_SIZE(c, size_a); |
| 4929 | } |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 4930 | else if (Py_REFCNT(a) == 1) { |
| 4931 | Py_INCREF(a); |
| 4932 | c = a; |
| 4933 | } |
| 4934 | else { |
| 4935 | alloc_a = size_a; |
| 4936 | c = _PyLong_New(size_a); |
| 4937 | if (c == NULL) |
| 4938 | goto error; |
| 4939 | } |
| 4940 | |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 4941 | if (d != NULL) { |
| 4942 | Py_SET_SIZE(d, size_a); |
| 4943 | } |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 4944 | else if (Py_REFCNT(b) == 1 && size_a <= alloc_b) { |
| 4945 | Py_INCREF(b); |
| 4946 | d = b; |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 4947 | Py_SET_SIZE(d, size_a); |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 4948 | } |
| 4949 | else { |
| 4950 | alloc_b = size_a; |
| 4951 | d = _PyLong_New(size_a); |
| 4952 | if (d == NULL) |
| 4953 | goto error; |
| 4954 | } |
| 4955 | a_end = a->ob_digit + size_a; |
| 4956 | b_end = b->ob_digit + size_b; |
| 4957 | |
| 4958 | /* compute new a and new b in parallel */ |
| 4959 | a_digit = a->ob_digit; |
| 4960 | b_digit = b->ob_digit; |
| 4961 | c_digit = c->ob_digit; |
| 4962 | d_digit = d->ob_digit; |
| 4963 | c_carry = 0; |
| 4964 | d_carry = 0; |
| 4965 | while (b_digit < b_end) { |
| 4966 | c_carry += (A * *a_digit) - (B * *b_digit); |
| 4967 | d_carry += (D * *b_digit++) - (C * *a_digit++); |
| 4968 | *c_digit++ = (digit)(c_carry & PyLong_MASK); |
| 4969 | *d_digit++ = (digit)(d_carry & PyLong_MASK); |
| 4970 | c_carry >>= PyLong_SHIFT; |
| 4971 | d_carry >>= PyLong_SHIFT; |
| 4972 | } |
| 4973 | while (a_digit < a_end) { |
| 4974 | c_carry += A * *a_digit; |
| 4975 | d_carry -= C * *a_digit++; |
| 4976 | *c_digit++ = (digit)(c_carry & PyLong_MASK); |
| 4977 | *d_digit++ = (digit)(d_carry & PyLong_MASK); |
| 4978 | c_carry >>= PyLong_SHIFT; |
| 4979 | d_carry >>= PyLong_SHIFT; |
| 4980 | } |
| 4981 | assert(c_carry == 0); |
| 4982 | assert(d_carry == 0); |
| 4983 | |
| 4984 | Py_INCREF(c); |
| 4985 | Py_INCREF(d); |
| 4986 | Py_DECREF(a); |
| 4987 | Py_DECREF(b); |
| 4988 | a = long_normalize(c); |
| 4989 | b = long_normalize(d); |
| 4990 | } |
| 4991 | Py_XDECREF(c); |
| 4992 | Py_XDECREF(d); |
| 4993 | |
| 4994 | simple: |
| 4995 | assert(Py_REFCNT(a) > 0); |
| 4996 | assert(Py_REFCNT(b) > 0); |
Victor Stinner | 5783fd2 | 2015-09-19 13:39:03 +0200 | [diff] [blame] | 4997 | /* Issue #24999: use two shifts instead of ">> 2*PyLong_SHIFT" to avoid |
| 4998 | undefined behaviour when LONG_MAX type is smaller than 60 bits */ |
| 4999 | #if LONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 5000 | /* a fits into a long, so b must too */ |
| 5001 | x = PyLong_AsLong((PyObject *)a); |
| 5002 | y = PyLong_AsLong((PyObject *)b); |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 5003 | #elif LLONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 5004 | x = PyLong_AsLongLong((PyObject *)a); |
| 5005 | y = PyLong_AsLongLong((PyObject *)b); |
| 5006 | #else |
| 5007 | # error "_PyLong_GCD" |
| 5008 | #endif |
| 5009 | x = Py_ABS(x); |
| 5010 | y = Py_ABS(y); |
| 5011 | Py_DECREF(a); |
| 5012 | Py_DECREF(b); |
| 5013 | |
| 5014 | /* usual Euclidean algorithm for longs */ |
| 5015 | while (y != 0) { |
| 5016 | t = y; |
| 5017 | y = x % y; |
| 5018 | x = t; |
| 5019 | } |
Victor Stinner | 5783fd2 | 2015-09-19 13:39:03 +0200 | [diff] [blame] | 5020 | #if LONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 5021 | return PyLong_FromLong(x); |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 5022 | #elif LLONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 5023 | return PyLong_FromLongLong(x); |
| 5024 | #else |
| 5025 | # error "_PyLong_GCD" |
| 5026 | #endif |
| 5027 | |
| 5028 | error: |
| 5029 | Py_DECREF(a); |
| 5030 | Py_DECREF(b); |
| 5031 | Py_XDECREF(c); |
| 5032 | Py_XDECREF(d); |
| 5033 | return NULL; |
| 5034 | } |
| 5035 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 5036 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 5037 | long_float(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 5038 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5039 | double result; |
| 5040 | result = PyLong_AsDouble(v); |
| 5041 | if (result == -1.0 && PyErr_Occurred()) |
| 5042 | return NULL; |
| 5043 | return PyFloat_FromDouble(result); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 5044 | } |
| 5045 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 5046 | static PyObject * |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 5047 | long_subtype_new(PyTypeObject *type, PyObject *x, PyObject *obase); |
| 5048 | |
| 5049 | /*[clinic input] |
| 5050 | @classmethod |
| 5051 | int.__new__ as long_new |
| 5052 | x: object(c_default="NULL") = 0 |
| 5053 | / |
| 5054 | base as obase: object(c_default="NULL") = 10 |
| 5055 | [clinic start generated code]*/ |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 5056 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5057 | static PyObject * |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 5058 | long_new_impl(PyTypeObject *type, PyObject *x, PyObject *obase) |
| 5059 | /*[clinic end generated code: output=e47cfe777ab0f24c input=81c98f418af9eb6f]*/ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5060 | { |
Gregory P. Smith | a689e52 | 2012-12-25 22:38:32 -0800 | [diff] [blame] | 5061 | Py_ssize_t base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5062 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5063 | if (type != &PyLong_Type) |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 5064 | return long_subtype_new(type, x, obase); /* Wimp out */ |
Serhiy Storchaka | 0b386d5 | 2012-12-28 09:42:11 +0200 | [diff] [blame] | 5065 | if (x == NULL) { |
| 5066 | if (obase != NULL) { |
| 5067 | PyErr_SetString(PyExc_TypeError, |
| 5068 | "int() missing string argument"); |
| 5069 | return NULL; |
| 5070 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5071 | return PyLong_FromLong(0L); |
Serhiy Storchaka | 0b386d5 | 2012-12-28 09:42:11 +0200 | [diff] [blame] | 5072 | } |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 5073 | if (obase == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5074 | return PyNumber_Long(x); |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 5075 | |
Gregory P. Smith | a689e52 | 2012-12-25 22:38:32 -0800 | [diff] [blame] | 5076 | base = PyNumber_AsSsize_t(obase, NULL); |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 5077 | if (base == -1 && PyErr_Occurred()) |
| 5078 | return NULL; |
Gregory P. Smith | a689e52 | 2012-12-25 22:38:32 -0800 | [diff] [blame] | 5079 | if ((base != 0 && base < 2) || base > 36) { |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 5080 | PyErr_SetString(PyExc_ValueError, |
Sanyam Khurana | 28b6248 | 2017-11-14 03:19:26 +0530 | [diff] [blame] | 5081 | "int() base must be >= 2 and <= 36, or 0"); |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 5082 | return NULL; |
| 5083 | } |
| 5084 | |
| 5085 | if (PyUnicode_Check(x)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5086 | return PyLong_FromUnicodeObject(x, (int)base); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5087 | else if (PyByteArray_Check(x) || PyBytes_Check(x)) { |
Serhiy Storchaka | 8f87eef | 2020-04-12 14:58:27 +0300 | [diff] [blame] | 5088 | const char *string; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5089 | if (PyByteArray_Check(x)) |
| 5090 | string = PyByteArray_AS_STRING(x); |
| 5091 | else |
| 5092 | string = PyBytes_AS_STRING(x); |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 5093 | return _PyLong_FromBytes(string, Py_SIZE(x), (int)base); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5094 | } |
| 5095 | else { |
| 5096 | PyErr_SetString(PyExc_TypeError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 5097 | "int() can't convert non-string with explicit base"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5098 | return NULL; |
| 5099 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5100 | } |
| 5101 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 5102 | /* Wimpy, slow approach to tp_new calls for subtypes of int: |
| 5103 | first create a regular int from whatever arguments we got, |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 5104 | then allocate a subtype instance and initialize it from |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 5105 | the regular int. The regular int is then thrown away. |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 5106 | */ |
| 5107 | static PyObject * |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 5108 | long_subtype_new(PyTypeObject *type, PyObject *x, PyObject *obase) |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 5109 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5110 | PyLongObject *tmp, *newobj; |
| 5111 | Py_ssize_t i, n; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 5112 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5113 | assert(PyType_IsSubtype(type, &PyLong_Type)); |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 5114 | tmp = (PyLongObject *)long_new_impl(&PyLong_Type, x, obase); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5115 | if (tmp == NULL) |
| 5116 | return NULL; |
Serhiy Storchaka | 1509580 | 2015-11-25 15:47:01 +0200 | [diff] [blame] | 5117 | assert(PyLong_Check(tmp)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5118 | n = Py_SIZE(tmp); |
| 5119 | if (n < 0) |
| 5120 | n = -n; |
| 5121 | newobj = (PyLongObject *)type->tp_alloc(type, n); |
| 5122 | if (newobj == NULL) { |
| 5123 | Py_DECREF(tmp); |
| 5124 | return NULL; |
| 5125 | } |
| 5126 | assert(PyLong_Check(newobj)); |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 5127 | Py_SET_SIZE(newobj, Py_SIZE(tmp)); |
| 5128 | for (i = 0; i < n; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5129 | newobj->ob_digit[i] = tmp->ob_digit[i]; |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 5130 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5131 | Py_DECREF(tmp); |
| 5132 | return (PyObject *)newobj; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 5133 | } |
| 5134 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5135 | /*[clinic input] |
| 5136 | int.__getnewargs__ |
| 5137 | [clinic start generated code]*/ |
| 5138 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 5139 | static PyObject * |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5140 | int___getnewargs___impl(PyObject *self) |
| 5141 | /*[clinic end generated code: output=839a49de3f00b61b input=5904770ab1fb8c75]*/ |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 5142 | { |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5143 | return Py_BuildValue("(N)", _PyLong_Copy((PyLongObject *)self)); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 5144 | } |
| 5145 | |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5146 | static PyObject * |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5147 | long_get0(PyObject *Py_UNUSED(self), void *Py_UNUSED(context)) |
| 5148 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5149 | return PyLong_FromLong(0L); |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 5150 | } |
| 5151 | |
| 5152 | static PyObject * |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5153 | long_get1(PyObject *Py_UNUSED(self), void *Py_UNUSED(ignored)) |
| 5154 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5155 | return PyLong_FromLong(1L); |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5156 | } |
| 5157 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5158 | /*[clinic input] |
| 5159 | int.__format__ |
| 5160 | |
| 5161 | format_spec: unicode |
| 5162 | / |
| 5163 | [clinic start generated code]*/ |
| 5164 | |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 5165 | static PyObject * |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5166 | int___format___impl(PyObject *self, PyObject *format_spec) |
| 5167 | /*[clinic end generated code: output=b4929dee9ae18689 input=e31944a9b3e428b7]*/ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 5168 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 5169 | _PyUnicodeWriter writer; |
| 5170 | int ret; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 5171 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5172 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 5173 | ret = _PyLong_FormatAdvancedWriter( |
| 5174 | &writer, |
| 5175 | self, |
| 5176 | format_spec, 0, PyUnicode_GET_LENGTH(format_spec)); |
| 5177 | if (ret == -1) { |
| 5178 | _PyUnicodeWriter_Dealloc(&writer); |
| 5179 | return NULL; |
| 5180 | } |
| 5181 | return _PyUnicodeWriter_Finish(&writer); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 5182 | } |
| 5183 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5184 | /* Return a pair (q, r) such that a = b * q + r, and |
| 5185 | abs(r) <= abs(b)/2, with equality possible only if q is even. |
| 5186 | In other words, q == a / b, rounded to the nearest integer using |
| 5187 | round-half-to-even. */ |
| 5188 | |
| 5189 | PyObject * |
Mark Dickinson | fa68a61 | 2010-06-07 18:47:09 +0000 | [diff] [blame] | 5190 | _PyLong_DivmodNear(PyObject *a, PyObject *b) |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5191 | { |
| 5192 | PyLongObject *quo = NULL, *rem = NULL; |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 5193 | PyObject *twice_rem, *result, *temp; |
HongWeipeng | 42acb7b | 2019-09-18 23:10:15 +0800 | [diff] [blame] | 5194 | int quo_is_odd, quo_is_neg; |
| 5195 | Py_ssize_t cmp; |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5196 | |
| 5197 | /* Equivalent Python code: |
| 5198 | |
| 5199 | def divmod_near(a, b): |
| 5200 | q, r = divmod(a, b) |
| 5201 | # round up if either r / b > 0.5, or r / b == 0.5 and q is odd. |
| 5202 | # The expression r / b > 0.5 is equivalent to 2 * r > b if b is |
| 5203 | # positive, 2 * r < b if b negative. |
| 5204 | greater_than_half = 2*r > b if b > 0 else 2*r < b |
| 5205 | exactly_half = 2*r == b |
| 5206 | if greater_than_half or exactly_half and q % 2 == 1: |
| 5207 | q += 1 |
| 5208 | r -= b |
| 5209 | return q, r |
| 5210 | |
| 5211 | */ |
| 5212 | if (!PyLong_Check(a) || !PyLong_Check(b)) { |
| 5213 | PyErr_SetString(PyExc_TypeError, |
| 5214 | "non-integer arguments in division"); |
| 5215 | return NULL; |
| 5216 | } |
| 5217 | |
| 5218 | /* Do a and b have different signs? If so, quotient is negative. */ |
| 5219 | quo_is_neg = (Py_SIZE(a) < 0) != (Py_SIZE(b) < 0); |
| 5220 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5221 | if (long_divrem((PyLongObject*)a, (PyLongObject*)b, &quo, &rem) < 0) |
| 5222 | goto error; |
| 5223 | |
| 5224 | /* compare twice the remainder with the divisor, to see |
| 5225 | if we need to adjust the quotient and remainder */ |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 5226 | twice_rem = long_lshift((PyObject *)rem, _PyLong_One); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5227 | if (twice_rem == NULL) |
| 5228 | goto error; |
| 5229 | if (quo_is_neg) { |
| 5230 | temp = long_neg((PyLongObject*)twice_rem); |
| 5231 | Py_DECREF(twice_rem); |
| 5232 | twice_rem = temp; |
| 5233 | if (twice_rem == NULL) |
| 5234 | goto error; |
| 5235 | } |
| 5236 | cmp = long_compare((PyLongObject *)twice_rem, (PyLongObject *)b); |
| 5237 | Py_DECREF(twice_rem); |
| 5238 | |
| 5239 | quo_is_odd = Py_SIZE(quo) != 0 && ((quo->ob_digit[0] & 1) != 0); |
| 5240 | if ((Py_SIZE(b) < 0 ? cmp < 0 : cmp > 0) || (cmp == 0 && quo_is_odd)) { |
| 5241 | /* fix up quotient */ |
| 5242 | if (quo_is_neg) |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 5243 | temp = long_sub(quo, (PyLongObject *)_PyLong_One); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5244 | else |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 5245 | temp = long_add(quo, (PyLongObject *)_PyLong_One); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5246 | Py_DECREF(quo); |
| 5247 | quo = (PyLongObject *)temp; |
| 5248 | if (quo == NULL) |
| 5249 | goto error; |
| 5250 | /* and remainder */ |
| 5251 | if (quo_is_neg) |
| 5252 | temp = long_add(rem, (PyLongObject *)b); |
| 5253 | else |
| 5254 | temp = long_sub(rem, (PyLongObject *)b); |
| 5255 | Py_DECREF(rem); |
| 5256 | rem = (PyLongObject *)temp; |
| 5257 | if (rem == NULL) |
| 5258 | goto error; |
| 5259 | } |
| 5260 | |
| 5261 | result = PyTuple_New(2); |
| 5262 | if (result == NULL) |
| 5263 | goto error; |
| 5264 | |
| 5265 | /* PyTuple_SET_ITEM steals references */ |
| 5266 | PyTuple_SET_ITEM(result, 0, (PyObject *)quo); |
| 5267 | PyTuple_SET_ITEM(result, 1, (PyObject *)rem); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5268 | return result; |
| 5269 | |
| 5270 | error: |
| 5271 | Py_XDECREF(quo); |
| 5272 | Py_XDECREF(rem); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5273 | return NULL; |
| 5274 | } |
| 5275 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 5276 | static PyObject * |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 5277 | long_round(PyObject *self, PyObject *args) |
| 5278 | { |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5279 | PyObject *o_ndigits=NULL, *temp, *result, *ndigits; |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 5280 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5281 | /* To round an integer m to the nearest 10**n (n positive), we make use of |
| 5282 | * the divmod_near operation, defined by: |
| 5283 | * |
| 5284 | * divmod_near(a, b) = (q, r) |
| 5285 | * |
| 5286 | * where q is the nearest integer to the quotient a / b (the |
| 5287 | * nearest even integer in the case of a tie) and r == a - q * b. |
| 5288 | * Hence q * b = a - r is the nearest multiple of b to a, |
| 5289 | * preferring even multiples in the case of a tie. |
| 5290 | * |
| 5291 | * So the nearest multiple of 10**n to m is: |
| 5292 | * |
| 5293 | * m - divmod_near(m, 10**n)[1]. |
| 5294 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5295 | if (!PyArg_ParseTuple(args, "|O", &o_ndigits)) |
| 5296 | return NULL; |
| 5297 | if (o_ndigits == NULL) |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5298 | return long_long(self); |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 5299 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5300 | ndigits = PyNumber_Index(o_ndigits); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5301 | if (ndigits == NULL) |
| 5302 | return NULL; |
Mark Dickinson | 1124e71 | 2009-01-28 21:25:58 +0000 | [diff] [blame] | 5303 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5304 | /* if ndigits >= 0 then no rounding is necessary; return self unchanged */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5305 | if (Py_SIZE(ndigits) >= 0) { |
| 5306 | Py_DECREF(ndigits); |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5307 | return long_long(self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5308 | } |
Mark Dickinson | 1124e71 | 2009-01-28 21:25:58 +0000 | [diff] [blame] | 5309 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5310 | /* result = self - divmod_near(self, 10 ** -ndigits)[1] */ |
| 5311 | temp = long_neg((PyLongObject*)ndigits); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5312 | Py_DECREF(ndigits); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5313 | ndigits = temp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5314 | if (ndigits == NULL) |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5315 | return NULL; |
Mark Dickinson | 1124e71 | 2009-01-28 21:25:58 +0000 | [diff] [blame] | 5316 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5317 | result = PyLong_FromLong(10L); |
| 5318 | if (result == NULL) { |
| 5319 | Py_DECREF(ndigits); |
| 5320 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5321 | } |
Mark Dickinson | 1124e71 | 2009-01-28 21:25:58 +0000 | [diff] [blame] | 5322 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5323 | temp = long_pow(result, ndigits, Py_None); |
| 5324 | Py_DECREF(ndigits); |
| 5325 | Py_DECREF(result); |
| 5326 | result = temp; |
| 5327 | if (result == NULL) |
| 5328 | return NULL; |
| 5329 | |
Mark Dickinson | fa68a61 | 2010-06-07 18:47:09 +0000 | [diff] [blame] | 5330 | temp = _PyLong_DivmodNear(self, result); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5331 | Py_DECREF(result); |
| 5332 | result = temp; |
| 5333 | if (result == NULL) |
| 5334 | return NULL; |
| 5335 | |
| 5336 | temp = long_sub((PyLongObject *)self, |
| 5337 | (PyLongObject *)PyTuple_GET_ITEM(result, 1)); |
| 5338 | Py_DECREF(result); |
| 5339 | result = temp; |
| 5340 | |
| 5341 | return result; |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 5342 | } |
| 5343 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5344 | /*[clinic input] |
| 5345 | int.__sizeof__ -> Py_ssize_t |
| 5346 | |
| 5347 | Returns size in memory, in bytes. |
| 5348 | [clinic start generated code]*/ |
| 5349 | |
| 5350 | static Py_ssize_t |
| 5351 | int___sizeof___impl(PyObject *self) |
| 5352 | /*[clinic end generated code: output=3303f008eaa6a0a5 input=9b51620c76fc4507]*/ |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 5353 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5354 | Py_ssize_t res; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 5355 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5356 | res = offsetof(PyLongObject, ob_digit) + Py_ABS(Py_SIZE(self))*sizeof(digit); |
| 5357 | return res; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 5358 | } |
| 5359 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5360 | /*[clinic input] |
| 5361 | int.bit_length |
| 5362 | |
| 5363 | Number of bits necessary to represent self in binary. |
| 5364 | |
| 5365 | >>> bin(37) |
| 5366 | '0b100101' |
| 5367 | >>> (37).bit_length() |
| 5368 | 6 |
| 5369 | [clinic start generated code]*/ |
| 5370 | |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5371 | static PyObject * |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5372 | int_bit_length_impl(PyObject *self) |
| 5373 | /*[clinic end generated code: output=fc1977c9353d6a59 input=e4eb7a587e849a32]*/ |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5374 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5375 | PyLongObject *result, *x, *y; |
Serhiy Storchaka | b2e64f9 | 2016-11-08 20:34:22 +0200 | [diff] [blame] | 5376 | Py_ssize_t ndigits; |
| 5377 | int msd_bits; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5378 | digit msd; |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5379 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5380 | assert(self != NULL); |
| 5381 | assert(PyLong_Check(self)); |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5382 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5383 | ndigits = Py_ABS(Py_SIZE(self)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5384 | if (ndigits == 0) |
| 5385 | return PyLong_FromLong(0); |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5386 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5387 | msd = ((PyLongObject *)self)->ob_digit[ndigits-1]; |
Niklas Fiekas | c5b7900 | 2020-01-16 15:09:19 +0100 | [diff] [blame] | 5388 | msd_bits = _Py_bit_length(msd); |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5389 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5390 | if (ndigits <= PY_SSIZE_T_MAX/PyLong_SHIFT) |
| 5391 | return PyLong_FromSsize_t((ndigits-1)*PyLong_SHIFT + msd_bits); |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5392 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5393 | /* expression above may overflow; use Python integers instead */ |
| 5394 | result = (PyLongObject *)PyLong_FromSsize_t(ndigits - 1); |
| 5395 | if (result == NULL) |
| 5396 | return NULL; |
| 5397 | x = (PyLongObject *)PyLong_FromLong(PyLong_SHIFT); |
| 5398 | if (x == NULL) |
| 5399 | goto error; |
| 5400 | y = (PyLongObject *)long_mul(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 | x = (PyLongObject *)PyLong_FromLong((long)msd_bits); |
| 5408 | if (x == NULL) |
| 5409 | goto error; |
| 5410 | y = (PyLongObject *)long_add(result, x); |
| 5411 | Py_DECREF(x); |
| 5412 | if (y == NULL) |
| 5413 | goto error; |
| 5414 | Py_DECREF(result); |
| 5415 | result = y; |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5416 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5417 | return (PyObject *)result; |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5418 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 5419 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5420 | Py_DECREF(result); |
| 5421 | return NULL; |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5422 | } |
| 5423 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 5424 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5425 | /*[clinic input] |
Lisa Roach | 5ac7043 | 2018-09-13 23:56:23 -0700 | [diff] [blame] | 5426 | int.as_integer_ratio |
| 5427 | |
| 5428 | Return integer ratio. |
| 5429 | |
| 5430 | Return a pair of integers, whose ratio is exactly equal to the original int |
| 5431 | and with a positive denominator. |
| 5432 | |
| 5433 | >>> (10).as_integer_ratio() |
| 5434 | (10, 1) |
| 5435 | >>> (-10).as_integer_ratio() |
| 5436 | (-10, 1) |
| 5437 | >>> (0).as_integer_ratio() |
| 5438 | (0, 1) |
| 5439 | [clinic start generated code]*/ |
| 5440 | |
| 5441 | static PyObject * |
| 5442 | int_as_integer_ratio_impl(PyObject *self) |
| 5443 | /*[clinic end generated code: output=e60803ae1cc8621a input=55ce3058e15de393]*/ |
| 5444 | { |
Raymond Hettinger | 00bc08e | 2018-09-14 01:00:11 -0700 | [diff] [blame] | 5445 | PyObject *ratio_tuple; |
Serhiy Storchaka | b2e2025 | 2018-10-20 00:46:31 +0300 | [diff] [blame] | 5446 | PyObject *numerator = long_long(self); |
Raymond Hettinger | 00bc08e | 2018-09-14 01:00:11 -0700 | [diff] [blame] | 5447 | if (numerator == NULL) { |
| 5448 | return NULL; |
| 5449 | } |
| 5450 | ratio_tuple = PyTuple_Pack(2, numerator, _PyLong_One); |
| 5451 | Py_DECREF(numerator); |
| 5452 | return ratio_tuple; |
Lisa Roach | 5ac7043 | 2018-09-13 23:56:23 -0700 | [diff] [blame] | 5453 | } |
| 5454 | |
| 5455 | /*[clinic input] |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5456 | int.to_bytes |
| 5457 | |
| 5458 | length: Py_ssize_t |
| 5459 | Length of bytes object to use. An OverflowError is raised if the |
| 5460 | integer is not representable with the given number of bytes. |
| 5461 | byteorder: unicode |
| 5462 | The byte order used to represent the integer. If byteorder is 'big', |
| 5463 | the most significant byte is at the beginning of the byte array. If |
| 5464 | byteorder is 'little', the most significant byte is at the end of the |
| 5465 | byte array. To request the native byte order of the host system, use |
| 5466 | `sys.byteorder' as the byte order value. |
| 5467 | * |
| 5468 | signed as is_signed: bool = False |
| 5469 | Determines whether two's complement is used to represent the integer. |
| 5470 | If signed is False and a negative integer is given, an OverflowError |
| 5471 | is raised. |
| 5472 | |
| 5473 | Return an array of bytes representing an integer. |
| 5474 | [clinic start generated code]*/ |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5475 | |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5476 | static PyObject * |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5477 | int_to_bytes_impl(PyObject *self, Py_ssize_t length, PyObject *byteorder, |
| 5478 | int is_signed) |
| 5479 | /*[clinic end generated code: output=89c801df114050a3 input=ddac63f4c7bf414c]*/ |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5480 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5481 | int little_endian; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5482 | PyObject *bytes; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5483 | |
Serhiy Storchaka | 196a7bc | 2017-02-02 16:54:45 +0200 | [diff] [blame] | 5484 | if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_little)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5485 | little_endian = 1; |
Serhiy Storchaka | 196a7bc | 2017-02-02 16:54:45 +0200 | [diff] [blame] | 5486 | else if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_big)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5487 | little_endian = 0; |
| 5488 | else { |
| 5489 | PyErr_SetString(PyExc_ValueError, |
| 5490 | "byteorder must be either 'little' or 'big'"); |
| 5491 | return NULL; |
| 5492 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5493 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5494 | if (length < 0) { |
| 5495 | PyErr_SetString(PyExc_ValueError, |
| 5496 | "length argument must be non-negative"); |
| 5497 | return NULL; |
| 5498 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5499 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5500 | bytes = PyBytes_FromStringAndSize(NULL, length); |
| 5501 | if (bytes == NULL) |
| 5502 | return NULL; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5503 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5504 | if (_PyLong_AsByteArray((PyLongObject *)self, |
| 5505 | (unsigned char *)PyBytes_AS_STRING(bytes), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5506 | length, little_endian, is_signed) < 0) { |
| 5507 | Py_DECREF(bytes); |
| 5508 | return NULL; |
| 5509 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5510 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5511 | return bytes; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5512 | } |
| 5513 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5514 | /*[clinic input] |
| 5515 | @classmethod |
| 5516 | int.from_bytes |
| 5517 | |
| 5518 | bytes as bytes_obj: object |
| 5519 | Holds the array of bytes to convert. The argument must either |
| 5520 | support the buffer protocol or be an iterable object producing bytes. |
| 5521 | Bytes and bytearray are examples of built-in objects that support the |
| 5522 | buffer protocol. |
| 5523 | byteorder: unicode |
| 5524 | The byte order used to represent the integer. If byteorder is 'big', |
| 5525 | the most significant byte is at the beginning of the byte array. If |
| 5526 | byteorder is 'little', the most significant byte is at the end of the |
| 5527 | byte array. To request the native byte order of the host system, use |
| 5528 | `sys.byteorder' as the byte order value. |
| 5529 | * |
| 5530 | signed as is_signed: bool = False |
| 5531 | Indicates whether two's complement is used to represent the integer. |
| 5532 | |
| 5533 | Return the integer represented by the given array of bytes. |
| 5534 | [clinic start generated code]*/ |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5535 | |
| 5536 | static PyObject * |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5537 | int_from_bytes_impl(PyTypeObject *type, PyObject *bytes_obj, |
| 5538 | PyObject *byteorder, int is_signed) |
| 5539 | /*[clinic end generated code: output=efc5d68e31f9314f input=cdf98332b6a821b0]*/ |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5540 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5541 | int little_endian; |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5542 | PyObject *long_obj, *bytes; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5543 | |
Serhiy Storchaka | 196a7bc | 2017-02-02 16:54:45 +0200 | [diff] [blame] | 5544 | if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_little)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5545 | little_endian = 1; |
Serhiy Storchaka | 196a7bc | 2017-02-02 16:54:45 +0200 | [diff] [blame] | 5546 | else if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_big)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5547 | little_endian = 0; |
| 5548 | else { |
| 5549 | PyErr_SetString(PyExc_ValueError, |
| 5550 | "byteorder must be either 'little' or 'big'"); |
| 5551 | return NULL; |
| 5552 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5553 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5554 | bytes = PyObject_Bytes(bytes_obj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5555 | if (bytes == NULL) |
| 5556 | return NULL; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5557 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5558 | long_obj = _PyLong_FromByteArray( |
| 5559 | (unsigned char *)PyBytes_AS_STRING(bytes), Py_SIZE(bytes), |
| 5560 | little_endian, is_signed); |
| 5561 | Py_DECREF(bytes); |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5562 | |
Zackery Spytz | 7bb9cd0 | 2018-10-05 15:02:23 -0600 | [diff] [blame] | 5563 | if (long_obj != NULL && type != &PyLong_Type) { |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 5564 | Py_SETREF(long_obj, PyObject_CallOneArg((PyObject *)type, long_obj)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5565 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5566 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5567 | return long_obj; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5568 | } |
| 5569 | |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5570 | static PyObject * |
| 5571 | long_long_meth(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 5572 | { |
| 5573 | return long_long(self); |
| 5574 | } |
| 5575 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 5576 | static PyMethodDef long_methods[] = { |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5577 | {"conjugate", long_long_meth, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5578 | "Returns self, the complex conjugate of any int."}, |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5579 | INT_BIT_LENGTH_METHODDEF |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5580 | INT_TO_BYTES_METHODDEF |
| 5581 | INT_FROM_BYTES_METHODDEF |
Lisa Roach | 5ac7043 | 2018-09-13 23:56:23 -0700 | [diff] [blame] | 5582 | INT_AS_INTEGER_RATIO_METHODDEF |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5583 | {"__trunc__", long_long_meth, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5584 | "Truncating an Integral returns itself."}, |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5585 | {"__floor__", long_long_meth, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5586 | "Flooring an Integral returns itself."}, |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5587 | {"__ceil__", long_long_meth, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5588 | "Ceiling of an Integral returns itself."}, |
| 5589 | {"__round__", (PyCFunction)long_round, METH_VARARGS, |
| 5590 | "Rounding an Integral returns itself.\n" |
| 5591 | "Rounding with an ndigits argument also returns an integer."}, |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5592 | INT___GETNEWARGS___METHODDEF |
| 5593 | INT___FORMAT___METHODDEF |
| 5594 | INT___SIZEOF___METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5595 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 5596 | }; |
| 5597 | |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5598 | static PyGetSetDef long_getset[] = { |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 5599 | {"real", |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5600 | (getter)long_long_meth, (setter)NULL, |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5601 | "the real part of a complex number", |
| 5602 | NULL}, |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 5603 | {"imag", |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5604 | long_get0, (setter)NULL, |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5605 | "the imaginary part of a complex number", |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 5606 | NULL}, |
| 5607 | {"numerator", |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5608 | (getter)long_long_meth, (setter)NULL, |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5609 | "the numerator of a rational number in lowest terms", |
| 5610 | NULL}, |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 5611 | {"denominator", |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5612 | long_get1, (setter)NULL, |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5613 | "the denominator of a rational number in lowest terms", |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 5614 | NULL}, |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5615 | {NULL} /* Sentinel */ |
| 5616 | }; |
| 5617 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5618 | PyDoc_STRVAR(long_doc, |
svelankar | 390a096 | 2017-03-08 19:29:01 -0500 | [diff] [blame] | 5619 | "int([x]) -> integer\n\ |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 5620 | int(x, base=10) -> integer\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5621 | \n\ |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 5622 | Convert a number or string to an integer, or return 0 if no arguments\n\ |
| 5623 | are given. If x is a number, return x.__int__(). For floating point\n\ |
| 5624 | numbers, this truncates towards zero.\n\ |
| 5625 | \n\ |
| 5626 | If x is not a number or if base is given, then x must be a string,\n\ |
| 5627 | bytes, or bytearray instance representing an integer literal in the\n\ |
| 5628 | given base. The literal can be preceded by '+' or '-' and be surrounded\n\ |
| 5629 | by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.\n\ |
| 5630 | Base 0 means to interpret the base from the string as an integer literal.\n\ |
| 5631 | >>> int('0b100', base=0)\n\ |
| 5632 | 4"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5633 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 5634 | static PyNumberMethods long_as_number = { |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 5635 | (binaryfunc)long_add, /*nb_add*/ |
| 5636 | (binaryfunc)long_sub, /*nb_subtract*/ |
| 5637 | (binaryfunc)long_mul, /*nb_multiply*/ |
| 5638 | long_mod, /*nb_remainder*/ |
| 5639 | long_divmod, /*nb_divmod*/ |
| 5640 | long_pow, /*nb_power*/ |
| 5641 | (unaryfunc)long_neg, /*nb_negative*/ |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5642 | long_long, /*tp_positive*/ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 5643 | (unaryfunc)long_abs, /*tp_absolute*/ |
| 5644 | (inquiry)long_bool, /*tp_bool*/ |
| 5645 | (unaryfunc)long_invert, /*nb_invert*/ |
| 5646 | long_lshift, /*nb_lshift*/ |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 5647 | long_rshift, /*nb_rshift*/ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 5648 | long_and, /*nb_and*/ |
| 5649 | long_xor, /*nb_xor*/ |
| 5650 | long_or, /*nb_or*/ |
| 5651 | long_long, /*nb_int*/ |
| 5652 | 0, /*nb_reserved*/ |
| 5653 | long_float, /*nb_float*/ |
| 5654 | 0, /* nb_inplace_add */ |
| 5655 | 0, /* nb_inplace_subtract */ |
| 5656 | 0, /* nb_inplace_multiply */ |
| 5657 | 0, /* nb_inplace_remainder */ |
| 5658 | 0, /* nb_inplace_power */ |
| 5659 | 0, /* nb_inplace_lshift */ |
| 5660 | 0, /* nb_inplace_rshift */ |
| 5661 | 0, /* nb_inplace_and */ |
| 5662 | 0, /* nb_inplace_xor */ |
| 5663 | 0, /* nb_inplace_or */ |
| 5664 | long_div, /* nb_floor_divide */ |
| 5665 | long_true_divide, /* nb_true_divide */ |
| 5666 | 0, /* nb_inplace_floor_divide */ |
| 5667 | 0, /* nb_inplace_true_divide */ |
| 5668 | long_long, /* nb_index */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 5669 | }; |
| 5670 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 5671 | PyTypeObject PyLong_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5672 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 5673 | "int", /* tp_name */ |
| 5674 | offsetof(PyLongObject, ob_digit), /* tp_basicsize */ |
| 5675 | sizeof(digit), /* tp_itemsize */ |
Inada Naoki | 7d40869 | 2019-05-29 17:23:27 +0900 | [diff] [blame] | 5676 | 0, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5677 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5678 | 0, /* tp_getattr */ |
| 5679 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5680 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5681 | long_to_decimal_string, /* tp_repr */ |
| 5682 | &long_as_number, /* tp_as_number */ |
| 5683 | 0, /* tp_as_sequence */ |
| 5684 | 0, /* tp_as_mapping */ |
| 5685 | (hashfunc)long_hash, /* tp_hash */ |
| 5686 | 0, /* tp_call */ |
Serhiy Storchaka | 96aeaec | 2019-05-06 22:29:40 +0300 | [diff] [blame] | 5687 | 0, /* tp_str */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5688 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 5689 | 0, /* tp_setattro */ |
| 5690 | 0, /* tp_as_buffer */ |
| 5691 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 5692 | Py_TPFLAGS_LONG_SUBCLASS, /* tp_flags */ |
| 5693 | long_doc, /* tp_doc */ |
| 5694 | 0, /* tp_traverse */ |
| 5695 | 0, /* tp_clear */ |
| 5696 | long_richcompare, /* tp_richcompare */ |
| 5697 | 0, /* tp_weaklistoffset */ |
| 5698 | 0, /* tp_iter */ |
| 5699 | 0, /* tp_iternext */ |
| 5700 | long_methods, /* tp_methods */ |
| 5701 | 0, /* tp_members */ |
| 5702 | long_getset, /* tp_getset */ |
| 5703 | 0, /* tp_base */ |
| 5704 | 0, /* tp_dict */ |
| 5705 | 0, /* tp_descr_get */ |
| 5706 | 0, /* tp_descr_set */ |
| 5707 | 0, /* tp_dictoffset */ |
| 5708 | 0, /* tp_init */ |
| 5709 | 0, /* tp_alloc */ |
| 5710 | long_new, /* tp_new */ |
| 5711 | PyObject_Del, /* tp_free */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 5712 | }; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5713 | |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 5714 | static PyTypeObject Int_InfoType; |
| 5715 | |
| 5716 | PyDoc_STRVAR(int_info__doc__, |
| 5717 | "sys.int_info\n\ |
| 5718 | \n\ |
Raymond Hettinger | 7117074 | 2019-09-11 07:17:32 -0700 | [diff] [blame] | 5719 | A named tuple that holds information about Python's\n\ |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 5720 | internal representation of integers. The attributes are read only."); |
| 5721 | |
| 5722 | static PyStructSequence_Field int_info_fields[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5723 | {"bits_per_digit", "size of a digit in bits"}, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 5724 | {"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] | 5725 | {NULL, NULL} |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 5726 | }; |
| 5727 | |
| 5728 | static PyStructSequence_Desc int_info_desc = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5729 | "sys.int_info", /* name */ |
| 5730 | int_info__doc__, /* doc */ |
| 5731 | int_info_fields, /* fields */ |
| 5732 | 2 /* number of fields */ |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 5733 | }; |
| 5734 | |
| 5735 | PyObject * |
| 5736 | PyLong_GetInfo(void) |
| 5737 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5738 | PyObject* int_info; |
| 5739 | int field = 0; |
| 5740 | int_info = PyStructSequence_New(&Int_InfoType); |
| 5741 | if (int_info == NULL) |
| 5742 | return NULL; |
| 5743 | PyStructSequence_SET_ITEM(int_info, field++, |
| 5744 | PyLong_FromLong(PyLong_SHIFT)); |
| 5745 | PyStructSequence_SET_ITEM(int_info, field++, |
| 5746 | PyLong_FromLong(sizeof(digit))); |
| 5747 | if (PyErr_Occurred()) { |
| 5748 | Py_CLEAR(int_info); |
| 5749 | return NULL; |
| 5750 | } |
| 5751 | return int_info; |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 5752 | } |
| 5753 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5754 | int |
Victor Stinner | 630c8df | 2019-12-17 13:02:18 +0100 | [diff] [blame] | 5755 | _PyLong_Init(PyThreadState *tstate) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5756 | { |
| 5757 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
Victor Stinner | 5dcc06f | 2019-11-21 08:51:59 +0100 | [diff] [blame] | 5758 | for (Py_ssize_t i=0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++) { |
| 5759 | sdigit ival = (sdigit)i - NSMALLNEGINTS; |
| 5760 | int size = (ival < 0) ? -1 : ((ival == 0) ? 0 : 1); |
Christian Heimes | dfc12ed | 2008-01-31 15:16:38 +0000 | [diff] [blame] | 5761 | |
Victor Stinner | 5dcc06f | 2019-11-21 08:51:59 +0100 | [diff] [blame] | 5762 | PyLongObject *v = _PyLong_New(1); |
| 5763 | if (!v) { |
| 5764 | return -1; |
| 5765 | } |
Christian Heimes | dfc12ed | 2008-01-31 15:16:38 +0000 | [diff] [blame] | 5766 | |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 5767 | Py_SET_SIZE(v, size); |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 5768 | v->ob_digit[0] = (digit)abs(ival); |
Victor Stinner | 5dcc06f | 2019-11-21 08:51:59 +0100 | [diff] [blame] | 5769 | |
Victor Stinner | 630c8df | 2019-12-17 13:02:18 +0100 | [diff] [blame] | 5770 | tstate->interp->small_ints[i] = v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5771 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5772 | #endif |
Victor Stinner | 5dcc06f | 2019-11-21 08:51:59 +0100 | [diff] [blame] | 5773 | |
Victor Stinner | 630c8df | 2019-12-17 13:02:18 +0100 | [diff] [blame] | 5774 | if (_Py_IsMainInterpreter(tstate)) { |
| 5775 | _PyLong_Zero = PyLong_FromLong(0); |
| 5776 | if (_PyLong_Zero == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 5777 | return 0; |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 5778 | } |
Victor Stinner | 630c8df | 2019-12-17 13:02:18 +0100 | [diff] [blame] | 5779 | |
| 5780 | _PyLong_One = PyLong_FromLong(1); |
| 5781 | if (_PyLong_One == NULL) { |
| 5782 | return 0; |
| 5783 | } |
| 5784 | |
| 5785 | /* initialize int_info */ |
| 5786 | if (Int_InfoType.tp_name == NULL) { |
| 5787 | if (PyStructSequence_InitType2(&Int_InfoType, &int_info_desc) < 0) { |
| 5788 | return 0; |
| 5789 | } |
| 5790 | } |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 5791 | } |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 5792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5793 | return 1; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5794 | } |
| 5795 | |
| 5796 | void |
Victor Stinner | 630c8df | 2019-12-17 13:02:18 +0100 | [diff] [blame] | 5797 | _PyLong_Fini(PyThreadState *tstate) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5798 | { |
Victor Stinner | 630c8df | 2019-12-17 13:02:18 +0100 | [diff] [blame] | 5799 | if (_Py_IsMainInterpreter(tstate)) { |
| 5800 | Py_CLEAR(_PyLong_One); |
| 5801 | Py_CLEAR(_PyLong_Zero); |
| 5802 | } |
| 5803 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5804 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
Victor Stinner | 5dcc06f | 2019-11-21 08:51:59 +0100 | [diff] [blame] | 5805 | for (Py_ssize_t i = 0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++) { |
Victor Stinner | 630c8df | 2019-12-17 13:02:18 +0100 | [diff] [blame] | 5806 | Py_CLEAR(tstate->interp->small_ints[i]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5807 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5808 | #endif |
| 5809 | } |