Author: stian
Branch: bigint-with-int
Changeset: r65915:8564d29c1285
Date: 2013-08-02 18:07 +0200
http://bitbucket.org/pypy/pypy/changeset/8564d29c1285/

Log:    Progress + tests

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
@@ -155,30 +155,30 @@
     return space.newbool(w_long1.num.ge(w_long2.num))
 
 def lt__Long_Int(space, w_long1, w_int2):
-    return space.newbool(w_long1.num.lt(rbigint.fromint(w_int2.intval)))
+    return space.newbool(w_long1.num.int_lt(w_int2.intval))
 def le__Long_Int(space, w_long1, w_int2):
-    return space.newbool(w_long1.num.le(rbigint.fromint(w_int2.intval)))
+    return space.newbool(w_long1.num.int_le(w_int2.intval))
 def eq__Long_Int(space, w_long1, w_int2):
-    return space.newbool(w_long1.num.eq(rbigint.fromint(w_int2.intval)))
+    return space.newbool(w_long1.num.int_eq(w_int2.intval))
 def ne__Long_Int(space, w_long1, w_int2):
-    return space.newbool(w_long1.num.ne(rbigint.fromint(w_int2.intval)))
+    return space.newbool(w_long1.num.int_ne(w_int2.intval))
 def gt__Long_Int(space, w_long1, w_int2):
-    return space.newbool(w_long1.num.gt(rbigint.fromint(w_int2.intval)))
+    return space.newbool(w_long1.num.int_gt(w_int2.intval))
 def ge__Long_Int(space, w_long1, w_int2):
-    return space.newbool(w_long1.num.ge(rbigint.fromint(w_int2.intval)))
+    return space.newbool(w_long1.num.int_ge(w_int2.intval))
 
 def lt__Int_Long(space, w_int1, w_long2):
-    return space.newbool(rbigint.fromint(w_int1.intval).lt(w_long2.num))
+    return space.newbool(w_long2.num.int_gt(w_int1.intval))
 def le__Int_Long(space, w_int1, w_long2):
-    return space.newbool(rbigint.fromint(w_int1.intval).le(w_long2.num))
+    return space.newbool(w_long2.num.int_ge(w_int1.intval))
 def eq__Int_Long(space, w_int1, w_long2):
-    return space.newbool(rbigint.fromint(w_int1.intval).eq(w_long2.num))
+    return space.newbool(w_long2.num.int_ne(w_int1.intval))
 def ne__Int_Long(space, w_int1, w_long2):
-    return space.newbool(rbigint.fromint(w_int1.intval).ne(w_long2.num))
+    return space.newbool(w_long2.num.int_eg(w_int1.intval))
 def gt__Int_Long(space, w_int1, w_long2):
-    return space.newbool(rbigint.fromint(w_int1.intval).gt(w_long2.num))
+    return space.newbool(w_long2.num.int_lt(w_int1.intval))
 def ge__Int_Long(space, w_int1, w_long2):
-    return space.newbool(rbigint.fromint(w_int1.intval).ge(w_long2.num))
+    return space.newbool(w_long2.num.int_le(w_int1.intval))
 
 
 def hash__Long(space, w_value):
diff --git a/rpython/rlib/rbigint.py b/rpython/rlib/rbigint.py
--- a/rpython/rlib/rbigint.py
+++ b/rpython/rlib/rbigint.py
@@ -482,6 +482,13 @@
             i += 1
         return True
 
+    @jit.elidable
+    def int_eq(self, other):
+        """ eq with int """
+        if self.numdigits() != 1 or self.digit(0) * self.sign != other:
+            return False
+        return True
+
     @jit.look_inside
     def ne(self, other):
         return not self.eq(other)
@@ -521,18 +528,52 @@
             i -= 1
         return False
 
+    @jit.elidable
+    def int_lt(self, other):
+        """ lt where other is an int """
+        if other >= 0 and self.sign < 0:
+            return True
+        elif other < 0 and self.sign >= 0:
+            return False
+        digits = self.numdigits()
+        if digits > 1:
+            if self.sign == 1 and other >= 0:
+                return False
+            else:
+                return True
+
+        d1 = self.sign * self.digit(0)
+        if d1 < other:
+            return True
+        return False
+
     @jit.look_inside
     def le(self, other):
         return not other.lt(self)
 
     @jit.look_inside
+    def int_le(self, other):
+        e = self.int_eq(other)
+        if e:
+            return True
+        return self.int_lt(other)
+
+    @jit.look_inside
     def gt(self, other):
         return other.lt(self)
 
     @jit.look_inside
+    def int_gt(self, other):
+        return not self.int_le(other)
+
+    @jit.look_inside
     def ge(self, other):
         return not self.lt(other)
 
+    @jit.look_inside
+    def int_ge(self, other):
+        return not self.int_lt(other)
+
     @jit.elidable
     def hash(self):
         return _hash(self)
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
@@ -320,6 +320,18 @@
         assert not f1.eq(f2)
         assert not f1.eq(f3)
 
+    def test_int_eq(self):
+        x = 5858
+        y = 58583
+        f1 = rbigint.fromlong(x)
+        f2 = rbigint.fromlong(-x)
+        f3 = rbigint.fromlong(y)
+        assert f1.int_eq(x)
+        assert f2.int_eq(-x)
+        assert f3.int_eq(y)
+        assert not f1.int_eq(-x)
+        assert not f1.int_eq(y)
+
     def test_eq_fastpath(self):
         x = 1234
         y = 1234
@@ -335,6 +347,15 @@
                 f2 = rbigint.fromlong(y)
                 assert (x < y) ==  f1.lt(f2)
 
+    def test_int_lt(self):
+        val = [0, 0x111111111111, 0x111111111112, 0x6FFFFFFF, 2**80]
+        short = [0, 0x111, 0x7FFFFFFF]
+        for x in gen_signs(val):
+            for y in gen_signs(short):
+                f1 = rbigint.fromlong(x)
+                assert (x < y) ==  f1.int_lt(y)
+                print "Pass (%d < %d) = %d" % (x, y, f1.int_lt(y))
+
     def test_order(self):
         f6 = rbigint.fromint(6)
         f7 = rbigint.fromint(7)
@@ -343,6 +364,13 @@
         assert (f6.gt(f6), f6.gt(f7), f7.gt(f6)) == (0,0,1)
         assert (f6.ge(f6), f6.ge(f7), f7.ge(f6)) == (1,0,1)
 
+    def test_int_order(self):
+        f6 = rbigint.fromint(6)
+        assert (f6.int_lt(6), f6.int_lt(7)) == (0,1)
+        assert (f6.int_le(6), f6.int_le(7)) == (1,1)
+        assert (f6.int_gt(6), f6.int_gt(7)) == (0,0)
+        assert (f6.int_ge(6), f6.int_ge(7)) == (1,0)
+
     def test_int_conversion(self):
         f1 = rbigint.fromlong(12332)
         f2 = rbigint.fromint(12332)
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to