[Ecls-list] Arrays to C and C to Arrays....

Juan Jose Garcia-Ripoll juanjose.garciaripoll at googlemail.com
Sun Nov 8 14:06:40 UTC 2009


On Sun, Nov 8, 2009 at 10:52 AM, Alexander Gavrilov
<angavrilov at gmail.com> wrote:
>> From: Brad Beer <bradwbeer at gmail.com>
>> > How can I convert C arrays to Lisp Arrays and vice-versa using only the FFI?
>
> On a somewhat related topic, I wonder if there is a lispy way to
> do efficient binary I/O of specialized floating point arrays?
> (Efficient here means the same kind of performance as you get
> by using fread/fwrite inside c-inline.) It seems that Clozure CL
> has a pair of non-standard methods to do fast untyped binary
> I/O with specialized vectors, but I couldn't find how to do it
> with ECL and SBCL. Maybe I didn't look well enough.

You did not find it because there was none. I have created an
unrelated function (EXT:ARRAY-RAW-DATA array) that serves for that
purpose and for more sophisticated stuff:

foo.lsp:

(with-open-file (s "foo.bin" :direction :output :if-exists :supersede
                   :element-type '(unsigned-byte 8))
 (let ((a (make-array 3 :initial-contents '(1.0d0 2.0d0 3.0d0)
                      :element-type 'double-float)))
   (write-sequence (ext:array-raw-data a) s)
   (format t "~&Written:~%~S" a)))

(with-open-file (s "foo.bin" :direction :input :element-type '(unsigned-byte 8))
 (let ((a (make-array 3 :initial-element 0.0d0
                      :element-type 'double-float)))
   (read-sequence (ext:array-raw-data a) s)
   (format t "~&Read:~%~S" a)))

> (load "foo")
;;; Loading #P"/Users/jjgarcia/foo.lsp"
Written:
#(1.0d0 2.0d0 3.0d0)
Read:
#(1.0d0 2.0d0 3.0d0)
#P"/Users/jjgarcia/foo.lsp"

As usual, available in CVS/git

Juanjo

-- 
Instituto de Física Fundamental, CSIC
c/ Serrano, 113b, Madrid 28006 (Spain)
http://juanjose.garciaripoll.googlepages.com




More information about the ecl-devel mailing list