<html><head></head><body><div class="ydpc7c4638dyahoo-style-wrap" style="font-family:Helvetica Neue, Helvetica, Arial, sans-serif;font-size:13px;"><div dir="ltr" data-setdir="false"><div><div class="ydp5556b27cyahoo-style-wrap" style="font-family:Helvetica Neue, Helvetica, Arial, sans-serif;font-size:13px;"><div dir="ltr" data-setdir="false"><div><div dir="ltr">Greetings all,</div><div dir="ltr"><br></div><div dir="ltr" data-setdir="false">I first sent this message to SBCL-bugs, since the code works on CCL. where they suggested it's an issue in CFFI. I'm hoping that someone here has an idea of what the problem is.</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr">I'm
wrapping some of the complex math functions in openlibm, and encounter the following stack trace when calling them:</div><div dir="ltr"><br></div><div dir="ltr"><div>The value<br> NIL<br>is not of type<br> SB-SYS:SYSTEM-AREA-POINTER<br>when binding SB-ALIEN::VALUE</div><div><br></div><div>...<br></div><div><br></div></div><div> 0: (CFFI::LIBFFI/CALL #.(SB-SYS:INT-SAP #X027C0610) NIL #.(SB-SYS:INT-SAP #X0574FFE0) #.(SB-SYS:INT-SAP #X0574FFF0))<br> Locals:<br> AVALUES = #.(SB-SYS:INT-SAP #X0574FFF0)<br> FFI-CIF = #.(SB-SYS:INT-SAP #X027C0610)<br> FUNCTION = NIL<br> RVALUE = #.(SB-SYS:INT-SAP #X0574FFE0)</div><div><br></div><div dir="ltr"><br></div><div dir="ltr">The
FUNCTION parameter, which SBCL expects to be a INT-SAP, is NIL. All the
other (non complex number) functions work, but those going through the
foreign-memory functions fail.<br></div><div dir="ltr"><br></div><div dir="ltr">Openlibm
is is available on any linux distribution via the package manager, and
on MS Windows via MSYS2. This is with SBCL 2.0.10 on MS Windows 10.To
reproduce the issue:<br></div><div dir="ltr"><br></div><font face="lucida console, sans-serif">(ql:quickload '(:cffi :cffi-libffi))<br><br>(cffi:define-foreign-library libm<br> ;; (:windows (:or "libopenlibm"<br> ;; #.(cl:merge-pathnames "libopenlibm.dll" cl:*compile-file-pathname*)))<br> (t (:default "libopenlibm")))<br>(cffi:load-foreign-library 'libm)<br><br>#+unix<br>(cffi:define-foreign-library libm<br> (t (:default "libopenlibm")))<br><br><br>(cffi:defcstruct (complex-double-c :class complex-double-type)<br> (dat :double :count 2))<br><br>(cl:defmethod cffi:translate-into-foreign-memory ((value cl:complex) (type complex-double-type) p)<br> (cffi:with-foreign-slots ((dat) p (:struct complex-double-c))<br> (cl:setf (cffi:mem-aref dat :double 0) (cl:realpart value)<br> (cffi:mem-aref dat :double 1) (cl:imagpart value))))<br><br>(cl:defmethod cffi:translate-from-foreign (p (type complex-double-type))<br> (cffi:with-foreign-slots ((dat) p (:struct complex-double-c))<br> (cl:complex (cffi:mem-aref dat :double 0)<br> (cffi:mem-aref dat :double 1))))<br><br>(cffi:defcfun "cacos" (:struct complex-double-c) (c (:struct complex-double-c)))<br><br>;;; Try to call the function via CFFI<br>(cacos #C(0.16340015462715485d0 0.35359829928359165d0))<br><br>;;; Lower level version of the above<br>(cl:defun complex-acos (c)<br> (cffi:foreign-funcall "casos"<br> (:struct complex-double-c) c (:struct complex-double-c)))<br></font><div><font face="lucida console, sans-serif">(complex-acos #C(0.16340015462715485d0 0.35359829928359165d0))</font></div><div><br></div><font face="lucida console, sans-serif"><font face="Helvetica Neue, Helvetica, Arial, sans-serif">Any ideas?</font></font></div><div><br><br></div></div></div></div><div><br></div></div></div></body></html>