Subject: ffi & float: what's wrong ?

From: Thibault Langlois <[email-masked]>
Date: Thu, 01 Apr 1999 10:59:56 +0100

Hello,
I trying to use clisp's ffi. I have some problems using floats.
The building process seem ok but the function that return float
dot not give expected results. Can anybody help me ?

Thibault Langlois

Here are the files:
ffitest.c:

int twice (int n) 
{ 
    return 2 * n;
}

float timespi (float n) 
{ 
    return 3.14 * n;
}

callffitest.lsp:
(use-package 'ffi)

(def-c-call-out twice
  (:name "twice")
  (:arguments (n int))
  (:return-type int))

(def-c-call-out timespi
  (:name "timespi")
  (:arguments (n single-float))
  (:return-type single-float))

Then I follow instructions from impnotes.html:

azimuth:/usr/local/src/clisp-1999-03-25/four$ clisp-link create-module-set 
ffitest callffitest.c
azimuth:/usr/local/src/clisp-1999-03-25/four$ gcc -c ffitest.c 
azimuth:/usr/local/src/clisp-1999-03-25/four$ cd ffitest
azimuth:/usr/local/src/clisp-1999-03-25/four/ffitest$ ln -s ../ffitest.o 
ffitest.o
azimuth:/usr/local/src/clisp-1999-03-25/four/ffitest$ cd ..
azimuth:/usr/local/src/clisp-1999-03-25/four$ base/lisp.run -M 
base/lispinit.mem -c callffitest.lsp
  i i i i i i i       ooooo    o        ooooooo   ooooo   ooooo 
  I I I I I I I      8     8   8           8     8     o  8    8
  I  \ `+' /  I      8         8           8     8        8    8
   \  `-+-'  /       8         8           8      ooooo   8oooo
    `-__|__-'        8         8           8           8  8
        |            8     o   8           8     o     8  8
  ------+------       ooooo    8oooooo  ooo8ooo   ooooo   8

Copyright (c) Bruno Haible, Michael Stoll 1992, 1993
Copyright (c) Bruno Haible, Marcus Daniels 1994-1997
Copyright (c) Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998
Copyright (c) Bruno Haible, Sam Steingold 1999

Compiling file /usr/local/src/clisp-1999-03-25/four/callffitest.lsp ...

Compilation of file /usr/local/src/clisp-1999-03-25/four/callffitest.lsp is 
finished.
0 errors, 0 warnings
Bye.
azimuth:/usr/local/src/clisp-1999-03-25/four$ clisp-link add-module-set ffitest 
base base+ffitest
gcc -O -W -Wswitch -Wcomment -Wpointer-arith -Wimplicit -Wreturn-type 
-fomit-frame-pointer -O2 -fexpensive-optimizations -DUNICODE -DDYNAMIC_FFI 
-I/usr/local/src/clisp-1999-03-25/four -c callffitest.c
In file included from callffitest.c:1:
/usr/local/src/clisp-1999-03-25/four/clisp.h:456: warning: register used for 
two global register variables
gcc -O -W -Wswitch -Wcomment -Wpointer-arith -Wimplicit -Wreturn-type 
-fomit-frame-pointer -O2 -fexpensive-optimizations -DUNICODE -DDYNAMIC_FFI 
-I/usr/local/src/clisp-1999-03-25/four -c modules.c
In file included from modules.d:11:
/usr/local/src/clisp-1999-03-25/four/clisp.h:456: warning: register used for 
two global register variables
gcc -O -W -Wswitch -Wcomment -Wpointer-arith -Wimplicit -Wreturn-type 
-fomit-frame-pointer -O2 -fexpensive-optimizations -DUNICODE -DDYNAMIC_FFI -x 
none modules.o callffitest.o ffitest.o lisp.a libsigsegv.a libintl.a 
libreadline.a libavcall.a libcallback.a -lncurses -ldl -o lisp.run
base+ffitest/lisp.run -M base/lispinit.mem -q -i -x (saveinitmem 
"base+ffitest/lispinit.mem")
832536 ;
524288
azimuth:/usr/local/src/clisp-1999-03-25/four$ base+ffitest/lisp.run -M 
base+ffitest/lispinit.mem -i callffitest
  i i i i i i i       ooooo    o        ooooooo   ooooo   ooooo 
  I I I I I I I      8     8   8           8     8     o  8    8
  I  \ `+' /  I      8         8           8     8        8    8
   \  `-+-'  /       8         8           8      ooooo   8oooo
    `-__|__-'        8         8           8           8  8
        |            8     o   8           8     o     8  8
  ------+------       ooooo    8oooooo  ooo8ooo   ooooo   8

Copyright (c) Bruno Haible, Michael Stoll 1992, 1993
Copyright (c) Bruno Haible, Marcus Daniels 1994-1997
Copyright (c) Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998
Copyright (c) Bruno Haible, Sam Steingold 1999

;; Loading file /usr/local/src/clisp-1999-03-25/four/callffitest.lsp ...
;; Loading of file /usr/local/src/clisp-1999-03-25/four/callffitest.lsp is 
finished.
[1]> (twice 5)
10                   ===========> correct
[2]> (timespi 2)

*** - 2 cannot be converted to the foreign type SINGLE-FLOAT
                     ===========> ok
1. Break [3]> 
[4]> (timespi 2.0)
0.0
                     ===========> why ?

I use a freshly compiled clisp (1999-03-25) on a GNU/Linux RedHat5.2 PC
with kernel 2.2.2

---

From: Bruno Haible <[email-masked]>
Date: Tue, 6 Apr 1999 12:04:24 +0200 (MET DST)

Thibault Langlois <[email-masked]> writes:
> ...

You compiled the function `timespi' with an ANSI C compiler, and
unfortunately `def-c-call-out' implies :language :c, which means
K&R C. K&R C and ANSI C differ in their calling convention for floats.
So, you have to write

  (def-call-out timespi
    (:name "timespi")
    (:language :stdc) ; means ANSI C
    (:arguments (n single-float))
    (:return-type single-float))

This is fixed in today's snapshot: def-c-call-out will now mean
:language :stdc.

                      Bruno

PS: Thanks for the complete example. Without being able to reproduce the
problem, I would have had no clue where to look.

---

From: Thibault Langlois <[email-masked]>
Date: Wed, 07 Apr 1999 19:02:48 +0100

Everything is working as expected now. Thanks a lot.

Thibault

| You compiled the function `timespi' with an ANSI C compiler, and
| unfortunately `def-c-call-out' implies :language :c, which means
| K&R C. K&R C and ANSI C differ in their calling convention for floats.
| So, you have to write
| 
|   (def-call-out timespi
|     (:name "timespi")
|     (:language :stdc) ; means ANSI C
|     (:arguments (n single-float))
|     (:return-type single-float))
| 
| This is fixed in today's snapshot: def-c-call-out will now mean
| :language :stdc.
| 
|                       Bruno
| 
| PS: Thanks for the complete example. Without being able to reproduce the
| problem, I would have had no clue where to look.