[pypy-svn] pypy jit-longlong: Finish writing the tests, and fixes (mostly of the kind "oups,

arigo commits-noreply at bitbucket.org
Sat Jan 8 16:35:40 CET 2011


Author: Armin Rigo <arigo at tunes.org>
Branch: jit-longlong
Changeset: r40495:bb911a48637a
Date: 2011-01-08 16:35 +0100
http://bitbucket.org/pypy/pypy/changeset/bb911a48637a/

Log:	Finish writing the tests, and fixes (mostly of the kind "oups, using
	r_longlongs means we'll get OverflowErrors when run untranslated).

diff --git a/pypy/jit/codewriter/support.py b/pypy/jit/codewriter/support.py
--- a/pypy/jit/codewriter/support.py
+++ b/pypy/jit/codewriter/support.py
@@ -226,14 +226,19 @@
 # long long support
 # -----------------
 
+def u_to_longlong(x):
+    return rffi.cast(lltype.SignedLongLong, x)
+
 def _ll_1_llong_is_true(xll):
     return bool(xll)
 
 def _ll_1_llong_neg(xll):
-    return -xll
+    y = -r_ulonglong(xll)
+    return u_to_longlong(y)
 
 def _ll_1_llong_invert(xll):
-    return ~xll
+    y = ~r_ulonglong(xll)
+    return u_to_longlong(y)
 
 def _ll_2_llong_lt(xll, yll):
     return xll < yll
@@ -266,25 +271,32 @@
     return xull >= yull
 
 def _ll_2_llong_add(xll, yll):
-    return xll + yll
+    z = r_ulonglong(xll) + r_ulonglong(yll)
+    return u_to_longlong(z)
 
 def _ll_2_llong_sub(xll, yll):
-    return xll - yll
+    z = r_ulonglong(xll) - r_ulonglong(yll)
+    return u_to_longlong(z)
 
 def _ll_2_llong_mul(xll, yll):
-    return xll * yll
+    z = r_ulonglong(xll) * r_ulonglong(yll)
+    return u_to_longlong(z)
 
 def _ll_2_llong_and(xll, yll):
-    return xll & yll
+    z = r_ulonglong(xll) & r_ulonglong(yll)
+    return u_to_longlong(z)
 
 def _ll_2_llong_or(xll, yll):
-    return xll | yll
+    z = r_ulonglong(xll) | r_ulonglong(yll)
+    return u_to_longlong(z)
 
 def _ll_2_llong_xor(xll, yll):
-    return xll ^ yll
+    z = r_ulonglong(xll) ^ r_ulonglong(yll)
+    return u_to_longlong(z)
 
 def _ll_2_llong_lshift(xll, y):
-    return xll << y
+    z = r_ulonglong(xll) << y
+    return u_to_longlong(z)
 
 def _ll_2_llong_rshift(xll, y):
     return xll >> y
@@ -296,7 +308,8 @@
     return r_longlong(x)
 
 def _ll_2_llong_from_two_ints(x_lo, x_hi):
-    return (r_longlong(x_hi) << 32) | r_longlong(r_uint(x_lo))
+    z = (r_ulonglong(x_hi) << 32) | r_ulonglong(r_uint(x_lo))
+    return u_to_longlong(z)
 
 def _ll_1_llong_to_int(xll):
     return intmask(xll)

diff --git a/pypy/jit/metainterp/blackhole.py b/pypy/jit/metainterp/blackhole.py
--- a/pypy/jit/metainterp/blackhole.py
+++ b/pypy/jit/metainterp/blackhole.py
@@ -298,6 +298,7 @@
             except JitException:
                 raise     # go through
             except Exception, e:
+                #import sys, pdb; pdb.post_mortem(sys.exc_info()[2])
                 lle = get_llexception(self.cpu, e)
                 self.handle_exception_in_frame(lle)
 

diff --git a/pypy/jit/codewriter/jtransform.py b/pypy/jit/codewriter/jtransform.py
--- a/pypy/jit/codewriter/jtransform.py
+++ b/pypy/jit/codewriter/jtransform.py
@@ -199,7 +199,6 @@
 
     rewrite_op_cast_pointer = rewrite_op_same_as
     rewrite_op_cast_opaque_ptr = rewrite_op_same_as   # rlib.rerased
-    def rewrite_op_cast_primitive(self, op): pass
     def rewrite_op_cast_bool_to_int(self, op): pass
     def rewrite_op_cast_bool_to_uint(self, op): pass
     def rewrite_op_cast_char_to_int(self, op): pass
@@ -875,6 +874,17 @@
                 return oplist
         ''' % (_op, _oopspec.lower(), _oopspec)).compile()
 
+    def rewrite_op_cast_primitive(self, op):
+        fromll = self._is_longlong(op.args[0].concretetype)
+        toll   = self._is_longlong(op.result.concretetype)
+        if fromll != toll:
+            if fromll:
+                opname = 'truncate_longlong_to_int'
+            else:
+                opname = 'cast_int_to_longlong'
+            op1 = SpaceOperation(opname, op.args, op.result)
+            return self.rewrite_operation(op1)
+
     # ----------
     # Renames, from the _old opname to the _new one.
     # The new operation is optionally further processed by rewrite_operation().

diff --git a/pypy/jit/backend/llgraph/llimpl.py b/pypy/jit/backend/llgraph/llimpl.py
--- a/pypy/jit/backend/llgraph/llimpl.py
+++ b/pypy/jit/backend/llgraph/llimpl.py
@@ -1082,7 +1082,7 @@
         if isinstance(x, r_longlong):
             return longlong2float(x)
         if isinstance(x, r_ulonglong):
-            return longlong2float(r_longlong(x))
+            return longlong2float(rffi.cast(lltype.SignedLongLong, x))
     raise TypeError(type(x))
 
 def cast_from_float(TYPE, x):

diff --git a/pypy/jit/metainterp/test/test_longlong.py b/pypy/jit/metainterp/test/test_longlong.py
--- a/pypy/jit/metainterp/test/test_longlong.py
+++ b/pypy/jit/metainterp/test/test_longlong.py
@@ -1,4 +1,4 @@
-from pypy.rlib.rarithmetic import r_longlong, r_uint, intmask
+from pypy.rlib.rarithmetic import r_longlong, r_ulonglong, r_uint, intmask
 from pypy.jit.metainterp.test.test_basic import LLJitMixin
 
 class WrongResult(Exception):
@@ -41,6 +41,7 @@
             compare(n, -6985, 346562560)
             compare(m, -5, 1474836480)
             if not n: raise WrongResult
+            if not r_longlong(m2): raise WrongResult
             if n-n: raise WrongResult
             compare(-n, 6984, -346562560)
             compare(~n, 6984, -346562561)
@@ -55,6 +56,12 @@
         def f(n1, n2):
             # n == -30000000000000
             n = (r_longlong(n1) << 32) | r_longlong(n2)
+            compare(n < n,  0, 0)
+            compare(n <= n, 0, 1)
+            compare(n == n, 0, 1)
+            compare(n != n, 0, 0)
+            compare(n >  n, 0, 0)
+            compare(n >= n, 0, 1)
             o = n + 2000000000
             compare(o, -6985, -1948404736)
             compare(n <  o, 0, 1)     # low word differs
@@ -65,6 +72,8 @@
             compare(n >= o, 0, 0)
             compare(o >  n, 0, 1)
             compare(o >= n, 0, 1)
+            compare(n == o, 0, 0)
+            compare(n != o, 0, 1)
             p = -o
             compare(n <  p, 0, 1)     # high word differs
             compare(n <= p, 0, 1)
@@ -74,9 +83,85 @@
             compare(n >= p, 0, 0)
             compare(p >  n, 0, 1)
             compare(p >= n, 0, 1)
+            compare(n == p, 0, 0)
+            compare(n != p, 0, 1)
             return 1
         self.interp_operations(f, [-6985, 346562560])
 
+    def test_binops(self):
+        def f(n1, n2, m1, m2, ii):
+            # n == -30000000000000, m == -20000000000, ii == 42
+            n = (r_longlong(n1) << 32) | r_longlong(n2)
+            m = (r_longlong(m1) << 32) | r_longlong(m2)
+            compare(n & m, -6989, 346562560)
+            compare(n | m, -1, 1474836480)
+            compare(n ^ m, 6988, 1128273920)
+            compare(n << 1, -13970, 693125120)
+            compare(r_longlong(5) << ii, 5120, 0)
+            compare(n >> 1, -3493, -1974202368)
+            compare(n >> 42, -1, -7)
+            return 1
+        self.interp_operations(f, [-6985, 346562560, -5, 1474836480, 42])
+
+    def test_floats(self):
+        def f(i):
+            # i == 1000000000
+            f = i * 123.5
+            n = r_longlong(f)
+            compare(n, 28, -1054051584)
+            return float(n)
+        res = self.interp_operations(f, [1000000000])
+        assert res == 123500000000.0
+
+    def test_unsigned_compare_ops(self):
+        def f(n1, n2):
+            # n == 30002000000000
+            n = (r_ulonglong(n1) << 32) | r_ulonglong(n2)
+            compare(n < n,  0, 0)
+            compare(n <= n, 0, 1)
+            compare(n == n, 0, 1)
+            compare(n != n, 0, 0)
+            compare(n >  n, 0, 0)
+            compare(n >= n, 0, 1)
+            o = n + 1000000000
+            compare(n <  o, 0, 1)     # low word differs
+            compare(n <= o, 0, 1)
+            compare(o <  n, 0, 0)
+            compare(o <= n, 0, 0)
+            compare(n >  o, 0, 0)
+            compare(n >= o, 0, 0)
+            compare(o >  n, 0, 1)
+            compare(o >= n, 0, 1)
+            compare(n == o, 0, 0)
+            compare(n != o, 0, 1)
+            p = ~n
+            compare(n <  p, 0, 1)     # high word differs
+            compare(n <= p, 0, 1)
+            compare(p <  n, 0, 0)
+            compare(p <= n, 0, 0)
+            compare(n >  p, 0, 0)
+            compare(n >= p, 0, 0)
+            compare(p >  n, 0, 1)
+            compare(p >= n, 0, 1)
+            compare(n == p, 0, 0)
+            compare(n != p, 0, 1)
+            return 1
+        self.interp_operations(f, [6985, 1653437440])
+
+    def test_unsigned_binops(self):
+        def f(n1, n2, ii):
+            # n == 30002000000000, ii == 42
+            n = (r_ulonglong(n1) << 32) | r_ulonglong(n2)
+            compare(n << 1, 13970, -988092416)
+            compare(r_ulonglong(5) << ii, 5120, 0)
+            compare(n >> 1, 3492, -1320764928)
+            compare(n >> 42, 0, 6)
+            p = ~n
+            compare(p >> 1, 2147480155, 1320764927)
+            compare(p >> 42, 0, 4194297)
+            return 1
+        self.interp_operations(f, [6985, 1653437440, 42])
+
     def test_long_long_field(self):
         from pypy.rlib.rarithmetic import r_longlong, intmask
         class A:


More information about the Pypy-commit mailing list