[gambit-list] passing floats in Gambit-C's FFI

Marc Feeley feeley at iro.umontreal.ca
Sun Mar 16 11:02:16 EDT 2008


On 16-Mar-08, at 10:06 AM, Edward Tate wrote:

> I have this code in file "gl-object.ss":
>
> (c-define-type float-ptr (pointer float))
>
> (c-define-type gl-obj (struct "GLObject"))
> (c-define-type gl-obj-ptr (pointer gl-obj))
>
> (c-declare "extern void gl_obj_set_pos();")
>
> (define gl-obj-set-pos
>  (c-lambda (gl-obj-ptr float float float) void "gl_obj_set_pos"))
>
> which calls this C code in "gl_object.c":
>
> void gl_obj_set_pos(struct GLObject *pobj, float x, float y, float z)
> {
>  printf("got x: %1.1f, y: %1.1f, z: %1.1f\n", x, y, z);
>  gl_obj_mx_set(pobj, 3, 0, x);
>  gl_obj_mx_set(pobj, 3, 1, y);
>  gl_obj_mx_set(pobj, 3, 2, z);
> }

Does the Scheme code include the C prototype for gl_obj_set_pos ?   
Typically you would put that prototype in a C header file, for example:

/* File: "gl_object.h" */
void gl_obj_set_pos(struct GLObject *pobj, float x, float y, float z);

and then in your Scheme file you would have:

(c-declare "#include \"gl_object.h\"")

(c-define-type float-ptr (pointer float))

(c-define-type gl-obj (struct "GLObject"))
(c-define-type gl-obj-ptr (pointer gl-obj))

(c-declare "extern void gl_obj_set_pos();")

(define gl-obj-set-pos
  (c-lambda (gl-obj-ptr float float float) void "gl_obj_set_pos"))

In other words the c-lambda's types simply indicate the types of the  
*actual parameters* that are passed in the generated *call* to  
gl_obj_set_pos.  In principle your C function (or macro or inline C  
code) could have formal parameters of a different type (for example  
"int"), and the C compiler will do the appropriate conversions.  A  
prototype can't be generated automatically because then you couldn't  
call C macros.

As a matter of style, I would write:

(c-define-type float* (pointer float))

Marc




More information about the Gambit-list mailing list