[pypy-commit] pypy default: fast path for 0 << n

cfbolz pypy.commits at gmail.com
Thu Jan 9 08:47:18 EST 2020


Author: Carl Friedrich Bolz-Tereick <cfbolz at gmx.de>
Branch: 
Changeset: r98496:5bc85c774501
Date: 2020-01-08 14:29 +0100
http://bitbucket.org/pypy/pypy/changeset/5bc85c774501/

Log:	fast path for 0 << n

diff --git a/rpython/rlib/rbigint.py b/rpython/rlib/rbigint.py
--- a/rpython/rlib/rbigint.py
+++ b/rpython/rlib/rbigint.py
@@ -1174,7 +1174,7 @@
     def lshift(self, int_other):
         if int_other < 0:
             raise ValueError("negative shift count")
-        elif int_other == 0:
+        elif int_other == 0 or self.sign == 0:
             return self
 
         # wordshift, remshift = divmod(int_other, SHIFT)
@@ -1183,8 +1183,6 @@
 
         if not remshift:
             # So we can avoid problems with eq, AND avoid the need for normalize.
-            if self.sign == 0:
-                return self
             return rbigint([NULLDIGIT] * wordshift + self._digits, self.sign, self.numdigits() + wordshift)
 
         oldsize = self.numdigits()
diff --git a/rpython/rlib/test/test_rbigint.py b/rpython/rlib/test/test_rbigint.py
--- a/rpython/rlib/test/test_rbigint.py
+++ b/rpython/rlib/test/test_rbigint.py
@@ -704,6 +704,10 @@
         # Chek value accuracy.
         assert rbigint.fromlong(18446744073709551615L).rshift(1).tolong() == 18446744073709551615L >> 1
 
+    def test_shift_optimization(self):
+        # does not crash with memory error
+        assert rbigint.fromint(0).lshift(sys.maxint).tolong() == 0
+
     def test_qshift(self):
         for x in range(10):
             for y in range(1, 161, 16):


More information about the pypy-commit mailing list