[Python-checkins] cpython: Divisions-by-two for a positive Py_ssize_t compile more cleanly with >>1 than

raymond.hettinger python-checkins at python.org
Mon Jul 20 06:25:57 CEST 2015


https://hg.python.org/cpython/rev/951552f602b2
changeset:   96954:951552f602b2
parent:      96950:4d3557500019
user:        Raymond Hettinger <python at rcn.com>
date:        Mon Jul 20 00:25:50 2015 -0400
summary:
  Divisions-by-two for a positive Py_ssize_t compile more cleanly with >>1 than /2.

files:
  Modules/_collectionsmodule.c |  2 +-
  Modules/_heapqmodule.c       |  6 +++---
  2 files changed, 4 insertions(+), 4 deletions(-)


diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c
--- a/Modules/_collectionsmodule.c
+++ b/Modules/_collectionsmodule.c
@@ -788,7 +788,7 @@
     block *rightblock = deque->rightblock;
     Py_ssize_t leftindex = deque->leftindex;
     Py_ssize_t rightindex = deque->rightindex;
-    Py_ssize_t n = Py_SIZE(deque) / 2;
+    Py_ssize_t n = Py_SIZE(deque) >> 1;
     Py_ssize_t i;
     PyObject *tmp;
 
diff --git a/Modules/_heapqmodule.c b/Modules/_heapqmodule.c
--- a/Modules/_heapqmodule.c
+++ b/Modules/_heapqmodule.c
@@ -66,7 +66,7 @@
 
     /* Bubble up the smaller child until hitting a leaf. */
     arr = _PyList_ITEMS(heap);
-    limit = endpos / 2;          /* smallest pos that has no child */
+    limit = endpos >> 1;         /* smallest pos that has no child */
     while (pos < limit) {
         /* Set childpos to index of smaller child.   */
         childpos = 2*pos + 1;    /* leftmost child position  */
@@ -347,7 +347,7 @@
        n is odd = 2*j+1, this is (2*j+1-1)/2 = j so j-1 is the largest,
        and that's again n//2-1.
     */
-    for (i = n/2 - 1 ; i >= 0 ; i--)
+    for (i = (n >> 1) - 1 ; i >= 0 ; i--)
         if (siftup_func((PyListObject *)heap, i))
             return NULL;
     Py_RETURN_NONE;
@@ -420,7 +420,7 @@
 
     /* Bubble up the smaller child until hitting a leaf. */
     arr = _PyList_ITEMS(heap);
-    limit = endpos / 2;          /* smallest pos that has no child */
+    limit = endpos >> 1;         /* smallest pos that has no child */
     while (pos < limit) {
         /* Set childpos to index of smaller child.   */
         childpos = 2*pos + 1;    /* leftmost child position  */

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list