caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Constructing all float tuples in C
@ 2005-08-05 14:16 brjackson
  2005-08-05 14:41 ` [Caml-list] " Daniel Bünzli
  0 siblings, 1 reply; 2+ messages in thread
From: brjackson @ 2005-08-05 14:16 UTC (permalink / raw)
  To: caml-list

Apologies to anybody that received a no subject message identical to that
which is below. It seems that I forgot to include it first time round.

Hi.

I've recently been experimenting with some Glu bindings for Ocaml. I am
aware of the excellent Lablgl but wanted something closer to the C API.
Unfortunately there seems to be a problem with a particular function that
returns a tuple of three floats. I think that I'm constructing the tuple
wrong since if it is changed so that it returns a bigarray it works.

With the following code gdb reports a crash in caml_format_float when
gluUnProject is called:

(* OCAML CODE *)
external gluUnProject : float -> float -> float ->
  (float, float64_elt, c_layout) Array1.t ->
  (float, float64_elt, c_layout) Array1.t ->
  (int, int_elt, c_layout) Array1.t ->
  (float * float * float) =
  "stub_gluUnProject_bytecode" "stub_gluUnProject_native"

/* C CODE */
value stub_gluUnProject_native(value v1, value v2, value v3, value v4,
value v5, value v6)
{
    CAMLparam5(v1, v2, v3, v4, v5);
    CAMLxparam1(v6);
    CAMLlocal1(result);
    result = alloc(Double_wosize * 3, Double_array_tag);
    double obj[3];

    fp_gluUnProject(Double_val(v1), Double_val(v2), Double_val(v3),
        (double *) Data_bigarray_val(v4), (double *) Data_bigarray_val(v5),
        (int *) Data_bigarray_val(v6), obj, obj+1, obj+2);

    Store_double_field(result, 0, obj[0]);
    Store_double_field(result, 1, obj[1]);
    Store_double_field(result, 2, obj[2]);

    CAMLreturn(result);
}


The values altered by fp_gluUnProject (obj[...]) are correct. If the code
is changed to the following (so that is returns a bigarray) it works:


(* OCAML CODE *)
external gluUnProject : float -> float -> float ->
  (float, float64_elt, c_layout) Array1.t ->
  (float, float64_elt, c_layout) Array1.t ->
  (int, int_elt, c_layout) Array1.t ->
  float, float64_elt, c_layout) Array1.t =
  "stub_gluUnProject_bytecode" "stub_gluUnProject_native"

/* C CODE */
value stub_gluUnProject_native(value v1, value v2, value v3, value v4,
value v5, value v6)
{
    CAMLparam5(v1, v2, v3, v4, v5);
    CAMLxparam1(v6);
    CAMLlocal1(result);
    double obj[3];

    fp_gluUnProject(Double_val(v1), Double_val(v2), Double_val(v3),
        (double *) Data_bigarray_val(v4), (double *) Data_bigarray_val(v5),
        (int *) Data_bigarray_val(v6), obj, obj+1, obj+2);

    result = alloc_bigarray_dims(BIGARRAY_FLOAT64 | BIGARRAY_C_LAYOUT,
            1, obj, 3);

    CAMLreturn(result);
}


I've have the feeling that the problem is either blindingly obvious or I
have misunderstood sections 18.3-18.5 of the manual.

Thanks for any insight/help.

Ben Jackson.


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] Constructing all float tuples in C
  2005-08-05 14:16 Constructing all float tuples in C brjackson
@ 2005-08-05 14:41 ` Daniel Bünzli
  0 siblings, 0 replies; 2+ messages in thread
From: Daniel Bünzli @ 2005-08-05 14:41 UTC (permalink / raw)
  To: brjackson; +Cc: caml-list

You are allocating an array of floats. But a tuple of floats is not  
an array of floats. In a tuple of floats, floats are boxed. Floats  
are unboxed only in records with float fields only or in arrays of  
floats.

So it should your code should be something like :

CAMLlocal1(t);
t = alloc_tuple (3);
Store_field (t, 0, copy_double(obj[0]));
Store_field (t, 1, copy_double(obj[1]));
Store_field (t, 2, copy_double(obj[2]));

Daniel


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2005-08-05 14:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-08-05 14:16 Constructing all float tuples in C brjackson
2005-08-05 14:41 ` [Caml-list] " Daniel Bünzli

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).