caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: [Caml-list] Using 16 bits integers
@ 2001-07-04  2:21 Arturo Borquez
  0 siblings, 0 replies; 4+ messages in thread
From: Arturo Borquez @ 2001-07-04  2:21 UTC (permalink / raw)
  To: jarvarvar; +Cc: caml-list

On Tue, 03 July 2001, jarvarvar@airtel.net wrote:

> 
> Hello, I'm just new to OCaml. I'm writing a program wich uses a lot of
> 16 bits operations, and writes them to a buffer. For writing into the
> buffer, it is needed for the data to be in 16 bits, not in 32 bits.
> Exactly, the program is like a sound synthesizer, so data cannot be send
> to sound card in 32 bits.
> My problem with OCaml is: how can I manage 16 bits data, and make
> operations with it? I had not seen any type in Ocaml with 16bits lenght
> (int is 32 bits in my machine). This will frustate my project if I do
> not find a solution, so I will use C if there is no other solution...
> but I would not like to do if I can avoid it, because I like very much
> Ocaml.
> 
> I'm hopping your answers. Thanks.
> 
Hi,
Very simple. Do all your programm computations in
OCaml native ints (32 or 64 bit CPU) and when put
these ints into the buffer then 'convert' them to
16 bits.

Example:

let put_int16_into_buffer buffer position int_value =
  buffer.[pos] <- char_of_int (int_value land 0xff);
  buffer.[pos + 1] <- char_of_int (int_value lsr 8)

You must verify endianness of your CPU to fetch LH or
HL. The example shows the case of little-endian used
in all Intel CPU's. In a similar way you can retrieve
ints from a buffer

let get_int16_to_int_from_buffer buffer position =
  let int_value = (int_of_char buffer.[position] +
    ((int_of_char buffer.[position + 1]) * 256) in
  int_value

Best Regards
Arturo Borquez 





Find the best deals on the web at AltaVista Shopping!
http://www.shopping.altavista.com
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Using 16 bits integers
  2001-07-03 18:05 jarvarvar
  2001-07-03 18:21 ` Brian Rogoff
@ 2001-07-03 18:34 ` David McClain
  1 sibling, 0 replies; 4+ messages in thread
From: David McClain @ 2001-07-03 18:34 UTC (permalink / raw)
  To: jarvarvar, caml-list

You can consider adding a very small amount of C code to your OCaml project.
Why throw out the baby with the bath water? I do this sort of thing for
sound synthesis all the time with OCaml. It is a terrific platform for this
kind of work!

- DM

----- Original Message -----
From: <jarvarvar@airtel.net>
To: <caml-list@inria.fr>
Sent: Tuesday, July 03, 2001 11:05 AM
Subject: [Caml-list] Using 16 bits integers


> Hello, I'm just new to OCaml. I'm writing a program wich uses a lot of
> 16 bits operations, and writes them to a buffer. For writing into the
> buffer, it is needed for the data to be in 16 bits, not in 32 bits.
> Exactly, the program is like a sound synthesizer, so data cannot be send
> to sound card in 32 bits.
> My problem with OCaml is: how can I manage 16 bits data, and make
> operations with it? I had not seen any type in Ocaml with 16bits lenght
> (int is 32 bits in my machine). This will frustate my project if I do
> not find a solution, so I will use C if there is no other solution...
> but I would not like to do if I can avoid it, because I like very much
> Ocaml.
>
> I'm hopping your answers. Thanks.
>
> -------------------
> Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ:
http://caml.inria.fr/FAQ/
> To unsubscribe, mail caml-list-request@inria.fr  Archives:
http://caml.inria.fr

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Using 16 bits integers
  2001-07-03 18:05 jarvarvar
@ 2001-07-03 18:21 ` Brian Rogoff
  2001-07-03 18:34 ` David McClain
  1 sibling, 0 replies; 4+ messages in thread
From: Brian Rogoff @ 2001-07-03 18:21 UTC (permalink / raw)
  To: jarvarvar; +Cc: caml-list

>From the manual section on Bigarray:

Big arrays can only contain integers and floating-point numbers, while
Caml arrays can contain arbitrary Caml data types. However, big arrays
provide more space-efficient storage of integer and floating-point
elements, in particular because they support ``small'' types such as
single-precision floats and 8 and 16-bit integers, in addition to the
standard Caml types of double-precision floats and 32 and 64-bit integers. 

...

type int16_signed_elt
type int16_unsigned_elt

It's an interesting question in general as to what should be the basic
types of the language, should we have modular types, unicode characters, 
etc. I have similar issues in that a standard file format that I use a lot
specifies 32 bit integers and if I want to follow that exactly I need to
use the less performant int32 rather than the 31 bit int on 32 bit 
machines. Fortunately, that problem will be lessened when we enter the 
64 bit world. 

BTW, if you still find the need to go to C, you can use the Bigarray code 
as a template to start from so that you can link your C into OCaml.

-- Brian

On Tue, 3 Jul 2001 jarvarvar@airtel.net wrote:

> Hello, I'm just new to OCaml. I'm writing a program wich uses a lot of
> 16 bits operations, and writes them to a buffer. For writing into the
> buffer, it is needed for the data to be in 16 bits, not in 32 bits.
> Exactly, the program is like a sound synthesizer, so data cannot be send
> to sound card in 32 bits.
> My problem with OCaml is: how can I manage 16 bits data, and make
> operations with it? I had not seen any type in Ocaml with 16bits lenght
> (int is 32 bits in my machine). This will frustate my project if I do
> not find a solution, so I will use C if there is no other solution...
> but I would not like to do if I can avoid it, because I like very much
> Ocaml.
> 
> I'm hopping your answers. Thanks.
> 
> -------------------
> Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
> To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr
> 

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* [Caml-list] Using 16 bits integers
@ 2001-07-03 18:05 jarvarvar
  2001-07-03 18:21 ` Brian Rogoff
  2001-07-03 18:34 ` David McClain
  0 siblings, 2 replies; 4+ messages in thread
From: jarvarvar @ 2001-07-03 18:05 UTC (permalink / raw)
  To: caml-list

Hello, I'm just new to OCaml. I'm writing a program wich uses a lot of
16 bits operations, and writes them to a buffer. For writing into the
buffer, it is needed for the data to be in 16 bits, not in 32 bits.
Exactly, the program is like a sound synthesizer, so data cannot be send
to sound card in 32 bits.
My problem with OCaml is: how can I manage 16 bits data, and make
operations with it? I had not seen any type in Ocaml with 16bits lenght
(int is 32 bits in my machine). This will frustate my project if I do
not find a solution, so I will use C if there is no other solution...
but I would not like to do if I can avoid it, because I like very much
Ocaml.

I'm hopping your answers. Thanks.

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

end of thread, other threads:[~2001-07-04  2:21 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-07-04  2:21 [Caml-list] Using 16 bits integers Arturo Borquez
  -- strict thread matches above, loose matches on Subject: below --
2001-07-03 18:05 jarvarvar
2001-07-03 18:21 ` Brian Rogoff
2001-07-03 18:34 ` David McClain

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