[pypy-commit] pypy math-improvements: Remove invert logic from rqshift (it is only used with positive numbers)

stian pypy.commits at gmail.com
Mon Nov 13 07:53:58 EST 2017


Author: stian
Branch: math-improvements
Changeset: r93002:3f4aca709e49
Date: 2017-11-13 13:53 +0100
http://bitbucket.org/pypy/pypy/changeset/3f4aca709e49/

Log:	Remove invert logic from rqshift (it is only used with positive
	numbers)

diff --git a/rpython/rlib/rbigint.py b/rpython/rlib/rbigint.py
--- a/rpython/rlib/rbigint.py
+++ b/rpython/rlib/rbigint.py
@@ -1314,38 +1314,22 @@
         wordshift = int_other / SHIFT
         loshift = int_other % SHIFT
         newsize = self.numdigits() - wordshift
-
-        invert = False
-        if self.sign == -1:
-            first = self.digit(0)
-            if first == 0:
-                a = self.invert().rqshift(int_other)
-                return a.invert()
-            invert = True
             
         if newsize <= 0:
-            if invert:
-                return ONENEGATIVERBIGINT
-            else:
-                return NULLRBIGINT
+            return NULLRBIGINT
                 
-        
         hishift = SHIFT - loshift
         z = rbigint([NULLDIGIT] * newsize, self.sign, newsize)
         i = 0
 
         while i < newsize:
             digit = self.udigit(wordshift)
-            if invert and i == 0 and wordshift == 0:
-                digit -= 1
             newdigit = (digit >> loshift)
             if i+1 < newsize:
                 newdigit |= (self.udigit(wordshift+1) << hishift)
             z.setdigit(i, newdigit)
             i += 1
-            wordshift += 1
-        if invert:
-            z.setdigit(0, z.digit(0)+1)      
+            wordshift += 1 
         z._normalize()
         return z
     rshift._always_inline_ = 'try' # It's so fast that it's always benefitial.
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
@@ -609,21 +609,18 @@
                     res1 = f1.lqshift(z).tolong() 
                     res2 = f1.rqshift(z).tolong() 
                     res3 = nf1.lqshift(z).tolong() 
-                    res4 = nf1.rqshift(z).tolong() 
+                     
                     
                     assert res1 == num << z
                     assert res2 == num >> z
                     assert res3 == -num << z
-                    assert res4 == -num >> z
                     
-        # Large digit, also invertion test.
+                    
+        # Large digit
         for x in range((1 << SHIFT) - 10, (1 << SHIFT) + 10):
             f1 = rbigint.fromlong(x)
-            nf1 = rbigint.fromlong(-x)
             assert f1.rqshift(SHIFT).tolong() == x >> SHIFT 
-            assert nf1.rqshift(SHIFT).tolong() == -x >> SHIFT
             assert f1.rqshift(SHIFT+1).tolong() == x >> (SHIFT+1)
-            assert nf1.rqshift(SHIFT+1).tolong() == -x >> (SHIFT+1)
                     
     def test_from_list_n_bits(self):
         for x in ([3L ** 30L, 5L ** 20L, 7 ** 300] +


More information about the pypy-commit mailing list