diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2019-05-19 11:14:38 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-19 11:14:38 (GMT) |
commit | a5119e7d75c9729fc36c059d05f3d7132e7f6bb4 (patch) | |
tree | b9e16c88635e3230a7b5d60b399ea71ff2b38494 /Modules | |
parent | 1d5bdef550d4395211fbe5f3c1444d7ea5bb54a2 (diff) | |
download | cpython-a5119e7d75c9729fc36c059d05f3d7132e7f6bb4.zip cpython-a5119e7d75c9729fc36c059d05f3d7132e7f6bb4.tar.gz cpython-a5119e7d75c9729fc36c059d05f3d7132e7f6bb4.tar.bz2 |
bpo-36957: Add _PyLong_Rshift() and _PyLong_Lshift(). (GH-13416)
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/mathmodule.c | 30 |
1 files changed, 7 insertions, 23 deletions
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 8213092..7a0044a 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -1671,18 +1671,13 @@ math_isqrt(PyObject *module, PyObject *n) } d = 0; while (--s >= 0) { - PyObject *q, *shift; + PyObject *q; size_t e = d; d = c >> s; /* q = (n >> 2*c - e - d + 1) // a */ - shift = PyLong_FromSize_t(2U*c - d - e + 1U); - if (shift == NULL) { - goto error; - } - q = PyNumber_Rshift(n, shift); - Py_DECREF(shift); + q = _PyLong_Rshift(n, 2U*c - d - e + 1U); if (q == NULL) { goto error; } @@ -1692,13 +1687,7 @@ math_isqrt(PyObject *module, PyObject *n) } /* a = (a << d - 1 - e) + q */ - shift = PyLong_FromSize_t(d - 1U - e); - if (shift == NULL) { - Py_DECREF(q); - goto error; - } - Py_SETREF(a, PyNumber_Lshift(a, shift)); - Py_DECREF(shift); + Py_SETREF(a, _PyLong_Lshift(a, d - 1U - e)); if (a == NULL) { Py_DECREF(q); goto error; @@ -1939,9 +1928,9 @@ static PyObject * math_factorial(PyObject *module, PyObject *arg) /*[clinic end generated code: output=6686f26fae00e9ca input=6d1c8105c0d91fb4]*/ { - long x; + long x, two_valuation; int overflow; - PyObject *result, *odd_part, *two_valuation, *pyint_form; + PyObject *result, *odd_part, *pyint_form; if (PyFloat_Check(arg)) { PyObject *lx; @@ -1990,13 +1979,8 @@ math_factorial(PyObject *module, PyObject *arg) odd_part = factorial_odd_part(x); if (odd_part == NULL) return NULL; - two_valuation = PyLong_FromLong(x - count_set_bits(x)); - if (two_valuation == NULL) { - Py_DECREF(odd_part); - return NULL; - } - result = PyNumber_Lshift(odd_part, two_valuation); - Py_DECREF(two_valuation); + two_valuation = x - count_set_bits(x); + result = _PyLong_Lshift(odd_part, two_valuation); Py_DECREF(odd_part); return result; } |