summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorMark Dickinson <mdickinson@enthought.com>2021-12-27 18:04:36 (GMT)
committerGitHub <noreply@github.com>2021-12-27 18:04:36 (GMT)
commit360fedc2d2ce6ccb0dab554ef45fe83f7aea1862 (patch)
treed05c41e34152d08b963691b2eff5d678bf5956a7 /Objects
parent2e3e0d23adca8d83722d939d6abd1e467d7578f7 (diff)
downloadcpython-360fedc2d2ce6ccb0dab554ef45fe83f7aea1862.zip
cpython-360fedc2d2ce6ccb0dab554ef45fe83f7aea1862.tar.gz
cpython-360fedc2d2ce6ccb0dab554ef45fe83f7aea1862.tar.bz2
bpo-46055: Streamline inner loop for right shifts (#30243)
Diffstat (limited to 'Objects')
-rw-r--r--Objects/longobject.c15
1 files changed, 8 insertions, 7 deletions
diff --git a/Objects/longobject.c b/Objects/longobject.c
index 77434e6..ddb3def 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -4491,7 +4491,7 @@ long_rshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift)
{
PyLongObject *z = NULL;
Py_ssize_t newsize, hishift, i, j;
- digit lomask, himask;
+ twodigits accum;
if (Py_SIZE(a) < 0) {
/* Right shifting negative numbers is harder */
@@ -4511,16 +4511,17 @@ long_rshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift)
if (newsize <= 0)
return PyLong_FromLong(0);
hishift = PyLong_SHIFT - remshift;
- lomask = ((digit)1 << hishift) - 1;
- himask = PyLong_MASK ^ lomask;
z = _PyLong_New(newsize);
if (z == NULL)
return NULL;
- for (i = 0, j = wordshift; i < newsize; i++, j++) {
- z->ob_digit[i] = (a->ob_digit[j] >> remshift) & lomask;
- if (i+1 < newsize)
- z->ob_digit[i] |= (a->ob_digit[j+1] << hishift) & himask;
+ j = wordshift;
+ accum = a->ob_digit[j++] >> remshift;
+ for (i = 0; j < Py_SIZE(a); i++, j++) {
+ accum |= (twodigits)a->ob_digit[j] << hishift;
+ z->ob_digit[i] = (digit)(accum & PyLong_MASK);
+ accum >>= PyLong_SHIFT;
}
+ z->ob_digit[i] = (digit)accum;
z = maybe_small_long(long_normalize(z));
}
return (PyObject *)z;