File tree Expand file tree Collapse file tree 3 files changed +40
-5
lines changed
Expand file tree Collapse file tree 3 files changed +40
-5
lines changed Original file line number Diff line number Diff 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 [_ _]
Original file line number Diff line number Diff line change @@ -350,6 +350,26 @@ def ffi_type(self):
350350 return clibffi .cast_type_to_ffitype (rffi .INT )
351351CInt ()
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+
353373class CDouble (CType ):
354374 def __init__ (self ):
355375 CType .__init__ (self , u"pixie.stdlib.CDouble" )
Original file line number Diff line number Diff line change 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
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
You can’t perform that action at this time.
0 commit comments