Skip to content

Commit 056495d

Browse files
committed
bpo-46055: Streamline inner loop for right shifts
1 parent cf15419 commit 056495d

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

Objects/longobject.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4491,7 +4491,6 @@ long_rshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift)
44914491
{
44924492
PyLongObject *z = NULL;
44934493
Py_ssize_t newsize, hishift, i, j;
4494-
digit lomask, himask;
44954494

44964495
if (Py_SIZE(a) < 0) {
44974496
/* Right shifting negative numbers is harder */
@@ -4511,16 +4510,17 @@ long_rshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift)
45114510
if (newsize <= 0)
45124511
return PyLong_FromLong(0);
45134512
hishift = PyLong_SHIFT - remshift;
4514-
lomask = ((digit)1 << hishift) - 1;
4515-
himask = PyLong_MASK ^ lomask;
45164513
z = _PyLong_New(newsize);
45174514
if (z == NULL)
45184515
return NULL;
4519-
for (i = 0, j = wordshift; i < newsize; i++, j++) {
4520-
z->ob_digit[i] = (a->ob_digit[j] >> remshift) & lomask;
4521-
if (i+1 < newsize)
4522-
z->ob_digit[i] |= (a->ob_digit[j+1] << hishift) & himask;
4516+
j = wordshift;
4517+
digit next = a->ob_digit[j++];
4518+
for (i = 0; j < Py_SIZE(a); i++, j++) {
4519+
digit high = next >> remshift;
4520+
next = a->ob_digit[j];
4521+
z->ob_digit[i] = (high | next << hishift) & PyLong_MASK;
45234522
}
4523+
z->ob_digit[i] = next >> remshift;
45244524
z = maybe_small_long(long_normalize(z));
45254525
}
45264526
return (PyObject *)z;

0 commit comments

Comments
 (0)