[pypy-svn] r40403 - in pypy/dist/pypy: rpython rpython/lltypesystem rpython/test translator/c/src

simonb at codespeak.net simonb at codespeak.net
Mon Mar 12 18:27:09 CET 2007


Author: simonb
Date: Mon Mar 12 18:27:07 2007
New Revision: 40403

Modified:
   pypy/dist/pypy/rpython/lltypesystem/lloperation.py
   pypy/dist/pypy/rpython/lltypesystem/opimpl.py
   pypy/dist/pypy/rpython/rfloat.py
   pypy/dist/pypy/rpython/test/test_rint.py
   pypy/dist/pypy/translator/c/src/float.h
Log:
allow rtyper to convert r_longlong to float (w help from richard)

Modified: pypy/dist/pypy/rpython/lltypesystem/lloperation.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/lloperation.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/lloperation.py	Mon Mar 12 18:27:07 2007
@@ -293,6 +293,7 @@
     'cast_int_to_longlong': LLOp(canfold=True),
     'cast_uint_to_int':     LLOp(canfold=True),
     'cast_uint_to_float':   LLOp(canfold=True),
+    'cast_longlong_to_float':LLOp(canfold=True),
     'cast_float_to_int':    LLOp(canraise=(OverflowError,)),
     'cast_float_to_uint':   LLOp(canfold=True),
     'truncate_longlong_to_int':LLOp(canfold=True),

Modified: pypy/dist/pypy/rpython/lltypesystem/opimpl.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/opimpl.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/opimpl.py	Mon Mar 12 18:27:07 2007
@@ -183,6 +183,13 @@
     assert type(u) is r_uint
     return float(u)
 
+def op_cast_longlong_to_float(i):
+    assert type(i) is r_longlong
+    # take first 31 bits
+    li = float(int(i & r_longlong(0x7fffffff)))
+    ui = float(int(i >> 31)) * float(0x80000000)
+    return ui + li
+
 def op_cast_int_to_char(b):
     assert type(b) is int
     return chr(b)

Modified: pypy/dist/pypy/rpython/rfloat.py
==============================================================================
--- pypy/dist/pypy/rpython/rfloat.py	(original)
+++ pypy/dist/pypy/rpython/rfloat.py	Mon Mar 12 18:27:07 2007
@@ -1,7 +1,7 @@
 from pypy.annotation.pairtype import pairtype
 from pypy.annotation import model as annmodel
 from pypy.rpython.lltypesystem.lltype import \
-     Signed, Unsigned, Bool, Float, Void, pyobjectptr
+     Signed, Unsigned, SignedLongLong, Bool, Float, Void, pyobjectptr
 from pypy.rpython.error import TyperError
 from pypy.rpython.rmodel import FloatRepr
 from pypy.rpython.rmodel import IntegerRepr, BoolRepr
@@ -173,6 +173,9 @@
         if r_from.lowleveltype == Signed and r_to.lowleveltype == Float:
             log.debug('explicit cast_int_to_float')
             return llops.genop('cast_int_to_float', [v], resulttype=Float)
+        if r_from.lowleveltype == SignedLongLong and r_to.lowleveltype == Float:
+            log.debug('explicit cast_longlong_to_float')
+            return llops.genop('cast_longlong_to_float', [v], resulttype=Float)
         return NotImplemented
 
 class __extend__(pairtype(FloatRepr, IntegerRepr)):

Modified: pypy/dist/pypy/rpython/test/test_rint.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rint.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rint.py	Mon Mar 12 18:27:07 2007
@@ -158,6 +158,25 @@
         assert type(res) is float
         assert res == 5.0
 
+    def test_float_conversion(self):
+        def f(ii):
+            return float(ii)
+        res = self.interpret(f, [r_longlong(100000000)])
+        assert type(res) is float
+        assert res == 100000000.
+        res = self.interpret(f, [r_longlong(1234567890123456789)])
+        assert type(res) is float
+        assert res == 1.2345678901234568e+18
+
+    def test_float_conversion_implicit(self):
+        def f(ii):
+            return 1.0 + ii
+        res = self.interpret(f, [r_longlong(100000000)])
+        assert type(res) is float
+        assert res == 100000001.
+        res = self.interpret(f, [r_longlong(1234567890123456789)])
+        assert type(res) is float
+        assert res == 1.2345678901234568e+18
 
     def test_rarithmetic(self):
         inttypes = [int, r_uint, r_longlong, r_ulonglong]

Modified: pypy/dist/pypy/translator/c/src/float.h
==============================================================================
--- pypy/dist/pypy/translator/c/src/float.h	(original)
+++ pypy/dist/pypy/translator/c/src/float.h	Mon Mar 12 18:27:07 2007
@@ -35,4 +35,5 @@
 #define OP_CAST_FLOAT_TO_UINT(x,r)   r = (unsigned long)(x)
 #define OP_CAST_INT_TO_FLOAT(x,r)    r = (double)(x)
 #define OP_CAST_UINT_TO_FLOAT(x,r)   r = (double)(x)
+#define OP_CAST_LONGLONG_TO_FLOAT(x,r) r = (double)(x)
 #define OP_CAST_BOOL_TO_FLOAT(x,r)   r = (double)(x)



More information about the Pypy-commit mailing list