From mboxrd@z Thu Jan 1 00:00:00 1970 X-Sympa-To: caml-list@inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id q35HKhun028351 for ; Thu, 5 Apr 2012 19:20:43 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApwBAHrTfU/RVdS0kGdsb2JhbAApGrh0CCIBAQEBCQkNBxQEI4IKAQEEEgITGQEbHQEDDAYFCzsjEQEFARw0B4ddAQMLBAcpoEkKjBaCcYR/ChknDVeBDgEFC5BPBJVrgRGNQz2EDA X-IronPort-AV: E=Sophos;i="4.75,377,1330902000"; d="scan'208";a="139233417" Received: from mail-wi0-f180.google.com ([209.85.212.180]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 05 Apr 2012 19:20:38 +0200 Received: by wibhm17 with SMTP id hm17so1379755wib.9 for ; Thu, 05 Apr 2012 10:20:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:subject:date:user-agent:references:in-reply-to:cc :mime-version:message-id:x-face:content-type :content-transfer-encoding; bh=UM3HqZOHSt7Ym87RBCneMknOokI9dVdx3Y7go3CikL4=; b=yN09igK3Mn8el5b/8ycdLGdSBcVV4Rj30XO2DeAhcRxSJ7mR18ku3hxN1mExp/vtQ9 EJZCScmtn8rmc04AEYWrI6mybJsmZ6GTK752L9Kq8R2X446nN/+t5kynNOuqJafXC5Pg tyKOqbFG+2rHThOd2zIbY4xrsbMxNb5O3Bl8lLO0v1sTlUFmMH/BTxLXFyijCb0kfRMp YedDi0zvX9mshk7uj8bk0lVojuQlEMb58mVhGx2ANdXRPgRfykNsWdAysuzLtL9uAkN6 yJrTW4CJLvmlpm1vdoaMto4V291eMfHrunmH+OKo6g0jlKjiigwOnQh1mkccl2iWbAVQ px7g== Received: by 10.216.136.232 with SMTP id w82mr2083202wei.119.1333646437872; Thu, 05 Apr 2012 10:20:37 -0700 (PDT) Received: from localhost.localnet (pat35-5-78-226-58-147.fbx.proxad.net. [78.226.58.147]) by mx.google.com with ESMTPS id l5sm15155482wia.11.2012.04.05.10.20.35 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 05 Apr 2012 10:20:36 -0700 (PDT) From: Florent Monnier To: caml-list@inria.fr Date: Thu, 5 Apr 2012 19:20:30 +0200 User-Agent: KMail/1.13.5 (Linux/2.6.33.7-desktop586-2mnb; KDE/4.4.5; i686; ; ) References: In-Reply-To: Cc: Anthony Tavener MIME-Version: 1.0 Message-Id: <201204051920.30187.monnier.florent@gmail.com> X-Face: -0"dKXwF0PiXr]fa$^)NJY7$;waqUckGcM7&q,VU?Xv\[=CiVM]g]pDs^xmfU9+Q=Z,OdfMHUR-7Ao%evJh.=aiq,#r0Ux0dm'!l|zeAXj||$>1_(Lv4Hc",&F}sbHeK0`SBA$_|XP Content-Type: Text/Plain; charset="iso-8859-15" Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by walapai.inria.fr id q35HKhun028351 X-Validation-by: monnier.florent@gmail.com Subject: Re: [Caml-list] Size of Bigarray elements, or matching with 'kind'? 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); }