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

simonb at codespeak.net simonb at codespeak.net
Wed Mar 14 18:31:09 CET 2007


Author: simonb
Date: Wed Mar 14 18:31:06 2007
New Revision: 40493

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_rfloat.py
   pypy/dist/pypy/translator/c/src/float.h
Log:
allow r_longlong(somefloat)

Modified: pypy/dist/pypy/rpython/lltypesystem/lloperation.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/lloperation.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/lloperation.py	Wed Mar 14 18:31:06 2007
@@ -296,6 +296,7 @@
     'cast_longlong_to_float':LLOp(canfold=True),
     'cast_float_to_int':    LLOp(canraise=(OverflowError,)),
     'cast_float_to_uint':   LLOp(canfold=True),
+    'cast_float_to_longlong':LLOp(canfold=True),
     'truncate_longlong_to_int':LLOp(canfold=True),
 
     # __________ pointer operations __________

Modified: pypy/dist/pypy/rpython/lltypesystem/opimpl.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/opimpl.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/opimpl.py	Wed Mar 14 18:31:06 2007
@@ -210,6 +210,14 @@
     assert type(f) is float
     return r_uint(int(f))
 
+def op_cast_float_to_longlong(f):
+    assert type(f) is float
+    r = float(0x100000000)
+    small = f / r
+    high = int(small)
+    truncated = int((small - high) * r)
+    return r_longlong(high) * 0x100000000 + truncated
+
 def op_cast_char_to_int(b):
     assert type(b) is str and len(b) == 1
     return ord(b)

Modified: pypy/dist/pypy/rpython/rfloat.py
==============================================================================
--- pypy/dist/pypy/rpython/rfloat.py	(original)
+++ pypy/dist/pypy/rpython/rfloat.py	Wed Mar 14 18:31:06 2007
@@ -186,6 +186,9 @@
         if r_from.lowleveltype == Float and r_to.lowleveltype == Signed:
             log.debug('explicit cast_float_to_int')
             return llops.genop('cast_float_to_int', [v], resulttype=Signed)
+        if r_from.lowleveltype == Float and r_to.lowleveltype == SignedLongLong:
+            log.debug('explicit cast_float_to_longlong')
+            return llops.genop('cast_float_to_longlong', [v], resulttype=SignedLongLong)
         return NotImplemented
 
 class __extend__(pairtype(BoolRepr, FloatRepr)):

Modified: pypy/dist/pypy/rpython/test/test_rfloat.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rfloat.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rfloat.py	Wed Mar 14 18:31:06 2007
@@ -2,7 +2,7 @@
 from pypy.translator.translator import TranslationContext
 from pypy.rpython.test import snippet
 from pypy.rpython.test.tool import BaseRtypingTest, LLRtypeMixin, OORtypeMixin
-from pypy.rlib.rarithmetic import r_uint
+from pypy.rlib.rarithmetic import r_uint, r_longlong
 
 class TestSnippet(object):
 
@@ -60,6 +60,21 @@
         res = self.interpret(fn, [2.34])
         assert res == fn(2.34) 
 
+    def test_longlong_conversion(self):
+        def fn(f):
+            return r_longlong(f)
+
+        res = self.interpret(fn, [1.0])
+        assert res == 1
+        assert type(res) is r_longlong 
+        res = self.interpret(fn, [2.34])
+        assert res == fn(2.34) 
+        big = float(0x7fffffffffffffff)
+        x = big - 1.e10
+        assert x != big
+        y = fn(x)
+        assert fn(x) == 9223372026854775808
+
     def test_to_r_uint(self):
         def fn(x):
             return r_uint(x)

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	Wed Mar 14 18:31:06 2007
@@ -37,3 +37,7 @@
 #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)
+
+#ifdef HAVE_LONG_LONG
+#define OP_CAST_FLOAT_TO_LONGLONG(x,r) r = (long long)(x)
+#endif



More information about the Pypy-commit mailing list