caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Richard Jones <rich@annexia.org>
To: "Michał Maciejewski" <michal.m.pl@gmail.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Operators for Int64 and Int32
Date: Thu, 3 Apr 2008 18:15:50 +0100	[thread overview]
Message-ID: <20080403171549.GA18505@annexia.org> (raw)
In-Reply-To: <906164100804030708p3e2788a0p29b69f4d46600622@mail.gmail.com>

On Thu, Apr 03, 2008 at 04:08:00PM +0200, Michał Maciejewski wrote:
> I'm quite new to OCaml but recently I had to write a wave file parsing
> application. I've decided to try OCaml instead of C++. The first
> problem I've encountered was of course too short native Integer type
> for representing some values in wave file header. So I was forced to
> use int64 type instead.

Yes, this is indeed an area where you have to be careful.  I'm writing
similar C-structure-parsing code and it is painful having to convert
between int/int32/int64 although of course there are good reasons for
it.

> The best solutions to those problem would be in my opinion to add
> something like this to standard library (to new module):
> 
> let ( +^^ ) a b = Int64.add a b
[etc.]

Yes, my code defines these operators too, although I picked slightly
different names for them, so perhaps it would be worth having these in
the stdlib if only because at least everyone would have the same name
for them :-)

Another thing you can do is to use functors to simplify writing code
that must work on int, int32 and int64 types.  eg take a basic
function like this:

  (* Check a value is in range 0 .. 2^bits-1. *)
  let range_unsigned v bits =
    let mask = lnot (mask bits) in
    (v land mask) = zero

The above function only works for ints.  But the same source code
could work for int32 as well, if you defined a few functions that you
need.  Note that the source of 'range_unsigned' is exactly the same
below as above:

  module I32 = struct
    let (<<) = Int32.shift_left
    let (>>) = Int32.shift_right_logical
    let (land) = Int32.logand
    let (lor) = Int32.logor
    let lnot = Int32.lognot
    let pred = Int32.pred
    let max_int = Int32.max_int
    let to_int = Int32.to_int
    let zero = Int32.zero
    let one = Int32.one
    let minus_one = Int32.minus_one
    let ff = 0xff_l
  
    (* Create a mask so many bits wide. *)
    let mask bits =
      if bits < 31 then
        pred (one << bits)
      else if bits = 31 then
        max_int
      else if bits = 32 then
        minus_one
      else
        invalid_arg "Bitmatch.I32.mask"

    (* Check a value is in range 0 .. 2^bits-1. *)
    let range_unsigned v bits =
      let mask = lnot (mask bits) in
      (v land mask) = zero
  end

(There's a few extra base functions in there because this example is
pulled from some real code which does the same thing over a collection
of bit-operating functions).

This can be converted into a functor relatively easily:

  module type IntegerType = sig
    type t
    val (<<) : t -> int -> t
    val (>>) : t -> int -> t
    val (land) : t -> t -> t
    val (lor) : t -> t -> t
    val lnot : t -> t
    val pred : t -> t
    val max_int : t
    val to_int : t -> int
    val zero : t
    val one : t
    val minus_one : t
    val ff : t
    val mask : int -> t
  end

  module type S = sig
    type t
    val range_unsigned : t -> int -> bool
  end

  module Make (I : IntegerType) : S with type t = I.t = struct
    include I
  
    (* Check a value is in range 0 .. 2^bits-1. *)
    let range_unsigned v bits =
      let mask = lnot (mask bits) in
      (v land mask) = zero
  end

And now you can use the functor to build int/int32/int64 versions of
the "range_unsigned" function automatically.  Of course this example
is a lot more worthwhile if you have a whole lot of these functions,
not just one :-)

  (* Make the module for int *)
  module I = Make (struct
    type t = int
    include Pervasives
    let (<<) = (lsl)
    let (>>) = (lsr)
    external to_int : int -> int = "%identity"
    let zero = 0
    let one = 1
    let minus_one = -1
    let ff = 0xff
  
    (* Create a mask so many bits wide. *)
    let mask bits =
      if bits < 30 then
        pred (one << bits)
      else if bits = 30 then
        max_int
      else if bits = 31 then
        minus_one
      else
        invalid_arg "Bitmatch.I.mask"
  end)

  (* Make the module for int32 *)
  module I32 = Make (struct
    include Int32
    let (<<) = Int32.shift_left
    let (>>) = Int32.shift_right_logical
    let (land) = Int32.logand
    let (lor) = Int32.logor
    let lnot = Int32.lognot
    let ff = 0xff_l
  
    (* Create a mask so many bits wide. *)
    let mask bits =
      if bits < 31 then
        pred (one << bits)
      else if bits = 31 then
        max_int
      else if bits = 32 then
        minus_one
      else
        invalid_arg "Bitmatch.I32.mask"
  end)

To check the functions have been defined:

  let () =
    ignore (I.range_unsigned 1 10);	(* the int version *)
    ignore (I32.range_unsigned 1_l 10); (* the int32 version *)

Rich.

-- 
Richard Jones
Red Hat


  parent reply	other threads:[~2008-04-03 17:15 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-04-03 14:08 Michał Maciejewski
2008-04-03 15:24 ` David Allsopp
2008-04-03 15:44   ` Bünzli Daniel
2008-04-03 19:00   ` David Thomas
2008-04-03 19:50   ` Michał Maciejewski
2008-04-03 22:22     ` Jon Harrop
2008-04-03 17:15 ` Richard Jones [this message]
2008-04-03 20:17 ` Erik de Castro Lopo
2008-04-03 20:39   ` Michał Maciejewski
2008-04-03 22:02     ` Erik de Castro Lopo
2008-04-03 22:17   ` Richard Jones
2008-04-04  1:47     ` Erik de Castro Lopo
2008-04-04 17:58       ` Adrien
2008-04-04 21:07         ` Richard Jones
2008-04-06 17:34           ` Adrien

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=20080403171549.GA18505@annexia.org \
    --to=rich@annexia.org \
    --cc=caml-list@inria.fr \
    --cc=michal.m.pl@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;
as well as URLs for NNTP newsgroup(s).