Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
ocaml, int32/64, bigarray and unsigned values ...
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-04-11 (13:01)
From: Eric Cooper <ecc@c...>
Subject: Re: [Caml-list] ocaml, int32/64, bigarray and unsigned values ...
On Mon, Apr 11, 2005 at 09:46:19AM +0200, Sven Luther wrote:
> I had plans to do a rewrite of GNU parted, a project which i am
> involved with, in ocaml, and am being blocked by a few issues.
> [...]
>   1) most disk partition tables and filesystem have a mapping from a
>   given disk 512 byte sector to a descriptive structure.
>   [...]
>   or to have access functions which transform parts of
>   a byte array into values. The first one is ugly, as i was aiming
>   for a purely ocaml solution (so i can build and arch/plateform
>   independent bytecode tool), and the second would probably be a
>   disaster speed wise, and also somewhat ugly unless properly
>   encapsulated in an abstract module.

I would use the second approach.  I would define a logically
equivalent OCaml record or class, and conversion functions between
that object and a string + offset (or Bigarray of bytes, plus
offset).  Passing around an offset into a larger byte array can save a
lot of copying.

You can probably structure your code so that you only convert to/from
bytes in a few places, not likely to be performance-critical.

> Which brings me to the second problem.
>   2) Disk descriptors like partition table and filesystems, need to
>   have exact values, and the values are mostly unsigned 8, 16, 32 or
>   64 bit integers, strings and bit fields. The int64 and int32 offer
>   these kind of values, but only the signed version. Is it save to
>   make calculation on a signed number and ignoring the sign bit ?
>   Does this not cause risk of overflow ?

That's the beauty of 2's-complement representation of signed numbers.
The sign bit is just a consequence of which half of the values encode
negative numbers, from -1 (0xFF...FF) to min_int (0x80...00), so the
leading bit is the sign bit.  You can just do arithmetic and interpret
the results as unsigned.

>   Also, i believe that bit fields are not easily available, altough
>   there is some support in the Int32 and int64 bit-wise operators,
>   but again we have the signed vs unsigned problem, altough it is
>   maybe ignored for bit operations ?

You can do anything you need with shifting and masking.  That should
probably also be hidden in the bytearray-to-record conversion

It would be very cool to have such a "hard core" utility as a
disk partition editor in OCaml!

Eric Cooper             e c c @ c m u . e d u