From: Anthony Tavener <anthony.tavener@gmail.com>
To: Florent Monnier <monnier.florent@gmail.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Size of Bigarray elements, or matching with 'kind'?
Date: Thu, 5 Apr 2012 11:51:08 -0600 [thread overview]
Message-ID: <CAN=ouMR4q-f0LvscSAxQBNn4+YZU15hi66KAK6-4MMyPq=_+sg@mail.gmail.com> (raw)
In-Reply-To: <201204051920.30187.monnier.florent@gmail.com>
[-- Attachment #1: Type: text/plain, Size: 3985 bytes --]
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
<monnier.florent@gmail.com>wrote:
> Le jeudi 05 avril 2012 07:57:45, Anthony Tavener a écrit :
> > 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 = Hashtbl.hash Bigarray.char;;
> > let ba_int8_signed = Hashtbl.hash int8_signed;;
> > let ba_int8_unsigned = Hashtbl.hash int8_unsigned;;
> > let ba_int16_signed = Hashtbl.hash int16_signed;;
> > let ba_int16_unsigned = Hashtbl.hash int16_unsigned;;
> > let ba_int32 = Hashtbl.hash Bigarray.int32;;
> > let ba_float32 = Hashtbl.hash float32;;
> > let ba_int64 = Hashtbl.hash Bigarray.int64;;
> > let ba_float64 = Hashtbl.hash float64;;
> > let ba_complex32 = Hashtbl.hash complex32;;
> > let ba_complex64 = Hashtbl.hash complex64;;
> > let ba_int = Hashtbl.hash Bigarray.int;;
> > let ba_nativeint = Hashtbl.hash Bigarray.nativeint;;
> >
> > let bigarray_bytes ba =
> > let elt_bytes = match Hashtbl.hash (Array1.kind ba) with
> >
> > | x when x=ba_char || x=ba_int8_signed || x=ba_int8_unsigned -> 1
> > | x when x=ba_int16_signed || x=ba_int16_unsigned -> 2
> > | x when x=ba_int32 || x=ba_float32 -> 4
> > | x when x=ba_int64 || x=ba_float64 || x=ba_complex32 -> 8
> > | x when x=ba_complex64 -> 16
> > | x when x=ba_int || x=ba_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=3962
> >
> > 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 -> int
> = "ml_ba_elem_size" "noalloc"
>
> let ba_sizeof ~ba =
> ((Bigarray.Array1.dim ba) * (elem_size ba)) ;;
>
> CAMLprim value ml_ba_elem_size( value _ba )
> {
> struct caml_bigarray *ba = Bigarray_val(_ba);
> int size;
> switch (ba->flags & BIGARRAY_KIND_MASK)
> {
> case BIGARRAY_SINT8:
> case BIGARRAY_UINT8:
> size = 1; break;
>
> case BIGARRAY_SINT16:
> case BIGARRAY_UINT16:
> size = 2; break;
>
> case BIGARRAY_INT32:
> case BIGARRAY_FLOAT32:
> case BIGARRAY_COMPLEX32:
> size = 4; break;
>
> case BIGARRAY_INT64:
> case BIGARRAY_FLOAT64:
> case BIGARRAY_COMPLEX64:
> size = 8; break;
>
> case BIGARRAY_CAML_INT:
> case BIGARRAY_NATIVE_INT:
> size = sizeof(value); break;
> }
> return Val_int(size);
> }
>
[-- Attachment #2: Type: text/html, Size: 4871 bytes --]
prev parent reply other threads:[~2012-04-05 17:51 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-04-05 5:57 Anthony Tavener
2012-04-05 17:20 ` Florent Monnier
2012-04-05 17:51 ` Anthony Tavener [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAN=ouMR4q-f0LvscSAxQBNn4+YZU15hi66KAK6-4MMyPq=_+sg@mail.gmail.com' \
--to=anthony.tavener@gmail.com \
--cc=caml-list@inria.fr \
--cc=monnier.florent@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox