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 | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 122 | /* Allocate a new int object with size digits. |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 123 | Return NULL and set exception if we run out of memory. */ |
| 124 | |
Mark Dickinson | 5a74bf6 | 2009-02-15 11:04:38 +0000 | [diff] [blame] | 125 | #define MAX_LONG_DIGITS \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 126 | ((PY_SSIZE_T_MAX - offsetof(PyLongObject, ob_digit))/sizeof(digit)) |
Mark Dickinson | 5a74bf6 | 2009-02-15 11:04:38 +0000 | [diff] [blame] | 127 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 128 | PyLongObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 129 | _PyLong_New(Py_ssize_t size) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 130 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 131 | PyLongObject *result; |
| 132 | /* Number of bytes needed is: offsetof(PyLongObject, ob_digit) + |
| 133 | sizeof(digit)*size. Previous incarnations of this code used |
| 134 | sizeof(PyVarObject) instead of the offsetof, but this risks being |
| 135 | incorrect in the presence of padding between the PyVarObject header |
| 136 | and the digits. */ |
| 137 | if (size > (Py_ssize_t)MAX_LONG_DIGITS) { |
| 138 | PyErr_SetString(PyExc_OverflowError, |
| 139 | "too many digits in integer"); |
| 140 | return NULL; |
| 141 | } |
| 142 | result = PyObject_MALLOC(offsetof(PyLongObject, ob_digit) + |
| 143 | size*sizeof(digit)); |
| 144 | if (!result) { |
| 145 | PyErr_NoMemory(); |
| 146 | return NULL; |
| 147 | } |
Victor Stinner | b509d52 | 2018-11-23 14:27:38 +0100 | [diff] [blame] | 148 | return (PyLongObject*)PyObject_INIT_VAR(result, &PyLong_Type, size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 151 | PyObject * |
| 152 | _PyLong_Copy(PyLongObject *src) |
| 153 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 154 | PyLongObject *result; |
| 155 | Py_ssize_t i; |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 156 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 157 | assert(src != NULL); |
| 158 | i = Py_SIZE(src); |
| 159 | if (i < 0) |
| 160 | i = -(i); |
| 161 | if (i < 2) { |
Mark Dickinson | bcc17ee | 2012-04-20 21:42:49 +0100 | [diff] [blame] | 162 | sdigit ival = MEDIUM_VALUE(src); |
animalize | 6b51998 | 2019-09-06 14:00:56 +0800 | [diff] [blame] | 163 | if (IS_SMALL_INT(ival)) { |
Greg Price | 5e63ab0 | 2019-08-24 10:19:37 -0700 | [diff] [blame] | 164 | return get_small_int(ival); |
| 165 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 166 | } |
| 167 | result = _PyLong_New(i); |
| 168 | if (result != NULL) { |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 169 | Py_SET_SIZE(result, Py_SIZE(src)); |
| 170 | while (--i >= 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 171 | result->ob_digit[i] = src->ob_digit[i]; |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 172 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 173 | } |
| 174 | return (PyObject *)result; |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 177 | /* Create a new int object from a C long int */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 178 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 179 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 180 | PyLong_FromLong(long ival) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 181 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 182 | PyLongObject *v; |
| 183 | unsigned long abs_ival; |
| 184 | unsigned long t; /* unsigned so >> doesn't propagate sign bit */ |
| 185 | int ndigits = 0; |
Mark Dickinson | 36820dd | 2016-09-10 20:17:36 +0100 | [diff] [blame] | 186 | int sign; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 187 | |
animalize | 6b51998 | 2019-09-06 14:00:56 +0800 | [diff] [blame] | 188 | if (IS_SMALL_INT(ival)) { |
Greg Price | 5e63ab0 | 2019-08-24 10:19:37 -0700 | [diff] [blame] | 189 | return get_small_int((sdigit)ival); |
| 190 | } |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 191 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 192 | if (ival < 0) { |
| 193 | /* negate: can't write this as abs_ival = -ival since that |
| 194 | invokes undefined behaviour when ival is LONG_MIN */ |
| 195 | abs_ival = 0U-(unsigned long)ival; |
| 196 | sign = -1; |
| 197 | } |
| 198 | else { |
| 199 | abs_ival = (unsigned long)ival; |
Mark Dickinson | 36820dd | 2016-09-10 20:17:36 +0100 | [diff] [blame] | 200 | sign = ival == 0 ? 0 : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 201 | } |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 202 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 203 | /* Fast path for single-digit ints */ |
| 204 | if (!(abs_ival >> PyLong_SHIFT)) { |
| 205 | v = _PyLong_New(1); |
| 206 | if (v) { |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 207 | Py_SET_SIZE(v, sign); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 208 | v->ob_digit[0] = Py_SAFE_DOWNCAST( |
| 209 | abs_ival, unsigned long, digit); |
| 210 | } |
| 211 | return (PyObject*)v; |
| 212 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 213 | |
Mark Dickinson | 249b898 | 2009-04-27 19:41:00 +0000 | [diff] [blame] | 214 | #if PyLong_SHIFT==15 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 215 | /* 2 digits */ |
| 216 | if (!(abs_ival >> 2*PyLong_SHIFT)) { |
| 217 | v = _PyLong_New(2); |
| 218 | if (v) { |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 219 | Py_SET_SIZE(v, 2 * sign); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 220 | v->ob_digit[0] = Py_SAFE_DOWNCAST( |
| 221 | abs_ival & PyLong_MASK, unsigned long, digit); |
| 222 | v->ob_digit[1] = Py_SAFE_DOWNCAST( |
| 223 | abs_ival >> PyLong_SHIFT, unsigned long, digit); |
| 224 | } |
| 225 | return (PyObject*)v; |
| 226 | } |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 227 | #endif |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 228 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 229 | /* Larger numbers: loop to determine number of digits */ |
| 230 | t = abs_ival; |
| 231 | while (t) { |
| 232 | ++ndigits; |
| 233 | t >>= PyLong_SHIFT; |
| 234 | } |
| 235 | v = _PyLong_New(ndigits); |
| 236 | if (v != NULL) { |
| 237 | digit *p = v->ob_digit; |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 238 | Py_SET_SIZE(v, ndigits * sign); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 239 | t = abs_ival; |
| 240 | while (t) { |
| 241 | *p++ = Py_SAFE_DOWNCAST( |
| 242 | t & PyLong_MASK, unsigned long, digit); |
| 243 | t >>= PyLong_SHIFT; |
| 244 | } |
| 245 | } |
| 246 | return (PyObject *)v; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Sergey Fedoseev | c6734ee | 2019-09-12 19:41:14 +0500 | [diff] [blame] | 249 | #define PYLONG_FROM_UINT(INT_TYPE, ival) \ |
| 250 | do { \ |
| 251 | if (IS_SMALL_UINT(ival)) { \ |
Victor Stinner | 6314abc | 2019-10-01 13:29:53 +0200 | [diff] [blame] | 252 | return get_small_int((sdigit)(ival)); \ |
Sergey Fedoseev | c6734ee | 2019-09-12 19:41:14 +0500 | [diff] [blame] | 253 | } \ |
| 254 | /* Count the number of Python digits. */ \ |
| 255 | Py_ssize_t ndigits = 0; \ |
| 256 | INT_TYPE t = (ival); \ |
| 257 | while (t) { \ |
| 258 | ++ndigits; \ |
| 259 | t >>= PyLong_SHIFT; \ |
| 260 | } \ |
| 261 | PyLongObject *v = _PyLong_New(ndigits); \ |
| 262 | if (v == NULL) { \ |
| 263 | return NULL; \ |
| 264 | } \ |
| 265 | digit *p = v->ob_digit; \ |
| 266 | while ((ival)) { \ |
| 267 | *p++ = (digit)((ival) & PyLong_MASK); \ |
| 268 | (ival) >>= PyLong_SHIFT; \ |
| 269 | } \ |
| 270 | return (PyObject *)v; \ |
| 271 | } while(0) |
| 272 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 273 | /* Create a new int object from a C unsigned long int */ |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 274 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 275 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 276 | PyLong_FromUnsignedLong(unsigned long ival) |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 277 | { |
Sergey Fedoseev | c6734ee | 2019-09-12 19:41:14 +0500 | [diff] [blame] | 278 | PYLONG_FROM_UINT(unsigned long, ival); |
| 279 | } |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 280 | |
Sergey Fedoseev | c6734ee | 2019-09-12 19:41:14 +0500 | [diff] [blame] | 281 | /* Create a new int object from a C unsigned long long int. */ |
| 282 | |
| 283 | PyObject * |
| 284 | PyLong_FromUnsignedLongLong(unsigned long long ival) |
| 285 | { |
| 286 | PYLONG_FROM_UINT(unsigned long long, ival); |
| 287 | } |
| 288 | |
| 289 | /* Create a new int object from a C size_t. */ |
| 290 | |
| 291 | PyObject * |
| 292 | PyLong_FromSize_t(size_t ival) |
| 293 | { |
| 294 | PYLONG_FROM_UINT(size_t, ival); |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 297 | /* Create a new int object from a C double */ |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 298 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 299 | PyObject * |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 300 | PyLong_FromDouble(double dval) |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 301 | { |
Sergey Fedoseev | 86a93fd | 2020-05-10 14:15:57 +0500 | [diff] [blame] | 302 | /* Try to get out cheap if this fits in a long. When a finite value of real |
| 303 | * floating type is converted to an integer type, the value is truncated |
| 304 | * toward zero. If the value of the integral part cannot be represented by |
| 305 | * the integer type, the behavior is undefined. Thus, we must check that |
| 306 | * value is in range (LONG_MIN - 1, LONG_MAX + 1). If a long has more bits |
| 307 | * of precision than a double, casting LONG_MIN - 1 to double may yield an |
| 308 | * approximation, but LONG_MAX + 1 is a power of two and can be represented |
| 309 | * as double exactly (assuming FLT_RADIX is 2 or 16), so for simplicity |
| 310 | * check against [-(LONG_MAX + 1), LONG_MAX + 1). |
| 311 | */ |
| 312 | const double int_max = (unsigned long)LONG_MAX + 1; |
| 313 | if (-int_max < dval && dval < int_max) { |
| 314 | return PyLong_FromLong((long)dval); |
| 315 | } |
| 316 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 317 | PyLongObject *v; |
| 318 | double frac; |
| 319 | int i, ndig, expo, neg; |
| 320 | neg = 0; |
| 321 | if (Py_IS_INFINITY(dval)) { |
| 322 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 323 | "cannot convert float infinity to integer"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 324 | return NULL; |
| 325 | } |
| 326 | if (Py_IS_NAN(dval)) { |
| 327 | PyErr_SetString(PyExc_ValueError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 328 | "cannot convert float NaN to integer"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 329 | return NULL; |
| 330 | } |
| 331 | if (dval < 0.0) { |
| 332 | neg = 1; |
| 333 | dval = -dval; |
| 334 | } |
| 335 | 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] | 336 | assert(expo > 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 337 | ndig = (expo-1) / PyLong_SHIFT + 1; /* Number of 'digits' in result */ |
| 338 | v = _PyLong_New(ndig); |
| 339 | if (v == NULL) |
| 340 | return NULL; |
| 341 | frac = ldexp(frac, (expo-1) % PyLong_SHIFT + 1); |
| 342 | for (i = ndig; --i >= 0; ) { |
| 343 | digit bits = (digit)frac; |
| 344 | v->ob_digit[i] = bits; |
| 345 | frac = frac - (double)bits; |
| 346 | frac = ldexp(frac, PyLong_SHIFT); |
| 347 | } |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 348 | if (neg) { |
| 349 | Py_SET_SIZE(v, -(Py_SIZE(v))); |
| 350 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 351 | return (PyObject *)v; |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 354 | /* Checking for overflow in PyLong_AsLong is a PITA since C doesn't define |
| 355 | * anything about what happens when a signed integer operation overflows, |
| 356 | * 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] | 357 | * then. The bit pattern for the largest positive signed long is |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 358 | * (unsigned long)LONG_MAX, and for the smallest negative signed long |
| 359 | * it is abs(LONG_MIN), which we could write -(unsigned long)LONG_MIN. |
| 360 | * However, some other compilers warn about applying unary minus to an |
| 361 | * unsigned operand. Hence the weird "0-". |
| 362 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 363 | #define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN) |
| 364 | #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] | 365 | |
Mark Dickinson | 20941de | 2020-05-27 13:43:17 +0100 | [diff] [blame] | 366 | /* Get a C long int from an int object or any object that has an __index__ |
Mark Dickinson | 8d48b43 | 2011-10-23 20:47:14 +0100 | [diff] [blame] | 367 | method. |
| 368 | |
| 369 | On overflow, return -1 and set *overflow to 1 or -1 depending on the sign of |
| 370 | the result. Otherwise *overflow is 0. |
| 371 | |
| 372 | For other errors (e.g., TypeError), return -1 and set an error condition. |
| 373 | In this case *overflow will be 0. |
| 374 | */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 375 | |
| 376 | long |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 377 | PyLong_AsLongAndOverflow(PyObject *vv, int *overflow) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 378 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 379 | /* This version by Tim Peters */ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 380 | PyLongObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 381 | unsigned long x, prev; |
| 382 | long res; |
| 383 | Py_ssize_t i; |
| 384 | int sign; |
Mark Dickinson | 20941de | 2020-05-27 13:43:17 +0100 | [diff] [blame] | 385 | int do_decref = 0; /* if PyNumber_Index was called */ |
Guido van Rossum | f753181 | 1998-05-26 14:33:37 +0000 | [diff] [blame] | 386 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 387 | *overflow = 0; |
| 388 | if (vv == NULL) { |
| 389 | PyErr_BadInternalCall(); |
| 390 | return -1; |
| 391 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 392 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 393 | if (PyLong_Check(vv)) { |
| 394 | v = (PyLongObject *)vv; |
| 395 | } |
| 396 | else { |
Serhiy Storchaka | 5f4b229d | 2020-05-28 10:33:45 +0300 | [diff] [blame^] | 397 | v = (PyLongObject *)_PyNumber_Index(vv); |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 398 | if (v == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 399 | return -1; |
| 400 | do_decref = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 401 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 402 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 403 | res = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 404 | i = Py_SIZE(v); |
Guido van Rossum | f753181 | 1998-05-26 14:33:37 +0000 | [diff] [blame] | 405 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 406 | switch (i) { |
| 407 | case -1: |
| 408 | res = -(sdigit)v->ob_digit[0]; |
| 409 | break; |
| 410 | case 0: |
| 411 | res = 0; |
| 412 | break; |
| 413 | case 1: |
| 414 | res = v->ob_digit[0]; |
| 415 | break; |
| 416 | default: |
| 417 | sign = 1; |
| 418 | x = 0; |
| 419 | if (i < 0) { |
| 420 | sign = -1; |
| 421 | i = -(i); |
| 422 | } |
| 423 | while (--i >= 0) { |
| 424 | prev = x; |
| 425 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 426 | if ((x >> PyLong_SHIFT) != prev) { |
| 427 | *overflow = sign; |
| 428 | goto exit; |
| 429 | } |
| 430 | } |
| 431 | /* Haven't lost any bits, but casting to long requires extra |
| 432 | * care (see comment above). |
| 433 | */ |
| 434 | if (x <= (unsigned long)LONG_MAX) { |
| 435 | res = (long)x * sign; |
| 436 | } |
| 437 | else if (sign < 0 && x == PY_ABS_LONG_MIN) { |
| 438 | res = LONG_MIN; |
| 439 | } |
| 440 | else { |
| 441 | *overflow = sign; |
| 442 | /* res is already set to -1 */ |
| 443 | } |
| 444 | } |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 445 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 446 | if (do_decref) { |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 447 | Py_DECREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 448 | } |
| 449 | return res; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Mark Dickinson | 20941de | 2020-05-27 13:43:17 +0100 | [diff] [blame] | 452 | /* Get a C long int from an int object or any object that has an __index__ |
Mark Dickinson | 8d48b43 | 2011-10-23 20:47:14 +0100 | [diff] [blame] | 453 | method. Return -1 and set an error if overflow occurs. */ |
| 454 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 455 | long |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 456 | PyLong_AsLong(PyObject *obj) |
| 457 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 458 | int overflow; |
| 459 | long result = PyLong_AsLongAndOverflow(obj, &overflow); |
| 460 | if (overflow) { |
| 461 | /* XXX: could be cute and give a different |
| 462 | message for overflow == -1 */ |
| 463 | PyErr_SetString(PyExc_OverflowError, |
| 464 | "Python int too large to convert to C long"); |
| 465 | } |
| 466 | return result; |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Mark Dickinson | 20941de | 2020-05-27 13:43:17 +0100 | [diff] [blame] | 469 | /* Get a C int from an int object or any object that has an __index__ |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 470 | method. Return -1 and set an error if overflow occurs. */ |
| 471 | |
| 472 | int |
| 473 | _PyLong_AsInt(PyObject *obj) |
| 474 | { |
| 475 | int overflow; |
| 476 | long result = PyLong_AsLongAndOverflow(obj, &overflow); |
| 477 | if (overflow || result > INT_MAX || result < INT_MIN) { |
| 478 | /* XXX: could be cute and give a different |
| 479 | message for overflow == -1 */ |
| 480 | PyErr_SetString(PyExc_OverflowError, |
| 481 | "Python int too large to convert to C int"); |
| 482 | return -1; |
| 483 | } |
| 484 | return (int)result; |
| 485 | } |
| 486 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 487 | /* Get a Py_ssize_t from an int object. |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 488 | Returns -1 and sets an error condition if overflow occurs. */ |
| 489 | |
| 490 | Py_ssize_t |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 491 | PyLong_AsSsize_t(PyObject *vv) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 492 | PyLongObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 493 | size_t x, prev; |
| 494 | Py_ssize_t i; |
| 495 | int sign; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 496 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 497 | if (vv == NULL) { |
| 498 | PyErr_BadInternalCall(); |
| 499 | return -1; |
| 500 | } |
| 501 | if (!PyLong_Check(vv)) { |
| 502 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 503 | return -1; |
| 504 | } |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 505 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 506 | v = (PyLongObject *)vv; |
| 507 | i = Py_SIZE(v); |
| 508 | switch (i) { |
| 509 | case -1: return -(sdigit)v->ob_digit[0]; |
| 510 | case 0: return 0; |
| 511 | case 1: return v->ob_digit[0]; |
| 512 | } |
| 513 | sign = 1; |
| 514 | x = 0; |
| 515 | if (i < 0) { |
| 516 | sign = -1; |
| 517 | i = -(i); |
| 518 | } |
| 519 | while (--i >= 0) { |
| 520 | prev = x; |
| 521 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 522 | if ((x >> PyLong_SHIFT) != prev) |
| 523 | goto overflow; |
| 524 | } |
| 525 | /* Haven't lost any bits, but casting to a signed type requires |
| 526 | * extra care (see comment above). |
| 527 | */ |
| 528 | if (x <= (size_t)PY_SSIZE_T_MAX) { |
| 529 | return (Py_ssize_t)x * sign; |
| 530 | } |
| 531 | else if (sign < 0 && x == PY_ABS_SSIZE_T_MIN) { |
| 532 | return PY_SSIZE_T_MIN; |
| 533 | } |
| 534 | /* else overflow */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 535 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 536 | overflow: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 537 | PyErr_SetString(PyExc_OverflowError, |
| 538 | "Python int too large to convert to C ssize_t"); |
| 539 | return -1; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 542 | /* Get a C unsigned long int from an int object. |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 543 | Returns -1 and sets an error condition if overflow occurs. */ |
| 544 | |
| 545 | unsigned long |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 546 | PyLong_AsUnsignedLong(PyObject *vv) |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 547 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 548 | PyLongObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 549 | unsigned long x, prev; |
| 550 | Py_ssize_t i; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 551 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 552 | if (vv == NULL) { |
| 553 | PyErr_BadInternalCall(); |
| 554 | return (unsigned long)-1; |
| 555 | } |
| 556 | if (!PyLong_Check(vv)) { |
| 557 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 558 | return (unsigned long)-1; |
| 559 | } |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 560 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 561 | v = (PyLongObject *)vv; |
| 562 | i = Py_SIZE(v); |
| 563 | x = 0; |
| 564 | if (i < 0) { |
| 565 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 566 | "can't convert negative value to unsigned int"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 567 | return (unsigned long) -1; |
| 568 | } |
| 569 | switch (i) { |
| 570 | case 0: return 0; |
| 571 | case 1: return v->ob_digit[0]; |
| 572 | } |
| 573 | while (--i >= 0) { |
| 574 | prev = x; |
| 575 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 576 | if ((x >> PyLong_SHIFT) != prev) { |
| 577 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 02515f7 | 2013-08-03 12:08:22 +0100 | [diff] [blame] | 578 | "Python int too large to convert " |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 579 | "to C unsigned long"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 580 | return (unsigned long) -1; |
| 581 | } |
| 582 | } |
| 583 | return x; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 586 | /* 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] | 587 | an error condition if overflow occurs. */ |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 588 | |
| 589 | size_t |
| 590 | PyLong_AsSize_t(PyObject *vv) |
| 591 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 592 | PyLongObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 593 | size_t x, prev; |
| 594 | Py_ssize_t i; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 595 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 596 | if (vv == NULL) { |
| 597 | PyErr_BadInternalCall(); |
| 598 | return (size_t) -1; |
| 599 | } |
| 600 | if (!PyLong_Check(vv)) { |
| 601 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 602 | return (size_t)-1; |
| 603 | } |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 604 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 605 | v = (PyLongObject *)vv; |
| 606 | i = Py_SIZE(v); |
| 607 | x = 0; |
| 608 | if (i < 0) { |
| 609 | PyErr_SetString(PyExc_OverflowError, |
| 610 | "can't convert negative value to size_t"); |
| 611 | return (size_t) -1; |
| 612 | } |
| 613 | switch (i) { |
| 614 | case 0: return 0; |
| 615 | case 1: return v->ob_digit[0]; |
| 616 | } |
| 617 | while (--i >= 0) { |
| 618 | prev = x; |
| 619 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 620 | if ((x >> PyLong_SHIFT) != prev) { |
| 621 | PyErr_SetString(PyExc_OverflowError, |
| 622 | "Python int too large to convert to C size_t"); |
Stefan Krah | b77c6c6 | 2011-09-12 16:22:47 +0200 | [diff] [blame] | 623 | return (size_t) -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | return x; |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 629 | /* 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] | 630 | Returns -1 and sets an error condition if an error occurs. */ |
| 631 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 632 | static unsigned long |
| 633 | _PyLong_AsUnsignedLongMask(PyObject *vv) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 634 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 635 | PyLongObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 636 | unsigned long x; |
| 637 | Py_ssize_t i; |
| 638 | int sign; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 639 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 640 | if (vv == NULL || !PyLong_Check(vv)) { |
| 641 | PyErr_BadInternalCall(); |
| 642 | return (unsigned long) -1; |
| 643 | } |
| 644 | v = (PyLongObject *)vv; |
| 645 | i = Py_SIZE(v); |
| 646 | switch (i) { |
| 647 | case 0: return 0; |
| 648 | case 1: return v->ob_digit[0]; |
| 649 | } |
| 650 | sign = 1; |
| 651 | x = 0; |
| 652 | if (i < 0) { |
| 653 | sign = -1; |
| 654 | i = -i; |
| 655 | } |
| 656 | while (--i >= 0) { |
| 657 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 658 | } |
| 659 | return x * sign; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 662 | unsigned long |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 663 | PyLong_AsUnsignedLongMask(PyObject *op) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 664 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 665 | PyLongObject *lo; |
| 666 | unsigned long val; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 667 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 668 | if (op == NULL) { |
| 669 | PyErr_BadInternalCall(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 670 | return (unsigned long)-1; |
| 671 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 672 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 673 | if (PyLong_Check(op)) { |
| 674 | return _PyLong_AsUnsignedLongMask(op); |
| 675 | } |
| 676 | |
Serhiy Storchaka | 5f4b229d | 2020-05-28 10:33:45 +0300 | [diff] [blame^] | 677 | lo = (PyLongObject *)_PyNumber_Index(op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 678 | if (lo == NULL) |
| 679 | return (unsigned long)-1; |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 680 | |
| 681 | val = _PyLong_AsUnsignedLongMask((PyObject *)lo); |
| 682 | Py_DECREF(lo); |
| 683 | return val; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 686 | int |
| 687 | _PyLong_Sign(PyObject *vv) |
| 688 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 689 | PyLongObject *v = (PyLongObject *)vv; |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 690 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 691 | assert(v != NULL); |
| 692 | assert(PyLong_Check(v)); |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 693 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 694 | return Py_SIZE(v) == 0 ? 0 : (Py_SIZE(v) < 0 ? -1 : 1); |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 697 | size_t |
| 698 | _PyLong_NumBits(PyObject *vv) |
| 699 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 700 | PyLongObject *v = (PyLongObject *)vv; |
| 701 | size_t result = 0; |
| 702 | Py_ssize_t ndigits; |
Serhiy Storchaka | b2e64f9 | 2016-11-08 20:34:22 +0200 | [diff] [blame] | 703 | int msd_bits; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 704 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 705 | assert(v != NULL); |
| 706 | assert(PyLong_Check(v)); |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 707 | ndigits = Py_ABS(Py_SIZE(v)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 708 | assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0); |
| 709 | if (ndigits > 0) { |
| 710 | digit msd = v->ob_digit[ndigits - 1]; |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 711 | if ((size_t)(ndigits - 1) > SIZE_MAX / (size_t)PyLong_SHIFT) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 712 | goto Overflow; |
Mark Dickinson | fc9adb6 | 2012-10-06 18:50:02 +0100 | [diff] [blame] | 713 | result = (size_t)(ndigits - 1) * (size_t)PyLong_SHIFT; |
Niklas Fiekas | c5b7900 | 2020-01-16 15:09:19 +0100 | [diff] [blame] | 714 | msd_bits = _Py_bit_length(msd); |
Serhiy Storchaka | b2e64f9 | 2016-11-08 20:34:22 +0200 | [diff] [blame] | 715 | if (SIZE_MAX - msd_bits < result) |
| 716 | goto Overflow; |
| 717 | result += msd_bits; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 718 | } |
| 719 | return result; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 720 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 721 | Overflow: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 722 | PyErr_SetString(PyExc_OverflowError, "int has too many bits " |
| 723 | "to express in a platform size_t"); |
| 724 | return (size_t)-1; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 727 | PyObject * |
| 728 | _PyLong_FromByteArray(const unsigned char* bytes, size_t n, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 729 | int little_endian, int is_signed) |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 730 | { |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 731 | const unsigned char* pstartbyte; /* LSB of bytes */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 732 | int incr; /* direction to move pstartbyte */ |
| 733 | const unsigned char* pendbyte; /* MSB of bytes */ |
| 734 | size_t numsignificantbytes; /* number of bytes that matter */ |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 735 | Py_ssize_t ndigits; /* number of Python int digits */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 736 | PyLongObject* v; /* result */ |
| 737 | Py_ssize_t idigit = 0; /* next free index in v->ob_digit */ |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 738 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 739 | if (n == 0) |
| 740 | return PyLong_FromLong(0L); |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 741 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 742 | if (little_endian) { |
| 743 | pstartbyte = bytes; |
| 744 | pendbyte = bytes + n - 1; |
| 745 | incr = 1; |
| 746 | } |
| 747 | else { |
| 748 | pstartbyte = bytes + n - 1; |
| 749 | pendbyte = bytes; |
| 750 | incr = -1; |
| 751 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 752 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 753 | if (is_signed) |
| 754 | is_signed = *pendbyte >= 0x80; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 755 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 756 | /* Compute numsignificantbytes. This consists of finding the most |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 757 | significant byte. Leading 0 bytes are insignificant if the number |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 758 | is positive, and leading 0xff bytes if negative. */ |
| 759 | { |
| 760 | size_t i; |
| 761 | const unsigned char* p = pendbyte; |
| 762 | const int pincr = -incr; /* search MSB to LSB */ |
Martin Panter | eb99570 | 2016-07-28 01:11:04 +0000 | [diff] [blame] | 763 | const unsigned char insignificant = is_signed ? 0xff : 0x00; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 764 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 765 | for (i = 0; i < n; ++i, p += pincr) { |
Martin Panter | eb99570 | 2016-07-28 01:11:04 +0000 | [diff] [blame] | 766 | if (*p != insignificant) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 767 | break; |
| 768 | } |
| 769 | numsignificantbytes = n - i; |
| 770 | /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so |
| 771 | actually has 2 significant bytes. OTOH, 0xff0001 == |
| 772 | -0x00ffff, so we wouldn't *need* to bump it there; but we |
| 773 | do for 0xffff = -0x0001. To be safe without bothering to |
| 774 | check every case, bump it regardless. */ |
| 775 | if (is_signed && numsignificantbytes < n) |
| 776 | ++numsignificantbytes; |
| 777 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 778 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 779 | /* How many Python int digits do we need? We have |
| 780 | 8*numsignificantbytes bits, and each Python int digit has |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 781 | PyLong_SHIFT bits, so it's the ceiling of the quotient. */ |
| 782 | /* catch overflow before it happens */ |
| 783 | if (numsignificantbytes > (PY_SSIZE_T_MAX - PyLong_SHIFT) / 8) { |
| 784 | PyErr_SetString(PyExc_OverflowError, |
| 785 | "byte array too long to convert to int"); |
| 786 | return NULL; |
| 787 | } |
| 788 | ndigits = (numsignificantbytes * 8 + PyLong_SHIFT - 1) / PyLong_SHIFT; |
| 789 | v = _PyLong_New(ndigits); |
| 790 | if (v == NULL) |
| 791 | return NULL; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 793 | /* Copy the bits over. The tricky parts are computing 2's-comp on |
| 794 | the fly for signed numbers, and dealing with the mismatch between |
| 795 | 8-bit bytes and (probably) 15-bit Python digits.*/ |
| 796 | { |
| 797 | size_t i; |
| 798 | twodigits carry = 1; /* for 2's-comp calculation */ |
| 799 | twodigits accum = 0; /* sliding register */ |
| 800 | unsigned int accumbits = 0; /* number of bits in accum */ |
| 801 | const unsigned char* p = pstartbyte; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 802 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 803 | for (i = 0; i < numsignificantbytes; ++i, p += incr) { |
| 804 | twodigits thisbyte = *p; |
| 805 | /* Compute correction for 2's comp, if needed. */ |
| 806 | if (is_signed) { |
| 807 | thisbyte = (0xff ^ thisbyte) + carry; |
| 808 | carry = thisbyte >> 8; |
| 809 | thisbyte &= 0xff; |
| 810 | } |
| 811 | /* Because we're going LSB to MSB, thisbyte is |
| 812 | more significant than what's already in accum, |
| 813 | so needs to be prepended to accum. */ |
orenmn | 86aa269 | 2017-03-06 10:42:47 +0200 | [diff] [blame] | 814 | accum |= thisbyte << accumbits; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 815 | accumbits += 8; |
| 816 | if (accumbits >= PyLong_SHIFT) { |
| 817 | /* There's enough to fill a Python digit. */ |
| 818 | assert(idigit < ndigits); |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 819 | v->ob_digit[idigit] = (digit)(accum & PyLong_MASK); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 820 | ++idigit; |
| 821 | accum >>= PyLong_SHIFT; |
| 822 | accumbits -= PyLong_SHIFT; |
| 823 | assert(accumbits < PyLong_SHIFT); |
| 824 | } |
| 825 | } |
| 826 | assert(accumbits < PyLong_SHIFT); |
| 827 | if (accumbits) { |
| 828 | assert(idigit < ndigits); |
| 829 | v->ob_digit[idigit] = (digit)accum; |
| 830 | ++idigit; |
| 831 | } |
| 832 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 833 | |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 834 | Py_SET_SIZE(v, is_signed ? -idigit : idigit); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 835 | return (PyObject *)long_normalize(v); |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | int |
| 839 | _PyLong_AsByteArray(PyLongObject* v, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 840 | unsigned char* bytes, size_t n, |
| 841 | int little_endian, int is_signed) |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 842 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 843 | Py_ssize_t i; /* index into v->ob_digit */ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 844 | Py_ssize_t ndigits; /* |v->ob_size| */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 845 | twodigits accum; /* sliding register */ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 846 | unsigned int accumbits; /* # bits in accum */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 847 | int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */ |
| 848 | digit carry; /* for computing 2's-comp */ |
| 849 | size_t j; /* # bytes filled */ |
| 850 | unsigned char* p; /* pointer to next byte in bytes */ |
| 851 | int pincr; /* direction to move p */ |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 852 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 853 | assert(v != NULL && PyLong_Check(v)); |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 854 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 855 | if (Py_SIZE(v) < 0) { |
| 856 | ndigits = -(Py_SIZE(v)); |
| 857 | if (!is_signed) { |
| 858 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 859 | "can't convert negative int to unsigned"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 860 | return -1; |
| 861 | } |
| 862 | do_twos_comp = 1; |
| 863 | } |
| 864 | else { |
| 865 | ndigits = Py_SIZE(v); |
| 866 | do_twos_comp = 0; |
| 867 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 868 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 869 | if (little_endian) { |
| 870 | p = bytes; |
| 871 | pincr = 1; |
| 872 | } |
| 873 | else { |
| 874 | p = bytes + n - 1; |
| 875 | pincr = -1; |
| 876 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 877 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 878 | /* Copy over all the Python digits. |
| 879 | It's crucial that every Python digit except for the MSD contribute |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 880 | 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] | 881 | normalized. */ |
| 882 | assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0); |
| 883 | j = 0; |
| 884 | accum = 0; |
| 885 | accumbits = 0; |
| 886 | carry = do_twos_comp ? 1 : 0; |
| 887 | for (i = 0; i < ndigits; ++i) { |
| 888 | digit thisdigit = v->ob_digit[i]; |
| 889 | if (do_twos_comp) { |
| 890 | thisdigit = (thisdigit ^ PyLong_MASK) + carry; |
| 891 | carry = thisdigit >> PyLong_SHIFT; |
| 892 | thisdigit &= PyLong_MASK; |
| 893 | } |
| 894 | /* Because we're going LSB to MSB, thisdigit is more |
| 895 | significant than what's already in accum, so needs to be |
| 896 | prepended to accum. */ |
| 897 | accum |= (twodigits)thisdigit << accumbits; |
Tim Peters | 8bc84b4 | 2001-06-12 19:17:03 +0000 | [diff] [blame] | 898 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 899 | /* The most-significant digit may be (probably is) at least |
| 900 | partly empty. */ |
| 901 | if (i == ndigits - 1) { |
| 902 | /* Count # of sign bits -- they needn't be stored, |
| 903 | * although for signed conversion we need later to |
| 904 | * make sure at least one sign bit gets stored. */ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 905 | digit s = do_twos_comp ? thisdigit ^ PyLong_MASK : thisdigit; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 906 | while (s != 0) { |
| 907 | s >>= 1; |
| 908 | accumbits++; |
| 909 | } |
| 910 | } |
| 911 | else |
| 912 | accumbits += PyLong_SHIFT; |
Tim Peters | 8bc84b4 | 2001-06-12 19:17:03 +0000 | [diff] [blame] | 913 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 914 | /* Store as many bytes as possible. */ |
| 915 | while (accumbits >= 8) { |
| 916 | if (j >= n) |
| 917 | goto Overflow; |
| 918 | ++j; |
| 919 | *p = (unsigned char)(accum & 0xff); |
| 920 | p += pincr; |
| 921 | accumbits -= 8; |
| 922 | accum >>= 8; |
| 923 | } |
| 924 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 925 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 926 | /* Store the straggler (if any). */ |
| 927 | assert(accumbits < 8); |
| 928 | assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */ |
| 929 | if (accumbits > 0) { |
| 930 | if (j >= n) |
| 931 | goto Overflow; |
| 932 | ++j; |
| 933 | if (do_twos_comp) { |
| 934 | /* Fill leading bits of the byte with sign bits |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 935 | (appropriately pretending that the int had an |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 936 | infinite supply of sign bits). */ |
| 937 | accum |= (~(twodigits)0) << accumbits; |
| 938 | } |
| 939 | *p = (unsigned char)(accum & 0xff); |
| 940 | p += pincr; |
| 941 | } |
| 942 | else if (j == n && n > 0 && is_signed) { |
| 943 | /* The main loop filled the byte array exactly, so the code |
| 944 | just above didn't get to ensure there's a sign bit, and the |
| 945 | loop below wouldn't add one either. Make sure a sign bit |
| 946 | exists. */ |
| 947 | unsigned char msb = *(p - pincr); |
| 948 | int sign_bit_set = msb >= 0x80; |
| 949 | assert(accumbits == 0); |
| 950 | if (sign_bit_set == do_twos_comp) |
| 951 | return 0; |
| 952 | else |
| 953 | goto Overflow; |
| 954 | } |
Tim Peters | 05607ad | 2001-06-13 21:01:27 +0000 | [diff] [blame] | 955 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 956 | /* Fill remaining bytes with copies of the sign bit. */ |
| 957 | { |
| 958 | unsigned char signbyte = do_twos_comp ? 0xffU : 0U; |
| 959 | for ( ; j < n; ++j, p += pincr) |
| 960 | *p = signbyte; |
| 961 | } |
Tim Peters | 05607ad | 2001-06-13 21:01:27 +0000 | [diff] [blame] | 962 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 963 | return 0; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 964 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 965 | Overflow: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 966 | PyErr_SetString(PyExc_OverflowError, "int too big to convert"); |
| 967 | return -1; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 968 | |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 969 | } |
| 970 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 971 | /* Create a new int object from a C pointer */ |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 972 | |
| 973 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 974 | PyLong_FromVoidPtr(void *p) |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 975 | { |
Mark Dickinson | 9104479 | 2012-10-18 19:21:43 +0100 | [diff] [blame] | 976 | #if SIZEOF_VOID_P <= SIZEOF_LONG |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 977 | return PyLong_FromUnsignedLong((unsigned long)(uintptr_t)p); |
Mark Dickinson | 9104479 | 2012-10-18 19:21:43 +0100 | [diff] [blame] | 978 | #else |
| 979 | |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 980 | #if SIZEOF_LONG_LONG < SIZEOF_VOID_P |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 981 | # error "PyLong_FromVoidPtr: sizeof(long long) < sizeof(void*)" |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 982 | #endif |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 983 | return PyLong_FromUnsignedLongLong((unsigned long long)(uintptr_t)p); |
Mark Dickinson | 9104479 | 2012-10-18 19:21:43 +0100 | [diff] [blame] | 984 | #endif /* SIZEOF_VOID_P <= SIZEOF_LONG */ |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 985 | |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 986 | } |
| 987 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 988 | /* Get a C pointer from an int object. */ |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 989 | |
| 990 | void * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 991 | PyLong_AsVoidPtr(PyObject *vv) |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 992 | { |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 993 | #if SIZEOF_VOID_P <= SIZEOF_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 994 | long x; |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 995 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 996 | if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0) |
| 997 | x = PyLong_AsLong(vv); |
| 998 | else |
| 999 | x = PyLong_AsUnsignedLong(vv); |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1000 | #else |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1001 | |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1002 | #if SIZEOF_LONG_LONG < SIZEOF_VOID_P |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1003 | # error "PyLong_AsVoidPtr: sizeof(long long) < sizeof(void*)" |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1004 | #endif |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1005 | long long x; |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1006 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1007 | if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0) |
| 1008 | x = PyLong_AsLongLong(vv); |
| 1009 | else |
| 1010 | x = PyLong_AsUnsignedLongLong(vv); |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1011 | |
| 1012 | #endif /* SIZEOF_VOID_P <= SIZEOF_LONG */ |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1013 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1014 | if (x == -1 && PyErr_Occurred()) |
| 1015 | return NULL; |
| 1016 | return (void *)x; |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1019 | /* Initial long long support by Chris Herborth (chrish@qnx.com), later |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1020 | * rewritten to use the newer PyLong_{As,From}ByteArray API. |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1021 | */ |
| 1022 | |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 1023 | #define PY_ABS_LLONG_MIN (0-(unsigned long long)LLONG_MIN) |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1024 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1025 | /* Create a new int object from a C long long int. */ |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1026 | |
| 1027 | PyObject * |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1028 | PyLong_FromLongLong(long long ival) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1029 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1030 | PyLongObject *v; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1031 | unsigned long long abs_ival; |
| 1032 | unsigned long long t; /* unsigned so >> doesn't propagate sign bit */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1033 | int ndigits = 0; |
| 1034 | int negative = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1035 | |
animalize | 6b51998 | 2019-09-06 14:00:56 +0800 | [diff] [blame] | 1036 | if (IS_SMALL_INT(ival)) { |
Greg Price | 5e63ab0 | 2019-08-24 10:19:37 -0700 | [diff] [blame] | 1037 | return get_small_int((sdigit)ival); |
| 1038 | } |
| 1039 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1040 | if (ival < 0) { |
| 1041 | /* avoid signed overflow on negation; see comments |
| 1042 | in PyLong_FromLong above. */ |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1043 | abs_ival = (unsigned long long)(-1-ival) + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1044 | negative = 1; |
| 1045 | } |
| 1046 | else { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1047 | abs_ival = (unsigned long long)ival; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1048 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1049 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1050 | /* Count the number of Python digits. |
| 1051 | We used to pick 5 ("big enough for anything"), but that's a |
| 1052 | waste of time and space given that 5*15 = 75 bits are rarely |
| 1053 | needed. */ |
| 1054 | t = abs_ival; |
| 1055 | while (t) { |
| 1056 | ++ndigits; |
| 1057 | t >>= PyLong_SHIFT; |
| 1058 | } |
| 1059 | v = _PyLong_New(ndigits); |
| 1060 | if (v != NULL) { |
| 1061 | digit *p = v->ob_digit; |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 1062 | Py_SET_SIZE(v, negative ? -ndigits : ndigits); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1063 | t = abs_ival; |
| 1064 | while (t) { |
| 1065 | *p++ = (digit)(t & PyLong_MASK); |
| 1066 | t >>= PyLong_SHIFT; |
| 1067 | } |
| 1068 | } |
| 1069 | return (PyObject *)v; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1072 | /* 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] | 1073 | |
| 1074 | PyObject * |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1075 | PyLong_FromSsize_t(Py_ssize_t ival) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1076 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1077 | PyLongObject *v; |
| 1078 | size_t abs_ival; |
| 1079 | size_t t; /* unsigned so >> doesn't propagate sign bit */ |
| 1080 | int ndigits = 0; |
| 1081 | int negative = 0; |
Mark Dickinson | 7ab6be2 | 2008-04-15 21:42:42 +0000 | [diff] [blame] | 1082 | |
animalize | 6b51998 | 2019-09-06 14:00:56 +0800 | [diff] [blame] | 1083 | if (IS_SMALL_INT(ival)) { |
Greg Price | 5e63ab0 | 2019-08-24 10:19:37 -0700 | [diff] [blame] | 1084 | return get_small_int((sdigit)ival); |
| 1085 | } |
| 1086 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1087 | if (ival < 0) { |
| 1088 | /* avoid signed overflow when ival = SIZE_T_MIN */ |
| 1089 | abs_ival = (size_t)(-1-ival)+1; |
| 1090 | negative = 1; |
| 1091 | } |
| 1092 | else { |
| 1093 | abs_ival = (size_t)ival; |
| 1094 | } |
Mark Dickinson | 7ab6be2 | 2008-04-15 21:42:42 +0000 | [diff] [blame] | 1095 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1096 | /* Count the number of Python digits. */ |
| 1097 | t = abs_ival; |
| 1098 | while (t) { |
| 1099 | ++ndigits; |
| 1100 | t >>= PyLong_SHIFT; |
| 1101 | } |
| 1102 | v = _PyLong_New(ndigits); |
| 1103 | if (v != NULL) { |
| 1104 | digit *p = v->ob_digit; |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 1105 | Py_SET_SIZE(v, negative ? -ndigits : ndigits); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1106 | t = abs_ival; |
| 1107 | while (t) { |
| 1108 | *p++ = (digit)(t & PyLong_MASK); |
| 1109 | t >>= PyLong_SHIFT; |
| 1110 | } |
| 1111 | } |
| 1112 | return (PyObject *)v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1115 | /* Get a C long long int from an int object or any object that has an |
Mark Dickinson | 20941de | 2020-05-27 13:43:17 +0100 | [diff] [blame] | 1116 | __index__ method. Return -1 and set an error if overflow occurs. */ |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1117 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1118 | long long |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1119 | PyLong_AsLongLong(PyObject *vv) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1120 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1121 | PyLongObject *v; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1122 | long long bytes; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1123 | int res; |
Mark Dickinson | 20941de | 2020-05-27 13:43:17 +0100 | [diff] [blame] | 1124 | int do_decref = 0; /* if PyNumber_Index was called */ |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1125 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1126 | if (vv == NULL) { |
| 1127 | PyErr_BadInternalCall(); |
| 1128 | return -1; |
| 1129 | } |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1130 | |
| 1131 | if (PyLong_Check(vv)) { |
| 1132 | v = (PyLongObject *)vv; |
| 1133 | } |
| 1134 | else { |
Serhiy Storchaka | 5f4b229d | 2020-05-28 10:33:45 +0300 | [diff] [blame^] | 1135 | v = (PyLongObject *)_PyNumber_Index(vv); |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1136 | if (v == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1137 | return -1; |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1138 | do_decref = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1139 | } |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1140 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1141 | res = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1142 | switch(Py_SIZE(v)) { |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1143 | case -1: |
| 1144 | bytes = -(sdigit)v->ob_digit[0]; |
| 1145 | break; |
| 1146 | case 0: |
| 1147 | bytes = 0; |
| 1148 | break; |
| 1149 | case 1: |
| 1150 | bytes = v->ob_digit[0]; |
| 1151 | break; |
| 1152 | default: |
| 1153 | res = _PyLong_AsByteArray((PyLongObject *)v, (unsigned char *)&bytes, |
Serhiy Storchaka | c4f3212 | 2013-12-11 21:26:36 +0200 | [diff] [blame] | 1154 | SIZEOF_LONG_LONG, PY_LITTLE_ENDIAN, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1155 | } |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1156 | if (do_decref) { |
| 1157 | Py_DECREF(v); |
| 1158 | } |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1159 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1160 | /* Plan 9 can't handle long long in ? : expressions */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1161 | if (res < 0) |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1162 | return (long long)-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1163 | else |
| 1164 | return bytes; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1167 | /* Get a C unsigned long long int from an int object. |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1168 | Return -1 and set an error if overflow occurs. */ |
| 1169 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1170 | unsigned long long |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1171 | PyLong_AsUnsignedLongLong(PyObject *vv) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1172 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1173 | PyLongObject *v; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1174 | unsigned long long bytes; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1175 | int res; |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1176 | |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 1177 | if (vv == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1178 | PyErr_BadInternalCall(); |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1179 | return (unsigned long long)-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1180 | } |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 1181 | if (!PyLong_Check(vv)) { |
| 1182 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1183 | return (unsigned long long)-1; |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 1184 | } |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1185 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1186 | v = (PyLongObject*)vv; |
| 1187 | switch(Py_SIZE(v)) { |
| 1188 | case 0: return 0; |
| 1189 | case 1: return v->ob_digit[0]; |
| 1190 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1191 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 1192 | res = _PyLong_AsByteArray((PyLongObject *)vv, (unsigned char *)&bytes, |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 1193 | SIZEOF_LONG_LONG, PY_LITTLE_ENDIAN, 0); |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1194 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1195 | /* Plan 9 can't handle long long in ? : expressions */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1196 | if (res < 0) |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1197 | return (unsigned long long)res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1198 | else |
| 1199 | return bytes; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1200 | } |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1201 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1202 | /* 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] | 1203 | Returns -1 and sets an error condition if an error occurs. */ |
| 1204 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1205 | static unsigned long long |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1206 | _PyLong_AsUnsignedLongLongMask(PyObject *vv) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1207 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1208 | PyLongObject *v; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1209 | unsigned long long x; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1210 | Py_ssize_t i; |
| 1211 | int sign; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1212 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1213 | if (vv == NULL || !PyLong_Check(vv)) { |
| 1214 | PyErr_BadInternalCall(); |
Zackery Spytz | dc24765 | 2019-06-06 14:39:23 -0600 | [diff] [blame] | 1215 | return (unsigned long long) -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1216 | } |
| 1217 | v = (PyLongObject *)vv; |
| 1218 | switch(Py_SIZE(v)) { |
| 1219 | case 0: return 0; |
| 1220 | case 1: return v->ob_digit[0]; |
| 1221 | } |
| 1222 | i = Py_SIZE(v); |
| 1223 | sign = 1; |
| 1224 | x = 0; |
| 1225 | if (i < 0) { |
| 1226 | sign = -1; |
| 1227 | i = -i; |
| 1228 | } |
| 1229 | while (--i >= 0) { |
| 1230 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 1231 | } |
| 1232 | return x * sign; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1233 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1234 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1235 | unsigned long long |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1236 | PyLong_AsUnsignedLongLongMask(PyObject *op) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1237 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1238 | PyLongObject *lo; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1239 | unsigned long long val; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1240 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1241 | if (op == NULL) { |
| 1242 | PyErr_BadInternalCall(); |
Zackery Spytz | dc24765 | 2019-06-06 14:39:23 -0600 | [diff] [blame] | 1243 | return (unsigned long long)-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1244 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1245 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1246 | if (PyLong_Check(op)) { |
| 1247 | return _PyLong_AsUnsignedLongLongMask(op); |
| 1248 | } |
| 1249 | |
Serhiy Storchaka | 5f4b229d | 2020-05-28 10:33:45 +0300 | [diff] [blame^] | 1250 | lo = (PyLongObject *)_PyNumber_Index(op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1251 | if (lo == NULL) |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1252 | return (unsigned long long)-1; |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1253 | |
| 1254 | val = _PyLong_AsUnsignedLongLongMask((PyObject *)lo); |
| 1255 | Py_DECREF(lo); |
| 1256 | return val; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1257 | } |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1258 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1259 | /* Get a C long long int from an int object or any object that has an |
Mark Dickinson | 20941de | 2020-05-27 13:43:17 +0100 | [diff] [blame] | 1260 | __index__ method. |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1261 | |
Mark Dickinson | 8d48b43 | 2011-10-23 20:47:14 +0100 | [diff] [blame] | 1262 | On overflow, return -1 and set *overflow to 1 or -1 depending on the sign of |
| 1263 | the result. Otherwise *overflow is 0. |
| 1264 | |
| 1265 | For other errors (e.g., TypeError), return -1 and set an error condition. |
| 1266 | In this case *overflow will be 0. |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1267 | */ |
| 1268 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1269 | long long |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1270 | PyLong_AsLongLongAndOverflow(PyObject *vv, int *overflow) |
| 1271 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1272 | /* This version by Tim Peters */ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1273 | PyLongObject *v; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1274 | unsigned long long x, prev; |
| 1275 | long long res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1276 | Py_ssize_t i; |
| 1277 | int sign; |
Mark Dickinson | 20941de | 2020-05-27 13:43:17 +0100 | [diff] [blame] | 1278 | int do_decref = 0; /* if PyNumber_Index was called */ |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1279 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1280 | *overflow = 0; |
| 1281 | if (vv == NULL) { |
| 1282 | PyErr_BadInternalCall(); |
| 1283 | return -1; |
| 1284 | } |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1285 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1286 | if (PyLong_Check(vv)) { |
| 1287 | v = (PyLongObject *)vv; |
| 1288 | } |
| 1289 | else { |
Serhiy Storchaka | 5f4b229d | 2020-05-28 10:33:45 +0300 | [diff] [blame^] | 1290 | v = (PyLongObject *)_PyNumber_Index(vv); |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1291 | if (v == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1292 | return -1; |
| 1293 | do_decref = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1294 | } |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1296 | res = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1297 | i = Py_SIZE(v); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1298 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1299 | switch (i) { |
| 1300 | case -1: |
| 1301 | res = -(sdigit)v->ob_digit[0]; |
| 1302 | break; |
| 1303 | case 0: |
| 1304 | res = 0; |
| 1305 | break; |
| 1306 | case 1: |
| 1307 | res = v->ob_digit[0]; |
| 1308 | break; |
| 1309 | default: |
| 1310 | sign = 1; |
| 1311 | x = 0; |
| 1312 | if (i < 0) { |
| 1313 | sign = -1; |
| 1314 | i = -(i); |
| 1315 | } |
| 1316 | while (--i >= 0) { |
| 1317 | prev = x; |
| 1318 | x = (x << PyLong_SHIFT) + v->ob_digit[i]; |
| 1319 | if ((x >> PyLong_SHIFT) != prev) { |
| 1320 | *overflow = sign; |
| 1321 | goto exit; |
| 1322 | } |
| 1323 | } |
| 1324 | /* Haven't lost any bits, but casting to long requires extra |
| 1325 | * care (see comment above). |
| 1326 | */ |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 1327 | if (x <= (unsigned long long)LLONG_MAX) { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1328 | res = (long long)x * sign; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1329 | } |
| 1330 | else if (sign < 0 && x == PY_ABS_LLONG_MIN) { |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 1331 | res = LLONG_MIN; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1332 | } |
| 1333 | else { |
| 1334 | *overflow = sign; |
| 1335 | /* res is already set to -1 */ |
| 1336 | } |
| 1337 | } |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 1338 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1339 | if (do_decref) { |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1340 | Py_DECREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1341 | } |
| 1342 | return res; |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
Serhiy Storchaka | 7cb7bcf | 2018-07-26 13:22:16 +0300 | [diff] [blame] | 1345 | int |
| 1346 | _PyLong_UnsignedShort_Converter(PyObject *obj, void *ptr) |
| 1347 | { |
| 1348 | unsigned long uval; |
| 1349 | |
| 1350 | if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) { |
| 1351 | PyErr_SetString(PyExc_ValueError, "value must be positive"); |
| 1352 | return 0; |
| 1353 | } |
| 1354 | uval = PyLong_AsUnsignedLong(obj); |
| 1355 | if (uval == (unsigned long)-1 && PyErr_Occurred()) |
| 1356 | return 0; |
| 1357 | if (uval > USHRT_MAX) { |
| 1358 | PyErr_SetString(PyExc_OverflowError, |
| 1359 | "Python int too large for C unsigned short"); |
| 1360 | return 0; |
| 1361 | } |
| 1362 | |
| 1363 | *(unsigned short *)ptr = Py_SAFE_DOWNCAST(uval, unsigned long, unsigned short); |
| 1364 | return 1; |
| 1365 | } |
| 1366 | |
| 1367 | int |
| 1368 | _PyLong_UnsignedInt_Converter(PyObject *obj, void *ptr) |
| 1369 | { |
| 1370 | unsigned long uval; |
| 1371 | |
| 1372 | if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) { |
| 1373 | PyErr_SetString(PyExc_ValueError, "value must be positive"); |
| 1374 | return 0; |
| 1375 | } |
| 1376 | uval = PyLong_AsUnsignedLong(obj); |
| 1377 | if (uval == (unsigned long)-1 && PyErr_Occurred()) |
| 1378 | return 0; |
| 1379 | if (uval > UINT_MAX) { |
| 1380 | PyErr_SetString(PyExc_OverflowError, |
| 1381 | "Python int too large for C unsigned int"); |
| 1382 | return 0; |
| 1383 | } |
| 1384 | |
| 1385 | *(unsigned int *)ptr = Py_SAFE_DOWNCAST(uval, unsigned long, unsigned int); |
| 1386 | return 1; |
| 1387 | } |
| 1388 | |
| 1389 | int |
| 1390 | _PyLong_UnsignedLong_Converter(PyObject *obj, void *ptr) |
| 1391 | { |
| 1392 | unsigned long uval; |
| 1393 | |
| 1394 | if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) { |
| 1395 | PyErr_SetString(PyExc_ValueError, "value must be positive"); |
| 1396 | return 0; |
| 1397 | } |
| 1398 | uval = PyLong_AsUnsignedLong(obj); |
| 1399 | if (uval == (unsigned long)-1 && PyErr_Occurred()) |
| 1400 | return 0; |
| 1401 | |
| 1402 | *(unsigned long *)ptr = uval; |
| 1403 | return 1; |
| 1404 | } |
| 1405 | |
| 1406 | int |
| 1407 | _PyLong_UnsignedLongLong_Converter(PyObject *obj, void *ptr) |
| 1408 | { |
| 1409 | unsigned long long uval; |
| 1410 | |
| 1411 | if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) { |
| 1412 | PyErr_SetString(PyExc_ValueError, "value must be positive"); |
| 1413 | return 0; |
| 1414 | } |
| 1415 | uval = PyLong_AsUnsignedLongLong(obj); |
| 1416 | if (uval == (unsigned long long)-1 && PyErr_Occurred()) |
| 1417 | return 0; |
| 1418 | |
| 1419 | *(unsigned long long *)ptr = uval; |
| 1420 | return 1; |
| 1421 | } |
| 1422 | |
| 1423 | int |
| 1424 | _PyLong_Size_t_Converter(PyObject *obj, void *ptr) |
| 1425 | { |
| 1426 | size_t uval; |
| 1427 | |
| 1428 | if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) { |
| 1429 | PyErr_SetString(PyExc_ValueError, "value must be positive"); |
| 1430 | return 0; |
| 1431 | } |
| 1432 | uval = PyLong_AsSize_t(obj); |
| 1433 | if (uval == (size_t)-1 && PyErr_Occurred()) |
| 1434 | return 0; |
| 1435 | |
| 1436 | *(size_t *)ptr = uval; |
| 1437 | return 1; |
| 1438 | } |
| 1439 | |
| 1440 | |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 1441 | #define CHECK_BINOP(v,w) \ |
| 1442 | do { \ |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1443 | if (!PyLong_Check(v) || !PyLong_Check(w)) \ |
| 1444 | Py_RETURN_NOTIMPLEMENTED; \ |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 1445 | } while(0) |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1446 | |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1447 | /* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n] |
| 1448 | * is modified in place, by adding y to it. Carries are propagated as far as |
| 1449 | * x[m-1], and the remaining carry (0 or 1) is returned. |
| 1450 | */ |
| 1451 | static digit |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1452 | 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] | 1453 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1454 | Py_ssize_t i; |
| 1455 | digit carry = 0; |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1456 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1457 | assert(m >= n); |
| 1458 | for (i = 0; i < n; ++i) { |
| 1459 | carry += x[i] + y[i]; |
| 1460 | x[i] = carry & PyLong_MASK; |
| 1461 | carry >>= PyLong_SHIFT; |
| 1462 | assert((carry & 1) == carry); |
| 1463 | } |
| 1464 | for (; carry && i < m; ++i) { |
| 1465 | carry += x[i]; |
| 1466 | x[i] = carry & PyLong_MASK; |
| 1467 | carry >>= PyLong_SHIFT; |
| 1468 | assert((carry & 1) == carry); |
| 1469 | } |
| 1470 | return carry; |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | /* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n] |
| 1474 | * is modified in place, by subtracting y from it. Borrows are propagated as |
| 1475 | * far as x[m-1], and the remaining borrow (0 or 1) is returned. |
| 1476 | */ |
| 1477 | static digit |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1478 | 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] | 1479 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1480 | Py_ssize_t i; |
| 1481 | digit borrow = 0; |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1482 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1483 | assert(m >= n); |
| 1484 | for (i = 0; i < n; ++i) { |
| 1485 | borrow = x[i] - y[i] - borrow; |
| 1486 | x[i] = borrow & PyLong_MASK; |
| 1487 | borrow >>= PyLong_SHIFT; |
| 1488 | borrow &= 1; /* keep only 1 sign bit */ |
| 1489 | } |
| 1490 | for (; borrow && i < m; ++i) { |
| 1491 | borrow = x[i] - borrow; |
| 1492 | x[i] = borrow & PyLong_MASK; |
| 1493 | borrow >>= PyLong_SHIFT; |
| 1494 | borrow &= 1; |
| 1495 | } |
| 1496 | return borrow; |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1497 | } |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1498 | |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 1499 | /* Shift digit vector a[0:m] d bits left, with 0 <= d < PyLong_SHIFT. Put |
| 1500 | * result in z[0:m], and return the d bits shifted out of the top. |
| 1501 | */ |
| 1502 | static digit |
| 1503 | 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] | 1504 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1505 | Py_ssize_t i; |
| 1506 | digit carry = 0; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1507 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1508 | assert(0 <= d && d < PyLong_SHIFT); |
| 1509 | for (i=0; i < m; i++) { |
| 1510 | twodigits acc = (twodigits)a[i] << d | carry; |
| 1511 | z[i] = (digit)acc & PyLong_MASK; |
| 1512 | carry = (digit)(acc >> PyLong_SHIFT); |
| 1513 | } |
| 1514 | return carry; |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 1515 | } |
| 1516 | |
| 1517 | /* Shift digit vector a[0:m] d bits right, with 0 <= d < PyLong_SHIFT. Put |
| 1518 | * result in z[0:m], and return the d bits shifted out of the bottom. |
| 1519 | */ |
| 1520 | static digit |
| 1521 | v_rshift(digit *z, digit *a, Py_ssize_t m, int d) |
| 1522 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1523 | Py_ssize_t i; |
| 1524 | digit carry = 0; |
| 1525 | digit mask = ((digit)1 << d) - 1U; |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 1526 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1527 | assert(0 <= d && d < PyLong_SHIFT); |
| 1528 | for (i=m; i-- > 0;) { |
| 1529 | twodigits acc = (twodigits)carry << PyLong_SHIFT | a[i]; |
| 1530 | carry = (digit)acc & mask; |
| 1531 | z[i] = (digit)(acc >> d); |
| 1532 | } |
| 1533 | return carry; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1534 | } |
| 1535 | |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1536 | /* Divide long pin, w/ size digits, by non-zero digit n, storing quotient |
| 1537 | in pout, and returning the remainder. pin and pout point at the LSD. |
| 1538 | 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] | 1539 | _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] | 1540 | immutable. */ |
| 1541 | |
| 1542 | static digit |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1543 | inplace_divrem1(digit *pout, digit *pin, Py_ssize_t size, digit n) |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1544 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1545 | twodigits rem = 0; |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1546 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1547 | assert(n > 0 && n <= PyLong_MASK); |
| 1548 | pin += size; |
| 1549 | pout += size; |
| 1550 | while (--size >= 0) { |
| 1551 | digit hi; |
| 1552 | rem = (rem << PyLong_SHIFT) | *--pin; |
| 1553 | *--pout = hi = (digit)(rem / n); |
| 1554 | rem -= (twodigits)hi * n; |
| 1555 | } |
| 1556 | return (digit)rem; |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1559 | /* Divide an integer by a digit, returning both the quotient |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1560 | (as function result) and the remainder (through *prem). |
| 1561 | The sign of a is ignored; n should not be zero. */ |
| 1562 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1563 | static PyLongObject * |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1564 | divrem1(PyLongObject *a, digit n, digit *prem) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1565 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 1566 | const Py_ssize_t size = Py_ABS(Py_SIZE(a)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1567 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1568 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1569 | assert(n > 0 && n <= PyLong_MASK); |
| 1570 | z = _PyLong_New(size); |
| 1571 | if (z == NULL) |
| 1572 | return NULL; |
| 1573 | *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n); |
| 1574 | return long_normalize(z); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1575 | } |
| 1576 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1577 | /* 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] | 1578 | string. (Return value is non-shared so that callers can modify the |
| 1579 | returned value if necessary.) */ |
| 1580 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1581 | static int |
| 1582 | long_to_decimal_string_internal(PyObject *aa, |
| 1583 | PyObject **p_output, |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1584 | _PyUnicodeWriter *writer, |
| 1585 | _PyBytesWriter *bytes_writer, |
| 1586 | char **bytes_str) |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1587 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1588 | PyLongObject *scratch, *a; |
Victor Stinner | 1285e5c | 2015-10-14 12:10:20 +0200 | [diff] [blame] | 1589 | PyObject *str = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1590 | Py_ssize_t size, strlen, size_a, i, j; |
| 1591 | digit *pout, *pin, rem, tenpow; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1592 | int negative; |
Mark Dickinson | 4e1de16 | 2016-08-29 17:26:43 +0100 | [diff] [blame] | 1593 | int d; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1594 | enum PyUnicode_Kind kind; |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1595 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1596 | a = (PyLongObject *)aa; |
| 1597 | if (a == NULL || !PyLong_Check(a)) { |
| 1598 | PyErr_BadInternalCall(); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1599 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1600 | } |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 1601 | size_a = Py_ABS(Py_SIZE(a)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1602 | negative = Py_SIZE(a) < 0; |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1603 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1604 | /* quick and dirty upper bound for the number of digits |
| 1605 | required to express a in base _PyLong_DECIMAL_BASE: |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1606 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1607 | #digits = 1 + floor(log2(a) / log2(_PyLong_DECIMAL_BASE)) |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1608 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1609 | But log2(a) < size_a * PyLong_SHIFT, and |
| 1610 | log2(_PyLong_DECIMAL_BASE) = log2(10) * _PyLong_DECIMAL_SHIFT |
Mark Dickinson | 4e1de16 | 2016-08-29 17:26:43 +0100 | [diff] [blame] | 1611 | > 3.3 * _PyLong_DECIMAL_SHIFT |
| 1612 | |
| 1613 | size_a * PyLong_SHIFT / (3.3 * _PyLong_DECIMAL_SHIFT) = |
| 1614 | size_a + size_a / d < size_a + size_a / floor(d), |
| 1615 | where d = (3.3 * _PyLong_DECIMAL_SHIFT) / |
| 1616 | (PyLong_SHIFT - 3.3 * _PyLong_DECIMAL_SHIFT) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1617 | */ |
Mark Dickinson | 4e1de16 | 2016-08-29 17:26:43 +0100 | [diff] [blame] | 1618 | d = (33 * _PyLong_DECIMAL_SHIFT) / |
| 1619 | (10 * PyLong_SHIFT - 33 * _PyLong_DECIMAL_SHIFT); |
| 1620 | assert(size_a < PY_SSIZE_T_MAX/2); |
| 1621 | size = 1 + size_a + size_a / d; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1622 | scratch = _PyLong_New(size); |
| 1623 | if (scratch == NULL) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1624 | return -1; |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1625 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1626 | /* convert array of base _PyLong_BASE digits in pin to an array of |
| 1627 | base _PyLong_DECIMAL_BASE digits in pout, following Knuth (TAOCP, |
| 1628 | Volume 2 (3rd edn), section 4.4, Method 1b). */ |
| 1629 | pin = a->ob_digit; |
| 1630 | pout = scratch->ob_digit; |
| 1631 | size = 0; |
| 1632 | for (i = size_a; --i >= 0; ) { |
| 1633 | digit hi = pin[i]; |
| 1634 | for (j = 0; j < size; j++) { |
| 1635 | twodigits z = (twodigits)pout[j] << PyLong_SHIFT | hi; |
| 1636 | hi = (digit)(z / _PyLong_DECIMAL_BASE); |
| 1637 | pout[j] = (digit)(z - (twodigits)hi * |
| 1638 | _PyLong_DECIMAL_BASE); |
| 1639 | } |
| 1640 | while (hi) { |
| 1641 | pout[size++] = hi % _PyLong_DECIMAL_BASE; |
| 1642 | hi /= _PyLong_DECIMAL_BASE; |
| 1643 | } |
| 1644 | /* check for keyboard interrupt */ |
| 1645 | SIGCHECK({ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 1646 | Py_DECREF(scratch); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1647 | return -1; |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 1648 | }); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1649 | } |
| 1650 | /* pout should have at least one digit, so that the case when a = 0 |
| 1651 | works correctly */ |
| 1652 | if (size == 0) |
| 1653 | pout[size++] = 0; |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1654 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1655 | /* calculate exact length of output string, and allocate */ |
| 1656 | strlen = negative + 1 + (size - 1) * _PyLong_DECIMAL_SHIFT; |
| 1657 | tenpow = 10; |
| 1658 | rem = pout[size-1]; |
| 1659 | while (rem >= tenpow) { |
| 1660 | tenpow *= 10; |
| 1661 | strlen++; |
| 1662 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1663 | if (writer) { |
Christian Heimes | 110ac16 | 2012-09-10 02:51:27 +0200 | [diff] [blame] | 1664 | if (_PyUnicodeWriter_Prepare(writer, strlen, '9') == -1) { |
| 1665 | Py_DECREF(scratch); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1666 | return -1; |
Christian Heimes | 110ac16 | 2012-09-10 02:51:27 +0200 | [diff] [blame] | 1667 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1668 | kind = writer->kind; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1669 | } |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1670 | else if (bytes_writer) { |
| 1671 | *bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, strlen); |
| 1672 | if (*bytes_str == NULL) { |
| 1673 | Py_DECREF(scratch); |
| 1674 | return -1; |
| 1675 | } |
| 1676 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1677 | else { |
| 1678 | str = PyUnicode_New(strlen, '9'); |
| 1679 | if (str == NULL) { |
| 1680 | Py_DECREF(scratch); |
| 1681 | return -1; |
| 1682 | } |
| 1683 | kind = PyUnicode_KIND(str); |
| 1684 | } |
| 1685 | |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1686 | #define WRITE_DIGITS(p) \ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1687 | do { \ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1688 | /* pout[0] through pout[size-2] contribute exactly \ |
| 1689 | _PyLong_DECIMAL_SHIFT digits each */ \ |
| 1690 | for (i=0; i < size - 1; i++) { \ |
| 1691 | rem = pout[i]; \ |
| 1692 | for (j = 0; j < _PyLong_DECIMAL_SHIFT; j++) { \ |
| 1693 | *--p = '0' + rem % 10; \ |
| 1694 | rem /= 10; \ |
| 1695 | } \ |
| 1696 | } \ |
| 1697 | /* pout[size-1]: always produce at least one decimal digit */ \ |
| 1698 | rem = pout[i]; \ |
| 1699 | do { \ |
| 1700 | *--p = '0' + rem % 10; \ |
| 1701 | rem /= 10; \ |
| 1702 | } while (rem != 0); \ |
| 1703 | \ |
| 1704 | /* and sign */ \ |
| 1705 | if (negative) \ |
| 1706 | *--p = '-'; \ |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1707 | } while (0) |
| 1708 | |
| 1709 | #define WRITE_UNICODE_DIGITS(TYPE) \ |
| 1710 | do { \ |
| 1711 | if (writer) \ |
| 1712 | p = (TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos + strlen; \ |
| 1713 | else \ |
| 1714 | p = (TYPE*)PyUnicode_DATA(str) + strlen; \ |
| 1715 | \ |
| 1716 | WRITE_DIGITS(p); \ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1717 | \ |
| 1718 | /* check we've counted correctly */ \ |
| 1719 | if (writer) \ |
| 1720 | assert(p == ((TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos)); \ |
| 1721 | else \ |
| 1722 | assert(p == (TYPE*)PyUnicode_DATA(str)); \ |
| 1723 | } while (0) |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1724 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1725 | /* fill the string right-to-left */ |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1726 | if (bytes_writer) { |
| 1727 | char *p = *bytes_str + strlen; |
| 1728 | WRITE_DIGITS(p); |
| 1729 | assert(p == *bytes_str); |
| 1730 | } |
| 1731 | else if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1732 | Py_UCS1 *p; |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1733 | WRITE_UNICODE_DIGITS(Py_UCS1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1734 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1735 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 1736 | Py_UCS2 *p; |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1737 | WRITE_UNICODE_DIGITS(Py_UCS2); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1738 | } |
| 1739 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1740 | Py_UCS4 *p; |
Victor Stinner | e577ab3 | 2012-05-29 18:51:10 +0200 | [diff] [blame] | 1741 | assert (kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1742 | WRITE_UNICODE_DIGITS(Py_UCS4); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1743 | } |
| 1744 | #undef WRITE_DIGITS |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1745 | #undef WRITE_UNICODE_DIGITS |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1746 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1747 | Py_DECREF(scratch); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1748 | if (writer) { |
| 1749 | writer->pos += strlen; |
| 1750 | } |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1751 | else if (bytes_writer) { |
| 1752 | (*bytes_str) += strlen; |
| 1753 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1754 | else { |
| 1755 | assert(_PyUnicode_CheckConsistency(str, 1)); |
| 1756 | *p_output = (PyObject *)str; |
| 1757 | } |
| 1758 | return 0; |
| 1759 | } |
| 1760 | |
| 1761 | static PyObject * |
| 1762 | long_to_decimal_string(PyObject *aa) |
| 1763 | { |
| 1764 | PyObject *v; |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1765 | if (long_to_decimal_string_internal(aa, &v, NULL, NULL, NULL) == -1) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1766 | return NULL; |
| 1767 | return v; |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1768 | } |
| 1769 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1770 | /* Convert an int object to a string, using a given conversion base, |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1771 | which should be one of 2, 8 or 16. Return a string object. |
| 1772 | If base is 2, 8 or 16, add the proper prefix '0b', '0o' or '0x' |
| 1773 | if alternate is nonzero. */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1774 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1775 | static int |
| 1776 | long_format_binary(PyObject *aa, int base, int alternate, |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1777 | PyObject **p_output, _PyUnicodeWriter *writer, |
| 1778 | _PyBytesWriter *bytes_writer, char **bytes_str) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1779 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1780 | PyLongObject *a = (PyLongObject *)aa; |
Victor Stinner | 1285e5c | 2015-10-14 12:10:20 +0200 | [diff] [blame] | 1781 | PyObject *v = NULL; |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1782 | Py_ssize_t sz; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1783 | Py_ssize_t size_a; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1784 | enum PyUnicode_Kind kind; |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1785 | int negative; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1786 | int bits; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1787 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1788 | assert(base == 2 || base == 8 || base == 16); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1789 | if (a == NULL || !PyLong_Check(a)) { |
| 1790 | PyErr_BadInternalCall(); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1791 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1792 | } |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 1793 | size_a = Py_ABS(Py_SIZE(a)); |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1794 | negative = Py_SIZE(a) < 0; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1795 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1796 | /* Compute a rough upper bound for the length of the string */ |
| 1797 | switch (base) { |
| 1798 | case 16: |
| 1799 | bits = 4; |
| 1800 | break; |
| 1801 | case 8: |
| 1802 | bits = 3; |
| 1803 | break; |
| 1804 | case 2: |
| 1805 | bits = 1; |
| 1806 | break; |
| 1807 | default: |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 1808 | Py_UNREACHABLE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1809 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1810 | |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1811 | /* Compute exact length 'sz' of output string. */ |
| 1812 | if (size_a == 0) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1813 | sz = 1; |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1814 | } |
| 1815 | else { |
| 1816 | Py_ssize_t size_a_in_bits; |
| 1817 | /* Ensure overflow doesn't occur during computation of sz. */ |
| 1818 | if (size_a > (PY_SSIZE_T_MAX - 3) / PyLong_SHIFT) { |
| 1819 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 02515f7 | 2013-08-03 12:08:22 +0100 | [diff] [blame] | 1820 | "int too large to format"); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1821 | return -1; |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1822 | } |
| 1823 | size_a_in_bits = (size_a - 1) * PyLong_SHIFT + |
Niklas Fiekas | c5b7900 | 2020-01-16 15:09:19 +0100 | [diff] [blame] | 1824 | _Py_bit_length(a->ob_digit[size_a - 1]); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1825 | /* Allow 1 character for a '-' sign. */ |
| 1826 | sz = negative + (size_a_in_bits + (bits - 1)) / bits; |
| 1827 | } |
| 1828 | if (alternate) { |
| 1829 | /* 2 characters for prefix */ |
| 1830 | sz += 2; |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1831 | } |
| 1832 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1833 | if (writer) { |
| 1834 | if (_PyUnicodeWriter_Prepare(writer, sz, 'x') == -1) |
| 1835 | return -1; |
| 1836 | kind = writer->kind; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1837 | } |
Victor Stinner | 199c9a6 | 2015-10-14 09:47:23 +0200 | [diff] [blame] | 1838 | else if (bytes_writer) { |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1839 | *bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, sz); |
| 1840 | if (*bytes_str == NULL) |
| 1841 | return -1; |
| 1842 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1843 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1844 | v = PyUnicode_New(sz, 'x'); |
| 1845 | if (v == NULL) |
| 1846 | return -1; |
| 1847 | kind = PyUnicode_KIND(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1848 | } |
Mark Dickinson | 8accd6b | 2009-09-17 19:39:12 +0000 | [diff] [blame] | 1849 | |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1850 | #define WRITE_DIGITS(p) \ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1851 | do { \ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1852 | if (size_a == 0) { \ |
| 1853 | *--p = '0'; \ |
| 1854 | } \ |
| 1855 | else { \ |
| 1856 | /* JRH: special case for power-of-2 bases */ \ |
| 1857 | twodigits accum = 0; \ |
| 1858 | int accumbits = 0; /* # of bits in accum */ \ |
| 1859 | Py_ssize_t i; \ |
| 1860 | for (i = 0; i < size_a; ++i) { \ |
| 1861 | accum |= (twodigits)a->ob_digit[i] << accumbits; \ |
| 1862 | accumbits += PyLong_SHIFT; \ |
| 1863 | assert(accumbits >= bits); \ |
| 1864 | do { \ |
| 1865 | char cdigit; \ |
| 1866 | cdigit = (char)(accum & (base - 1)); \ |
| 1867 | cdigit += (cdigit < 10) ? '0' : 'a'-10; \ |
| 1868 | *--p = cdigit; \ |
| 1869 | accumbits -= bits; \ |
| 1870 | accum >>= bits; \ |
| 1871 | } while (i < size_a-1 ? accumbits >= bits : accum > 0); \ |
| 1872 | } \ |
| 1873 | } \ |
| 1874 | \ |
| 1875 | if (alternate) { \ |
| 1876 | if (base == 16) \ |
| 1877 | *--p = 'x'; \ |
| 1878 | else if (base == 8) \ |
| 1879 | *--p = 'o'; \ |
| 1880 | else /* (base == 2) */ \ |
| 1881 | *--p = 'b'; \ |
| 1882 | *--p = '0'; \ |
| 1883 | } \ |
| 1884 | if (negative) \ |
| 1885 | *--p = '-'; \ |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1886 | } while (0) |
| 1887 | |
| 1888 | #define WRITE_UNICODE_DIGITS(TYPE) \ |
| 1889 | do { \ |
| 1890 | if (writer) \ |
| 1891 | p = (TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos + sz; \ |
| 1892 | else \ |
| 1893 | p = (TYPE*)PyUnicode_DATA(v) + sz; \ |
| 1894 | \ |
| 1895 | WRITE_DIGITS(p); \ |
| 1896 | \ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1897 | if (writer) \ |
| 1898 | assert(p == ((TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos)); \ |
| 1899 | else \ |
| 1900 | assert(p == (TYPE*)PyUnicode_DATA(v)); \ |
| 1901 | } while (0) |
| 1902 | |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1903 | if (bytes_writer) { |
| 1904 | char *p = *bytes_str + sz; |
| 1905 | WRITE_DIGITS(p); |
| 1906 | assert(p == *bytes_str); |
| 1907 | } |
| 1908 | else if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1909 | Py_UCS1 *p; |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1910 | WRITE_UNICODE_DIGITS(Py_UCS1); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1911 | } |
| 1912 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 1913 | Py_UCS2 *p; |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1914 | WRITE_UNICODE_DIGITS(Py_UCS2); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1915 | } |
| 1916 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1917 | Py_UCS4 *p; |
Victor Stinner | e577ab3 | 2012-05-29 18:51:10 +0200 | [diff] [blame] | 1918 | assert (kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1919 | WRITE_UNICODE_DIGITS(Py_UCS4); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1920 | } |
| 1921 | #undef WRITE_DIGITS |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1922 | #undef WRITE_UNICODE_DIGITS |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1923 | |
| 1924 | if (writer) { |
| 1925 | writer->pos += sz; |
| 1926 | } |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1927 | else if (bytes_writer) { |
| 1928 | (*bytes_str) += sz; |
| 1929 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1930 | else { |
| 1931 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 1932 | *p_output = v; |
| 1933 | } |
| 1934 | return 0; |
| 1935 | } |
| 1936 | |
| 1937 | PyObject * |
| 1938 | _PyLong_Format(PyObject *obj, int base) |
| 1939 | { |
| 1940 | PyObject *str; |
| 1941 | int err; |
| 1942 | if (base == 10) |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1943 | err = long_to_decimal_string_internal(obj, &str, NULL, NULL, NULL); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1944 | else |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1945 | err = long_format_binary(obj, base, 1, &str, NULL, NULL, NULL); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1946 | if (err == -1) |
| 1947 | return NULL; |
| 1948 | return str; |
| 1949 | } |
| 1950 | |
| 1951 | int |
| 1952 | _PyLong_FormatWriter(_PyUnicodeWriter *writer, |
| 1953 | PyObject *obj, |
| 1954 | int base, int alternate) |
| 1955 | { |
| 1956 | if (base == 10) |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1957 | return long_to_decimal_string_internal(obj, NULL, writer, |
| 1958 | NULL, NULL); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1959 | else |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1960 | return long_format_binary(obj, base, alternate, NULL, writer, |
| 1961 | NULL, NULL); |
| 1962 | } |
| 1963 | |
| 1964 | char* |
| 1965 | _PyLong_FormatBytesWriter(_PyBytesWriter *writer, char *str, |
| 1966 | PyObject *obj, |
| 1967 | int base, int alternate) |
| 1968 | { |
| 1969 | char *str2; |
| 1970 | int res; |
| 1971 | str2 = str; |
| 1972 | if (base == 10) |
| 1973 | res = long_to_decimal_string_internal(obj, NULL, NULL, |
| 1974 | writer, &str2); |
| 1975 | else |
| 1976 | res = long_format_binary(obj, base, alternate, NULL, NULL, |
| 1977 | writer, &str2); |
| 1978 | if (res < 0) |
| 1979 | return NULL; |
| 1980 | assert(str2 != NULL); |
| 1981 | return str2; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1984 | /* Table of digit values for 8-bit string -> integer conversion. |
| 1985 | * '0' maps to 0, ..., '9' maps to 9. |
| 1986 | * 'a' and 'A' map to 10, ..., 'z' and 'Z' map to 35. |
| 1987 | * All other indices map to 37. |
| 1988 | * 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] | 1989 | * base B digit iff _PyLong_DigitValue[Py_CHARPyLong_MASK(c)] < B. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1990 | */ |
Raymond Hettinger | 3563153 | 2009-01-09 03:58:09 +0000 | [diff] [blame] | 1991 | unsigned char _PyLong_DigitValue[256] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1992 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 1993 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 1994 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 1995 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 37, 37, 37, 37, 37, 37, |
| 1996 | 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
| 1997 | 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37, |
| 1998 | 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
| 1999 | 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37, |
| 2000 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2001 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2002 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2003 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2004 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2005 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2006 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2007 | 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] | 2008 | }; |
| 2009 | |
| 2010 | /* *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] | 2011 | * 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] | 2012 | * non-digit (which may be *str!). A normalized int is returned. |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 2013 | * The point to this routine is that it takes time linear in the number of |
| 2014 | * string characters. |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2015 | * |
| 2016 | * Return values: |
| 2017 | * -1 on syntax error (exception needs to be set, *res is untouched) |
| 2018 | * 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] | 2019 | */ |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2020 | static int |
| 2021 | long_from_binary_base(const char **str, int base, PyLongObject **res) |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 2022 | { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 2023 | const char *p = *str; |
| 2024 | const char *start = p; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2025 | char prev = 0; |
Serhiy Storchaka | 29ba688 | 2017-12-03 22:16:21 +0200 | [diff] [blame] | 2026 | Py_ssize_t digits = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2027 | int bits_per_char; |
| 2028 | Py_ssize_t n; |
| 2029 | PyLongObject *z; |
| 2030 | twodigits accum; |
| 2031 | int bits_in_accum; |
| 2032 | digit *pdigit; |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 2033 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2034 | assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0); |
| 2035 | n = base; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2036 | for (bits_per_char = -1; n; ++bits_per_char) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2037 | n >>= 1; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2038 | } |
| 2039 | /* count digits and set p to end-of-string */ |
| 2040 | while (_PyLong_DigitValue[Py_CHARMASK(*p)] < base || *p == '_') { |
| 2041 | if (*p == '_') { |
| 2042 | if (prev == '_') { |
| 2043 | *str = p - 1; |
| 2044 | return -1; |
| 2045 | } |
| 2046 | } else { |
| 2047 | ++digits; |
| 2048 | } |
| 2049 | prev = *p; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2050 | ++p; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2051 | } |
| 2052 | if (prev == '_') { |
| 2053 | /* Trailing underscore not allowed. */ |
| 2054 | *str = p - 1; |
| 2055 | return -1; |
| 2056 | } |
| 2057 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2058 | *str = p; |
Serhiy Storchaka | 85c0b89 | 2017-10-03 14:13:44 +0300 | [diff] [blame] | 2059 | /* n <- the number of Python digits needed, |
| 2060 | = ceiling((digits * bits_per_char) / PyLong_SHIFT). */ |
| 2061 | if (digits > (PY_SSIZE_T_MAX - (PyLong_SHIFT - 1)) / bits_per_char) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2062 | PyErr_SetString(PyExc_ValueError, |
| 2063 | "int string too large to convert"); |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2064 | *res = NULL; |
| 2065 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2066 | } |
Serhiy Storchaka | 85c0b89 | 2017-10-03 14:13:44 +0300 | [diff] [blame] | 2067 | n = (digits * bits_per_char + PyLong_SHIFT - 1) / PyLong_SHIFT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2068 | z = _PyLong_New(n); |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2069 | if (z == NULL) { |
| 2070 | *res = NULL; |
| 2071 | return 0; |
| 2072 | } |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2073 | /* Read string from right, and fill in int from left; i.e., |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2074 | * from least to most significant in both. |
| 2075 | */ |
| 2076 | accum = 0; |
| 2077 | bits_in_accum = 0; |
| 2078 | pdigit = z->ob_digit; |
| 2079 | while (--p >= start) { |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2080 | int k; |
| 2081 | if (*p == '_') { |
| 2082 | continue; |
| 2083 | } |
| 2084 | k = (int)_PyLong_DigitValue[Py_CHARMASK(*p)]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2085 | assert(k >= 0 && k < base); |
| 2086 | accum |= (twodigits)k << bits_in_accum; |
| 2087 | bits_in_accum += bits_per_char; |
| 2088 | if (bits_in_accum >= PyLong_SHIFT) { |
| 2089 | *pdigit++ = (digit)(accum & PyLong_MASK); |
| 2090 | assert(pdigit - z->ob_digit <= n); |
| 2091 | accum >>= PyLong_SHIFT; |
| 2092 | bits_in_accum -= PyLong_SHIFT; |
| 2093 | assert(bits_in_accum < PyLong_SHIFT); |
| 2094 | } |
| 2095 | } |
| 2096 | if (bits_in_accum) { |
| 2097 | assert(bits_in_accum <= PyLong_SHIFT); |
| 2098 | *pdigit++ = (digit)accum; |
| 2099 | assert(pdigit - z->ob_digit <= n); |
| 2100 | } |
| 2101 | while (pdigit - z->ob_digit < n) |
| 2102 | *pdigit++ = 0; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2103 | *res = long_normalize(z); |
| 2104 | return 0; |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 2105 | } |
| 2106 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2107 | /* Parses an int from a bytestring. Leading and trailing whitespace will be |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2108 | * ignored. |
| 2109 | * |
| 2110 | * If successful, a PyLong object will be returned and 'pend' will be pointing |
| 2111 | * to the first unused byte unless it's NULL. |
| 2112 | * |
| 2113 | * If unsuccessful, NULL will be returned. |
| 2114 | */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2115 | PyObject * |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 2116 | PyLong_FromString(const char *str, char **pend, int base) |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 2117 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2118 | int sign = 1, error_if_nonzero = 0; |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 2119 | const char *start, *orig_str = str; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2120 | PyLongObject *z = NULL; |
| 2121 | PyObject *strobj; |
| 2122 | Py_ssize_t slen; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2123 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2124 | if ((base != 0 && base < 2) || base > 36) { |
| 2125 | PyErr_SetString(PyExc_ValueError, |
| 2126 | "int() arg 2 must be >= 2 and <= 36"); |
| 2127 | return NULL; |
| 2128 | } |
Jordon Xu | 2ec7010 | 2019-09-11 00:04:08 +0800 | [diff] [blame] | 2129 | while (*str != '\0' && Py_ISSPACE(*str)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2130 | str++; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2131 | } |
| 2132 | if (*str == '+') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2133 | ++str; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2134 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2135 | else if (*str == '-') { |
| 2136 | ++str; |
| 2137 | sign = -1; |
| 2138 | } |
| 2139 | if (base == 0) { |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2140 | if (str[0] != '0') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2141 | base = 10; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2142 | } |
| 2143 | else if (str[1] == 'x' || str[1] == 'X') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2144 | base = 16; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2145 | } |
| 2146 | else if (str[1] == 'o' || str[1] == 'O') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2147 | base = 8; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2148 | } |
| 2149 | else if (str[1] == 'b' || str[1] == 'B') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2150 | base = 2; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2151 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2152 | else { |
| 2153 | /* "old" (C-style) octal literal, now invalid. |
| 2154 | it might still be zero though */ |
| 2155 | error_if_nonzero = 1; |
| 2156 | base = 10; |
| 2157 | } |
| 2158 | } |
| 2159 | if (str[0] == '0' && |
| 2160 | ((base == 16 && (str[1] == 'x' || str[1] == 'X')) || |
| 2161 | (base == 8 && (str[1] == 'o' || str[1] == 'O')) || |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2162 | (base == 2 && (str[1] == 'b' || str[1] == 'B')))) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2163 | str += 2; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2164 | /* One underscore allowed here. */ |
| 2165 | if (*str == '_') { |
| 2166 | ++str; |
| 2167 | } |
| 2168 | } |
| 2169 | if (str[0] == '_') { |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 2170 | /* May not start with underscores. */ |
| 2171 | goto onError; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2172 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2174 | start = str; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2175 | if ((base & (base - 1)) == 0) { |
| 2176 | int res = long_from_binary_base(&str, base, &z); |
| 2177 | if (res < 0) { |
| 2178 | /* Syntax error. */ |
| 2179 | goto onError; |
| 2180 | } |
| 2181 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2182 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2183 | /*** |
| 2184 | Binary bases can be converted in time linear in the number of digits, because |
| 2185 | Python's representation base is binary. Other bases (including decimal!) use |
| 2186 | the simple quadratic-time algorithm below, complicated by some speed tricks. |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2187 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2188 | First some math: the largest integer that can be expressed in N base-B digits |
| 2189 | is B**N-1. Consequently, if we have an N-digit input in base B, the worst- |
| 2190 | case number of Python digits needed to hold it is the smallest integer n s.t. |
| 2191 | |
| 2192 | BASE**n-1 >= B**N-1 [or, adding 1 to both sides] |
| 2193 | BASE**n >= B**N [taking logs to base BASE] |
| 2194 | n >= log(B**N)/log(BASE) = N * log(B)/log(BASE) |
| 2195 | |
| 2196 | 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] | 2197 | 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] | 2198 | and the result is computed into it. |
| 2199 | |
| 2200 | The input string is actually treated as being in base base**i (i.e., i digits |
| 2201 | are processed at a time), where two more static arrays hold: |
| 2202 | |
| 2203 | convwidth_base[base] = the largest integer i such that base**i <= BASE |
| 2204 | convmultmax_base[base] = base ** convwidth_base[base] |
| 2205 | |
| 2206 | The first of these is the largest i such that i consecutive input digits |
| 2207 | must fit in a single Python digit. The second is effectively the input |
| 2208 | base we're really using. |
| 2209 | |
| 2210 | Viewing the input as a sequence <c0, c1, ..., c_n-1> of digits in base |
| 2211 | convmultmax_base[base], the result is "simply" |
| 2212 | |
| 2213 | (((c0*B + c1)*B + c2)*B + c3)*B + ... ))) + c_n-1 |
| 2214 | |
| 2215 | where B = convmultmax_base[base]. |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2216 | |
| 2217 | Error analysis: as above, the number of Python digits `n` needed is worst- |
| 2218 | case |
| 2219 | |
| 2220 | n >= N * log(B)/log(BASE) |
| 2221 | |
| 2222 | where `N` is the number of input digits in base `B`. This is computed via |
| 2223 | |
| 2224 | size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1; |
| 2225 | |
| 2226 | below. Two numeric concerns are how much space this can waste, and whether |
| 2227 | the computed result can be too small. To be concrete, assume BASE = 2**15, |
| 2228 | which is the default (and it's unlikely anyone changes that). |
| 2229 | |
| 2230 | Waste isn't a problem: provided the first input digit isn't 0, the difference |
| 2231 | between the worst-case input with N digits and the smallest input with N |
| 2232 | digits is about a factor of B, but B is small compared to BASE so at most |
| 2233 | one allocated Python digit can remain unused on that count. If |
| 2234 | N*log(B)/log(BASE) is mathematically an exact integer, then truncating that |
| 2235 | and adding 1 returns a result 1 larger than necessary. However, that can't |
| 2236 | happen: whenever B is a power of 2, long_from_binary_base() is called |
| 2237 | instead, and it's impossible for B**i to be an integer power of 2**15 when |
| 2238 | B is not a power of 2 (i.e., it's impossible for N*log(B)/log(BASE) to be |
| 2239 | an exact integer when B is not a power of 2, since B**i has a prime factor |
| 2240 | other than 2 in that case, but (2**15)**j's only prime factor is 2). |
| 2241 | |
| 2242 | The computed result can be too small if the true value of N*log(B)/log(BASE) |
| 2243 | is a little bit larger than an exact integer, but due to roundoff errors (in |
| 2244 | computing log(B), log(BASE), their quotient, and/or multiplying that by N) |
| 2245 | yields a numeric result a little less than that integer. Unfortunately, "how |
| 2246 | close can a transcendental function get to an integer over some range?" |
| 2247 | questions are generally theoretically intractable. Computer analysis via |
| 2248 | continued fractions is practical: expand log(B)/log(BASE) via continued |
| 2249 | fractions, giving a sequence i/j of "the best" rational approximations. Then |
| 2250 | j*log(B)/log(BASE) is approximately equal to (the integer) i. This shows that |
| 2251 | we can get very close to being in trouble, but very rarely. For example, |
| 2252 | 76573 is a denominator in one of the continued-fraction approximations to |
| 2253 | log(10)/log(2**15), and indeed: |
| 2254 | |
| 2255 | >>> log(10)/log(2**15)*76573 |
| 2256 | 16958.000000654003 |
| 2257 | |
| 2258 | is very close to an integer. If we were working with IEEE single-precision, |
| 2259 | rounding errors could kill us. Finding worst cases in IEEE double-precision |
| 2260 | requires better-than-double-precision log() functions, and Tim didn't bother. |
| 2261 | 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] | 2262 | 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] | 2263 | This should happen extremely rarely, and in fact I don't have a test case |
| 2264 | that triggers it(!). Instead the code was tested by artificially allocating |
| 2265 | just 1 digit at the start, so that the copying code was exercised for every |
| 2266 | digit beyond the first. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2267 | ***/ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2268 | twodigits c; /* current input character */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2269 | Py_ssize_t size_z; |
Serhiy Storchaka | 29ba688 | 2017-12-03 22:16:21 +0200 | [diff] [blame] | 2270 | Py_ssize_t digits = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2271 | int i; |
| 2272 | int convwidth; |
| 2273 | twodigits convmultmax, convmult; |
| 2274 | digit *pz, *pzstop; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2275 | const char *scan, *lastdigit; |
| 2276 | char prev = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2277 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2278 | static double log_base_BASE[37] = {0.0e0,}; |
| 2279 | static int convwidth_base[37] = {0,}; |
| 2280 | static twodigits convmultmax_base[37] = {0,}; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2281 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2282 | if (log_base_BASE[base] == 0.0) { |
| 2283 | twodigits convmax = base; |
| 2284 | int i = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2285 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2286 | log_base_BASE[base] = (log((double)base) / |
| 2287 | log((double)PyLong_BASE)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2288 | for (;;) { |
| 2289 | twodigits next = convmax * base; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2290 | if (next > PyLong_BASE) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2291 | break; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2292 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2293 | convmax = next; |
| 2294 | ++i; |
| 2295 | } |
| 2296 | convmultmax_base[base] = convmax; |
| 2297 | assert(i > 0); |
| 2298 | convwidth_base[base] = i; |
| 2299 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2300 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2301 | /* Find length of the string of numeric characters. */ |
| 2302 | scan = str; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2303 | lastdigit = str; |
| 2304 | |
| 2305 | while (_PyLong_DigitValue[Py_CHARMASK(*scan)] < base || *scan == '_') { |
| 2306 | if (*scan == '_') { |
| 2307 | if (prev == '_') { |
| 2308 | /* Only one underscore allowed. */ |
| 2309 | str = lastdigit + 1; |
| 2310 | goto onError; |
| 2311 | } |
| 2312 | } |
| 2313 | else { |
| 2314 | ++digits; |
| 2315 | lastdigit = scan; |
| 2316 | } |
| 2317 | prev = *scan; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2318 | ++scan; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2319 | } |
| 2320 | if (prev == '_') { |
| 2321 | /* Trailing underscore not allowed. */ |
| 2322 | /* Set error pointer to first underscore. */ |
| 2323 | str = lastdigit + 1; |
| 2324 | goto onError; |
| 2325 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2326 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2327 | /* Create an int object that can contain the largest possible |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2328 | * integer with this base and length. Note that there's no |
| 2329 | * need to initialize z->ob_digit -- no slot is read up before |
| 2330 | * being stored into. |
| 2331 | */ |
Victor Stinner | dd431b3 | 2017-12-08 00:06:55 +0100 | [diff] [blame] | 2332 | double fsize_z = (double)digits * log_base_BASE[base] + 1.0; |
| 2333 | if (fsize_z > (double)MAX_LONG_DIGITS) { |
Serhiy Storchaka | 29ba688 | 2017-12-03 22:16:21 +0200 | [diff] [blame] | 2334 | /* The same exception as in _PyLong_New(). */ |
| 2335 | PyErr_SetString(PyExc_OverflowError, |
| 2336 | "too many digits in integer"); |
| 2337 | return NULL; |
| 2338 | } |
| 2339 | size_z = (Py_ssize_t)fsize_z; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2340 | /* Uncomment next line to test exceedingly rare copy code */ |
| 2341 | /* size_z = 1; */ |
| 2342 | assert(size_z > 0); |
| 2343 | z = _PyLong_New(size_z); |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2344 | if (z == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2345 | return NULL; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2346 | } |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 2347 | Py_SET_SIZE(z, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2348 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2349 | /* `convwidth` consecutive input digits are treated as a single |
| 2350 | * digit in base `convmultmax`. |
| 2351 | */ |
| 2352 | convwidth = convwidth_base[base]; |
| 2353 | convmultmax = convmultmax_base[base]; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2354 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2355 | /* Work ;-) */ |
| 2356 | while (str < scan) { |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2357 | if (*str == '_') { |
| 2358 | str++; |
| 2359 | continue; |
| 2360 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2361 | /* grab up to convwidth digits from the input string */ |
| 2362 | c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)]; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2363 | for (i = 1; i < convwidth && str != scan; ++str) { |
| 2364 | if (*str == '_') { |
| 2365 | continue; |
| 2366 | } |
| 2367 | i++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2368 | c = (twodigits)(c * base + |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2369 | (int)_PyLong_DigitValue[Py_CHARMASK(*str)]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2370 | assert(c < PyLong_BASE); |
| 2371 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2372 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2373 | convmult = convmultmax; |
| 2374 | /* Calculate the shift only if we couldn't get |
| 2375 | * convwidth digits. |
| 2376 | */ |
| 2377 | if (i != convwidth) { |
| 2378 | convmult = base; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2379 | for ( ; i > 1; --i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2380 | convmult *= base; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2381 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2382 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2383 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2384 | /* Multiply z by convmult, and add c. */ |
| 2385 | pz = z->ob_digit; |
| 2386 | pzstop = pz + Py_SIZE(z); |
| 2387 | for (; pz < pzstop; ++pz) { |
| 2388 | c += (twodigits)*pz * convmult; |
| 2389 | *pz = (digit)(c & PyLong_MASK); |
| 2390 | c >>= PyLong_SHIFT; |
| 2391 | } |
| 2392 | /* carry off the current end? */ |
| 2393 | if (c) { |
| 2394 | assert(c < PyLong_BASE); |
| 2395 | if (Py_SIZE(z) < size_z) { |
| 2396 | *pz = (digit)c; |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 2397 | Py_SET_SIZE(z, Py_SIZE(z) + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2398 | } |
| 2399 | else { |
| 2400 | PyLongObject *tmp; |
| 2401 | /* Extremely rare. Get more space. */ |
| 2402 | assert(Py_SIZE(z) == size_z); |
| 2403 | tmp = _PyLong_New(size_z + 1); |
| 2404 | if (tmp == NULL) { |
| 2405 | Py_DECREF(z); |
| 2406 | return NULL; |
| 2407 | } |
| 2408 | memcpy(tmp->ob_digit, |
| 2409 | z->ob_digit, |
| 2410 | sizeof(digit) * size_z); |
| 2411 | Py_DECREF(z); |
| 2412 | z = tmp; |
| 2413 | z->ob_digit[size_z] = (digit)c; |
| 2414 | ++size_z; |
| 2415 | } |
| 2416 | } |
| 2417 | } |
| 2418 | } |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2419 | if (z == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2420 | return NULL; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2421 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2422 | if (error_if_nonzero) { |
| 2423 | /* reset the base to 0, else the exception message |
| 2424 | doesn't make too much sense */ |
| 2425 | base = 0; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2426 | if (Py_SIZE(z) != 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2427 | goto onError; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2428 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2429 | /* there might still be other problems, therefore base |
| 2430 | remains zero here for the same reason */ |
| 2431 | } |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2432 | if (str == start) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2433 | goto onError; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2434 | } |
| 2435 | if (sign < 0) { |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 2436 | Py_SET_SIZE(z, -(Py_SIZE(z))); |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2437 | } |
Jordon Xu | 2ec7010 | 2019-09-11 00:04:08 +0800 | [diff] [blame] | 2438 | while (*str && Py_ISSPACE(*str)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2439 | str++; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2440 | } |
| 2441 | if (*str != '\0') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2442 | goto onError; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2443 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2444 | long_normalize(z); |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2445 | z = maybe_small_long(z); |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2446 | if (z == NULL) { |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2447 | return NULL; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2448 | } |
| 2449 | if (pend != NULL) { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 2450 | *pend = (char *)str; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2451 | } |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2452 | return (PyObject *) z; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2453 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2454 | onError: |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2455 | if (pend != NULL) { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 2456 | *pend = (char *)str; |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2457 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2458 | Py_XDECREF(z); |
| 2459 | slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200; |
| 2460 | strobj = PyUnicode_FromStringAndSize(orig_str, slen); |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 2461 | if (strobj == 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 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2464 | PyErr_Format(PyExc_ValueError, |
Serhiy Storchaka | 579ddc2 | 2013-08-03 21:14:05 +0300 | [diff] [blame] | 2465 | "invalid literal for int() with base %d: %.200R", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2466 | base, strobj); |
| 2467 | Py_DECREF(strobj); |
| 2468 | return NULL; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2469 | } |
| 2470 | |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2471 | /* Since PyLong_FromString doesn't have a length parameter, |
| 2472 | * check here for possible NULs in the string. |
| 2473 | * |
| 2474 | * Reports an invalid literal as a bytes object. |
| 2475 | */ |
| 2476 | PyObject * |
| 2477 | _PyLong_FromBytes(const char *s, Py_ssize_t len, int base) |
| 2478 | { |
| 2479 | PyObject *result, *strobj; |
| 2480 | char *end = NULL; |
| 2481 | |
Serhiy Storchaka | 20b39b2 | 2014-09-28 11:27:24 +0300 | [diff] [blame] | 2482 | result = PyLong_FromString(s, &end, base); |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2483 | if (end == NULL || (result != NULL && end == s + len)) |
| 2484 | return result; |
| 2485 | Py_XDECREF(result); |
| 2486 | strobj = PyBytes_FromStringAndSize(s, Py_MIN(len, 200)); |
| 2487 | if (strobj != NULL) { |
| 2488 | PyErr_Format(PyExc_ValueError, |
Serhiy Storchaka | 579ddc2 | 2013-08-03 21:14:05 +0300 | [diff] [blame] | 2489 | "invalid literal for int() with base %d: %.200R", |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2490 | base, strobj); |
| 2491 | Py_DECREF(strobj); |
| 2492 | } |
| 2493 | return NULL; |
| 2494 | } |
| 2495 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2496 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2497 | PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2498 | { |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 2499 | PyObject *v, *unicode = PyUnicode_FromWideChar(u, length); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2500 | if (unicode == NULL) |
| 2501 | return NULL; |
| 2502 | v = PyLong_FromUnicodeObject(unicode, base); |
| 2503 | Py_DECREF(unicode); |
| 2504 | return v; |
| 2505 | } |
| 2506 | |
| 2507 | PyObject * |
| 2508 | PyLong_FromUnicodeObject(PyObject *u, int base) |
| 2509 | { |
Serhiy Storchaka | 579ddc2 | 2013-08-03 21:14:05 +0300 | [diff] [blame] | 2510 | PyObject *result, *asciidig; |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 2511 | const char *buffer; |
| 2512 | char *end = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2513 | Py_ssize_t buflen; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2514 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2515 | asciidig = _PyUnicode_TransformDecimalAndSpaceToASCII(u); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 2516 | if (asciidig == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2517 | return NULL; |
Serhiy Storchaka | 9b6c60c | 2017-11-13 21:23:48 +0200 | [diff] [blame] | 2518 | assert(PyUnicode_IS_ASCII(asciidig)); |
| 2519 | /* Simply get a pointer to existing ASCII characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2520 | buffer = PyUnicode_AsUTF8AndSize(asciidig, &buflen); |
Serhiy Storchaka | 9b6c60c | 2017-11-13 21:23:48 +0200 | [diff] [blame] | 2521 | assert(buffer != NULL); |
| 2522 | |
| 2523 | result = PyLong_FromString(buffer, &end, base); |
| 2524 | if (end == NULL || (result != NULL && end == buffer + buflen)) { |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 2525 | Py_DECREF(asciidig); |
Serhiy Storchaka | 9b6c60c | 2017-11-13 21:23:48 +0200 | [diff] [blame] | 2526 | return result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2527 | } |
Serhiy Storchaka | 9b6c60c | 2017-11-13 21:23:48 +0200 | [diff] [blame] | 2528 | Py_DECREF(asciidig); |
| 2529 | Py_XDECREF(result); |
Serhiy Storchaka | 579ddc2 | 2013-08-03 21:14:05 +0300 | [diff] [blame] | 2530 | PyErr_Format(PyExc_ValueError, |
| 2531 | "invalid literal for int() with base %d: %.200R", |
| 2532 | base, u); |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2533 | return NULL; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2534 | } |
| 2535 | |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2536 | /* forward */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2537 | static PyLongObject *x_divrem |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2538 | (PyLongObject *, PyLongObject *, PyLongObject **); |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 2539 | static PyObject *long_long(PyObject *v); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2540 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2541 | /* Int division with remainder, top-level routine */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2542 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2543 | static int |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2544 | long_divrem(PyLongObject *a, PyLongObject *b, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2545 | PyLongObject **pdiv, PyLongObject **prem) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2546 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 2547 | 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] | 2548 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2549 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2550 | if (size_b == 0) { |
| 2551 | PyErr_SetString(PyExc_ZeroDivisionError, |
| 2552 | "integer division or modulo by zero"); |
| 2553 | return -1; |
| 2554 | } |
| 2555 | if (size_a < size_b || |
| 2556 | (size_a == size_b && |
| 2557 | a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) { |
| 2558 | /* |a| < |b|. */ |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 2559 | *prem = (PyLongObject *)long_long((PyObject *)a); |
Mark Dickinson | b820d7f | 2016-08-22 12:24:46 +0100 | [diff] [blame] | 2560 | if (*prem == NULL) { |
Mark Dickinson | b820d7f | 2016-08-22 12:24:46 +0100 | [diff] [blame] | 2561 | return -1; |
| 2562 | } |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 2563 | Py_INCREF(_PyLong_Zero); |
| 2564 | *pdiv = (PyLongObject*)_PyLong_Zero; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2565 | return 0; |
| 2566 | } |
| 2567 | if (size_b == 1) { |
| 2568 | digit rem = 0; |
| 2569 | z = divrem1(a, b->ob_digit[0], &rem); |
| 2570 | if (z == NULL) |
| 2571 | return -1; |
| 2572 | *prem = (PyLongObject *) PyLong_FromLong((long)rem); |
| 2573 | if (*prem == NULL) { |
| 2574 | Py_DECREF(z); |
| 2575 | return -1; |
| 2576 | } |
| 2577 | } |
| 2578 | else { |
| 2579 | z = x_divrem(a, b, prem); |
| 2580 | if (z == NULL) |
| 2581 | return -1; |
| 2582 | } |
| 2583 | /* Set the signs. |
| 2584 | The quotient z has the sign of a*b; |
| 2585 | the remainder r has the sign of a, |
| 2586 | so a = b*z + r. */ |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 2587 | if ((Py_SIZE(a) < 0) != (Py_SIZE(b) < 0)) { |
| 2588 | _PyLong_Negate(&z); |
| 2589 | if (z == NULL) { |
| 2590 | Py_CLEAR(*prem); |
| 2591 | return -1; |
| 2592 | } |
| 2593 | } |
| 2594 | if (Py_SIZE(a) < 0 && Py_SIZE(*prem) != 0) { |
| 2595 | _PyLong_Negate(prem); |
| 2596 | if (*prem == NULL) { |
| 2597 | Py_DECREF(z); |
| 2598 | Py_CLEAR(*prem); |
| 2599 | return -1; |
| 2600 | } |
| 2601 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2602 | *pdiv = maybe_small_long(z); |
| 2603 | return 0; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2604 | } |
| 2605 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2606 | /* Unsigned int division with remainder -- the algorithm. The arguments v1 |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 2607 | 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] | 2608 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2609 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2610 | x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2611 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2612 | PyLongObject *v, *w, *a; |
| 2613 | Py_ssize_t i, k, size_v, size_w; |
| 2614 | int d; |
| 2615 | digit wm1, wm2, carry, q, r, vtop, *v0, *vk, *w0, *ak; |
| 2616 | twodigits vv; |
| 2617 | sdigit zhi; |
| 2618 | stwodigits z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2619 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2620 | /* We follow Knuth [The Art of Computer Programming, Vol. 2 (3rd |
| 2621 | edn.), section 4.3.1, Algorithm D], except that we don't explicitly |
| 2622 | handle the special case when the initial estimate q for a quotient |
| 2623 | digit is >= PyLong_BASE: the max value for q is PyLong_BASE+1, and |
| 2624 | that won't overflow a digit. */ |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 2625 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2626 | /* allocate space; w will also be used to hold the final remainder */ |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 2627 | size_v = Py_ABS(Py_SIZE(v1)); |
| 2628 | size_w = Py_ABS(Py_SIZE(w1)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2629 | assert(size_v >= size_w && size_w >= 2); /* Assert checks by div() */ |
| 2630 | v = _PyLong_New(size_v+1); |
| 2631 | if (v == NULL) { |
| 2632 | *prem = NULL; |
| 2633 | return NULL; |
| 2634 | } |
| 2635 | w = _PyLong_New(size_w); |
| 2636 | if (w == NULL) { |
| 2637 | Py_DECREF(v); |
| 2638 | *prem = NULL; |
| 2639 | return NULL; |
| 2640 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2641 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2642 | /* normalize: shift w1 left so that its top digit is >= PyLong_BASE/2. |
| 2643 | 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] | 2644 | d = PyLong_SHIFT - _Py_bit_length(w1->ob_digit[size_w-1]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2645 | carry = v_lshift(w->ob_digit, w1->ob_digit, size_w, d); |
| 2646 | assert(carry == 0); |
| 2647 | carry = v_lshift(v->ob_digit, v1->ob_digit, size_v, d); |
| 2648 | if (carry != 0 || v->ob_digit[size_v-1] >= w->ob_digit[size_w-1]) { |
| 2649 | v->ob_digit[size_v] = carry; |
| 2650 | size_v++; |
| 2651 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2652 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2653 | /* Now v->ob_digit[size_v-1] < w->ob_digit[size_w-1], so quotient has |
| 2654 | at most (and usually exactly) k = size_v - size_w digits. */ |
| 2655 | k = size_v - size_w; |
| 2656 | assert(k >= 0); |
| 2657 | a = _PyLong_New(k); |
| 2658 | if (a == NULL) { |
| 2659 | Py_DECREF(w); |
| 2660 | Py_DECREF(v); |
| 2661 | *prem = NULL; |
| 2662 | return NULL; |
| 2663 | } |
| 2664 | v0 = v->ob_digit; |
| 2665 | w0 = w->ob_digit; |
| 2666 | wm1 = w0[size_w-1]; |
| 2667 | wm2 = w0[size_w-2]; |
| 2668 | for (vk = v0+k, ak = a->ob_digit + k; vk-- > v0;) { |
| 2669 | /* inner loop: divide vk[0:size_w+1] by w0[0:size_w], giving |
| 2670 | single-digit quotient q, remainder in vk[0:size_w]. */ |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2671 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2672 | SIGCHECK({ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2673 | Py_DECREF(a); |
| 2674 | Py_DECREF(w); |
| 2675 | Py_DECREF(v); |
| 2676 | *prem = NULL; |
| 2677 | return NULL; |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 2678 | }); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2679 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2680 | /* estimate quotient digit q; may overestimate by 1 (rare) */ |
| 2681 | vtop = vk[size_w]; |
| 2682 | assert(vtop <= wm1); |
| 2683 | vv = ((twodigits)vtop << PyLong_SHIFT) | vk[size_w-1]; |
| 2684 | q = (digit)(vv / wm1); |
| 2685 | r = (digit)(vv - (twodigits)wm1 * q); /* r = vv % wm1 */ |
| 2686 | while ((twodigits)wm2 * q > (((twodigits)r << PyLong_SHIFT) |
| 2687 | | vk[size_w-2])) { |
| 2688 | --q; |
| 2689 | r += wm1; |
| 2690 | if (r >= PyLong_BASE) |
| 2691 | break; |
| 2692 | } |
| 2693 | assert(q <= PyLong_BASE); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2694 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2695 | /* subtract q*w0[0:size_w] from vk[0:size_w+1] */ |
| 2696 | zhi = 0; |
| 2697 | for (i = 0; i < size_w; ++i) { |
| 2698 | /* invariants: -PyLong_BASE <= -q <= zhi <= 0; |
| 2699 | -PyLong_BASE * q <= z < PyLong_BASE */ |
| 2700 | z = (sdigit)vk[i] + zhi - |
| 2701 | (stwodigits)q * (stwodigits)w0[i]; |
| 2702 | vk[i] = (digit)z & PyLong_MASK; |
| 2703 | zhi = (sdigit)Py_ARITHMETIC_RIGHT_SHIFT(stwodigits, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2704 | z, PyLong_SHIFT); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2705 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2706 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2707 | /* add w back if q was too large (this branch taken rarely) */ |
| 2708 | assert((sdigit)vtop + zhi == -1 || (sdigit)vtop + zhi == 0); |
| 2709 | if ((sdigit)vtop + zhi < 0) { |
| 2710 | carry = 0; |
| 2711 | for (i = 0; i < size_w; ++i) { |
| 2712 | carry += vk[i] + w0[i]; |
| 2713 | vk[i] = carry & PyLong_MASK; |
| 2714 | carry >>= PyLong_SHIFT; |
| 2715 | } |
| 2716 | --q; |
| 2717 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2718 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2719 | /* store quotient digit */ |
| 2720 | assert(q < PyLong_BASE); |
| 2721 | *--ak = q; |
| 2722 | } |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 2723 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2724 | /* unshift remainder; we reuse w to store the result */ |
| 2725 | carry = v_rshift(w0, v0, size_w, d); |
| 2726 | assert(carry==0); |
| 2727 | Py_DECREF(v); |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 2728 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2729 | *prem = long_normalize(w); |
| 2730 | return long_normalize(a); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2731 | } |
| 2732 | |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2733 | /* For a nonzero PyLong a, express a in the form x * 2**e, with 0.5 <= |
| 2734 | abs(x) < 1.0 and e >= 0; return x and put e in *e. Here x is |
| 2735 | rounded to DBL_MANT_DIG significant bits using round-half-to-even. |
| 2736 | If a == 0, return 0.0 and set *e = 0. If the resulting exponent |
| 2737 | e is larger than PY_SSIZE_T_MAX, raise OverflowError and return |
| 2738 | -1.0. */ |
| 2739 | |
| 2740 | /* attempt to define 2.0**DBL_MANT_DIG as a compile-time constant */ |
| 2741 | #if DBL_MANT_DIG == 53 |
| 2742 | #define EXP2_DBL_MANT_DIG 9007199254740992.0 |
| 2743 | #else |
| 2744 | #define EXP2_DBL_MANT_DIG (ldexp(1.0, DBL_MANT_DIG)) |
| 2745 | #endif |
| 2746 | |
| 2747 | double |
| 2748 | _PyLong_Frexp(PyLongObject *a, Py_ssize_t *e) |
| 2749 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2750 | Py_ssize_t a_size, a_bits, shift_digits, shift_bits, x_size; |
| 2751 | /* See below for why x_digits is always large enough. */ |
Dong-hee Na | b88cd58 | 2020-05-04 22:32:42 +0900 | [diff] [blame] | 2752 | digit rem; |
| 2753 | digit x_digits[2 + (DBL_MANT_DIG + 1) / PyLong_SHIFT] = {0,}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2754 | double dx; |
| 2755 | /* Correction term for round-half-to-even rounding. For a digit x, |
| 2756 | "x + half_even_correction[x & 7]" gives x rounded to the nearest |
| 2757 | multiple of 4, rounding ties to a multiple of 8. */ |
| 2758 | 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] | 2759 | |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 2760 | a_size = Py_ABS(Py_SIZE(a)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2761 | if (a_size == 0) { |
| 2762 | /* Special case for 0: significand 0.0, exponent 0. */ |
| 2763 | *e = 0; |
| 2764 | return 0.0; |
| 2765 | } |
Niklas Fiekas | c5b7900 | 2020-01-16 15:09:19 +0100 | [diff] [blame] | 2766 | a_bits = _Py_bit_length(a->ob_digit[a_size-1]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2767 | /* The following is an overflow-free version of the check |
| 2768 | "if ((a_size - 1) * PyLong_SHIFT + a_bits > PY_SSIZE_T_MAX) ..." */ |
| 2769 | if (a_size >= (PY_SSIZE_T_MAX - 1) / PyLong_SHIFT + 1 && |
| 2770 | (a_size > (PY_SSIZE_T_MAX - 1) / PyLong_SHIFT + 1 || |
| 2771 | a_bits > (PY_SSIZE_T_MAX - 1) % PyLong_SHIFT + 1)) |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2772 | goto overflow; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2773 | a_bits = (a_size - 1) * PyLong_SHIFT + a_bits; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2774 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2775 | /* Shift the first DBL_MANT_DIG + 2 bits of a into x_digits[0:x_size] |
| 2776 | (shifting left if a_bits <= DBL_MANT_DIG + 2). |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2777 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2778 | Number of digits needed for result: write // for floor division. |
| 2779 | Then if shifting left, we end up using |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2780 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2781 | 1 + a_size + (DBL_MANT_DIG + 2 - a_bits) // PyLong_SHIFT |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2782 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2783 | digits. If shifting right, we use |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2784 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2785 | a_size - (a_bits - DBL_MANT_DIG - 2) // PyLong_SHIFT |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2786 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2787 | digits. Using a_size = 1 + (a_bits - 1) // PyLong_SHIFT along with |
| 2788 | the inequalities |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2789 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2790 | m // PyLong_SHIFT + n // PyLong_SHIFT <= (m + n) // PyLong_SHIFT |
| 2791 | m // PyLong_SHIFT - n // PyLong_SHIFT <= |
| 2792 | 1 + (m - n - 1) // PyLong_SHIFT, |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2793 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2794 | 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] | 2795 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2796 | x_size <= 2 + (DBL_MANT_DIG + 1) // PyLong_SHIFT |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2797 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2798 | in both cases. |
| 2799 | */ |
| 2800 | if (a_bits <= DBL_MANT_DIG + 2) { |
| 2801 | shift_digits = (DBL_MANT_DIG + 2 - a_bits) / PyLong_SHIFT; |
| 2802 | shift_bits = (DBL_MANT_DIG + 2 - a_bits) % PyLong_SHIFT; |
Dong-hee Na | b88cd58 | 2020-05-04 22:32:42 +0900 | [diff] [blame] | 2803 | x_size = shift_digits; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2804 | rem = v_lshift(x_digits + x_size, a->ob_digit, a_size, |
| 2805 | (int)shift_bits); |
| 2806 | x_size += a_size; |
| 2807 | x_digits[x_size++] = rem; |
| 2808 | } |
| 2809 | else { |
| 2810 | shift_digits = (a_bits - DBL_MANT_DIG - 2) / PyLong_SHIFT; |
| 2811 | shift_bits = (a_bits - DBL_MANT_DIG - 2) % PyLong_SHIFT; |
| 2812 | rem = v_rshift(x_digits, a->ob_digit + shift_digits, |
| 2813 | a_size - shift_digits, (int)shift_bits); |
| 2814 | x_size = a_size - shift_digits; |
| 2815 | /* For correct rounding below, we need the least significant |
| 2816 | bit of x to be 'sticky' for this shift: if any of the bits |
| 2817 | shifted out was nonzero, we set the least significant bit |
| 2818 | of x. */ |
| 2819 | if (rem) |
| 2820 | x_digits[0] |= 1; |
| 2821 | else |
| 2822 | while (shift_digits > 0) |
| 2823 | if (a->ob_digit[--shift_digits]) { |
| 2824 | x_digits[0] |= 1; |
| 2825 | break; |
| 2826 | } |
| 2827 | } |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2828 | 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] | 2829 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2830 | /* Round, and convert to double. */ |
| 2831 | x_digits[0] += half_even_correction[x_digits[0] & 7]; |
| 2832 | dx = x_digits[--x_size]; |
| 2833 | while (x_size > 0) |
| 2834 | dx = dx * PyLong_BASE + x_digits[--x_size]; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2835 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2836 | /* Rescale; make correction if result is 1.0. */ |
| 2837 | dx /= 4.0 * EXP2_DBL_MANT_DIG; |
| 2838 | if (dx == 1.0) { |
| 2839 | if (a_bits == PY_SSIZE_T_MAX) |
| 2840 | goto overflow; |
| 2841 | dx = 0.5; |
| 2842 | a_bits += 1; |
| 2843 | } |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2844 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2845 | *e = a_bits; |
| 2846 | return Py_SIZE(a) < 0 ? -dx : dx; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2847 | |
| 2848 | overflow: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2849 | /* exponent > PY_SSIZE_T_MAX */ |
| 2850 | PyErr_SetString(PyExc_OverflowError, |
| 2851 | "huge integer: number of bits overflows a Py_ssize_t"); |
| 2852 | *e = 0; |
| 2853 | return -1.0; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2854 | } |
| 2855 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2856 | /* 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] | 2857 | using the round-half-to-even rule in the case of a tie. */ |
| 2858 | |
| 2859 | double |
| 2860 | PyLong_AsDouble(PyObject *v) |
| 2861 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2862 | Py_ssize_t exponent; |
| 2863 | double x; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2864 | |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 2865 | if (v == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2866 | PyErr_BadInternalCall(); |
| 2867 | return -1.0; |
| 2868 | } |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 2869 | if (!PyLong_Check(v)) { |
| 2870 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 2871 | return -1.0; |
| 2872 | } |
Yury Selivanov | 186c30b | 2016-02-05 19:40:01 -0500 | [diff] [blame] | 2873 | if (Py_ABS(Py_SIZE(v)) <= 1) { |
Yury Selivanov | a0fcaca | 2016-02-06 12:21:33 -0500 | [diff] [blame] | 2874 | /* Fast path; single digit long (31 bits) will cast safely |
Mark Dickinson | 1dc3c89 | 2016-08-21 10:33:36 +0100 | [diff] [blame] | 2875 | to double. This improves performance of FP/long operations |
| 2876 | by 20%. |
Yury Selivanov | 186c30b | 2016-02-05 19:40:01 -0500 | [diff] [blame] | 2877 | */ |
| 2878 | return (double)MEDIUM_VALUE((PyLongObject *)v); |
| 2879 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2880 | x = _PyLong_Frexp((PyLongObject *)v, &exponent); |
| 2881 | if ((x == -1.0 && PyErr_Occurred()) || exponent > DBL_MAX_EXP) { |
| 2882 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 02515f7 | 2013-08-03 12:08:22 +0100 | [diff] [blame] | 2883 | "int too large to convert to float"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2884 | return -1.0; |
| 2885 | } |
| 2886 | return ldexp(x, (int)exponent); |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2887 | } |
| 2888 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2889 | /* Methods */ |
| 2890 | |
HongWeipeng | 42acb7b | 2019-09-18 23:10:15 +0800 | [diff] [blame] | 2891 | /* if a < b, return a negative number |
| 2892 | if a == b, return 0 |
| 2893 | if a > b, return a positive number */ |
| 2894 | |
| 2895 | static Py_ssize_t |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2896 | long_compare(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2897 | { |
HongWeipeng | 42acb7b | 2019-09-18 23:10:15 +0800 | [diff] [blame] | 2898 | Py_ssize_t sign = Py_SIZE(a) - Py_SIZE(b); |
| 2899 | if (sign == 0) { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 2900 | Py_ssize_t i = Py_ABS(Py_SIZE(a)); |
HongWeipeng | 42acb7b | 2019-09-18 23:10:15 +0800 | [diff] [blame] | 2901 | sdigit diff = 0; |
| 2902 | while (--i >= 0) { |
| 2903 | diff = (sdigit) a->ob_digit[i] - (sdigit) b->ob_digit[i]; |
| 2904 | if (diff) { |
| 2905 | break; |
| 2906 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2907 | } |
HongWeipeng | 42acb7b | 2019-09-18 23:10:15 +0800 | [diff] [blame] | 2908 | sign = Py_SIZE(a) < 0 ? -diff : diff; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2909 | } |
HongWeipeng | 42acb7b | 2019-09-18 23:10:15 +0800 | [diff] [blame] | 2910 | return sign; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2911 | } |
| 2912 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2913 | static PyObject * |
| 2914 | long_richcompare(PyObject *self, PyObject *other, int op) |
| 2915 | { |
HongWeipeng | 42acb7b | 2019-09-18 23:10:15 +0800 | [diff] [blame] | 2916 | Py_ssize_t result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2917 | CHECK_BINOP(self, other); |
| 2918 | if (self == other) |
| 2919 | result = 0; |
| 2920 | else |
| 2921 | result = long_compare((PyLongObject*)self, (PyLongObject*)other); |
stratakis | e8b1965 | 2017-11-02 11:32:54 +0100 | [diff] [blame] | 2922 | Py_RETURN_RICHCOMPARE(result, 0, op); |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2923 | } |
| 2924 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2925 | static Py_hash_t |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2926 | long_hash(PyLongObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2927 | { |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 2928 | Py_uhash_t x; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2929 | Py_ssize_t i; |
| 2930 | int sign; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2931 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2932 | i = Py_SIZE(v); |
| 2933 | switch(i) { |
| 2934 | case -1: return v->ob_digit[0]==1 ? -2 : -(sdigit)v->ob_digit[0]; |
| 2935 | case 0: return 0; |
| 2936 | case 1: return v->ob_digit[0]; |
| 2937 | } |
| 2938 | sign = 1; |
| 2939 | x = 0; |
| 2940 | if (i < 0) { |
| 2941 | sign = -1; |
| 2942 | i = -(i); |
| 2943 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2944 | while (--i >= 0) { |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 2945 | /* Here x is a quantity in the range [0, _PyHASH_MODULUS); we |
| 2946 | want to compute x * 2**PyLong_SHIFT + v->ob_digit[i] modulo |
| 2947 | _PyHASH_MODULUS. |
| 2948 | |
| 2949 | The computation of x * 2**PyLong_SHIFT % _PyHASH_MODULUS |
| 2950 | amounts to a rotation of the bits of x. To see this, write |
| 2951 | |
| 2952 | x * 2**PyLong_SHIFT = y * 2**_PyHASH_BITS + z |
| 2953 | |
| 2954 | where y = x >> (_PyHASH_BITS - PyLong_SHIFT) gives the top |
| 2955 | PyLong_SHIFT bits of x (those that are shifted out of the |
| 2956 | original _PyHASH_BITS bits, and z = (x << PyLong_SHIFT) & |
| 2957 | _PyHASH_MODULUS gives the bottom _PyHASH_BITS - PyLong_SHIFT |
| 2958 | bits of x, shifted up. Then since 2**_PyHASH_BITS is |
| 2959 | congruent to 1 modulo _PyHASH_MODULUS, y*2**_PyHASH_BITS is |
| 2960 | congruent to y modulo _PyHASH_MODULUS. So |
| 2961 | |
| 2962 | x * 2**PyLong_SHIFT = y + z (mod _PyHASH_MODULUS). |
| 2963 | |
| 2964 | The right-hand side is just the result of rotating the |
| 2965 | _PyHASH_BITS bits of x left by PyLong_SHIFT places; since |
| 2966 | not all _PyHASH_BITS bits of x are 1s, the same is true |
| 2967 | after rotation, so 0 <= y+z < _PyHASH_MODULUS and y + z is |
| 2968 | the reduction of x*2**PyLong_SHIFT modulo |
| 2969 | _PyHASH_MODULUS. */ |
| 2970 | x = ((x << PyLong_SHIFT) & _PyHASH_MODULUS) | |
| 2971 | (x >> (_PyHASH_BITS - PyLong_SHIFT)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2972 | x += v->ob_digit[i]; |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 2973 | if (x >= _PyHASH_MODULUS) |
| 2974 | x -= _PyHASH_MODULUS; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2975 | } |
| 2976 | x = x * sign; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 2977 | if (x == (Py_uhash_t)-1) |
| 2978 | x = (Py_uhash_t)-2; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2979 | return (Py_hash_t)x; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2980 | } |
| 2981 | |
| 2982 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2983 | /* Add the absolute values of two integers. */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2984 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2985 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2986 | x_add(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2987 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 2988 | 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] | 2989 | PyLongObject *z; |
| 2990 | Py_ssize_t i; |
| 2991 | digit carry = 0; |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 2992 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2993 | /* Ensure a is the larger of the two: */ |
| 2994 | if (size_a < size_b) { |
| 2995 | { PyLongObject *temp = a; a = b; b = temp; } |
| 2996 | { Py_ssize_t size_temp = size_a; |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2997 | size_a = size_b; |
| 2998 | size_b = size_temp; } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2999 | } |
| 3000 | z = _PyLong_New(size_a+1); |
| 3001 | if (z == NULL) |
| 3002 | return NULL; |
| 3003 | for (i = 0; i < size_b; ++i) { |
| 3004 | carry += a->ob_digit[i] + b->ob_digit[i]; |
| 3005 | z->ob_digit[i] = carry & PyLong_MASK; |
| 3006 | carry >>= PyLong_SHIFT; |
| 3007 | } |
| 3008 | for (; i < size_a; ++i) { |
| 3009 | carry += a->ob_digit[i]; |
| 3010 | z->ob_digit[i] = carry & PyLong_MASK; |
| 3011 | carry >>= PyLong_SHIFT; |
| 3012 | } |
| 3013 | z->ob_digit[i] = carry; |
| 3014 | return long_normalize(z); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3015 | } |
| 3016 | |
| 3017 | /* Subtract the absolute values of two integers. */ |
| 3018 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3019 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3020 | x_sub(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3021 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3022 | 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] | 3023 | PyLongObject *z; |
| 3024 | Py_ssize_t i; |
| 3025 | int sign = 1; |
| 3026 | digit borrow = 0; |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 3027 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3028 | /* Ensure a is the larger of the two: */ |
| 3029 | if (size_a < size_b) { |
| 3030 | sign = -1; |
| 3031 | { PyLongObject *temp = a; a = b; b = temp; } |
| 3032 | { Py_ssize_t size_temp = size_a; |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3033 | size_a = size_b; |
| 3034 | size_b = size_temp; } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3035 | } |
| 3036 | else if (size_a == size_b) { |
| 3037 | /* Find highest digit where a and b differ: */ |
| 3038 | i = size_a; |
| 3039 | while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) |
| 3040 | ; |
| 3041 | if (i < 0) |
| 3042 | return (PyLongObject *)PyLong_FromLong(0); |
| 3043 | if (a->ob_digit[i] < b->ob_digit[i]) { |
| 3044 | sign = -1; |
| 3045 | { PyLongObject *temp = a; a = b; b = temp; } |
| 3046 | } |
| 3047 | size_a = size_b = i+1; |
| 3048 | } |
| 3049 | z = _PyLong_New(size_a); |
| 3050 | if (z == NULL) |
| 3051 | return NULL; |
| 3052 | for (i = 0; i < size_b; ++i) { |
| 3053 | /* The following assumes unsigned arithmetic |
| 3054 | works module 2**N for some N>PyLong_SHIFT. */ |
| 3055 | borrow = a->ob_digit[i] - b->ob_digit[i] - borrow; |
| 3056 | z->ob_digit[i] = borrow & PyLong_MASK; |
| 3057 | borrow >>= PyLong_SHIFT; |
| 3058 | borrow &= 1; /* Keep only one sign bit */ |
| 3059 | } |
| 3060 | for (; i < size_a; ++i) { |
| 3061 | borrow = a->ob_digit[i] - borrow; |
| 3062 | z->ob_digit[i] = borrow & PyLong_MASK; |
| 3063 | borrow >>= PyLong_SHIFT; |
| 3064 | borrow &= 1; /* Keep only one sign bit */ |
| 3065 | } |
| 3066 | assert(borrow == 0); |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 3067 | if (sign < 0) { |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 3068 | Py_SET_SIZE(z, -Py_SIZE(z)); |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 3069 | } |
HongWeipeng | 036fe85 | 2019-11-26 15:54:49 +0800 | [diff] [blame] | 3070 | return maybe_small_long(long_normalize(z)); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3071 | } |
| 3072 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3073 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3074 | long_add(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3075 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3076 | PyLongObject *z; |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 3077 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3078 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3079 | |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3080 | 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] | 3081 | return PyLong_FromLong(MEDIUM_VALUE(a) + MEDIUM_VALUE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3082 | } |
| 3083 | if (Py_SIZE(a) < 0) { |
| 3084 | if (Py_SIZE(b) < 0) { |
| 3085 | z = x_add(a, b); |
Serhiy Storchaka | e63e5d6 | 2016-06-04 00:06:45 +0300 | [diff] [blame] | 3086 | if (z != NULL) { |
| 3087 | /* x_add received at least one multiple-digit int, |
| 3088 | and thus z must be a multiple-digit int. |
| 3089 | That also means z is not an element of |
| 3090 | small_ints, so negating it in-place is safe. */ |
| 3091 | assert(Py_REFCNT(z) == 1); |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 3092 | Py_SET_SIZE(z, -(Py_SIZE(z))); |
Serhiy Storchaka | e63e5d6 | 2016-06-04 00:06:45 +0300 | [diff] [blame] | 3093 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3094 | } |
| 3095 | else |
| 3096 | z = x_sub(b, a); |
| 3097 | } |
| 3098 | else { |
| 3099 | if (Py_SIZE(b) < 0) |
| 3100 | z = x_sub(a, b); |
| 3101 | else |
| 3102 | z = x_add(a, b); |
| 3103 | } |
| 3104 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3105 | } |
| 3106 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3107 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3108 | long_sub(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3109 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3110 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3111 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3112 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3113 | |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3114 | 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] | 3115 | return PyLong_FromLong(MEDIUM_VALUE(a) - MEDIUM_VALUE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3116 | } |
| 3117 | if (Py_SIZE(a) < 0) { |
HongWeipeng | 036fe85 | 2019-11-26 15:54:49 +0800 | [diff] [blame] | 3118 | if (Py_SIZE(b) < 0) { |
| 3119 | z = x_sub(b, a); |
| 3120 | } |
| 3121 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3122 | z = x_add(a, b); |
HongWeipeng | 036fe85 | 2019-11-26 15:54:49 +0800 | [diff] [blame] | 3123 | if (z != NULL) { |
| 3124 | assert(Py_SIZE(z) == 0 || Py_REFCNT(z) == 1); |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 3125 | Py_SET_SIZE(z, -(Py_SIZE(z))); |
HongWeipeng | 036fe85 | 2019-11-26 15:54:49 +0800 | [diff] [blame] | 3126 | } |
Serhiy Storchaka | e63e5d6 | 2016-06-04 00:06:45 +0300 | [diff] [blame] | 3127 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3128 | } |
| 3129 | else { |
| 3130 | if (Py_SIZE(b) < 0) |
| 3131 | z = x_add(a, b); |
| 3132 | else |
| 3133 | z = x_sub(a, b); |
| 3134 | } |
| 3135 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3136 | } |
| 3137 | |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3138 | /* Grade school multiplication, ignoring the signs. |
| 3139 | * Returns the absolute value of the product, or NULL if error. |
| 3140 | */ |
| 3141 | static PyLongObject * |
| 3142 | x_mul(PyLongObject *a, PyLongObject *b) |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3143 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3144 | PyLongObject *z; |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3145 | Py_ssize_t size_a = Py_ABS(Py_SIZE(a)); |
| 3146 | Py_ssize_t size_b = Py_ABS(Py_SIZE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3147 | Py_ssize_t i; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3148 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3149 | z = _PyLong_New(size_a + size_b); |
| 3150 | if (z == NULL) |
| 3151 | return NULL; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3152 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3153 | memset(z->ob_digit, 0, Py_SIZE(z) * sizeof(digit)); |
| 3154 | if (a == b) { |
| 3155 | /* Efficient squaring per HAC, Algorithm 14.16: |
| 3156 | * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf |
| 3157 | * Gives slightly less than a 2x speedup when a == b, |
| 3158 | * via exploiting that each entry in the multiplication |
| 3159 | * pyramid appears twice (except for the size_a squares). |
| 3160 | */ |
| 3161 | for (i = 0; i < size_a; ++i) { |
| 3162 | twodigits carry; |
| 3163 | twodigits f = a->ob_digit[i]; |
| 3164 | digit *pz = z->ob_digit + (i << 1); |
| 3165 | digit *pa = a->ob_digit + i + 1; |
| 3166 | digit *paend = a->ob_digit + size_a; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3167 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3168 | SIGCHECK({ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3169 | Py_DECREF(z); |
| 3170 | return NULL; |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 3171 | }); |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 3172 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3173 | carry = *pz + f * f; |
| 3174 | *pz++ = (digit)(carry & PyLong_MASK); |
| 3175 | carry >>= PyLong_SHIFT; |
| 3176 | assert(carry <= PyLong_MASK); |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 3177 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3178 | /* Now f is added in twice in each column of the |
| 3179 | * pyramid it appears. Same as adding f<<1 once. |
| 3180 | */ |
| 3181 | f <<= 1; |
| 3182 | while (pa < paend) { |
| 3183 | carry += *pz + *pa++ * f; |
| 3184 | *pz++ = (digit)(carry & PyLong_MASK); |
| 3185 | carry >>= PyLong_SHIFT; |
| 3186 | assert(carry <= (PyLong_MASK << 1)); |
| 3187 | } |
| 3188 | if (carry) { |
| 3189 | carry += *pz; |
| 3190 | *pz++ = (digit)(carry & PyLong_MASK); |
| 3191 | carry >>= PyLong_SHIFT; |
| 3192 | } |
| 3193 | if (carry) |
| 3194 | *pz += (digit)(carry & PyLong_MASK); |
| 3195 | assert((carry >> PyLong_SHIFT) == 0); |
| 3196 | } |
| 3197 | } |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 3198 | else { /* a is not the same as b -- gradeschool int mult */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3199 | for (i = 0; i < size_a; ++i) { |
| 3200 | twodigits carry = 0; |
| 3201 | twodigits f = a->ob_digit[i]; |
| 3202 | digit *pz = z->ob_digit + i; |
| 3203 | digit *pb = b->ob_digit; |
| 3204 | digit *pbend = b->ob_digit + size_b; |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 3205 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3206 | SIGCHECK({ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3207 | Py_DECREF(z); |
| 3208 | return NULL; |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 3209 | }); |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 3210 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3211 | while (pb < pbend) { |
| 3212 | carry += *pz + *pb++ * f; |
| 3213 | *pz++ = (digit)(carry & PyLong_MASK); |
| 3214 | carry >>= PyLong_SHIFT; |
| 3215 | assert(carry <= PyLong_MASK); |
| 3216 | } |
| 3217 | if (carry) |
| 3218 | *pz += (digit)(carry & PyLong_MASK); |
| 3219 | assert((carry >> PyLong_SHIFT) == 0); |
| 3220 | } |
| 3221 | } |
| 3222 | return long_normalize(z); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3223 | } |
| 3224 | |
| 3225 | /* A helper for Karatsuba multiplication (k_mul). |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 3226 | Takes an int "n" and an integer "size" representing the place to |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3227 | split, and sets low and high such that abs(n) == (high << size) + low, |
| 3228 | viewing the shift as being by digits. The sign bit is ignored, and |
| 3229 | the return values are >= 0. |
| 3230 | Returns 0 on success, -1 on failure. |
| 3231 | */ |
| 3232 | static int |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3233 | kmul_split(PyLongObject *n, |
| 3234 | Py_ssize_t size, |
| 3235 | PyLongObject **high, |
| 3236 | PyLongObject **low) |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3237 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3238 | PyLongObject *hi, *lo; |
| 3239 | Py_ssize_t size_lo, size_hi; |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3240 | const Py_ssize_t size_n = Py_ABS(Py_SIZE(n)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3241 | |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 3242 | size_lo = Py_MIN(size_n, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3243 | size_hi = size_n - size_lo; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3244 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3245 | if ((hi = _PyLong_New(size_hi)) == NULL) |
| 3246 | return -1; |
| 3247 | if ((lo = _PyLong_New(size_lo)) == NULL) { |
| 3248 | Py_DECREF(hi); |
| 3249 | return -1; |
| 3250 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3251 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3252 | memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit)); |
| 3253 | 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] | 3254 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3255 | *high = long_normalize(hi); |
| 3256 | *low = long_normalize(lo); |
| 3257 | return 0; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3258 | } |
| 3259 | |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3260 | static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b); |
| 3261 | |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3262 | /* Karatsuba multiplication. Ignores the input signs, and returns the |
| 3263 | * absolute value of the product (or NULL if error). |
| 3264 | * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295). |
| 3265 | */ |
| 3266 | static PyLongObject * |
| 3267 | k_mul(PyLongObject *a, PyLongObject *b) |
| 3268 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3269 | Py_ssize_t asize = Py_ABS(Py_SIZE(a)); |
| 3270 | Py_ssize_t bsize = Py_ABS(Py_SIZE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3271 | PyLongObject *ah = NULL; |
| 3272 | PyLongObject *al = NULL; |
| 3273 | PyLongObject *bh = NULL; |
| 3274 | PyLongObject *bl = NULL; |
| 3275 | PyLongObject *ret = NULL; |
| 3276 | PyLongObject *t1, *t2, *t3; |
| 3277 | Py_ssize_t shift; /* the number of digits we split off */ |
| 3278 | Py_ssize_t i; |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 3279 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3280 | /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl |
| 3281 | * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl |
| 3282 | * Then the original product is |
| 3283 | * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl |
| 3284 | * By picking X to be a power of 2, "*X" is just shifting, and it's |
| 3285 | * been reduced to 3 multiplies on numbers half the size. |
| 3286 | */ |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3287 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3288 | /* We want to split based on the larger number; fiddle so that b |
| 3289 | * is largest. |
| 3290 | */ |
| 3291 | if (asize > bsize) { |
| 3292 | t1 = a; |
| 3293 | a = b; |
| 3294 | b = t1; |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 3295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3296 | i = asize; |
| 3297 | asize = bsize; |
| 3298 | bsize = i; |
| 3299 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3300 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3301 | /* Use gradeschool math when either number is too small. */ |
| 3302 | i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF; |
| 3303 | if (asize <= i) { |
| 3304 | if (asize == 0) |
| 3305 | return (PyLongObject *)PyLong_FromLong(0); |
| 3306 | else |
| 3307 | return x_mul(a, b); |
| 3308 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3309 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3310 | /* If a is small compared to b, splitting on b gives a degenerate |
| 3311 | * case with ah==0, and Karatsuba may be (even much) less efficient |
| 3312 | * than "grade school" then. However, we can still win, by viewing |
| 3313 | * b as a string of "big digits", each of width a->ob_size. That |
| 3314 | * leads to a sequence of balanced calls to k_mul. |
| 3315 | */ |
| 3316 | if (2 * asize <= bsize) |
| 3317 | return k_lopsided_mul(a, b); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3318 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3319 | /* Split a & b into hi & lo pieces. */ |
| 3320 | shift = bsize >> 1; |
| 3321 | if (kmul_split(a, shift, &ah, &al) < 0) goto fail; |
| 3322 | assert(Py_SIZE(ah) > 0); /* the split isn't degenerate */ |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3323 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3324 | if (a == b) { |
| 3325 | bh = ah; |
| 3326 | bl = al; |
| 3327 | Py_INCREF(bh); |
| 3328 | Py_INCREF(bl); |
| 3329 | } |
| 3330 | else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3331 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3332 | /* The plan: |
| 3333 | * 1. Allocate result space (asize + bsize digits: that's always |
| 3334 | * enough). |
| 3335 | * 2. Compute ah*bh, and copy into result at 2*shift. |
| 3336 | * 3. Compute al*bl, and copy into result at 0. Note that this |
| 3337 | * can't overlap with #2. |
| 3338 | * 4. Subtract al*bl from the result, starting at shift. This may |
| 3339 | * underflow (borrow out of the high digit), but we don't care: |
| 3340 | * we're effectively doing unsigned arithmetic mod |
| 3341 | * BASE**(sizea + sizeb), and so long as the *final* result fits, |
| 3342 | * borrows and carries out of the high digit can be ignored. |
| 3343 | * 5. Subtract ah*bh from the result, starting at shift. |
| 3344 | * 6. Compute (ah+al)*(bh+bl), and add it into the result starting |
| 3345 | * at shift. |
| 3346 | */ |
Tim Peters | d64c1de | 2002-08-12 17:36:03 +0000 | [diff] [blame] | 3347 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3348 | /* 1. Allocate result space. */ |
| 3349 | ret = _PyLong_New(asize + bsize); |
| 3350 | if (ret == NULL) goto fail; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3351 | #ifdef Py_DEBUG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3352 | /* Fill with trash, to catch reference to uninitialized digits. */ |
| 3353 | memset(ret->ob_digit, 0xDF, Py_SIZE(ret) * sizeof(digit)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3354 | #endif |
Tim Peters | 44121a6 | 2002-08-12 06:17:58 +0000 | [diff] [blame] | 3355 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3356 | /* 2. t1 <- ah*bh, and copy into high digits of result. */ |
| 3357 | if ((t1 = k_mul(ah, bh)) == NULL) goto fail; |
| 3358 | assert(Py_SIZE(t1) >= 0); |
| 3359 | assert(2*shift + Py_SIZE(t1) <= Py_SIZE(ret)); |
| 3360 | memcpy(ret->ob_digit + 2*shift, t1->ob_digit, |
| 3361 | Py_SIZE(t1) * sizeof(digit)); |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 3362 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3363 | /* Zero-out the digits higher than the ah*bh copy. */ |
| 3364 | i = Py_SIZE(ret) - 2*shift - Py_SIZE(t1); |
| 3365 | if (i) |
| 3366 | memset(ret->ob_digit + 2*shift + Py_SIZE(t1), 0, |
| 3367 | i * sizeof(digit)); |
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 | /* 3. t2 <- al*bl, and copy into the low digits. */ |
| 3370 | if ((t2 = k_mul(al, bl)) == NULL) { |
| 3371 | Py_DECREF(t1); |
| 3372 | goto fail; |
| 3373 | } |
| 3374 | assert(Py_SIZE(t2) >= 0); |
| 3375 | assert(Py_SIZE(t2) <= 2*shift); /* no overlap with high digits */ |
| 3376 | memcpy(ret->ob_digit, t2->ob_digit, Py_SIZE(t2) * sizeof(digit)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3377 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3378 | /* Zero out remaining digits. */ |
| 3379 | i = 2*shift - Py_SIZE(t2); /* number of uninitialized digits */ |
| 3380 | if (i) |
| 3381 | memset(ret->ob_digit + Py_SIZE(t2), 0, i * sizeof(digit)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3382 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3383 | /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first |
| 3384 | * because it's fresher in cache. |
| 3385 | */ |
| 3386 | i = Py_SIZE(ret) - shift; /* # digits after shift */ |
| 3387 | (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, Py_SIZE(t2)); |
| 3388 | Py_DECREF(t2); |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 3389 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3390 | (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, Py_SIZE(t1)); |
| 3391 | Py_DECREF(t1); |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 3392 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3393 | /* 6. t3 <- (ah+al)(bh+bl), and add into result. */ |
| 3394 | if ((t1 = x_add(ah, al)) == NULL) goto fail; |
| 3395 | Py_DECREF(ah); |
| 3396 | Py_DECREF(al); |
| 3397 | ah = al = NULL; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3398 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3399 | if (a == b) { |
| 3400 | t2 = t1; |
| 3401 | Py_INCREF(t2); |
| 3402 | } |
| 3403 | else if ((t2 = x_add(bh, bl)) == NULL) { |
| 3404 | Py_DECREF(t1); |
| 3405 | goto fail; |
| 3406 | } |
| 3407 | Py_DECREF(bh); |
| 3408 | Py_DECREF(bl); |
| 3409 | bh = bl = NULL; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3410 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3411 | t3 = k_mul(t1, t2); |
| 3412 | Py_DECREF(t1); |
| 3413 | Py_DECREF(t2); |
| 3414 | if (t3 == NULL) goto fail; |
| 3415 | assert(Py_SIZE(t3) >= 0); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3416 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3417 | /* Add t3. It's not obvious why we can't run out of room here. |
| 3418 | * See the (*) comment after this function. |
| 3419 | */ |
| 3420 | (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, Py_SIZE(t3)); |
| 3421 | Py_DECREF(t3); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3422 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3423 | return long_normalize(ret); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3424 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3425 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3426 | Py_XDECREF(ret); |
| 3427 | Py_XDECREF(ah); |
| 3428 | Py_XDECREF(al); |
| 3429 | Py_XDECREF(bh); |
| 3430 | Py_XDECREF(bl); |
| 3431 | return NULL; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3432 | } |
| 3433 | |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3434 | /* (*) Why adding t3 can't "run out of room" above. |
| 3435 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3436 | Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts |
| 3437 | to start with: |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3438 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3439 | 1. For any integer i, i = c(i/2) + f(i/2). In particular, |
| 3440 | bsize = c(bsize/2) + f(bsize/2). |
| 3441 | 2. shift = f(bsize/2) |
| 3442 | 3. asize <= bsize |
| 3443 | 4. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this |
| 3444 | routine, so asize > bsize/2 >= f(bsize/2) in this routine. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3445 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3446 | We allocated asize + bsize result digits, and add t3 into them at an offset |
| 3447 | of shift. This leaves asize+bsize-shift allocated digit positions for t3 |
| 3448 | to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) = |
| 3449 | asize + c(bsize/2) available digit positions. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3450 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3451 | bh has c(bsize/2) digits, and bl at most f(size/2) digits. So bh+hl has |
| 3452 | at most c(bsize/2) digits + 1 bit. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3453 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3454 | If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2) |
| 3455 | digits, and al has at most f(bsize/2) digits in any case. So ah+al has at |
| 3456 | most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3457 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3458 | The product (ah+al)*(bh+bl) therefore has at most |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3459 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3460 | 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] | 3461 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3462 | and we have asize + c(bsize/2) available digit positions. We need to show |
| 3463 | this is always enough. An instance of c(bsize/2) cancels out in both, so |
| 3464 | the question reduces to whether asize digits is enough to hold |
| 3465 | (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits. If asize < bsize, |
| 3466 | then we're asking whether asize digits >= f(bsize/2) digits + 2 bits. By #4, |
| 3467 | 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] | 3468 | 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] | 3469 | 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] | 3470 | c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits |
| 3471 | is enough to hold 2 bits. This is so if bsize >= 2, which holds because |
| 3472 | bsize >= KARATSUBA_CUTOFF >= 2. |
Tim Peters | 8e966ee | 2002-08-14 16:36:23 +0000 | [diff] [blame] | 3473 | |
Tim Peters | 48d52c0 | 2002-08-14 17:07:32 +0000 | [diff] [blame] | 3474 | Note that since there's always enough room for (ah+al)*(bh+bl), and that's |
| 3475 | clearly >= each of ah*bh and al*bl, there's always enough room to subtract |
| 3476 | ah*bh and al*bl too. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3477 | */ |
| 3478 | |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3479 | /* b has at least twice the digits of a, and a is big enough that Karatsuba |
| 3480 | * would pay off *if* the inputs had balanced sizes. View b as a sequence |
| 3481 | * of slices, each with a->ob_size digits, and multiply the slices by a, |
| 3482 | * one at a time. This gives k_mul balanced inputs to work with, and is |
| 3483 | * also cache-friendly (we compute one double-width slice of the result |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 3484 | * at a time, then move on, never backtracking except for the helpful |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3485 | * single-width slice overlap between successive partial sums). |
| 3486 | */ |
| 3487 | static PyLongObject * |
| 3488 | k_lopsided_mul(PyLongObject *a, PyLongObject *b) |
| 3489 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3490 | const Py_ssize_t asize = Py_ABS(Py_SIZE(a)); |
| 3491 | Py_ssize_t bsize = Py_ABS(Py_SIZE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3492 | Py_ssize_t nbdone; /* # of b digits already multiplied */ |
| 3493 | PyLongObject *ret; |
| 3494 | PyLongObject *bslice = NULL; |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3495 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3496 | assert(asize > KARATSUBA_CUTOFF); |
| 3497 | assert(2 * asize <= bsize); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3498 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3499 | /* Allocate result space, and zero it out. */ |
| 3500 | ret = _PyLong_New(asize + bsize); |
| 3501 | if (ret == NULL) |
| 3502 | return NULL; |
| 3503 | memset(ret->ob_digit, 0, Py_SIZE(ret) * sizeof(digit)); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3505 | /* Successive slices of b are copied into bslice. */ |
| 3506 | bslice = _PyLong_New(asize); |
| 3507 | if (bslice == NULL) |
| 3508 | goto fail; |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3509 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3510 | nbdone = 0; |
| 3511 | while (bsize > 0) { |
| 3512 | PyLongObject *product; |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 3513 | const Py_ssize_t nbtouse = Py_MIN(bsize, asize); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3514 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3515 | /* Multiply the next slice of b by a. */ |
| 3516 | memcpy(bslice->ob_digit, b->ob_digit + nbdone, |
| 3517 | nbtouse * sizeof(digit)); |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 3518 | Py_SET_SIZE(bslice, nbtouse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3519 | product = k_mul(a, bslice); |
| 3520 | if (product == NULL) |
| 3521 | goto fail; |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3522 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3523 | /* Add into result. */ |
| 3524 | (void)v_iadd(ret->ob_digit + nbdone, Py_SIZE(ret) - nbdone, |
| 3525 | product->ob_digit, Py_SIZE(product)); |
| 3526 | Py_DECREF(product); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3527 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3528 | bsize -= nbtouse; |
| 3529 | nbdone += nbtouse; |
| 3530 | } |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3531 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3532 | Py_DECREF(bslice); |
| 3533 | return long_normalize(ret); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3534 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3535 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3536 | Py_DECREF(ret); |
| 3537 | Py_XDECREF(bslice); |
| 3538 | return NULL; |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3539 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3540 | |
| 3541 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3542 | long_mul(PyLongObject *a, PyLongObject *b) |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3543 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3544 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3545 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3546 | CHECK_BINOP(a, b); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3547 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3548 | /* fast path for single-digit multiplication */ |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3549 | 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] | 3550 | stwodigits v = (stwodigits)(MEDIUM_VALUE(a)) * MEDIUM_VALUE(b); |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 3551 | return PyLong_FromLongLong((long long)v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3552 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3553 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3554 | z = k_mul(a, b); |
| 3555 | /* Negate if exactly one of the inputs is negative. */ |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 3556 | if (((Py_SIZE(a) ^ Py_SIZE(b)) < 0) && z) { |
| 3557 | _PyLong_Negate(&z); |
| 3558 | if (z == NULL) |
| 3559 | return NULL; |
| 3560 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3561 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3562 | } |
| 3563 | |
Yury Selivanov | e0b2309 | 2016-02-11 10:26:27 -0500 | [diff] [blame] | 3564 | /* Fast modulo division for single-digit longs. */ |
| 3565 | static PyObject * |
| 3566 | fast_mod(PyLongObject *a, PyLongObject *b) |
| 3567 | { |
| 3568 | sdigit left = a->ob_digit[0]; |
| 3569 | sdigit right = b->ob_digit[0]; |
| 3570 | sdigit mod; |
| 3571 | |
| 3572 | assert(Py_ABS(Py_SIZE(a)) == 1); |
| 3573 | assert(Py_ABS(Py_SIZE(b)) == 1); |
| 3574 | |
| 3575 | if (Py_SIZE(a) == Py_SIZE(b)) { |
| 3576 | /* 'a' and 'b' have the same sign. */ |
| 3577 | mod = left % right; |
| 3578 | } |
| 3579 | else { |
| 3580 | /* Either 'a' or 'b' is negative. */ |
| 3581 | mod = right - 1 - (left - 1) % right; |
| 3582 | } |
| 3583 | |
Victor Stinner | f963c13 | 2016-03-23 18:36:54 +0100 | [diff] [blame] | 3584 | return PyLong_FromLong(mod * (sdigit)Py_SIZE(b)); |
Yury Selivanov | e0b2309 | 2016-02-11 10:26:27 -0500 | [diff] [blame] | 3585 | } |
| 3586 | |
| 3587 | /* Fast floor division for single-digit longs. */ |
| 3588 | static PyObject * |
| 3589 | fast_floor_div(PyLongObject *a, PyLongObject *b) |
| 3590 | { |
| 3591 | sdigit left = a->ob_digit[0]; |
| 3592 | sdigit right = b->ob_digit[0]; |
| 3593 | sdigit div; |
| 3594 | |
| 3595 | assert(Py_ABS(Py_SIZE(a)) == 1); |
| 3596 | assert(Py_ABS(Py_SIZE(b)) == 1); |
| 3597 | |
| 3598 | if (Py_SIZE(a) == Py_SIZE(b)) { |
| 3599 | /* 'a' and 'b' have the same sign. */ |
| 3600 | div = left / right; |
| 3601 | } |
| 3602 | else { |
| 3603 | /* Either 'a' or 'b' is negative. */ |
| 3604 | div = -1 - (left - 1) / right; |
| 3605 | } |
| 3606 | |
| 3607 | return PyLong_FromLong(div); |
| 3608 | } |
| 3609 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3610 | /* The / and % operators are now defined in terms of divmod(). |
| 3611 | The expression a mod b has the value a - b*floor(a/b). |
| 3612 | The long_divrem function gives the remainder after division of |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3613 | |a| by |b|, with the sign of a. This is also expressed |
| 3614 | as a - b*trunc(a/b), if trunc truncates towards zero. |
| 3615 | Some examples: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3616 | a b a rem b a mod b |
| 3617 | 13 10 3 3 |
| 3618 | -13 10 -3 7 |
| 3619 | 13 -10 3 -7 |
| 3620 | -13 -10 -3 -3 |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3621 | 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] | 3622 | have different signs. We then subtract one from the 'div' |
| 3623 | part of the outcome to keep the invariant intact. */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3624 | |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3625 | /* Compute |
| 3626 | * *pdiv, *pmod = divmod(v, w) |
| 3627 | * NULL can be passed for pdiv or pmod, in which case that part of |
| 3628 | * the result is simply thrown away. The caller owns a reference to |
| 3629 | * each of these it requests (does not pass NULL for). |
| 3630 | */ |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3631 | static int |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3632 | l_divmod(PyLongObject *v, PyLongObject *w, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3633 | PyLongObject **pdiv, PyLongObject **pmod) |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3634 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3635 | PyLongObject *div, *mod; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3636 | |
Yury Selivanov | e0b2309 | 2016-02-11 10:26:27 -0500 | [diff] [blame] | 3637 | if (Py_ABS(Py_SIZE(v)) == 1 && Py_ABS(Py_SIZE(w)) == 1) { |
| 3638 | /* Fast path for single-digit longs */ |
| 3639 | div = NULL; |
| 3640 | if (pdiv != NULL) { |
| 3641 | div = (PyLongObject *)fast_floor_div(v, w); |
| 3642 | if (div == NULL) { |
| 3643 | return -1; |
| 3644 | } |
| 3645 | } |
| 3646 | if (pmod != NULL) { |
| 3647 | mod = (PyLongObject *)fast_mod(v, w); |
| 3648 | if (mod == NULL) { |
| 3649 | Py_XDECREF(div); |
| 3650 | return -1; |
| 3651 | } |
| 3652 | *pmod = mod; |
| 3653 | } |
| 3654 | if (pdiv != NULL) { |
| 3655 | /* We only want to set `*pdiv` when `*pmod` is |
| 3656 | set successfully. */ |
| 3657 | *pdiv = div; |
| 3658 | } |
| 3659 | return 0; |
| 3660 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3661 | if (long_divrem(v, w, &div, &mod) < 0) |
| 3662 | return -1; |
| 3663 | if ((Py_SIZE(mod) < 0 && Py_SIZE(w) > 0) || |
| 3664 | (Py_SIZE(mod) > 0 && Py_SIZE(w) < 0)) { |
| 3665 | PyLongObject *temp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3666 | temp = (PyLongObject *) long_add(mod, w); |
| 3667 | Py_DECREF(mod); |
| 3668 | mod = temp; |
| 3669 | if (mod == NULL) { |
| 3670 | Py_DECREF(div); |
| 3671 | return -1; |
| 3672 | } |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 3673 | temp = (PyLongObject *) long_sub(div, (PyLongObject *)_PyLong_One); |
| 3674 | if (temp == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3675 | Py_DECREF(mod); |
| 3676 | Py_DECREF(div); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3677 | return -1; |
| 3678 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3679 | Py_DECREF(div); |
| 3680 | div = temp; |
| 3681 | } |
| 3682 | if (pdiv != NULL) |
| 3683 | *pdiv = div; |
| 3684 | else |
| 3685 | Py_DECREF(div); |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3686 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3687 | if (pmod != NULL) |
| 3688 | *pmod = mod; |
| 3689 | else |
| 3690 | Py_DECREF(mod); |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3691 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3692 | return 0; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3693 | } |
| 3694 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3695 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3696 | long_div(PyObject *a, PyObject *b) |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3697 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3698 | PyLongObject *div; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3699 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3700 | CHECK_BINOP(a, b); |
Yury Selivanov | e0b2309 | 2016-02-11 10:26:27 -0500 | [diff] [blame] | 3701 | |
| 3702 | if (Py_ABS(Py_SIZE(a)) == 1 && Py_ABS(Py_SIZE(b)) == 1) { |
| 3703 | return fast_floor_div((PyLongObject*)a, (PyLongObject*)b); |
| 3704 | } |
| 3705 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3706 | if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, NULL) < 0) |
| 3707 | div = NULL; |
| 3708 | return (PyObject *)div; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3709 | } |
| 3710 | |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3711 | /* PyLong/PyLong -> float, with correctly rounded result. */ |
| 3712 | |
| 3713 | #define MANT_DIG_DIGITS (DBL_MANT_DIG / PyLong_SHIFT) |
| 3714 | #define MANT_DIG_BITS (DBL_MANT_DIG % PyLong_SHIFT) |
| 3715 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3716 | static PyObject * |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3717 | long_true_divide(PyObject *v, PyObject *w) |
Tim Peters | 20dab9f | 2001-09-04 05:31:47 +0000 | [diff] [blame] | 3718 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3719 | PyLongObject *a, *b, *x; |
| 3720 | Py_ssize_t a_size, b_size, shift, extra_bits, diff, x_size, x_bits; |
| 3721 | digit mask, low; |
| 3722 | int inexact, negate, a_is_small, b_is_small; |
| 3723 | double dx, result; |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 3724 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3725 | CHECK_BINOP(v, w); |
| 3726 | a = (PyLongObject *)v; |
| 3727 | b = (PyLongObject *)w; |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 3728 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3729 | /* |
| 3730 | Method in a nutshell: |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3731 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3732 | 0. reduce to case a, b > 0; filter out obvious underflow/overflow |
| 3733 | 1. choose a suitable integer 'shift' |
| 3734 | 2. use integer arithmetic to compute x = floor(2**-shift*a/b) |
| 3735 | 3. adjust x for correct rounding |
| 3736 | 4. convert x to a double dx with the same value |
| 3737 | 5. return ldexp(dx, shift). |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3738 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3739 | In more detail: |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3740 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3741 | 0. For any a, a/0 raises ZeroDivisionError; for nonzero b, 0/b |
| 3742 | returns either 0.0 or -0.0, depending on the sign of b. For a and |
| 3743 | b both nonzero, ignore signs of a and b, and add the sign back in |
| 3744 | at the end. Now write a_bits and b_bits for the bit lengths of a |
| 3745 | and b respectively (that is, a_bits = 1 + floor(log_2(a)); likewise |
| 3746 | for b). Then |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3747 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3748 | 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] | 3749 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3750 | So if a_bits - b_bits > DBL_MAX_EXP then a/b > 2**DBL_MAX_EXP and |
| 3751 | so overflows. Similarly, if a_bits - b_bits < DBL_MIN_EXP - |
| 3752 | DBL_MANT_DIG - 1 then a/b underflows to 0. With these cases out of |
| 3753 | the way, we can assume that |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3754 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3755 | 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] | 3756 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3757 | 1. The integer 'shift' is chosen so that x has the right number of |
| 3758 | bits for a double, plus two or three extra bits that will be used |
| 3759 | in the rounding decisions. Writing a_bits and b_bits for the |
| 3760 | number of significant bits in a and b respectively, a |
| 3761 | straightforward formula for shift is: |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3762 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3763 | shift = a_bits - b_bits - DBL_MANT_DIG - 2 |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3764 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3765 | This is fine in the usual case, but if a/b is smaller than the |
| 3766 | smallest normal float then it can lead to double rounding on an |
| 3767 | IEEE 754 platform, giving incorrectly rounded results. So we |
| 3768 | adjust the formula slightly. The actual formula used is: |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3769 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3770 | 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] | 3771 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3772 | 2. The quantity x is computed by first shifting a (left -shift bits |
| 3773 | if shift <= 0, right shift bits if shift > 0) and then dividing by |
| 3774 | b. For both the shift and the division, we keep track of whether |
| 3775 | the result is inexact, in a flag 'inexact'; this information is |
| 3776 | needed at the rounding stage. |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3777 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3778 | With the choice of shift above, together with our assumption that |
| 3779 | a_bits - b_bits >= DBL_MIN_EXP - DBL_MANT_DIG - 1, it follows |
| 3780 | that x >= 1. |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3781 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3782 | 3. Now x * 2**shift <= a/b < (x+1) * 2**shift. We want to replace |
| 3783 | this with an exactly representable float of the form |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3784 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3785 | round(x/2**extra_bits) * 2**(extra_bits+shift). |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3786 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3787 | For float representability, we need x/2**extra_bits < |
| 3788 | 2**DBL_MANT_DIG and extra_bits + shift >= DBL_MIN_EXP - |
| 3789 | DBL_MANT_DIG. This translates to the condition: |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3790 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3791 | extra_bits >= MAX(x_bits, DBL_MIN_EXP - shift) - DBL_MANT_DIG |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3793 | To round, we just modify the bottom digit of x in-place; this can |
| 3794 | end up giving a digit with value > PyLONG_MASK, but that's not a |
| 3795 | problem since digits can hold values up to 2*PyLONG_MASK+1. |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3796 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3797 | With the original choices for shift above, extra_bits will always |
| 3798 | be 2 or 3. Then rounding under the round-half-to-even rule, we |
| 3799 | round up iff the most significant of the extra bits is 1, and |
| 3800 | either: (a) the computation of x in step 2 had an inexact result, |
| 3801 | or (b) at least one other of the extra bits is 1, or (c) the least |
| 3802 | significant bit of x (above those to be rounded) is 1. |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3803 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3804 | 4. Conversion to a double is straightforward; all floating-point |
| 3805 | operations involved in the conversion are exact, so there's no |
| 3806 | danger of rounding errors. |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3808 | 5. Use ldexp(x, shift) to compute x*2**shift, the final result. |
| 3809 | The result will always be exactly representable as a double, except |
| 3810 | in the case that it overflows. To avoid dependence on the exact |
| 3811 | behaviour of ldexp on overflow, we check for overflow before |
| 3812 | applying ldexp. The result of ldexp is adjusted for sign before |
| 3813 | returning. |
| 3814 | */ |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3815 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3816 | /* Reduce to case where a and b are both positive. */ |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3817 | a_size = Py_ABS(Py_SIZE(a)); |
| 3818 | b_size = Py_ABS(Py_SIZE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3819 | negate = (Py_SIZE(a) < 0) ^ (Py_SIZE(b) < 0); |
| 3820 | if (b_size == 0) { |
| 3821 | PyErr_SetString(PyExc_ZeroDivisionError, |
| 3822 | "division by zero"); |
| 3823 | goto error; |
| 3824 | } |
| 3825 | if (a_size == 0) |
| 3826 | goto underflow_or_zero; |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3827 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3828 | /* Fast path for a and b small (exactly representable in a double). |
| 3829 | Relies on floating-point division being correctly rounded; results |
| 3830 | may be subject to double rounding on x86 machines that operate with |
| 3831 | the x87 FPU set to 64-bit precision. */ |
| 3832 | a_is_small = a_size <= MANT_DIG_DIGITS || |
| 3833 | (a_size == MANT_DIG_DIGITS+1 && |
| 3834 | a->ob_digit[MANT_DIG_DIGITS] >> MANT_DIG_BITS == 0); |
| 3835 | b_is_small = b_size <= MANT_DIG_DIGITS || |
| 3836 | (b_size == MANT_DIG_DIGITS+1 && |
| 3837 | b->ob_digit[MANT_DIG_DIGITS] >> MANT_DIG_BITS == 0); |
| 3838 | if (a_is_small && b_is_small) { |
| 3839 | double da, db; |
| 3840 | da = a->ob_digit[--a_size]; |
| 3841 | while (a_size > 0) |
| 3842 | da = da * PyLong_BASE + a->ob_digit[--a_size]; |
| 3843 | db = b->ob_digit[--b_size]; |
| 3844 | while (b_size > 0) |
| 3845 | db = db * PyLong_BASE + b->ob_digit[--b_size]; |
| 3846 | result = da / db; |
| 3847 | goto success; |
| 3848 | } |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 3849 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3850 | /* Catch obvious cases of underflow and overflow */ |
| 3851 | diff = a_size - b_size; |
| 3852 | if (diff > PY_SSIZE_T_MAX/PyLong_SHIFT - 1) |
| 3853 | /* Extreme overflow */ |
| 3854 | goto overflow; |
| 3855 | else if (diff < 1 - PY_SSIZE_T_MAX/PyLong_SHIFT) |
| 3856 | /* Extreme underflow */ |
| 3857 | goto underflow_or_zero; |
| 3858 | /* Next line is now safe from overflowing a Py_ssize_t */ |
Niklas Fiekas | c5b7900 | 2020-01-16 15:09:19 +0100 | [diff] [blame] | 3859 | diff = diff * PyLong_SHIFT + _Py_bit_length(a->ob_digit[a_size - 1]) - |
| 3860 | _Py_bit_length(b->ob_digit[b_size - 1]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3861 | /* Now diff = a_bits - b_bits. */ |
| 3862 | if (diff > DBL_MAX_EXP) |
| 3863 | goto overflow; |
| 3864 | else if (diff < DBL_MIN_EXP - DBL_MANT_DIG - 1) |
| 3865 | goto underflow_or_zero; |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 3866 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3867 | /* Choose value for shift; see comments for step 1 above. */ |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 3868 | shift = Py_MAX(diff, DBL_MIN_EXP) - DBL_MANT_DIG - 2; |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3869 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3870 | inexact = 0; |
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 | /* x = abs(a * 2**-shift) */ |
| 3873 | if (shift <= 0) { |
| 3874 | Py_ssize_t i, shift_digits = -shift / PyLong_SHIFT; |
| 3875 | digit rem; |
| 3876 | /* x = a << -shift */ |
| 3877 | if (a_size >= PY_SSIZE_T_MAX - 1 - shift_digits) { |
| 3878 | /* In practice, it's probably impossible to end up |
| 3879 | here. Both a and b would have to be enormous, |
| 3880 | using close to SIZE_T_MAX bytes of memory each. */ |
| 3881 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3882 | "intermediate overflow during division"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3883 | goto error; |
| 3884 | } |
| 3885 | x = _PyLong_New(a_size + shift_digits + 1); |
| 3886 | if (x == NULL) |
| 3887 | goto error; |
| 3888 | for (i = 0; i < shift_digits; i++) |
| 3889 | x->ob_digit[i] = 0; |
| 3890 | rem = v_lshift(x->ob_digit + shift_digits, a->ob_digit, |
| 3891 | a_size, -shift % PyLong_SHIFT); |
| 3892 | x->ob_digit[a_size + shift_digits] = rem; |
| 3893 | } |
| 3894 | else { |
| 3895 | Py_ssize_t shift_digits = shift / PyLong_SHIFT; |
| 3896 | digit rem; |
| 3897 | /* x = a >> shift */ |
| 3898 | assert(a_size >= shift_digits); |
| 3899 | x = _PyLong_New(a_size - shift_digits); |
| 3900 | if (x == NULL) |
| 3901 | goto error; |
| 3902 | rem = v_rshift(x->ob_digit, a->ob_digit + shift_digits, |
| 3903 | a_size - shift_digits, shift % PyLong_SHIFT); |
| 3904 | /* set inexact if any of the bits shifted out is nonzero */ |
| 3905 | if (rem) |
| 3906 | inexact = 1; |
| 3907 | while (!inexact && shift_digits > 0) |
| 3908 | if (a->ob_digit[--shift_digits]) |
| 3909 | inexact = 1; |
| 3910 | } |
| 3911 | long_normalize(x); |
| 3912 | x_size = Py_SIZE(x); |
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 | /* x //= b. If the remainder is nonzero, set inexact. We own the only |
| 3915 | reference to x, so it's safe to modify it in-place. */ |
| 3916 | if (b_size == 1) { |
| 3917 | digit rem = inplace_divrem1(x->ob_digit, x->ob_digit, x_size, |
| 3918 | b->ob_digit[0]); |
| 3919 | long_normalize(x); |
| 3920 | if (rem) |
| 3921 | inexact = 1; |
| 3922 | } |
| 3923 | else { |
| 3924 | PyLongObject *div, *rem; |
| 3925 | div = x_divrem(x, b, &rem); |
| 3926 | Py_DECREF(x); |
| 3927 | x = div; |
| 3928 | if (x == NULL) |
| 3929 | goto error; |
| 3930 | if (Py_SIZE(rem)) |
| 3931 | inexact = 1; |
| 3932 | Py_DECREF(rem); |
| 3933 | } |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3934 | x_size = Py_ABS(Py_SIZE(x)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3935 | assert(x_size > 0); /* result of division is never zero */ |
Niklas Fiekas | c5b7900 | 2020-01-16 15:09:19 +0100 | [diff] [blame] | 3936 | 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] | 3937 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3938 | /* The number of extra bits that have to be rounded away. */ |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 3939 | 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] | 3940 | assert(extra_bits == 2 || extra_bits == 3); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3941 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3942 | /* Round by directly modifying the low digit of x. */ |
| 3943 | mask = (digit)1 << (extra_bits - 1); |
| 3944 | low = x->ob_digit[0] | inexact; |
Mark Dickinson | dc590a4 | 2016-08-21 10:23:23 +0100 | [diff] [blame] | 3945 | if ((low & mask) && (low & (3U*mask-1U))) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3946 | low += mask; |
Mark Dickinson | dc590a4 | 2016-08-21 10:23:23 +0100 | [diff] [blame] | 3947 | x->ob_digit[0] = low & ~(2U*mask-1U); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3948 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3949 | /* Convert x to a double dx; the conversion is exact. */ |
| 3950 | dx = x->ob_digit[--x_size]; |
| 3951 | while (x_size > 0) |
| 3952 | dx = dx * PyLong_BASE + x->ob_digit[--x_size]; |
| 3953 | Py_DECREF(x); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3954 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3955 | /* Check whether ldexp result will overflow a double. */ |
| 3956 | if (shift + x_bits >= DBL_MAX_EXP && |
| 3957 | (shift + x_bits > DBL_MAX_EXP || dx == ldexp(1.0, (int)x_bits))) |
| 3958 | goto overflow; |
| 3959 | result = ldexp(dx, (int)shift); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3960 | |
| 3961 | success: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3962 | return PyFloat_FromDouble(negate ? -result : result); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3963 | |
| 3964 | underflow_or_zero: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3965 | return PyFloat_FromDouble(negate ? -0.0 : 0.0); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3966 | |
| 3967 | overflow: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3968 | PyErr_SetString(PyExc_OverflowError, |
| 3969 | "integer division result too large for a float"); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3970 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3971 | return NULL; |
Tim Peters | 20dab9f | 2001-09-04 05:31:47 +0000 | [diff] [blame] | 3972 | } |
| 3973 | |
| 3974 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3975 | long_mod(PyObject *a, PyObject *b) |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3976 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3977 | PyLongObject *mod; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3979 | CHECK_BINOP(a, b); |
| 3980 | |
Yury Selivanov | e0b2309 | 2016-02-11 10:26:27 -0500 | [diff] [blame] | 3981 | if (Py_ABS(Py_SIZE(a)) == 1 && Py_ABS(Py_SIZE(b)) == 1) { |
| 3982 | return fast_mod((PyLongObject*)a, (PyLongObject*)b); |
| 3983 | } |
| 3984 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3985 | if (l_divmod((PyLongObject*)a, (PyLongObject*)b, NULL, &mod) < 0) |
| 3986 | mod = NULL; |
| 3987 | return (PyObject *)mod; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3988 | } |
| 3989 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3990 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3991 | long_divmod(PyObject *a, PyObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3992 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3993 | PyLongObject *div, *mod; |
| 3994 | PyObject *z; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3995 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3996 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3997 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3998 | if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, &mod) < 0) { |
| 3999 | return NULL; |
| 4000 | } |
| 4001 | z = PyTuple_New(2); |
| 4002 | if (z != NULL) { |
Sergey Fedoseev | ea6207d | 2019-02-21 15:01:11 +0500 | [diff] [blame] | 4003 | PyTuple_SET_ITEM(z, 0, (PyObject *) div); |
| 4004 | PyTuple_SET_ITEM(z, 1, (PyObject *) mod); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4005 | } |
| 4006 | else { |
| 4007 | Py_DECREF(div); |
| 4008 | Py_DECREF(mod); |
| 4009 | } |
| 4010 | return z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4011 | } |
| 4012 | |
Mark Dickinson | c529967 | 2019-06-02 10:24:06 +0100 | [diff] [blame] | 4013 | |
| 4014 | /* Compute an inverse to a modulo n, or raise ValueError if a is not |
| 4015 | invertible modulo n. Assumes n is positive. The inverse returned |
| 4016 | is whatever falls out of the extended Euclidean algorithm: it may |
| 4017 | be either positive or negative, but will be smaller than n in |
| 4018 | absolute value. |
| 4019 | |
| 4020 | Pure Python equivalent for long_invmod: |
| 4021 | |
| 4022 | def invmod(a, n): |
| 4023 | b, c = 1, 0 |
| 4024 | while n: |
| 4025 | q, r = divmod(a, n) |
| 4026 | a, b, c, n = n, c, b - q*c, r |
| 4027 | |
| 4028 | # at this point a is the gcd of the original inputs |
| 4029 | if a == 1: |
| 4030 | return b |
| 4031 | raise ValueError("Not invertible") |
| 4032 | */ |
| 4033 | |
| 4034 | static PyLongObject * |
| 4035 | long_invmod(PyLongObject *a, PyLongObject *n) |
| 4036 | { |
| 4037 | PyLongObject *b, *c; |
| 4038 | |
| 4039 | /* Should only ever be called for positive n */ |
| 4040 | assert(Py_SIZE(n) > 0); |
| 4041 | |
| 4042 | b = (PyLongObject *)PyLong_FromLong(1L); |
| 4043 | if (b == NULL) { |
| 4044 | return NULL; |
| 4045 | } |
| 4046 | c = (PyLongObject *)PyLong_FromLong(0L); |
| 4047 | if (c == NULL) { |
| 4048 | Py_DECREF(b); |
| 4049 | return NULL; |
| 4050 | } |
| 4051 | Py_INCREF(a); |
| 4052 | Py_INCREF(n); |
| 4053 | |
| 4054 | /* references now owned: a, b, c, n */ |
| 4055 | while (Py_SIZE(n) != 0) { |
| 4056 | PyLongObject *q, *r, *s, *t; |
| 4057 | |
| 4058 | if (l_divmod(a, n, &q, &r) == -1) { |
| 4059 | goto Error; |
| 4060 | } |
| 4061 | Py_DECREF(a); |
| 4062 | a = n; |
| 4063 | n = r; |
| 4064 | t = (PyLongObject *)long_mul(q, c); |
| 4065 | Py_DECREF(q); |
| 4066 | if (t == NULL) { |
| 4067 | goto Error; |
| 4068 | } |
| 4069 | s = (PyLongObject *)long_sub(b, t); |
| 4070 | Py_DECREF(t); |
| 4071 | if (s == NULL) { |
| 4072 | goto Error; |
| 4073 | } |
| 4074 | Py_DECREF(b); |
| 4075 | b = c; |
| 4076 | c = s; |
| 4077 | } |
| 4078 | /* references now owned: a, b, c, n */ |
| 4079 | |
| 4080 | Py_DECREF(c); |
| 4081 | Py_DECREF(n); |
Petr Viktorin | 1e375c6 | 2019-06-03 02:28:29 +0200 | [diff] [blame] | 4082 | if (long_compare(a, (PyLongObject *)_PyLong_One)) { |
Mark Dickinson | c529967 | 2019-06-02 10:24:06 +0100 | [diff] [blame] | 4083 | /* a != 1; we don't have an inverse. */ |
| 4084 | Py_DECREF(a); |
| 4085 | Py_DECREF(b); |
| 4086 | PyErr_SetString(PyExc_ValueError, |
| 4087 | "base is not invertible for the given modulus"); |
| 4088 | return NULL; |
| 4089 | } |
| 4090 | else { |
| 4091 | /* a == 1; b gives an inverse modulo n */ |
| 4092 | Py_DECREF(a); |
| 4093 | return b; |
| 4094 | } |
| 4095 | |
| 4096 | Error: |
| 4097 | Py_DECREF(a); |
| 4098 | Py_DECREF(b); |
| 4099 | Py_DECREF(c); |
| 4100 | Py_DECREF(n); |
| 4101 | return NULL; |
| 4102 | } |
| 4103 | |
| 4104 | |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4105 | /* pow(v, w, x) */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4106 | static PyObject * |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 4107 | long_pow(PyObject *v, PyObject *w, PyObject *x) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4108 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4109 | PyLongObject *a, *b, *c; /* a,b,c = v,w,x */ |
| 4110 | int negativeOutput = 0; /* if x<0 return negative output */ |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 4111 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4112 | PyLongObject *z = NULL; /* accumulated result */ |
| 4113 | Py_ssize_t i, j, k; /* counters */ |
| 4114 | PyLongObject *temp = NULL; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4115 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4116 | /* 5-ary values. If the exponent is large enough, table is |
| 4117 | * precomputed so that table[i] == a**i % c for i in range(32). |
| 4118 | */ |
| 4119 | PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 4120 | 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] | 4121 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4122 | /* a, b, c = v, w, x */ |
| 4123 | CHECK_BINOP(v, w); |
| 4124 | a = (PyLongObject*)v; Py_INCREF(a); |
| 4125 | b = (PyLongObject*)w; Py_INCREF(b); |
| 4126 | if (PyLong_Check(x)) { |
| 4127 | c = (PyLongObject *)x; |
| 4128 | Py_INCREF(x); |
| 4129 | } |
| 4130 | else if (x == Py_None) |
| 4131 | c = NULL; |
| 4132 | else { |
| 4133 | Py_DECREF(a); |
| 4134 | Py_DECREF(b); |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 4135 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4136 | } |
Tim Peters | 4c483c4 | 2001-09-05 06:24:58 +0000 | [diff] [blame] | 4137 | |
Mark Dickinson | c529967 | 2019-06-02 10:24:06 +0100 | [diff] [blame] | 4138 | if (Py_SIZE(b) < 0 && c == NULL) { |
| 4139 | /* if exponent is negative and there's no modulus: |
| 4140 | return a float. This works because we know |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4141 | that this calls float_pow() which converts its |
| 4142 | arguments to double. */ |
Mark Dickinson | c529967 | 2019-06-02 10:24:06 +0100 | [diff] [blame] | 4143 | Py_DECREF(a); |
| 4144 | Py_DECREF(b); |
| 4145 | return PyFloat_Type.tp_as_number->nb_power(v, w, x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4146 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4147 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4148 | if (c) { |
| 4149 | /* if modulus == 0: |
| 4150 | raise ValueError() */ |
| 4151 | if (Py_SIZE(c) == 0) { |
| 4152 | PyErr_SetString(PyExc_ValueError, |
| 4153 | "pow() 3rd argument cannot be 0"); |
| 4154 | goto Error; |
| 4155 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4156 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4157 | /* if modulus < 0: |
| 4158 | negativeOutput = True |
| 4159 | modulus = -modulus */ |
| 4160 | if (Py_SIZE(c) < 0) { |
| 4161 | negativeOutput = 1; |
| 4162 | temp = (PyLongObject *)_PyLong_Copy(c); |
| 4163 | if (temp == NULL) |
| 4164 | goto Error; |
| 4165 | Py_DECREF(c); |
| 4166 | c = temp; |
| 4167 | temp = NULL; |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 4168 | _PyLong_Negate(&c); |
| 4169 | if (c == NULL) |
| 4170 | goto Error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4171 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4172 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4173 | /* if modulus == 1: |
| 4174 | return 0 */ |
| 4175 | if ((Py_SIZE(c) == 1) && (c->ob_digit[0] == 1)) { |
| 4176 | z = (PyLongObject *)PyLong_FromLong(0L); |
| 4177 | goto Done; |
| 4178 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4179 | |
Mark Dickinson | c529967 | 2019-06-02 10:24:06 +0100 | [diff] [blame] | 4180 | /* if exponent is negative, negate the exponent and |
| 4181 | replace the base with a modular inverse */ |
| 4182 | if (Py_SIZE(b) < 0) { |
| 4183 | temp = (PyLongObject *)_PyLong_Copy(b); |
| 4184 | if (temp == NULL) |
| 4185 | goto Error; |
| 4186 | Py_DECREF(b); |
| 4187 | b = temp; |
| 4188 | temp = NULL; |
| 4189 | _PyLong_Negate(&b); |
| 4190 | if (b == NULL) |
| 4191 | goto Error; |
| 4192 | |
| 4193 | temp = long_invmod(a, c); |
| 4194 | if (temp == NULL) |
| 4195 | goto Error; |
| 4196 | Py_DECREF(a); |
| 4197 | a = temp; |
| 4198 | } |
| 4199 | |
Tim Peters | 81a9315 | 2013-10-05 16:53:52 -0500 | [diff] [blame] | 4200 | /* Reduce base by modulus in some cases: |
| 4201 | 1. If base < 0. Forcing the base non-negative makes things easier. |
| 4202 | 2. If base is obviously larger than the modulus. The "small |
| 4203 | exponent" case later can multiply directly by base repeatedly, |
| 4204 | while the "large exponent" case multiplies directly by base 31 |
| 4205 | times. It can be unboundedly faster to multiply by |
| 4206 | base % modulus instead. |
| 4207 | We could _always_ do this reduction, but l_divmod() isn't cheap, |
| 4208 | so we only do it when it buys something. */ |
| 4209 | if (Py_SIZE(a) < 0 || Py_SIZE(a) > Py_SIZE(c)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4210 | if (l_divmod(a, c, NULL, &temp) < 0) |
| 4211 | goto Error; |
| 4212 | Py_DECREF(a); |
| 4213 | a = temp; |
| 4214 | temp = NULL; |
| 4215 | } |
| 4216 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4217 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4218 | /* At this point a, b, and c are guaranteed non-negative UNLESS |
| 4219 | c is NULL, in which case a may be negative. */ |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4220 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4221 | z = (PyLongObject *)PyLong_FromLong(1L); |
| 4222 | if (z == NULL) |
| 4223 | goto Error; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4224 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4225 | /* Perform a modular reduction, X = X % c, but leave X alone if c |
| 4226 | * is NULL. |
| 4227 | */ |
| 4228 | #define REDUCE(X) \ |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4229 | do { \ |
| 4230 | if (c != NULL) { \ |
| 4231 | if (l_divmod(X, c, NULL, &temp) < 0) \ |
| 4232 | goto Error; \ |
| 4233 | Py_XDECREF(X); \ |
| 4234 | X = temp; \ |
| 4235 | temp = NULL; \ |
| 4236 | } \ |
| 4237 | } while(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 | /* Multiply two values, then reduce the result: |
| 4240 | result = X*Y % c. If c is NULL, skip the mod. */ |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4241 | #define MULT(X, Y, result) \ |
| 4242 | do { \ |
| 4243 | temp = (PyLongObject *)long_mul(X, Y); \ |
| 4244 | if (temp == NULL) \ |
| 4245 | goto Error; \ |
| 4246 | Py_XDECREF(result); \ |
| 4247 | result = temp; \ |
| 4248 | temp = NULL; \ |
| 4249 | REDUCE(result); \ |
| 4250 | } while(0) |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4251 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4252 | if (Py_SIZE(b) <= FIVEARY_CUTOFF) { |
| 4253 | /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */ |
| 4254 | /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */ |
| 4255 | for (i = Py_SIZE(b) - 1; i >= 0; --i) { |
| 4256 | digit bi = b->ob_digit[i]; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4257 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4258 | for (j = (digit)1 << (PyLong_SHIFT-1); j != 0; j >>= 1) { |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4259 | MULT(z, z, z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4260 | if (bi & j) |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4261 | MULT(z, a, z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4262 | } |
| 4263 | } |
| 4264 | } |
| 4265 | else { |
| 4266 | /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */ |
| 4267 | Py_INCREF(z); /* still holds 1L */ |
| 4268 | table[0] = z; |
| 4269 | for (i = 1; i < 32; ++i) |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4270 | MULT(table[i-1], a, table[i]); |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4271 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4272 | for (i = Py_SIZE(b) - 1; i >= 0; --i) { |
| 4273 | const digit bi = b->ob_digit[i]; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4274 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4275 | for (j = PyLong_SHIFT - 5; j >= 0; j -= 5) { |
| 4276 | const int index = (bi >> j) & 0x1f; |
| 4277 | for (k = 0; k < 5; ++k) |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4278 | MULT(z, z, z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4279 | if (index) |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4280 | MULT(z, table[index], z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4281 | } |
| 4282 | } |
| 4283 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4285 | if (negativeOutput && (Py_SIZE(z) != 0)) { |
| 4286 | temp = (PyLongObject *)long_sub(z, c); |
| 4287 | if (temp == NULL) |
| 4288 | goto Error; |
| 4289 | Py_DECREF(z); |
| 4290 | z = temp; |
| 4291 | temp = NULL; |
| 4292 | } |
| 4293 | goto Done; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4294 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4295 | Error: |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 4296 | Py_CLEAR(z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4297 | /* fall through */ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4298 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4299 | if (Py_SIZE(b) > FIVEARY_CUTOFF) { |
| 4300 | for (i = 0; i < 32; ++i) |
| 4301 | Py_XDECREF(table[i]); |
| 4302 | } |
| 4303 | Py_DECREF(a); |
| 4304 | Py_DECREF(b); |
| 4305 | Py_XDECREF(c); |
| 4306 | Py_XDECREF(temp); |
| 4307 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4308 | } |
| 4309 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4310 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 4311 | long_invert(PyLongObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4312 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4313 | /* Implement ~x as -(x+1) */ |
| 4314 | PyLongObject *x; |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 4315 | if (Py_ABS(Py_SIZE(v)) <=1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4316 | return PyLong_FromLong(-(MEDIUM_VALUE(v)+1)); |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 4317 | x = (PyLongObject *) long_add(v, (PyLongObject *)_PyLong_One); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4318 | if (x == NULL) |
| 4319 | return NULL; |
Mark Dickinson | 583c6e8 | 2016-08-29 16:40:29 +0100 | [diff] [blame] | 4320 | _PyLong_Negate(&x); |
| 4321 | /* No need for maybe_small_long here, since any small |
| 4322 | longs will have been caught in the Py_SIZE <= 1 fast path. */ |
| 4323 | return (PyObject *)x; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4324 | } |
| 4325 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4326 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 4327 | long_neg(PyLongObject *v) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4328 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4329 | PyLongObject *z; |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 4330 | if (Py_ABS(Py_SIZE(v)) <= 1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4331 | return PyLong_FromLong(-MEDIUM_VALUE(v)); |
| 4332 | z = (PyLongObject *)_PyLong_Copy(v); |
| 4333 | if (z != NULL) |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 4334 | Py_SET_SIZE(z, -(Py_SIZE(v))); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4335 | return (PyObject *)z; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4336 | } |
| 4337 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4338 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 4339 | long_abs(PyLongObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4340 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4341 | if (Py_SIZE(v) < 0) |
| 4342 | return long_neg(v); |
| 4343 | else |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 4344 | return long_long((PyObject *)v); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4345 | } |
| 4346 | |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 4347 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 4348 | long_bool(PyLongObject *v) |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 4349 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4350 | return Py_SIZE(v) != 0; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4351 | } |
| 4352 | |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4353 | /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */ |
| 4354 | static int |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4355 | divmod_shift(PyObject *shiftby, Py_ssize_t *wordshift, digit *remshift) |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4356 | { |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4357 | assert(PyLong_Check(shiftby)); |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4358 | assert(Py_SIZE(shiftby) >= 0); |
| 4359 | Py_ssize_t lshiftby = PyLong_AsSsize_t((PyObject *)shiftby); |
| 4360 | if (lshiftby >= 0) { |
| 4361 | *wordshift = lshiftby / PyLong_SHIFT; |
| 4362 | *remshift = lshiftby % PyLong_SHIFT; |
| 4363 | return 0; |
| 4364 | } |
| 4365 | /* PyLong_Check(shiftby) is true and Py_SIZE(shiftby) >= 0, so it must |
| 4366 | be that PyLong_AsSsize_t raised an OverflowError. */ |
| 4367 | assert(PyErr_ExceptionMatches(PyExc_OverflowError)); |
| 4368 | PyErr_Clear(); |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4369 | PyLongObject *wordshift_obj = divrem1((PyLongObject *)shiftby, PyLong_SHIFT, remshift); |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4370 | if (wordshift_obj == NULL) { |
| 4371 | return -1; |
| 4372 | } |
| 4373 | *wordshift = PyLong_AsSsize_t((PyObject *)wordshift_obj); |
| 4374 | Py_DECREF(wordshift_obj); |
| 4375 | if (*wordshift >= 0 && *wordshift < PY_SSIZE_T_MAX / (Py_ssize_t)sizeof(digit)) { |
| 4376 | return 0; |
| 4377 | } |
| 4378 | PyErr_Clear(); |
| 4379 | /* Clip the value. With such large wordshift the right shift |
| 4380 | returns 0 and the left shift raises an error in _PyLong_New(). */ |
| 4381 | *wordshift = PY_SSIZE_T_MAX / sizeof(digit); |
| 4382 | *remshift = 0; |
| 4383 | return 0; |
| 4384 | } |
| 4385 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4386 | static PyObject * |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4387 | long_rshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4388 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4389 | PyLongObject *z = NULL; |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4390 | Py_ssize_t newsize, hishift, i, j; |
| 4391 | digit lomask, himask; |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4392 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4393 | if (Py_SIZE(a) < 0) { |
| 4394 | /* Right shifting negative numbers is harder */ |
| 4395 | PyLongObject *a1, *a2; |
| 4396 | a1 = (PyLongObject *) long_invert(a); |
| 4397 | if (a1 == NULL) |
Mark Dickinson | 92ca535 | 2016-09-17 17:50:50 +0100 | [diff] [blame] | 4398 | return NULL; |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4399 | a2 = (PyLongObject *) long_rshift1(a1, wordshift, remshift); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4400 | Py_DECREF(a1); |
| 4401 | if (a2 == NULL) |
Mark Dickinson | 92ca535 | 2016-09-17 17:50:50 +0100 | [diff] [blame] | 4402 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4403 | z = (PyLongObject *) long_invert(a2); |
| 4404 | Py_DECREF(a2); |
| 4405 | } |
| 4406 | else { |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4407 | newsize = Py_SIZE(a) - wordshift; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4408 | if (newsize <= 0) |
| 4409 | return PyLong_FromLong(0); |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4410 | hishift = PyLong_SHIFT - remshift; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4411 | lomask = ((digit)1 << hishift) - 1; |
| 4412 | himask = PyLong_MASK ^ lomask; |
| 4413 | z = _PyLong_New(newsize); |
| 4414 | if (z == NULL) |
Mark Dickinson | 92ca535 | 2016-09-17 17:50:50 +0100 | [diff] [blame] | 4415 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4416 | for (i = 0, j = wordshift; i < newsize; i++, j++) { |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4417 | z->ob_digit[i] = (a->ob_digit[j] >> remshift) & lomask; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4418 | if (i+1 < newsize) |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4419 | z->ob_digit[i] |= (a->ob_digit[j+1] << hishift) & himask; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4420 | } |
Mark Dickinson | 92ca535 | 2016-09-17 17:50:50 +0100 | [diff] [blame] | 4421 | z = maybe_small_long(long_normalize(z)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4422 | } |
Mark Dickinson | 92ca535 | 2016-09-17 17:50:50 +0100 | [diff] [blame] | 4423 | return (PyObject *)z; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4424 | } |
| 4425 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4426 | static PyObject * |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4427 | long_rshift(PyObject *a, PyObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4428 | { |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4429 | Py_ssize_t wordshift; |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4430 | digit remshift; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4431 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4432 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 4433 | |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4434 | if (Py_SIZE(b) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4435 | PyErr_SetString(PyExc_ValueError, "negative shift count"); |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 4436 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4437 | } |
Mark Dickinson | 82a9527 | 2016-08-29 19:27:06 +0100 | [diff] [blame] | 4438 | if (Py_SIZE(a) == 0) { |
| 4439 | return PyLong_FromLong(0); |
| 4440 | } |
Serhiy Storchaka | 918403c | 2017-03-30 09:47:07 +0300 | [diff] [blame] | 4441 | if (divmod_shift(b, &wordshift, &remshift) < 0) |
| 4442 | return NULL; |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4443 | return long_rshift1((PyLongObject *)a, wordshift, remshift); |
| 4444 | } |
| 4445 | |
| 4446 | /* Return a >> shiftby. */ |
| 4447 | PyObject * |
| 4448 | _PyLong_Rshift(PyObject *a, size_t shiftby) |
| 4449 | { |
| 4450 | Py_ssize_t wordshift; |
| 4451 | digit remshift; |
| 4452 | |
| 4453 | assert(PyLong_Check(a)); |
| 4454 | if (Py_SIZE(a) == 0) { |
| 4455 | return PyLong_FromLong(0); |
| 4456 | } |
| 4457 | wordshift = shiftby / PyLong_SHIFT; |
| 4458 | remshift = shiftby % PyLong_SHIFT; |
| 4459 | return long_rshift1((PyLongObject *)a, wordshift, remshift); |
| 4460 | } |
| 4461 | |
| 4462 | static PyObject * |
| 4463 | long_lshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift) |
| 4464 | { |
| 4465 | /* This version due to Tim Peters */ |
| 4466 | PyLongObject *z = NULL; |
| 4467 | Py_ssize_t oldsize, newsize, i, j; |
| 4468 | twodigits accum; |
| 4469 | |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 4470 | oldsize = Py_ABS(Py_SIZE(a)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4471 | newsize = oldsize + wordshift; |
| 4472 | if (remshift) |
| 4473 | ++newsize; |
| 4474 | z = _PyLong_New(newsize); |
| 4475 | if (z == NULL) |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 4476 | return NULL; |
| 4477 | if (Py_SIZE(a) < 0) { |
| 4478 | assert(Py_REFCNT(z) == 1); |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 4479 | Py_SET_SIZE(z, -Py_SIZE(z)); |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 4480 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4481 | for (i = 0; i < wordshift; i++) |
| 4482 | z->ob_digit[i] = 0; |
| 4483 | accum = 0; |
| 4484 | for (i = wordshift, j = 0; j < oldsize; i++, j++) { |
| 4485 | accum |= (twodigits)a->ob_digit[j] << remshift; |
| 4486 | z->ob_digit[i] = (digit)(accum & PyLong_MASK); |
| 4487 | accum >>= PyLong_SHIFT; |
| 4488 | } |
| 4489 | if (remshift) |
| 4490 | z->ob_digit[newsize-1] = (digit)accum; |
| 4491 | else |
| 4492 | assert(!accum); |
| 4493 | z = long_normalize(z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4494 | return (PyObject *) maybe_small_long(z); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4495 | } |
| 4496 | |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 4497 | static PyObject * |
| 4498 | long_lshift(PyObject *a, PyObject *b) |
| 4499 | { |
| 4500 | Py_ssize_t wordshift; |
| 4501 | digit remshift; |
| 4502 | |
| 4503 | CHECK_BINOP(a, b); |
| 4504 | |
| 4505 | if (Py_SIZE(b) < 0) { |
| 4506 | PyErr_SetString(PyExc_ValueError, "negative shift count"); |
| 4507 | return NULL; |
| 4508 | } |
| 4509 | if (Py_SIZE(a) == 0) { |
| 4510 | return PyLong_FromLong(0); |
| 4511 | } |
| 4512 | if (divmod_shift(b, &wordshift, &remshift) < 0) |
| 4513 | return NULL; |
| 4514 | return long_lshift1((PyLongObject *)a, wordshift, remshift); |
| 4515 | } |
| 4516 | |
| 4517 | /* Return a << shiftby. */ |
| 4518 | PyObject * |
| 4519 | _PyLong_Lshift(PyObject *a, size_t shiftby) |
| 4520 | { |
| 4521 | Py_ssize_t wordshift; |
| 4522 | digit remshift; |
| 4523 | |
| 4524 | assert(PyLong_Check(a)); |
| 4525 | if (Py_SIZE(a) == 0) { |
| 4526 | return PyLong_FromLong(0); |
| 4527 | } |
| 4528 | wordshift = shiftby / PyLong_SHIFT; |
| 4529 | remshift = shiftby % PyLong_SHIFT; |
| 4530 | return long_lshift1((PyLongObject *)a, wordshift, remshift); |
| 4531 | } |
| 4532 | |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4533 | /* Compute two's complement of digit vector a[0:m], writing result to |
| 4534 | z[0:m]. The digit vector a need not be normalized, but should not |
| 4535 | be entirely zero. a and z may point to the same digit vector. */ |
| 4536 | |
| 4537 | static void |
| 4538 | v_complement(digit *z, digit *a, Py_ssize_t m) |
| 4539 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4540 | Py_ssize_t i; |
| 4541 | digit carry = 1; |
| 4542 | for (i = 0; i < m; ++i) { |
| 4543 | carry += a[i] ^ PyLong_MASK; |
| 4544 | z[i] = carry & PyLong_MASK; |
| 4545 | carry >>= PyLong_SHIFT; |
| 4546 | } |
| 4547 | assert(carry == 0); |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4548 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 4549 | |
| 4550 | /* Bitwise and/xor/or operations */ |
| 4551 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4552 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 4553 | long_bitwise(PyLongObject *a, |
Benjamin Peterson | 513762f | 2012-12-26 16:43:33 -0600 | [diff] [blame] | 4554 | char op, /* '&', '|', '^' */ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4555 | PyLongObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4556 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4557 | int nega, negb, negz; |
| 4558 | Py_ssize_t size_a, size_b, size_z, i; |
| 4559 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4560 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4561 | /* Bitwise operations for negative numbers operate as though |
| 4562 | on a two's complement representation. So convert arguments |
| 4563 | from sign-magnitude to two's complement, and convert the |
| 4564 | result back to sign-magnitude at the end. */ |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4565 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4566 | /* If a is negative, replace it by its two's complement. */ |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 4567 | size_a = Py_ABS(Py_SIZE(a)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4568 | nega = Py_SIZE(a) < 0; |
| 4569 | if (nega) { |
| 4570 | z = _PyLong_New(size_a); |
| 4571 | if (z == NULL) |
| 4572 | return NULL; |
| 4573 | v_complement(z->ob_digit, a->ob_digit, size_a); |
| 4574 | a = z; |
| 4575 | } |
| 4576 | else |
| 4577 | /* Keep reference count consistent. */ |
| 4578 | Py_INCREF(a); |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4579 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4580 | /* Same for b. */ |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 4581 | size_b = Py_ABS(Py_SIZE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4582 | negb = Py_SIZE(b) < 0; |
| 4583 | if (negb) { |
| 4584 | z = _PyLong_New(size_b); |
| 4585 | if (z == NULL) { |
| 4586 | Py_DECREF(a); |
| 4587 | return NULL; |
| 4588 | } |
| 4589 | v_complement(z->ob_digit, b->ob_digit, size_b); |
| 4590 | b = z; |
| 4591 | } |
| 4592 | else |
| 4593 | Py_INCREF(b); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4594 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4595 | /* Swap a and b if necessary to ensure size_a >= size_b. */ |
| 4596 | if (size_a < size_b) { |
| 4597 | z = a; a = b; b = z; |
| 4598 | size_z = size_a; size_a = size_b; size_b = size_z; |
| 4599 | negz = nega; nega = negb; negb = negz; |
| 4600 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4601 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4602 | /* JRH: The original logic here was to allocate the result value (z) |
| 4603 | as the longer of the two operands. However, there are some cases |
| 4604 | where the result is guaranteed to be shorter than that: AND of two |
| 4605 | positives, OR of two negatives: use the shorter number. AND with |
| 4606 | mixed signs: use the positive number. OR with mixed signs: use the |
| 4607 | negative number. |
| 4608 | */ |
| 4609 | switch (op) { |
| 4610 | case '^': |
| 4611 | negz = nega ^ negb; |
| 4612 | size_z = size_a; |
| 4613 | break; |
| 4614 | case '&': |
| 4615 | negz = nega & negb; |
| 4616 | size_z = negb ? size_a : size_b; |
| 4617 | break; |
| 4618 | case '|': |
| 4619 | negz = nega | negb; |
| 4620 | size_z = negb ? size_b : size_a; |
| 4621 | break; |
| 4622 | default: |
stratakis | a10d426 | 2019-03-18 18:59:20 +0100 | [diff] [blame] | 4623 | Py_UNREACHABLE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4624 | } |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 4625 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4626 | /* We allow an extra digit if z is negative, to make sure that |
| 4627 | the final two's complement of z doesn't overflow. */ |
| 4628 | z = _PyLong_New(size_z + negz); |
| 4629 | if (z == NULL) { |
| 4630 | Py_DECREF(a); |
| 4631 | Py_DECREF(b); |
| 4632 | return NULL; |
| 4633 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4634 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4635 | /* Compute digits for overlap of a and b. */ |
| 4636 | switch(op) { |
| 4637 | case '&': |
| 4638 | for (i = 0; i < size_b; ++i) |
| 4639 | z->ob_digit[i] = a->ob_digit[i] & b->ob_digit[i]; |
| 4640 | break; |
| 4641 | case '|': |
| 4642 | for (i = 0; i < size_b; ++i) |
| 4643 | z->ob_digit[i] = a->ob_digit[i] | b->ob_digit[i]; |
| 4644 | break; |
| 4645 | case '^': |
| 4646 | for (i = 0; i < size_b; ++i) |
| 4647 | z->ob_digit[i] = a->ob_digit[i] ^ b->ob_digit[i]; |
| 4648 | break; |
| 4649 | default: |
stratakis | a10d426 | 2019-03-18 18:59:20 +0100 | [diff] [blame] | 4650 | Py_UNREACHABLE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4651 | } |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4652 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4653 | /* Copy any remaining digits of a, inverting if necessary. */ |
| 4654 | if (op == '^' && negb) |
| 4655 | for (; i < size_z; ++i) |
| 4656 | z->ob_digit[i] = a->ob_digit[i] ^ PyLong_MASK; |
| 4657 | else if (i < size_z) |
| 4658 | memcpy(&z->ob_digit[i], &a->ob_digit[i], |
| 4659 | (size_z-i)*sizeof(digit)); |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4660 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4661 | /* Complement result if negative. */ |
| 4662 | if (negz) { |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 4663 | Py_SET_SIZE(z, -(Py_SIZE(z))); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4664 | z->ob_digit[size_z] = PyLong_MASK; |
| 4665 | v_complement(z->ob_digit, z->ob_digit, size_z+1); |
| 4666 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4667 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4668 | Py_DECREF(a); |
| 4669 | Py_DECREF(b); |
| 4670 | return (PyObject *)maybe_small_long(long_normalize(z)); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4671 | } |
| 4672 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4673 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 4674 | long_and(PyObject *a, PyObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4675 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4676 | PyObject *c; |
| 4677 | CHECK_BINOP(a, b); |
| 4678 | c = long_bitwise((PyLongObject*)a, '&', (PyLongObject*)b); |
| 4679 | return c; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4680 | } |
| 4681 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4682 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 4683 | long_xor(PyObject *a, PyObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4684 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4685 | PyObject *c; |
| 4686 | CHECK_BINOP(a, b); |
| 4687 | c = long_bitwise((PyLongObject*)a, '^', (PyLongObject*)b); |
| 4688 | return c; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4689 | } |
| 4690 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4691 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 4692 | long_or(PyObject *a, PyObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4693 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4694 | PyObject *c; |
| 4695 | CHECK_BINOP(a, b); |
| 4696 | c = long_bitwise((PyLongObject*)a, '|', (PyLongObject*)b); |
| 4697 | return c; |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 4698 | } |
| 4699 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4700 | static PyObject * |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 4701 | long_long(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 4702 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4703 | if (PyLong_CheckExact(v)) |
| 4704 | Py_INCREF(v); |
| 4705 | else |
| 4706 | v = _PyLong_Copy((PyLongObject *)v); |
| 4707 | return v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 4708 | } |
| 4709 | |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 4710 | PyObject * |
| 4711 | _PyLong_GCD(PyObject *aarg, PyObject *barg) |
| 4712 | { |
| 4713 | PyLongObject *a, *b, *c = NULL, *d = NULL, *r; |
| 4714 | stwodigits x, y, q, s, t, c_carry, d_carry; |
| 4715 | stwodigits A, B, C, D, T; |
| 4716 | int nbits, k; |
| 4717 | Py_ssize_t size_a, size_b, alloc_a, alloc_b; |
| 4718 | digit *a_digit, *b_digit, *c_digit, *d_digit, *a_end, *b_end; |
| 4719 | |
| 4720 | a = (PyLongObject *)aarg; |
| 4721 | b = (PyLongObject *)barg; |
| 4722 | size_a = Py_SIZE(a); |
| 4723 | size_b = Py_SIZE(b); |
| 4724 | if (-2 <= size_a && size_a <= 2 && -2 <= size_b && size_b <= 2) { |
| 4725 | Py_INCREF(a); |
| 4726 | Py_INCREF(b); |
| 4727 | goto simple; |
| 4728 | } |
| 4729 | |
| 4730 | /* Initial reduction: make sure that 0 <= b <= a. */ |
| 4731 | a = (PyLongObject *)long_abs(a); |
| 4732 | if (a == NULL) |
| 4733 | return NULL; |
| 4734 | b = (PyLongObject *)long_abs(b); |
| 4735 | if (b == NULL) { |
| 4736 | Py_DECREF(a); |
| 4737 | return NULL; |
| 4738 | } |
| 4739 | if (long_compare(a, b) < 0) { |
| 4740 | r = a; |
| 4741 | a = b; |
| 4742 | b = r; |
| 4743 | } |
| 4744 | /* We now own references to a and b */ |
| 4745 | |
| 4746 | alloc_a = Py_SIZE(a); |
| 4747 | alloc_b = Py_SIZE(b); |
| 4748 | /* reduce until a fits into 2 digits */ |
| 4749 | while ((size_a = Py_SIZE(a)) > 2) { |
Niklas Fiekas | c5b7900 | 2020-01-16 15:09:19 +0100 | [diff] [blame] | 4750 | nbits = _Py_bit_length(a->ob_digit[size_a-1]); |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 4751 | /* extract top 2*PyLong_SHIFT bits of a into x, along with |
| 4752 | corresponding bits of b into y */ |
| 4753 | size_b = Py_SIZE(b); |
| 4754 | assert(size_b <= size_a); |
| 4755 | if (size_b == 0) { |
| 4756 | if (size_a < alloc_a) { |
| 4757 | r = (PyLongObject *)_PyLong_Copy(a); |
| 4758 | Py_DECREF(a); |
| 4759 | } |
| 4760 | else |
| 4761 | r = a; |
| 4762 | Py_DECREF(b); |
| 4763 | Py_XDECREF(c); |
| 4764 | Py_XDECREF(d); |
| 4765 | return (PyObject *)r; |
| 4766 | } |
| 4767 | x = (((twodigits)a->ob_digit[size_a-1] << (2*PyLong_SHIFT-nbits)) | |
| 4768 | ((twodigits)a->ob_digit[size_a-2] << (PyLong_SHIFT-nbits)) | |
| 4769 | (a->ob_digit[size_a-3] >> nbits)); |
| 4770 | |
| 4771 | y = ((size_b >= size_a - 2 ? b->ob_digit[size_a-3] >> nbits : 0) | |
| 4772 | (size_b >= size_a - 1 ? (twodigits)b->ob_digit[size_a-2] << (PyLong_SHIFT-nbits) : 0) | |
| 4773 | (size_b >= size_a ? (twodigits)b->ob_digit[size_a-1] << (2*PyLong_SHIFT-nbits) : 0)); |
| 4774 | |
| 4775 | /* inner loop of Lehmer's algorithm; A, B, C, D never grow |
| 4776 | larger than PyLong_MASK during the algorithm. */ |
| 4777 | A = 1; B = 0; C = 0; D = 1; |
| 4778 | for (k=0;; k++) { |
| 4779 | if (y-C == 0) |
| 4780 | break; |
| 4781 | q = (x+(A-1))/(y-C); |
| 4782 | s = B+q*D; |
| 4783 | t = x-q*y; |
| 4784 | if (s > t) |
| 4785 | break; |
| 4786 | x = y; y = t; |
| 4787 | t = A+q*C; A = D; B = C; C = s; D = t; |
| 4788 | } |
| 4789 | |
| 4790 | if (k == 0) { |
| 4791 | /* no progress; do a Euclidean step */ |
| 4792 | if (l_divmod(a, b, NULL, &r) < 0) |
| 4793 | goto error; |
| 4794 | Py_DECREF(a); |
| 4795 | a = b; |
| 4796 | b = r; |
| 4797 | alloc_a = alloc_b; |
| 4798 | alloc_b = Py_SIZE(b); |
| 4799 | continue; |
| 4800 | } |
| 4801 | |
| 4802 | /* |
| 4803 | a, b = A*b-B*a, D*a-C*b if k is odd |
| 4804 | a, b = A*a-B*b, D*b-C*a if k is even |
| 4805 | */ |
| 4806 | if (k&1) { |
| 4807 | T = -A; A = -B; B = T; |
| 4808 | T = -C; C = -D; D = T; |
| 4809 | } |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 4810 | if (c != NULL) { |
| 4811 | Py_SET_SIZE(c, size_a); |
| 4812 | } |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 4813 | else if (Py_REFCNT(a) == 1) { |
| 4814 | Py_INCREF(a); |
| 4815 | c = a; |
| 4816 | } |
| 4817 | else { |
| 4818 | alloc_a = size_a; |
| 4819 | c = _PyLong_New(size_a); |
| 4820 | if (c == NULL) |
| 4821 | goto error; |
| 4822 | } |
| 4823 | |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 4824 | if (d != NULL) { |
| 4825 | Py_SET_SIZE(d, size_a); |
| 4826 | } |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 4827 | else if (Py_REFCNT(b) == 1 && size_a <= alloc_b) { |
| 4828 | Py_INCREF(b); |
| 4829 | d = b; |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 4830 | Py_SET_SIZE(d, size_a); |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 4831 | } |
| 4832 | else { |
| 4833 | alloc_b = size_a; |
| 4834 | d = _PyLong_New(size_a); |
| 4835 | if (d == NULL) |
| 4836 | goto error; |
| 4837 | } |
| 4838 | a_end = a->ob_digit + size_a; |
| 4839 | b_end = b->ob_digit + size_b; |
| 4840 | |
| 4841 | /* compute new a and new b in parallel */ |
| 4842 | a_digit = a->ob_digit; |
| 4843 | b_digit = b->ob_digit; |
| 4844 | c_digit = c->ob_digit; |
| 4845 | d_digit = d->ob_digit; |
| 4846 | c_carry = 0; |
| 4847 | d_carry = 0; |
| 4848 | while (b_digit < b_end) { |
| 4849 | c_carry += (A * *a_digit) - (B * *b_digit); |
| 4850 | d_carry += (D * *b_digit++) - (C * *a_digit++); |
| 4851 | *c_digit++ = (digit)(c_carry & PyLong_MASK); |
| 4852 | *d_digit++ = (digit)(d_carry & PyLong_MASK); |
| 4853 | c_carry >>= PyLong_SHIFT; |
| 4854 | d_carry >>= PyLong_SHIFT; |
| 4855 | } |
| 4856 | while (a_digit < a_end) { |
| 4857 | c_carry += A * *a_digit; |
| 4858 | d_carry -= C * *a_digit++; |
| 4859 | *c_digit++ = (digit)(c_carry & PyLong_MASK); |
| 4860 | *d_digit++ = (digit)(d_carry & PyLong_MASK); |
| 4861 | c_carry >>= PyLong_SHIFT; |
| 4862 | d_carry >>= PyLong_SHIFT; |
| 4863 | } |
| 4864 | assert(c_carry == 0); |
| 4865 | assert(d_carry == 0); |
| 4866 | |
| 4867 | Py_INCREF(c); |
| 4868 | Py_INCREF(d); |
| 4869 | Py_DECREF(a); |
| 4870 | Py_DECREF(b); |
| 4871 | a = long_normalize(c); |
| 4872 | b = long_normalize(d); |
| 4873 | } |
| 4874 | Py_XDECREF(c); |
| 4875 | Py_XDECREF(d); |
| 4876 | |
| 4877 | simple: |
| 4878 | assert(Py_REFCNT(a) > 0); |
| 4879 | assert(Py_REFCNT(b) > 0); |
Victor Stinner | 5783fd2 | 2015-09-19 13:39:03 +0200 | [diff] [blame] | 4880 | /* Issue #24999: use two shifts instead of ">> 2*PyLong_SHIFT" to avoid |
| 4881 | undefined behaviour when LONG_MAX type is smaller than 60 bits */ |
| 4882 | #if LONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 4883 | /* a fits into a long, so b must too */ |
| 4884 | x = PyLong_AsLong((PyObject *)a); |
| 4885 | y = PyLong_AsLong((PyObject *)b); |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 4886 | #elif LLONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 4887 | x = PyLong_AsLongLong((PyObject *)a); |
| 4888 | y = PyLong_AsLongLong((PyObject *)b); |
| 4889 | #else |
| 4890 | # error "_PyLong_GCD" |
| 4891 | #endif |
| 4892 | x = Py_ABS(x); |
| 4893 | y = Py_ABS(y); |
| 4894 | Py_DECREF(a); |
| 4895 | Py_DECREF(b); |
| 4896 | |
| 4897 | /* usual Euclidean algorithm for longs */ |
| 4898 | while (y != 0) { |
| 4899 | t = y; |
| 4900 | y = x % y; |
| 4901 | x = t; |
| 4902 | } |
Victor Stinner | 5783fd2 | 2015-09-19 13:39:03 +0200 | [diff] [blame] | 4903 | #if LONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 4904 | return PyLong_FromLong(x); |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 4905 | #elif LLONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 4906 | return PyLong_FromLongLong(x); |
| 4907 | #else |
| 4908 | # error "_PyLong_GCD" |
| 4909 | #endif |
| 4910 | |
| 4911 | error: |
| 4912 | Py_DECREF(a); |
| 4913 | Py_DECREF(b); |
| 4914 | Py_XDECREF(c); |
| 4915 | Py_XDECREF(d); |
| 4916 | return NULL; |
| 4917 | } |
| 4918 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4919 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 4920 | long_float(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 4921 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4922 | double result; |
| 4923 | result = PyLong_AsDouble(v); |
| 4924 | if (result == -1.0 && PyErr_Occurred()) |
| 4925 | return NULL; |
| 4926 | return PyFloat_FromDouble(result); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 4927 | } |
| 4928 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4929 | static PyObject * |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 4930 | long_subtype_new(PyTypeObject *type, PyObject *x, PyObject *obase); |
| 4931 | |
| 4932 | /*[clinic input] |
| 4933 | @classmethod |
| 4934 | int.__new__ as long_new |
| 4935 | x: object(c_default="NULL") = 0 |
| 4936 | / |
| 4937 | base as obase: object(c_default="NULL") = 10 |
| 4938 | [clinic start generated code]*/ |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 4939 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4940 | static PyObject * |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 4941 | long_new_impl(PyTypeObject *type, PyObject *x, PyObject *obase) |
| 4942 | /*[clinic end generated code: output=e47cfe777ab0f24c input=81c98f418af9eb6f]*/ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4943 | { |
Gregory P. Smith | a689e52 | 2012-12-25 22:38:32 -0800 | [diff] [blame] | 4944 | Py_ssize_t base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4945 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4946 | if (type != &PyLong_Type) |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 4947 | return long_subtype_new(type, x, obase); /* Wimp out */ |
Serhiy Storchaka | 0b386d5 | 2012-12-28 09:42:11 +0200 | [diff] [blame] | 4948 | if (x == NULL) { |
| 4949 | if (obase != NULL) { |
| 4950 | PyErr_SetString(PyExc_TypeError, |
| 4951 | "int() missing string argument"); |
| 4952 | return NULL; |
| 4953 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4954 | return PyLong_FromLong(0L); |
Serhiy Storchaka | 0b386d5 | 2012-12-28 09:42:11 +0200 | [diff] [blame] | 4955 | } |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 4956 | if (obase == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4957 | return PyNumber_Long(x); |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 4958 | |
Gregory P. Smith | a689e52 | 2012-12-25 22:38:32 -0800 | [diff] [blame] | 4959 | base = PyNumber_AsSsize_t(obase, NULL); |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 4960 | if (base == -1 && PyErr_Occurred()) |
| 4961 | return NULL; |
Gregory P. Smith | a689e52 | 2012-12-25 22:38:32 -0800 | [diff] [blame] | 4962 | if ((base != 0 && base < 2) || base > 36) { |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 4963 | PyErr_SetString(PyExc_ValueError, |
Sanyam Khurana | 28b6248 | 2017-11-14 03:19:26 +0530 | [diff] [blame] | 4964 | "int() base must be >= 2 and <= 36, or 0"); |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 4965 | return NULL; |
| 4966 | } |
| 4967 | |
| 4968 | if (PyUnicode_Check(x)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4969 | return PyLong_FromUnicodeObject(x, (int)base); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4970 | else if (PyByteArray_Check(x) || PyBytes_Check(x)) { |
Serhiy Storchaka | 8f87eef | 2020-04-12 14:58:27 +0300 | [diff] [blame] | 4971 | const char *string; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4972 | if (PyByteArray_Check(x)) |
| 4973 | string = PyByteArray_AS_STRING(x); |
| 4974 | else |
| 4975 | string = PyBytes_AS_STRING(x); |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 4976 | return _PyLong_FromBytes(string, Py_SIZE(x), (int)base); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4977 | } |
| 4978 | else { |
| 4979 | PyErr_SetString(PyExc_TypeError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4980 | "int() can't convert non-string with explicit base"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4981 | return NULL; |
| 4982 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4983 | } |
| 4984 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4985 | /* Wimpy, slow approach to tp_new calls for subtypes of int: |
| 4986 | first create a regular int from whatever arguments we got, |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 4987 | then allocate a subtype instance and initialize it from |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4988 | the regular int. The regular int is then thrown away. |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 4989 | */ |
| 4990 | static PyObject * |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 4991 | long_subtype_new(PyTypeObject *type, PyObject *x, PyObject *obase) |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 4992 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4993 | PyLongObject *tmp, *newobj; |
| 4994 | Py_ssize_t i, n; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 4995 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4996 | assert(PyType_IsSubtype(type, &PyLong_Type)); |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 4997 | tmp = (PyLongObject *)long_new_impl(&PyLong_Type, x, obase); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4998 | if (tmp == NULL) |
| 4999 | return NULL; |
Serhiy Storchaka | 1509580 | 2015-11-25 15:47:01 +0200 | [diff] [blame] | 5000 | assert(PyLong_Check(tmp)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5001 | n = Py_SIZE(tmp); |
| 5002 | if (n < 0) |
| 5003 | n = -n; |
| 5004 | newobj = (PyLongObject *)type->tp_alloc(type, n); |
| 5005 | if (newobj == NULL) { |
| 5006 | Py_DECREF(tmp); |
| 5007 | return NULL; |
| 5008 | } |
| 5009 | assert(PyLong_Check(newobj)); |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 5010 | Py_SET_SIZE(newobj, Py_SIZE(tmp)); |
| 5011 | for (i = 0; i < n; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5012 | newobj->ob_digit[i] = tmp->ob_digit[i]; |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 5013 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5014 | Py_DECREF(tmp); |
| 5015 | return (PyObject *)newobj; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 5016 | } |
| 5017 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5018 | /*[clinic input] |
| 5019 | int.__getnewargs__ |
| 5020 | [clinic start generated code]*/ |
| 5021 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 5022 | static PyObject * |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5023 | int___getnewargs___impl(PyObject *self) |
| 5024 | /*[clinic end generated code: output=839a49de3f00b61b input=5904770ab1fb8c75]*/ |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 5025 | { |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5026 | return Py_BuildValue("(N)", _PyLong_Copy((PyLongObject *)self)); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 5027 | } |
| 5028 | |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5029 | static PyObject * |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5030 | long_get0(PyObject *Py_UNUSED(self), void *Py_UNUSED(context)) |
| 5031 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5032 | return PyLong_FromLong(0L); |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 5033 | } |
| 5034 | |
| 5035 | static PyObject * |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5036 | long_get1(PyObject *Py_UNUSED(self), void *Py_UNUSED(ignored)) |
| 5037 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5038 | return PyLong_FromLong(1L); |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5039 | } |
| 5040 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5041 | /*[clinic input] |
| 5042 | int.__format__ |
| 5043 | |
| 5044 | format_spec: unicode |
| 5045 | / |
| 5046 | [clinic start generated code]*/ |
| 5047 | |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 5048 | static PyObject * |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5049 | int___format___impl(PyObject *self, PyObject *format_spec) |
| 5050 | /*[clinic end generated code: output=b4929dee9ae18689 input=e31944a9b3e428b7]*/ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 5051 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 5052 | _PyUnicodeWriter writer; |
| 5053 | int ret; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 5054 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5055 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 5056 | ret = _PyLong_FormatAdvancedWriter( |
| 5057 | &writer, |
| 5058 | self, |
| 5059 | format_spec, 0, PyUnicode_GET_LENGTH(format_spec)); |
| 5060 | if (ret == -1) { |
| 5061 | _PyUnicodeWriter_Dealloc(&writer); |
| 5062 | return NULL; |
| 5063 | } |
| 5064 | return _PyUnicodeWriter_Finish(&writer); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 5065 | } |
| 5066 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5067 | /* Return a pair (q, r) such that a = b * q + r, and |
| 5068 | abs(r) <= abs(b)/2, with equality possible only if q is even. |
| 5069 | In other words, q == a / b, rounded to the nearest integer using |
| 5070 | round-half-to-even. */ |
| 5071 | |
| 5072 | PyObject * |
Mark Dickinson | fa68a61 | 2010-06-07 18:47:09 +0000 | [diff] [blame] | 5073 | _PyLong_DivmodNear(PyObject *a, PyObject *b) |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5074 | { |
| 5075 | PyLongObject *quo = NULL, *rem = NULL; |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 5076 | PyObject *twice_rem, *result, *temp; |
HongWeipeng | 42acb7b | 2019-09-18 23:10:15 +0800 | [diff] [blame] | 5077 | int quo_is_odd, quo_is_neg; |
| 5078 | Py_ssize_t cmp; |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5079 | |
| 5080 | /* Equivalent Python code: |
| 5081 | |
| 5082 | def divmod_near(a, b): |
| 5083 | q, r = divmod(a, b) |
| 5084 | # round up if either r / b > 0.5, or r / b == 0.5 and q is odd. |
| 5085 | # The expression r / b > 0.5 is equivalent to 2 * r > b if b is |
| 5086 | # positive, 2 * r < b if b negative. |
| 5087 | greater_than_half = 2*r > b if b > 0 else 2*r < b |
| 5088 | exactly_half = 2*r == b |
| 5089 | if greater_than_half or exactly_half and q % 2 == 1: |
| 5090 | q += 1 |
| 5091 | r -= b |
| 5092 | return q, r |
| 5093 | |
| 5094 | */ |
| 5095 | if (!PyLong_Check(a) || !PyLong_Check(b)) { |
| 5096 | PyErr_SetString(PyExc_TypeError, |
| 5097 | "non-integer arguments in division"); |
| 5098 | return NULL; |
| 5099 | } |
| 5100 | |
| 5101 | /* Do a and b have different signs? If so, quotient is negative. */ |
| 5102 | quo_is_neg = (Py_SIZE(a) < 0) != (Py_SIZE(b) < 0); |
| 5103 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5104 | if (long_divrem((PyLongObject*)a, (PyLongObject*)b, &quo, &rem) < 0) |
| 5105 | goto error; |
| 5106 | |
| 5107 | /* compare twice the remainder with the divisor, to see |
| 5108 | if we need to adjust the quotient and remainder */ |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 5109 | twice_rem = long_lshift((PyObject *)rem, _PyLong_One); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5110 | if (twice_rem == NULL) |
| 5111 | goto error; |
| 5112 | if (quo_is_neg) { |
| 5113 | temp = long_neg((PyLongObject*)twice_rem); |
| 5114 | Py_DECREF(twice_rem); |
| 5115 | twice_rem = temp; |
| 5116 | if (twice_rem == NULL) |
| 5117 | goto error; |
| 5118 | } |
| 5119 | cmp = long_compare((PyLongObject *)twice_rem, (PyLongObject *)b); |
| 5120 | Py_DECREF(twice_rem); |
| 5121 | |
| 5122 | quo_is_odd = Py_SIZE(quo) != 0 && ((quo->ob_digit[0] & 1) != 0); |
| 5123 | if ((Py_SIZE(b) < 0 ? cmp < 0 : cmp > 0) || (cmp == 0 && quo_is_odd)) { |
| 5124 | /* fix up quotient */ |
| 5125 | if (quo_is_neg) |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 5126 | temp = long_sub(quo, (PyLongObject *)_PyLong_One); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5127 | else |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 5128 | temp = long_add(quo, (PyLongObject *)_PyLong_One); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5129 | Py_DECREF(quo); |
| 5130 | quo = (PyLongObject *)temp; |
| 5131 | if (quo == NULL) |
| 5132 | goto error; |
| 5133 | /* and remainder */ |
| 5134 | if (quo_is_neg) |
| 5135 | temp = long_add(rem, (PyLongObject *)b); |
| 5136 | else |
| 5137 | temp = long_sub(rem, (PyLongObject *)b); |
| 5138 | Py_DECREF(rem); |
| 5139 | rem = (PyLongObject *)temp; |
| 5140 | if (rem == NULL) |
| 5141 | goto error; |
| 5142 | } |
| 5143 | |
| 5144 | result = PyTuple_New(2); |
| 5145 | if (result == NULL) |
| 5146 | goto error; |
| 5147 | |
| 5148 | /* PyTuple_SET_ITEM steals references */ |
| 5149 | PyTuple_SET_ITEM(result, 0, (PyObject *)quo); |
| 5150 | PyTuple_SET_ITEM(result, 1, (PyObject *)rem); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5151 | return result; |
| 5152 | |
| 5153 | error: |
| 5154 | Py_XDECREF(quo); |
| 5155 | Py_XDECREF(rem); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5156 | return NULL; |
| 5157 | } |
| 5158 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 5159 | static PyObject * |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 5160 | long_round(PyObject *self, PyObject *args) |
| 5161 | { |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5162 | PyObject *o_ndigits=NULL, *temp, *result, *ndigits; |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 5163 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5164 | /* To round an integer m to the nearest 10**n (n positive), we make use of |
| 5165 | * the divmod_near operation, defined by: |
| 5166 | * |
| 5167 | * divmod_near(a, b) = (q, r) |
| 5168 | * |
| 5169 | * where q is the nearest integer to the quotient a / b (the |
| 5170 | * nearest even integer in the case of a tie) and r == a - q * b. |
| 5171 | * Hence q * b = a - r is the nearest multiple of b to a, |
| 5172 | * preferring even multiples in the case of a tie. |
| 5173 | * |
| 5174 | * So the nearest multiple of 10**n to m is: |
| 5175 | * |
| 5176 | * m - divmod_near(m, 10**n)[1]. |
| 5177 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5178 | if (!PyArg_ParseTuple(args, "|O", &o_ndigits)) |
| 5179 | return NULL; |
| 5180 | if (o_ndigits == NULL) |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5181 | return long_long(self); |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 5182 | |
Serhiy Storchaka | 5f4b229d | 2020-05-28 10:33:45 +0300 | [diff] [blame^] | 5183 | ndigits = _PyNumber_Index(o_ndigits); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5184 | if (ndigits == NULL) |
| 5185 | return NULL; |
Mark Dickinson | 1124e71 | 2009-01-28 21:25:58 +0000 | [diff] [blame] | 5186 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5187 | /* if ndigits >= 0 then no rounding is necessary; return self unchanged */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5188 | if (Py_SIZE(ndigits) >= 0) { |
| 5189 | Py_DECREF(ndigits); |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5190 | return long_long(self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5191 | } |
Mark Dickinson | 1124e71 | 2009-01-28 21:25:58 +0000 | [diff] [blame] | 5192 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5193 | /* result = self - divmod_near(self, 10 ** -ndigits)[1] */ |
| 5194 | temp = long_neg((PyLongObject*)ndigits); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5195 | Py_DECREF(ndigits); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5196 | ndigits = temp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5197 | if (ndigits == NULL) |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5198 | return NULL; |
Mark Dickinson | 1124e71 | 2009-01-28 21:25:58 +0000 | [diff] [blame] | 5199 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5200 | result = PyLong_FromLong(10L); |
| 5201 | if (result == NULL) { |
| 5202 | Py_DECREF(ndigits); |
| 5203 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5204 | } |
Mark Dickinson | 1124e71 | 2009-01-28 21:25:58 +0000 | [diff] [blame] | 5205 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5206 | temp = long_pow(result, ndigits, Py_None); |
| 5207 | Py_DECREF(ndigits); |
| 5208 | Py_DECREF(result); |
| 5209 | result = temp; |
| 5210 | if (result == NULL) |
| 5211 | return NULL; |
| 5212 | |
Mark Dickinson | fa68a61 | 2010-06-07 18:47:09 +0000 | [diff] [blame] | 5213 | temp = _PyLong_DivmodNear(self, result); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 5214 | Py_DECREF(result); |
| 5215 | result = temp; |
| 5216 | if (result == NULL) |
| 5217 | return NULL; |
| 5218 | |
| 5219 | temp = long_sub((PyLongObject *)self, |
| 5220 | (PyLongObject *)PyTuple_GET_ITEM(result, 1)); |
| 5221 | Py_DECREF(result); |
| 5222 | result = temp; |
| 5223 | |
| 5224 | return result; |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 5225 | } |
| 5226 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5227 | /*[clinic input] |
| 5228 | int.__sizeof__ -> Py_ssize_t |
| 5229 | |
| 5230 | Returns size in memory, in bytes. |
| 5231 | [clinic start generated code]*/ |
| 5232 | |
| 5233 | static Py_ssize_t |
| 5234 | int___sizeof___impl(PyObject *self) |
| 5235 | /*[clinic end generated code: output=3303f008eaa6a0a5 input=9b51620c76fc4507]*/ |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 5236 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5237 | Py_ssize_t res; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 5238 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5239 | res = offsetof(PyLongObject, ob_digit) + Py_ABS(Py_SIZE(self))*sizeof(digit); |
| 5240 | return res; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 5241 | } |
| 5242 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5243 | /*[clinic input] |
| 5244 | int.bit_length |
| 5245 | |
| 5246 | Number of bits necessary to represent self in binary. |
| 5247 | |
| 5248 | >>> bin(37) |
| 5249 | '0b100101' |
| 5250 | >>> (37).bit_length() |
| 5251 | 6 |
| 5252 | [clinic start generated code]*/ |
| 5253 | |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5254 | static PyObject * |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5255 | int_bit_length_impl(PyObject *self) |
| 5256 | /*[clinic end generated code: output=fc1977c9353d6a59 input=e4eb7a587e849a32]*/ |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5257 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5258 | PyLongObject *result, *x, *y; |
Serhiy Storchaka | b2e64f9 | 2016-11-08 20:34:22 +0200 | [diff] [blame] | 5259 | Py_ssize_t ndigits; |
| 5260 | int msd_bits; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5261 | digit msd; |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5262 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5263 | assert(self != NULL); |
| 5264 | assert(PyLong_Check(self)); |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5265 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5266 | ndigits = Py_ABS(Py_SIZE(self)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5267 | if (ndigits == 0) |
| 5268 | return PyLong_FromLong(0); |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5269 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5270 | msd = ((PyLongObject *)self)->ob_digit[ndigits-1]; |
Niklas Fiekas | c5b7900 | 2020-01-16 15:09:19 +0100 | [diff] [blame] | 5271 | msd_bits = _Py_bit_length(msd); |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5272 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5273 | if (ndigits <= PY_SSIZE_T_MAX/PyLong_SHIFT) |
| 5274 | return PyLong_FromSsize_t((ndigits-1)*PyLong_SHIFT + msd_bits); |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5275 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5276 | /* expression above may overflow; use Python integers instead */ |
| 5277 | result = (PyLongObject *)PyLong_FromSsize_t(ndigits - 1); |
| 5278 | if (result == NULL) |
| 5279 | return NULL; |
| 5280 | x = (PyLongObject *)PyLong_FromLong(PyLong_SHIFT); |
| 5281 | if (x == NULL) |
| 5282 | goto error; |
| 5283 | y = (PyLongObject *)long_mul(result, x); |
| 5284 | Py_DECREF(x); |
| 5285 | if (y == NULL) |
| 5286 | goto error; |
| 5287 | Py_DECREF(result); |
| 5288 | result = y; |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5290 | x = (PyLongObject *)PyLong_FromLong((long)msd_bits); |
| 5291 | if (x == NULL) |
| 5292 | goto error; |
| 5293 | y = (PyLongObject *)long_add(result, x); |
| 5294 | Py_DECREF(x); |
| 5295 | if (y == NULL) |
| 5296 | goto error; |
| 5297 | Py_DECREF(result); |
| 5298 | result = y; |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5299 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5300 | return (PyObject *)result; |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5301 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 5302 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5303 | Py_DECREF(result); |
| 5304 | return NULL; |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5305 | } |
| 5306 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 5307 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5308 | /*[clinic input] |
Lisa Roach | 5ac7043 | 2018-09-13 23:56:23 -0700 | [diff] [blame] | 5309 | int.as_integer_ratio |
| 5310 | |
| 5311 | Return integer ratio. |
| 5312 | |
| 5313 | Return a pair of integers, whose ratio is exactly equal to the original int |
| 5314 | and with a positive denominator. |
| 5315 | |
| 5316 | >>> (10).as_integer_ratio() |
| 5317 | (10, 1) |
| 5318 | >>> (-10).as_integer_ratio() |
| 5319 | (-10, 1) |
| 5320 | >>> (0).as_integer_ratio() |
| 5321 | (0, 1) |
| 5322 | [clinic start generated code]*/ |
| 5323 | |
| 5324 | static PyObject * |
| 5325 | int_as_integer_ratio_impl(PyObject *self) |
| 5326 | /*[clinic end generated code: output=e60803ae1cc8621a input=55ce3058e15de393]*/ |
| 5327 | { |
Raymond Hettinger | 00bc08e | 2018-09-14 01:00:11 -0700 | [diff] [blame] | 5328 | PyObject *ratio_tuple; |
Serhiy Storchaka | b2e2025 | 2018-10-20 00:46:31 +0300 | [diff] [blame] | 5329 | PyObject *numerator = long_long(self); |
Raymond Hettinger | 00bc08e | 2018-09-14 01:00:11 -0700 | [diff] [blame] | 5330 | if (numerator == NULL) { |
| 5331 | return NULL; |
| 5332 | } |
| 5333 | ratio_tuple = PyTuple_Pack(2, numerator, _PyLong_One); |
| 5334 | Py_DECREF(numerator); |
| 5335 | return ratio_tuple; |
Lisa Roach | 5ac7043 | 2018-09-13 23:56:23 -0700 | [diff] [blame] | 5336 | } |
| 5337 | |
| 5338 | /*[clinic input] |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5339 | int.to_bytes |
| 5340 | |
| 5341 | length: Py_ssize_t |
| 5342 | Length of bytes object to use. An OverflowError is raised if the |
| 5343 | integer is not representable with the given number of bytes. |
| 5344 | byteorder: unicode |
| 5345 | The byte order used to represent the integer. If byteorder is 'big', |
| 5346 | the most significant byte is at the beginning of the byte array. If |
| 5347 | byteorder is 'little', the most significant byte is at the end of the |
| 5348 | byte array. To request the native byte order of the host system, use |
| 5349 | `sys.byteorder' as the byte order value. |
| 5350 | * |
| 5351 | signed as is_signed: bool = False |
| 5352 | Determines whether two's complement is used to represent the integer. |
| 5353 | If signed is False and a negative integer is given, an OverflowError |
| 5354 | is raised. |
| 5355 | |
| 5356 | Return an array of bytes representing an integer. |
| 5357 | [clinic start generated code]*/ |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5358 | |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5359 | static PyObject * |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5360 | int_to_bytes_impl(PyObject *self, Py_ssize_t length, PyObject *byteorder, |
| 5361 | int is_signed) |
| 5362 | /*[clinic end generated code: output=89c801df114050a3 input=ddac63f4c7bf414c]*/ |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5363 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5364 | int little_endian; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5365 | PyObject *bytes; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5366 | |
Serhiy Storchaka | 196a7bc | 2017-02-02 16:54:45 +0200 | [diff] [blame] | 5367 | if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_little)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5368 | little_endian = 1; |
Serhiy Storchaka | 196a7bc | 2017-02-02 16:54:45 +0200 | [diff] [blame] | 5369 | else if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_big)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5370 | little_endian = 0; |
| 5371 | else { |
| 5372 | PyErr_SetString(PyExc_ValueError, |
| 5373 | "byteorder must be either 'little' or 'big'"); |
| 5374 | return NULL; |
| 5375 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5376 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5377 | if (length < 0) { |
| 5378 | PyErr_SetString(PyExc_ValueError, |
| 5379 | "length argument must be non-negative"); |
| 5380 | return NULL; |
| 5381 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5382 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5383 | bytes = PyBytes_FromStringAndSize(NULL, length); |
| 5384 | if (bytes == NULL) |
| 5385 | return NULL; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5386 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5387 | if (_PyLong_AsByteArray((PyLongObject *)self, |
| 5388 | (unsigned char *)PyBytes_AS_STRING(bytes), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5389 | length, little_endian, is_signed) < 0) { |
| 5390 | Py_DECREF(bytes); |
| 5391 | return NULL; |
| 5392 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5393 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5394 | return bytes; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5395 | } |
| 5396 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5397 | /*[clinic input] |
| 5398 | @classmethod |
| 5399 | int.from_bytes |
| 5400 | |
| 5401 | bytes as bytes_obj: object |
| 5402 | Holds the array of bytes to convert. The argument must either |
| 5403 | support the buffer protocol or be an iterable object producing bytes. |
| 5404 | Bytes and bytearray are examples of built-in objects that support the |
| 5405 | buffer protocol. |
| 5406 | byteorder: unicode |
| 5407 | The byte order used to represent the integer. If byteorder is 'big', |
| 5408 | the most significant byte is at the beginning of the byte array. If |
| 5409 | byteorder is 'little', the most significant byte is at the end of the |
| 5410 | byte array. To request the native byte order of the host system, use |
| 5411 | `sys.byteorder' as the byte order value. |
| 5412 | * |
| 5413 | signed as is_signed: bool = False |
| 5414 | Indicates whether two's complement is used to represent the integer. |
| 5415 | |
| 5416 | Return the integer represented by the given array of bytes. |
| 5417 | [clinic start generated code]*/ |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5418 | |
| 5419 | static PyObject * |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5420 | int_from_bytes_impl(PyTypeObject *type, PyObject *bytes_obj, |
| 5421 | PyObject *byteorder, int is_signed) |
| 5422 | /*[clinic end generated code: output=efc5d68e31f9314f input=cdf98332b6a821b0]*/ |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5423 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5424 | int little_endian; |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5425 | PyObject *long_obj, *bytes; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5426 | |
Serhiy Storchaka | 196a7bc | 2017-02-02 16:54:45 +0200 | [diff] [blame] | 5427 | if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_little)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5428 | little_endian = 1; |
Serhiy Storchaka | 196a7bc | 2017-02-02 16:54:45 +0200 | [diff] [blame] | 5429 | else if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_big)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5430 | little_endian = 0; |
| 5431 | else { |
| 5432 | PyErr_SetString(PyExc_ValueError, |
| 5433 | "byteorder must be either 'little' or 'big'"); |
| 5434 | return NULL; |
| 5435 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5436 | |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5437 | bytes = PyObject_Bytes(bytes_obj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5438 | if (bytes == NULL) |
| 5439 | return NULL; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5440 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5441 | long_obj = _PyLong_FromByteArray( |
| 5442 | (unsigned char *)PyBytes_AS_STRING(bytes), Py_SIZE(bytes), |
| 5443 | little_endian, is_signed); |
| 5444 | Py_DECREF(bytes); |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5445 | |
Zackery Spytz | 7bb9cd0 | 2018-10-05 15:02:23 -0600 | [diff] [blame] | 5446 | if (long_obj != NULL && type != &PyLong_Type) { |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 5447 | Py_SETREF(long_obj, PyObject_CallOneArg((PyObject *)type, long_obj)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5448 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5449 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5450 | return long_obj; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5451 | } |
| 5452 | |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5453 | static PyObject * |
| 5454 | long_long_meth(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 5455 | { |
| 5456 | return long_long(self); |
| 5457 | } |
| 5458 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 5459 | static PyMethodDef long_methods[] = { |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5460 | {"conjugate", long_long_meth, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5461 | "Returns self, the complex conjugate of any int."}, |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5462 | INT_BIT_LENGTH_METHODDEF |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5463 | INT_TO_BYTES_METHODDEF |
| 5464 | INT_FROM_BYTES_METHODDEF |
Lisa Roach | 5ac7043 | 2018-09-13 23:56:23 -0700 | [diff] [blame] | 5465 | INT_AS_INTEGER_RATIO_METHODDEF |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5466 | {"__trunc__", long_long_meth, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5467 | "Truncating an Integral returns itself."}, |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5468 | {"__floor__", long_long_meth, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5469 | "Flooring an Integral returns itself."}, |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5470 | {"__ceil__", long_long_meth, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5471 | "Ceiling of an Integral returns itself."}, |
| 5472 | {"__round__", (PyCFunction)long_round, METH_VARARGS, |
| 5473 | "Rounding an Integral returns itself.\n" |
| 5474 | "Rounding with an ndigits argument also returns an integer."}, |
Serhiy Storchaka | 495e880 | 2017-02-01 23:12:20 +0200 | [diff] [blame] | 5475 | INT___GETNEWARGS___METHODDEF |
| 5476 | INT___FORMAT___METHODDEF |
| 5477 | INT___SIZEOF___METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5478 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 5479 | }; |
| 5480 | |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5481 | static PyGetSetDef long_getset[] = { |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 5482 | {"real", |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5483 | (getter)long_long_meth, (setter)NULL, |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5484 | "the real part of a complex number", |
| 5485 | NULL}, |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 5486 | {"imag", |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5487 | long_get0, (setter)NULL, |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5488 | "the imaginary part of a complex number", |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 5489 | NULL}, |
| 5490 | {"numerator", |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5491 | (getter)long_long_meth, (setter)NULL, |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5492 | "the numerator of a rational number in lowest terms", |
| 5493 | NULL}, |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 5494 | {"denominator", |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5495 | long_get1, (setter)NULL, |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5496 | "the denominator of a rational number in lowest terms", |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 5497 | NULL}, |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5498 | {NULL} /* Sentinel */ |
| 5499 | }; |
| 5500 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5501 | PyDoc_STRVAR(long_doc, |
svelankar | 390a096 | 2017-03-08 19:29:01 -0500 | [diff] [blame] | 5502 | "int([x]) -> integer\n\ |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 5503 | int(x, base=10) -> integer\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5504 | \n\ |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 5505 | Convert a number or string to an integer, or return 0 if no arguments\n\ |
| 5506 | are given. If x is a number, return x.__int__(). For floating point\n\ |
| 5507 | numbers, this truncates towards zero.\n\ |
| 5508 | \n\ |
| 5509 | If x is not a number or if base is given, then x must be a string,\n\ |
| 5510 | bytes, or bytearray instance representing an integer literal in the\n\ |
| 5511 | given base. The literal can be preceded by '+' or '-' and be surrounded\n\ |
| 5512 | by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.\n\ |
| 5513 | Base 0 means to interpret the base from the string as an integer literal.\n\ |
| 5514 | >>> int('0b100', base=0)\n\ |
| 5515 | 4"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5516 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 5517 | static PyNumberMethods long_as_number = { |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 5518 | (binaryfunc)long_add, /*nb_add*/ |
| 5519 | (binaryfunc)long_sub, /*nb_subtract*/ |
| 5520 | (binaryfunc)long_mul, /*nb_multiply*/ |
| 5521 | long_mod, /*nb_remainder*/ |
| 5522 | long_divmod, /*nb_divmod*/ |
| 5523 | long_pow, /*nb_power*/ |
| 5524 | (unaryfunc)long_neg, /*nb_negative*/ |
Serhiy Storchaka | 6405fee | 2018-04-30 15:35:08 +0300 | [diff] [blame] | 5525 | long_long, /*tp_positive*/ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 5526 | (unaryfunc)long_abs, /*tp_absolute*/ |
| 5527 | (inquiry)long_bool, /*tp_bool*/ |
| 5528 | (unaryfunc)long_invert, /*nb_invert*/ |
| 5529 | long_lshift, /*nb_lshift*/ |
Serhiy Storchaka | a5119e7 | 2019-05-19 14:14:38 +0300 | [diff] [blame] | 5530 | long_rshift, /*nb_rshift*/ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 5531 | long_and, /*nb_and*/ |
| 5532 | long_xor, /*nb_xor*/ |
| 5533 | long_or, /*nb_or*/ |
| 5534 | long_long, /*nb_int*/ |
| 5535 | 0, /*nb_reserved*/ |
| 5536 | long_float, /*nb_float*/ |
| 5537 | 0, /* nb_inplace_add */ |
| 5538 | 0, /* nb_inplace_subtract */ |
| 5539 | 0, /* nb_inplace_multiply */ |
| 5540 | 0, /* nb_inplace_remainder */ |
| 5541 | 0, /* nb_inplace_power */ |
| 5542 | 0, /* nb_inplace_lshift */ |
| 5543 | 0, /* nb_inplace_rshift */ |
| 5544 | 0, /* nb_inplace_and */ |
| 5545 | 0, /* nb_inplace_xor */ |
| 5546 | 0, /* nb_inplace_or */ |
| 5547 | long_div, /* nb_floor_divide */ |
| 5548 | long_true_divide, /* nb_true_divide */ |
| 5549 | 0, /* nb_inplace_floor_divide */ |
| 5550 | 0, /* nb_inplace_true_divide */ |
| 5551 | long_long, /* nb_index */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 5552 | }; |
| 5553 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 5554 | PyTypeObject PyLong_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5555 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 5556 | "int", /* tp_name */ |
| 5557 | offsetof(PyLongObject, ob_digit), /* tp_basicsize */ |
| 5558 | sizeof(digit), /* tp_itemsize */ |
Inada Naoki | 7d40869 | 2019-05-29 17:23:27 +0900 | [diff] [blame] | 5559 | 0, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5560 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5561 | 0, /* tp_getattr */ |
| 5562 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5563 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5564 | long_to_decimal_string, /* tp_repr */ |
| 5565 | &long_as_number, /* tp_as_number */ |
| 5566 | 0, /* tp_as_sequence */ |
| 5567 | 0, /* tp_as_mapping */ |
| 5568 | (hashfunc)long_hash, /* tp_hash */ |
| 5569 | 0, /* tp_call */ |
Serhiy Storchaka | 96aeaec | 2019-05-06 22:29:40 +0300 | [diff] [blame] | 5570 | 0, /* tp_str */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5571 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 5572 | 0, /* tp_setattro */ |
| 5573 | 0, /* tp_as_buffer */ |
| 5574 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 5575 | Py_TPFLAGS_LONG_SUBCLASS, /* tp_flags */ |
| 5576 | long_doc, /* tp_doc */ |
| 5577 | 0, /* tp_traverse */ |
| 5578 | 0, /* tp_clear */ |
| 5579 | long_richcompare, /* tp_richcompare */ |
| 5580 | 0, /* tp_weaklistoffset */ |
| 5581 | 0, /* tp_iter */ |
| 5582 | 0, /* tp_iternext */ |
| 5583 | long_methods, /* tp_methods */ |
| 5584 | 0, /* tp_members */ |
| 5585 | long_getset, /* tp_getset */ |
| 5586 | 0, /* tp_base */ |
| 5587 | 0, /* tp_dict */ |
| 5588 | 0, /* tp_descr_get */ |
| 5589 | 0, /* tp_descr_set */ |
| 5590 | 0, /* tp_dictoffset */ |
| 5591 | 0, /* tp_init */ |
| 5592 | 0, /* tp_alloc */ |
| 5593 | long_new, /* tp_new */ |
| 5594 | PyObject_Del, /* tp_free */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 5595 | }; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5596 | |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 5597 | static PyTypeObject Int_InfoType; |
| 5598 | |
| 5599 | PyDoc_STRVAR(int_info__doc__, |
| 5600 | "sys.int_info\n\ |
| 5601 | \n\ |
Raymond Hettinger | 7117074 | 2019-09-11 07:17:32 -0700 | [diff] [blame] | 5602 | A named tuple that holds information about Python's\n\ |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 5603 | internal representation of integers. The attributes are read only."); |
| 5604 | |
| 5605 | static PyStructSequence_Field int_info_fields[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5606 | {"bits_per_digit", "size of a digit in bits"}, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 5607 | {"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] | 5608 | {NULL, NULL} |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 5609 | }; |
| 5610 | |
| 5611 | static PyStructSequence_Desc int_info_desc = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5612 | "sys.int_info", /* name */ |
| 5613 | int_info__doc__, /* doc */ |
| 5614 | int_info_fields, /* fields */ |
| 5615 | 2 /* number of fields */ |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 5616 | }; |
| 5617 | |
| 5618 | PyObject * |
| 5619 | PyLong_GetInfo(void) |
| 5620 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5621 | PyObject* int_info; |
| 5622 | int field = 0; |
| 5623 | int_info = PyStructSequence_New(&Int_InfoType); |
| 5624 | if (int_info == NULL) |
| 5625 | return NULL; |
| 5626 | PyStructSequence_SET_ITEM(int_info, field++, |
| 5627 | PyLong_FromLong(PyLong_SHIFT)); |
| 5628 | PyStructSequence_SET_ITEM(int_info, field++, |
| 5629 | PyLong_FromLong(sizeof(digit))); |
| 5630 | if (PyErr_Occurred()) { |
| 5631 | Py_CLEAR(int_info); |
| 5632 | return NULL; |
| 5633 | } |
| 5634 | return int_info; |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 5635 | } |
| 5636 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5637 | int |
Victor Stinner | 630c8df | 2019-12-17 13:02:18 +0100 | [diff] [blame] | 5638 | _PyLong_Init(PyThreadState *tstate) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5639 | { |
| 5640 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
Victor Stinner | 5dcc06f | 2019-11-21 08:51:59 +0100 | [diff] [blame] | 5641 | for (Py_ssize_t i=0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++) { |
| 5642 | sdigit ival = (sdigit)i - NSMALLNEGINTS; |
| 5643 | int size = (ival < 0) ? -1 : ((ival == 0) ? 0 : 1); |
Christian Heimes | dfc12ed | 2008-01-31 15:16:38 +0000 | [diff] [blame] | 5644 | |
Victor Stinner | 5dcc06f | 2019-11-21 08:51:59 +0100 | [diff] [blame] | 5645 | PyLongObject *v = _PyLong_New(1); |
| 5646 | if (!v) { |
| 5647 | return -1; |
| 5648 | } |
Christian Heimes | dfc12ed | 2008-01-31 15:16:38 +0000 | [diff] [blame] | 5649 | |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 5650 | Py_SET_SIZE(v, size); |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 5651 | v->ob_digit[0] = (digit)abs(ival); |
Victor Stinner | 5dcc06f | 2019-11-21 08:51:59 +0100 | [diff] [blame] | 5652 | |
Victor Stinner | 630c8df | 2019-12-17 13:02:18 +0100 | [diff] [blame] | 5653 | tstate->interp->small_ints[i] = v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5654 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5655 | #endif |
Victor Stinner | 5dcc06f | 2019-11-21 08:51:59 +0100 | [diff] [blame] | 5656 | |
Victor Stinner | 630c8df | 2019-12-17 13:02:18 +0100 | [diff] [blame] | 5657 | if (_Py_IsMainInterpreter(tstate)) { |
| 5658 | _PyLong_Zero = PyLong_FromLong(0); |
| 5659 | if (_PyLong_Zero == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 5660 | return 0; |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 5661 | } |
Victor Stinner | 630c8df | 2019-12-17 13:02:18 +0100 | [diff] [blame] | 5662 | |
| 5663 | _PyLong_One = PyLong_FromLong(1); |
| 5664 | if (_PyLong_One == NULL) { |
| 5665 | return 0; |
| 5666 | } |
| 5667 | |
| 5668 | /* initialize int_info */ |
| 5669 | if (Int_InfoType.tp_name == NULL) { |
| 5670 | if (PyStructSequence_InitType2(&Int_InfoType, &int_info_desc) < 0) { |
| 5671 | return 0; |
| 5672 | } |
| 5673 | } |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 5674 | } |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 5675 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5676 | return 1; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5677 | } |
| 5678 | |
| 5679 | void |
Victor Stinner | 630c8df | 2019-12-17 13:02:18 +0100 | [diff] [blame] | 5680 | _PyLong_Fini(PyThreadState *tstate) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5681 | { |
Victor Stinner | 630c8df | 2019-12-17 13:02:18 +0100 | [diff] [blame] | 5682 | if (_Py_IsMainInterpreter(tstate)) { |
| 5683 | Py_CLEAR(_PyLong_One); |
| 5684 | Py_CLEAR(_PyLong_Zero); |
| 5685 | } |
| 5686 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5687 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
Victor Stinner | 5dcc06f | 2019-11-21 08:51:59 +0100 | [diff] [blame] | 5688 | for (Py_ssize_t i = 0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++) { |
Victor Stinner | 630c8df | 2019-12-17 13:02:18 +0100 | [diff] [blame] | 5689 | Py_CLEAR(tstate->interp->small_ints[i]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5690 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5691 | #endif |
| 5692 | } |