bpo-36957: Add _PyLong_Rshift() and _PyLong_Lshift(). (GH-13416)

This commit is contained in:
Serhiy Storchaka 2019-05-19 14:14:38 +03:00 committed by GitHub
parent 1d5bdef550
commit a5119e7d75
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 87 additions and 53 deletions

View file

@ -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;
}