caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Size of Bigarray elements, or matching with 'kind'?
@ 2012-04-05  5:57 Anthony Tavener
  2012-04-05 17:20 ` Florent Monnier
  0 siblings, 1 reply; 3+ messages in thread
From: Anthony Tavener @ 2012-04-05  5:57 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 2018 bytes --]

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!

 -Tony

[-- Attachment #2: Type: text/html, Size: 2748 bytes --]

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

end of thread, other threads:[~2012-04-05 17:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-04-05  5:57 [Caml-list] Size of Bigarray elements, or matching with 'kind'? Anthony Tavener
2012-04-05 17:20 ` Florent Monnier
2012-04-05 17:51   ` Anthony Tavener

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).