[pypy-commit] pypy bigint-with-int: Fix a cornercase of eq where rbigint is two. Fix int_lt, make objectspace tests pass

stian noreply at buildbot.pypy.org
Sat Aug 3 22:37:09 CEST 2013


Author: stian
Branch: bigint-with-int
Changeset: r65938:db61d384e3e3
Date: 2013-08-03 18:02 +0200
http://bitbucket.org/pypy/pypy/changeset/db61d384e3e3/

Log:	Fix a cornercase of eq where rbigint is two. Fix int_lt, make
	objectspace tests pass

diff --git a/pypy/objspace/std/longobject.py b/pypy/objspace/std/longobject.py
--- a/pypy/objspace/std/longobject.py
+++ b/pypy/objspace/std/longobject.py
@@ -172,9 +172,9 @@
 def le__Int_Long(space, w_int1, w_long2):
     return space.newbool(w_long2.num.int_ge(w_int1.intval))
 def eq__Int_Long(space, w_int1, w_long2):
-    return space.newbool(w_long2.num.int_ne(w_int1.intval))
+    return space.newbool(not w_long2.num.int_ne(w_int1.intval))
 def ne__Int_Long(space, w_int1, w_long2):
-    return space.newbool(w_long2.num.int_eq(w_int1.intval))
+    return space.newbool(not w_long2.num.int_eq(w_int1.intval))
 def gt__Int_Long(space, w_int1, w_long2):
     return space.newbool(w_long2.num.int_lt(w_int1.intval))
 def ge__Int_Long(space, w_int1, w_long2):
diff --git a/rpython/rlib/rbigint.py b/rpython/rlib/rbigint.py
--- a/rpython/rlib/rbigint.py
+++ b/rpython/rlib/rbigint.py
@@ -485,8 +485,15 @@
     @jit.elidable
     def int_eq(self, other):
         """ eq with int """
-        if self.numdigits() != 1 or self.digit(0) * self.sign != other:
+        
+        if self.numdigits() > 2:
             return False
+        try:
+            if self.toint() != other:
+                return False
+        except OverflowError:
+            return False
+
         return True
 
     @jit.look_inside
@@ -536,13 +543,20 @@
     @jit.elidable
     def int_lt(self, other):
         """ lt where other is an int """
-        if other >= 0 and self.sign < 0:
+        osign = 1
+        if other == 0:
+            osign = 0
+        elif other < 0:
+            osign = -1
+ 
+        if self.sign > osign:
+            return False
+        elif self.sign < osign:
             return True
-        elif other < 0 and self.sign >= 0:
-            return False
+
         digits = self.numdigits()
         if digits > 1:
-            if self.sign == 1 and other >= 0:
+            if self.sign == 1 and other < 0:
                 return False
             else:
                 return True
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
@@ -569,6 +569,15 @@
                     res2 = getattr(operator, mod)(x, y)
                     assert res1 == res2
 
+    def test_int_bitwise(self):
+        for x in gen_signs([0, 1, 5, 11, 42, 43, 3 ** 30]):
+            for y in gen_signs([0, 1, 5, 11, 42, 4]):
+                lx = rbigint.fromlong(x)
+                for mod in "xor and_ or_".split():
+                    res1 = getattr(lx, "int_"+mod)(y).tolong()
+                    res2 = getattr(operator, mod)(x, y)
+                    assert res1 == res2
+
     def test_mul_eq_shift(self):
         p2 = rbigint.fromlong(1).lshift(63)
         f1 = rbigint.fromlong(0).lshift(63)
@@ -716,7 +725,7 @@
     def test_int_divmod(self):
         x = 12345678901234567890L
         for i in range(100):
-            y = randint(0, 1 << 60)
+            y = randint(0, 1 << 30)
             for sx, sy in (1, 1), (1, -1), (-1, -1), (-1, 1):
                 sx *= x
                 sy *= y


More information about the pypy-commit mailing list