Skip to content

Commit 9b556c0

Browse files
Merge pull request #460 from nilern/master
Add CFloat ffi type.
2 parents c0ebc25 + a7a81cd commit 9b556c0

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

pixie/ffi-infer.pxi

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,14 @@ return 0;
104104
(= (:type of-type) :function) (callback-type of-type in-struct?)
105105
:else 'pixie.stdlib/CVoidP))
106106

107+
(def float-types {32 'pixie.stdlib/CFloat
108+
64 'pixie.stdlib/CDouble})
109+
107110
(defmethod edn-to-ctype :float
108-
[{:keys [size]} _]
109-
(cond
110-
(= size 8) 'pixie.stdlib/CDouble
111-
:else (assert false "unknown type")))
111+
[{:keys [size] :as tp} _]
112+
(let [tp-found (get float-types (* 8 size))]
113+
(assert tp-found (str "No type found for " tp))
114+
tp-found))
112115

113116
(defmethod edn-to-ctype :void
114117
[_ _]

pixie/vm/libs/ffi.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,26 @@ def ffi_type(self):
350350
return clibffi.cast_type_to_ffitype(rffi.INT)
351351
CInt()
352352

353+
class CFloat(CType):
354+
def __init__(self):
355+
CType.__init__(self, u"pixie.stdlib.CFloat")
356+
357+
def ffi_get_value(self, ptr):
358+
casted = rffi.cast(rffi.FLOATP, ptr)
359+
return Float(rffi.cast(rffi.DOUBLE, casted[0]))
360+
361+
def ffi_set_value(self, ptr, val):
362+
val = to_float(val)
363+
casted = rffi.cast(rffi.FLOATP, ptr)
364+
casted[0] = rffi.cast(rffi.FLOAT, val.float_val())
365+
366+
def ffi_size(self):
367+
return rffi.sizeof(rffi.FLOAT)
368+
369+
def ffi_type(self):
370+
return clibffi.cast_type_to_ffitype(rffi.FLOAT)
371+
CFloat()
372+
353373
class CDouble(CType):
354374
def __init__(self):
355375
CType.__init__(self, u"pixie.stdlib.CDouble")

tests/pixie/tests/test-ffi.pxi

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns pixie.tests.test-ffi
22
(require pixie.test :as t)
3-
(require pixie.math :as m))
3+
(require pixie.math :as m)
4+
(require pixie.ffi-infer :as i))
45

56

67

@@ -36,6 +37,17 @@
3637
(t/deftest test-ffi-infer
3738
(t/assert= 0.5 (m/asin (m/sin 0.5))))
3839

40+
(t/deftest test-cdouble
41+
(i/with-config {:library "m"
42+
:cxx-flags ["-lm"]
43+
:includes ["math.h"]}
44+
(i/defcfn sinf)
45+
(i/defcfn asinf)
46+
(i/defcfn cosf)
47+
(i/defcfn powf))
48+
(t/assert= 0.5 (asinf (sinf 0.5)))
49+
(t/assert= 1.0 (+ (powf (sinf 0.5) 2.0) (powf (cosf 0.5) 2.0))))
50+
3951

4052
(t/deftest test-ffi-callbacks
4153
(let [MAX 255

0 commit comments

Comments
 (0)