From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id q35HpElI028810 for ; Thu, 5 Apr 2012 19:51:14 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AqsBANjafU9KfVI0imdsb2JhbAArGq9wAYh5CCIBAQEKCQ0HEgYjggkBAQEDARICExkBGx0BAwELBgULOyEBAREBBQEcBhMbB4ddAQMGBQspnAkKjBaCcYR3ChknDVeIdgEFC4oWhjkEiFmNEoERiiaDHT2EKQ X-IronPort-AV: E=Sophos;i="4.75,377,1330902000"; d="scan'208";a="152899946" Received: from mail-wg0-f52.google.com ([74.125.82.52]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 05 Apr 2012 19:51:09 +0200 Received: by wgbgn7 with SMTP id gn7so1688622wgb.9 for ; Thu, 05 Apr 2012 10:51:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=g03F/Z2bYhqnPB2AHbxzz57cvKb5+UWG183VASVQYvA=; b=UfZ48LbFuF+khrtsClWN4Op8BSoBNTDktQu84UiBt4AO/uaOxXh3onmPMttPnu2G7+ coYnBFOBcguU3sEGISLK6XDqM4NkMS0HzbUgG07kEby8vkv9Af5xBDjRH7RZWwYPxAXu voxb+d2F2z4/GqtlGhx829dfEXf23fHcTt2AM0+LFjR8i5QFibe51sSenxa6bozNHNnl ZH/jewta/i3+bHusHCZcs5DnU+zPfkBrr/w531cAcpzJnlRLMygU8aOExUwPXrFsu2N/ gcP2Ypw8eRsOytx7H1o6Hq6r4mFgV2HxvAhGNeUhcAsB8KETls9T9FSpetDRJA3RoymY GX8A== MIME-Version: 1.0 Received: by 10.216.134.24 with SMTP id r24mr2158677wei.84.1333648268992; Thu, 05 Apr 2012 10:51:08 -0700 (PDT) Received: by 10.223.6.25 with HTTP; Thu, 5 Apr 2012 10:51:08 -0700 (PDT) In-Reply-To: <201204051920.30187.monnier.florent@gmail.com> References: <201204051920.30187.monnier.florent@gmail.com> Date: Thu, 5 Apr 2012 11:51:08 -0600 Message-ID: From: Anthony Tavener To: Florent Monnier Cc: caml-list@inria.fr Content-Type: multipart/alternative; boundary=0016e6d77d7f5420ab04bcf22ff7 Subject: Re: [Caml-list] Size of Bigarray elements, or matching with 'kind'? --0016e6d77d7f5420ab04bcf22ff7 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Ah, yeah. That was an alternative I was considering, though I wasn't sure how to get the size of nativeint -- now I see: sizeof(value)! :) Still not the ideal, but probably a little nicer than using 'hash' to reveal abstracted values. ;) BTW, thank-you Florent, for your articles on interfacing with C... I know I've referenced them a few times. On Thu, Apr 5, 2012 at 11:20 AM, Florent Monnier wrote: > Le jeudi 05 avril 2012 07:57:45, Anthony Tavener a =E9crit : > > I'm trying to determine the size of a Bigarray, in bytes. > > > > I've tried several things... ending up with this as the only solution: > > > > -------------------------- > > let ba_char =3D Hashtbl.hash Bigarray.char;; > > let ba_int8_signed =3D Hashtbl.hash int8_signed;; > > let ba_int8_unsigned =3D Hashtbl.hash int8_unsigned;; > > let ba_int16_signed =3D Hashtbl.hash int16_signed;; > > let ba_int16_unsigned =3D Hashtbl.hash int16_unsigned;; > > let ba_int32 =3D Hashtbl.hash Bigarray.int32;; > > let ba_float32 =3D Hashtbl.hash float32;; > > let ba_int64 =3D Hashtbl.hash Bigarray.int64;; > > let ba_float64 =3D Hashtbl.hash float64;; > > let ba_complex32 =3D Hashtbl.hash complex32;; > > let ba_complex64 =3D Hashtbl.hash complex64;; > > let ba_int =3D Hashtbl.hash Bigarray.int;; > > let ba_nativeint =3D Hashtbl.hash Bigarray.nativeint;; > > > > let bigarray_bytes ba =3D > > let elt_bytes =3D match Hashtbl.hash (Array1.kind ba) with > > > > | x when x=3Dba_char || x=3Dba_int8_signed || x=3Dba_int8_unsigned = -> 1 > > | x when x=3Dba_int16_signed || x=3Dba_int16_unsigned ->= 2 > > | x when x=3Dba_int32 || x=3Dba_float32 ->= 4 > > | x when x=3Dba_int64 || x=3Dba_float64 || x=3Dba_complex32 = -> 8 > > | x when x=3Dba_complex64 -> 16 > > | x when x=3Dba_int || x=3Dba_nativeint -> > > > > Nativeint.size lsr 8 > > > > | _ -> failwith "Unknown Bigarray kind." > > > > in (Array1.dim ba) * elt_bytes;; > > -------------------------- > > > > Which is a rather ugly hack! It feels dirty like Obj.magic, while being > > completely inelegant, and even has a fail case! Can't get much worse. :) > > Of course, internally, bigarray will know it's own size... sometimes the > > price of abstraction... *sigh* > > > > I found a related Mantis entry from 6 years back: > > http://caml.inria.fr/mantis/view.php?id=3D3962 > > > > Well... does anyone have some better suggestions? Please? :) > > Oh, I can make it look a little prettier by plugging in the constants > eked > > out by Hashtbl.hash. ;D > > > > Why do I want this? Interfacing with OpenGL (glcaml). > > > > Thanks for looking, and more again if you have a good tip! > > Hi Anthony, > > Had the same problem with glMLite. > I use the function below, but if there is a better solution I would be > interested too: > > val ba_sizeof: ba:('a, 'b, Bigarray.c_layout) Bigarray.Array1.t -> int > > external elem_size: ba:('a, 'b, Bigarray.c_layout) Bigarray.Array1.t -> i= nt > =3D "ml_ba_elem_size" "noalloc" > > let ba_sizeof ~ba =3D > ((Bigarray.Array1.dim ba) * (elem_size ba)) ;; > > CAMLprim value ml_ba_elem_size( value _ba ) > { > struct caml_bigarray *ba =3D Bigarray_val(_ba); > int size; > switch (ba->flags & BIGARRAY_KIND_MASK) > { > case BIGARRAY_SINT8: > case BIGARRAY_UINT8: > size =3D 1; break; > > case BIGARRAY_SINT16: > case BIGARRAY_UINT16: > size =3D 2; break; > > case BIGARRAY_INT32: > case BIGARRAY_FLOAT32: > case BIGARRAY_COMPLEX32: > size =3D 4; break; > > case BIGARRAY_INT64: > case BIGARRAY_FLOAT64: > case BIGARRAY_COMPLEX64: > size =3D 8; break; > > case BIGARRAY_CAML_INT: > case BIGARRAY_NATIVE_INT: > size =3D sizeof(value); break; > } > return Val_int(size); > } > --0016e6d77d7f5420ab04bcf22ff7 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Ah, yeah. That was an alternative I was considering, though I wasn't su= re how to get the size of nativeint -- now I see: sizeof(value)! :)

=
Still not the ideal, but probably a little nicer than using 'hash&= #39; to reveal abstracted values. ;)

BTW, thank-you Florent, for your articles on interfacin= g with C... I know I've referenced them a few times.


On Thu, Apr 5, 2012 at 11:20 AM, Flor= ent Monnier <monnier.florent@gmail.com> wrote:
Le jeudi 05 avril 2012 07:57:45, Anthony Tav= ener a =E9crit :
> I'm trying to determine the size of a Bigar= ray, in bytes.
>
> I've tried several things... ending up with this as the only solut= ion:
>
> --------------------------
> let ba_char =3D Hashtbl.hash Bigarray.char;;
> let ba_int8_signed =3D Hashtbl.hash int8_signed;;
> let ba_int8_unsigned =3D Hashtbl.hash int8_unsigned;;
> let ba_int16_signed =3D Hashtbl.hash int16_signed;;
> let ba_int16_unsigned =3D Hashtbl.hash int16_unsigned;;
> let ba_int32 =3D Hashtbl.hash Bigarray.int32;;
> let ba_float32 =3D Hashtbl.hash float32;;
> let ba_int64 =3D Hashtbl.hash Bigarray.int64;;
> let ba_float64 =3D Hashtbl.hash float64;;
> let ba_complex32 =3D Hashtbl.hash complex32;;
> let ba_complex64 =3D Hashtbl.hash complex64;;
> let ba_int =3D Hashtbl.hash Bigarray.int;;
> let ba_nativeint =3D Hashtbl.hash Bigarray.nativeint;;
>
> let bigarray_bytes ba =3D
> =A0 let elt_bytes =3D match Hashtbl.hash (Array1.kind ba) with
>
> =A0 =A0 | x when x=3Dba_char || x=3Dba_int8_signed || x=3Dba_int8_unsi= gned -> 1
> =A0 =A0 | x when x=3Dba_int16_signed || x=3Dba_int16_unsigned =A0 =A0 = =A0 =A0 =A0 =A0-> 2
> =A0 =A0 | x when x=3Dba_int32 || x=3Dba_float32 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0-> 4
> =A0 =A0 | x when x=3Dba_int64 || x=3Dba_float64 || x=3Dba_complex32 = =A0 =A0 =A0 =A0-> 8
> =A0 =A0 | x when x=3Dba_complex64 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0-> 16
> =A0 =A0 | x when x=3Dba_int || x=3Dba_nativeint =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0->
>
> Nativeint.size lsr 8
>
> =A0 =A0 | _ -> failwith "Unknown Bigarray kind."
>
> =A0 in (Array1.dim ba) * elt_bytes;;
> --------------------------
>
> Which is a rather ugly hack! It feels dirty like Obj.magic, while bein= g
> completely inelegant, and even has a fail case! Can't get much wor= se. :)
> Of course, internally, bigarray will know it's own size... sometim= es the
> price of abstraction... *sigh*
>
> I found a related Mantis entry from 6 years back:
> http://caml.inria.fr/mantis/view.php?id=3D3962
>
> Well... does anyone have some better suggestions? Please? :)
> Oh, I can make it look a little prettier by plugging in the constants = eked
> out by Hashtbl.hash. ;D
>
> Why do I want this? Interfacing with OpenGL (glcaml).
>
> Thanks for looking, and more again if you have a good tip!

Hi Anthony,

Had the same problem with glMLite.
I use the function below, but if there is a better solution I would be
interested too:

val ba_sizeof: ba:('a, 'b, Bigarray.c_layout) Bigarray.Array1.t -&g= t; int

external elem_size: ba:('a, 'b, Bigarray.c_layout) Bigarray.Array1.= t -> int
=A0=3D "ml_ba_elem_size" "noalloc"

let ba_sizeof ~ba =3D
=A0((Bigarray.Array1.dim ba) * (elem_size ba)) ;;

CAMLprim value ml_ba_elem_size( value _ba )
{
=A0 =A0struct caml_bigarray *ba =3D Bigarray_val(_ba);
=A0 =A0int size;
=A0 =A0switch (ba->flags & BIGARRAY_KIND_MASK)
=A0 =A0{
=A0 =A0 =A0 =A0case BIGARRAY_SINT8:
=A0 =A0 =A0 =A0case BIGARRAY_UINT8:
=A0 =A0 =A0 =A0 =A0 =A0size =3D 1; break;

=A0 =A0 =A0 =A0case BIGARRAY_SINT16:
=A0 =A0 =A0 =A0case BIGARRAY_UINT16:
=A0 =A0 =A0 =A0 =A0 =A0size =3D 2; break;

=A0 =A0 =A0 =A0case BIGARRAY_INT32:
=A0 =A0 =A0 =A0case BIGARRAY_FLOAT32:
=A0 =A0 =A0 =A0case BIGARRAY_COMPLEX32:
=A0 =A0 =A0 =A0 =A0 =A0size =3D 4; break;

=A0 =A0 =A0 =A0case BIGARRAY_INT64:
=A0 =A0 =A0 =A0case BIGARRAY_FLOAT64:
=A0 =A0 =A0 =A0case BIGARRAY_COMPLEX64:
=A0 =A0 =A0 =A0 =A0 =A0size =3D 8; break;

=A0 =A0 =A0 =A0case BIGARRAY_CAML_INT:
=A0 =A0 =A0 =A0case BIGARRAY_NATIVE_INT:
=A0 =A0 =A0 =A0 =A0 =A0size =3D sizeof(value); break;
=A0 =A0}
=A0 =A0return Val_int(size);
}

--0016e6d77d7f5420ab04bcf22ff7--