[pypy-svn] pypy jitypes2: implement float2longlong and longlong2float in C, and keep the old version just for untranslated tests

antocuni commits-noreply at bitbucket.org
Wed Dec 22 12:30:39 CET 2010


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: jitypes2
Changeset: r40178:01d8c931e032
Date: 2010-12-22 12:30 +0100
http://bitbucket.org/pypy/pypy/changeset/01d8c931e032/

Log:	implement float2longlong and longlong2float in C, and keep the old
	version just for untranslated tests

diff --git a/pypy/rlib/libffi.py b/pypy/rlib/libffi.py
--- a/pypy/rlib/libffi.py
+++ b/pypy/rlib/libffi.py
@@ -189,11 +189,12 @@
         func._push_longlong(self.floatval, ll_args, i)
 
 
+# -------- implement longlong2float and float2longlong --------
 DOUBLE_ARRAY_PTR = lltype.Ptr(lltype.Array(rffi.DOUBLE))
 LONGLONG_ARRAY_PTR = lltype.Ptr(lltype.Array(rffi.LONGLONG))
 
- at jit.dont_look_inside
-def longlong2float(llval):
+# these definitions are used only in tests, when not translated
+def longlong2float_emulator(llval):
     d_array = lltype.malloc(DOUBLE_ARRAY_PTR.TO, 1, flavor='raw')
     ll_array = rffi.cast(LONGLONG_ARRAY_PTR, d_array)
     ll_array[0] = llval
@@ -201,8 +202,7 @@
     lltype.free(d_array, flavor='raw')
     return floatval
 
- at jit.dont_look_inside
-def float2longlong(floatval):
+def float2longlong_emulator(floatval):
     d_array = lltype.malloc(DOUBLE_ARRAY_PTR.TO, 1, flavor='raw')
     ll_array = rffi.cast(LONGLONG_ARRAY_PTR, d_array)
     d_array[0] = floatval
@@ -210,6 +210,26 @@
     lltype.free(d_array, flavor='raw')
     return llval
 
+from pypy.translator.tool.cbuild import ExternalCompilationInfo
+eci = ExternalCompilationInfo(post_include_bits=["""
+static double pypy__longlong2float(long long x) {
+    return *((double*)&x);
+}
+static long long pypy__float2longlong(double x) {
+    return *((long long*)&x);
+}
+"""])
+
+longlong2float = rffi.llexternal(
+    "pypy__longlong2float", [rffi.LONGLONG], rffi.DOUBLE,
+    _callable=longlong2float_emulator, compilation_info=eci,
+    _nowrapper=True, pure_function=True)
+
+float2longlong = rffi.llexternal(
+    "pypy__float2longlong", [rffi.DOUBLE], rffi.LONGLONG,
+    _callable=float2longlong_emulator, compilation_info=eci,
+    _nowrapper=True, pure_function=True)
+
 
 # ======================================================================
 

diff --git a/pypy/rlib/test/test_libffi.py b/pypy/rlib/test/test_libffi.py
--- a/pypy/rlib/test/test_libffi.py
+++ b/pypy/rlib/test/test_libffi.py
@@ -52,11 +52,17 @@
         assert not ALLOCATED
 
     def test_longlong_as_float(self):
+        from pypy.translator.c.test.test_genc import compile
         maxint64 = r_longlong(9223372036854775807)
-        d = longlong2float(maxint64)
-        ll = float2longlong(d)
-        assert ll == maxint64
-        
+        def fn(x):
+            d = longlong2float(x)
+            ll = float2longlong(d)
+            return ll
+        assert fn(maxint64) == maxint64
+        #
+        fn2 = compile(fn, [r_longlong])
+        res = fn2(maxint64)
+        assert res == maxint64
 
 class TestLibffiCall(BaseFfiTest):
     """


More information about the Pypy-commit mailing list