Skip to content

Commit cfe5b6c

Browse files
committed
Divisions-by-two for a positive Py_ssize_t compile more cleanly with >>1 than /2.
1 parent 498b5e9 commit cfe5b6c

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

Modules/_collectionsmodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ deque_reverse(dequeobject *deque, PyObject *unused)
788788
block *rightblock = deque->rightblock;
789789
Py_ssize_t leftindex = deque->leftindex;
790790
Py_ssize_t rightindex = deque->rightindex;
791-
Py_ssize_t n = Py_SIZE(deque) / 2;
791+
Py_ssize_t n = Py_SIZE(deque) >> 1;
792792
Py_ssize_t i;
793793
PyObject *tmp;
794794

Modules/_heapqmodule.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ siftup(PyListObject *heap, Py_ssize_t pos)
6666

6767
/* Bubble up the smaller child until hitting a leaf. */
6868
arr = _PyList_ITEMS(heap);
69-
limit = endpos / 2; /* smallest pos that has no child */
69+
limit = endpos >> 1; /* smallest pos that has no child */
7070
while (pos < limit) {
7171
/* Set childpos to index of smaller child. */
7272
childpos = 2*pos + 1; /* leftmost child position */
@@ -347,7 +347,7 @@ heapify_internal(PyObject *heap, int siftup_func(PyListObject *, Py_ssize_t))
347347
n is odd = 2*j+1, this is (2*j+1-1)/2 = j so j-1 is the largest,
348348
and that's again n//2-1.
349349
*/
350-
for (i = n/2 - 1 ; i >= 0 ; i--)
350+
for (i = (n >> 1) - 1 ; i >= 0 ; i--)
351351
if (siftup_func((PyListObject *)heap, i))
352352
return NULL;
353353
Py_RETURN_NONE;
@@ -420,7 +420,7 @@ siftup_max(PyListObject *heap, Py_ssize_t pos)
420420

421421
/* Bubble up the smaller child until hitting a leaf. */
422422
arr = _PyList_ITEMS(heap);
423-
limit = endpos / 2; /* smallest pos that has no child */
423+
limit = endpos >> 1; /* smallest pos that has no child */
424424
while (pos < limit) {
425425
/* Set childpos to index of smaller child. */
426426
childpos = 2*pos + 1; /* leftmost child position */

0 commit comments

Comments
 (0)