[pypy-svn] r79715 - in pypy/branch/jitypes2/pypy: module/_ffi module/_ffi/test rlib

antocuni at codespeak.net antocuni at codespeak.net
Wed Dec 1 15:37:25 CET 2010


Author: antocuni
Date: Wed Dec  1 15:37:23 2010
New Revision: 79715

Modified:
   pypy/branch/jitypes2/pypy/module/_ffi/interp_ffi.py
   pypy/branch/jitypes2/pypy/module/_ffi/test/test__ffi.py
   pypy/branch/jitypes2/pypy/rlib/libffi.py
Log:
add support for float arguments/return value also at applevel


Modified: pypy/branch/jitypes2/pypy/module/_ffi/interp_ffi.py
==============================================================================
--- pypy/branch/jitypes2/pypy/module/_ffi/interp_ffi.py	(original)
+++ pypy/branch/jitypes2/pypy/module/_ffi/interp_ffi.py	Wed Dec  1 15:37:23 2010
@@ -79,6 +79,8 @@
                 argchain.arg(intmask(space.uint_w(w_arg)))
             elif kind == 'f':
                 argchain.arg(space.float_w(w_arg))
+            elif kind == 's':
+                argchain.arg_singlefloat(space.float_w(w_arg))
             else:
                 assert False, "Argument kind '%s' not supported" % kind
         return argchain
@@ -95,6 +97,10 @@
         elif reskind == 'f':
             floatres = self.func.call(argchain, rffi.DOUBLE)
             return space.wrap(floatres)
+        elif reskind == 's':
+            # the result is a float, but widened to be inside a double
+            floatres = self.func.call(argchain, rffi.FLOAT)
+            return space.wrap(floatres)
         else:
             voidres = self.func.call(argchain, lltype.Void)
             assert voidres is None

Modified: pypy/branch/jitypes2/pypy/module/_ffi/test/test__ffi.py
==============================================================================
--- pypy/branch/jitypes2/pypy/module/_ffi/test/test__ffi.py	(original)
+++ pypy/branch/jitypes2/pypy/module/_ffi/test/test__ffi.py	Wed Dec  1 15:37:23 2010
@@ -45,6 +45,13 @@
         pow = libm.getpointer('pow', [], types.void)
         pow_addr = rffi.cast(rffi.LONG, pow.funcsym)
         cls.w_pow_addr = space.wrap(pow_addr)
+        #
+        # these are needed for test_single_float_args
+        from ctypes import c_float
+        f_12_34 = c_float(12.34).value
+        f_56_78 = c_float(56.78).value
+        f_result = c_float(f_12_34 + f_56_78).value
+        cls.w_f_12_34_plus_56_78 = space.wrap(f_result)
 
     def test_libload(self):
         import _ffi
@@ -119,7 +126,6 @@
                 return x+y;
             }
         """
-        import sys
         from _ffi import CDLL, types
         libfoo = CDLL(self.libfoo_name)
         sum_xy = libfoo.getfunc('sum_xy_us', [types.ushort, types.ushort],
@@ -127,6 +133,20 @@
         assert sum_xy(32000, 8000) == 40000
         assert sum_xy(60000, 30000) == 90000 % 65536
 
+    def test_single_float_args(self):
+        """
+            float sum_xy_float(float x, float y)
+            {
+                return x+y;
+            }
+        """
+        from _ffi import CDLL, types
+        libfoo = CDLL(self.libfoo_name)
+        sum_xy = libfoo.getfunc('sum_xy_float', [types.float, types.float],
+                                types.float)
+        res = sum_xy(12.34, 56.78)
+        assert res == self.f_12_34_plus_56_78
+
     def test_TypeError_numargs(self):
         from _ffi import CDLL, types
         libfoo = CDLL(self.libfoo_name)

Modified: pypy/branch/jitypes2/pypy/rlib/libffi.py
==============================================================================
--- pypy/branch/jitypes2/pypy/rlib/libffi.py	(original)
+++ pypy/branch/jitypes2/pypy/rlib/libffi.py	Wed Dec  1 15:37:23 2010
@@ -40,7 +40,7 @@
         """
         if   ffi_type is types.void:    return 'v'
         elif ffi_type is types.double:  return 'f'
-        elif ffi_type is types.float:   return 'f'
+        elif ffi_type is types.float:   return 's'
         elif ffi_type is types.pointer: return 'i'
         #
         elif ffi_type is types.schar:   return 'i'



More information about the Pypy-commit mailing list