Skip to content
This repository
Browse code

[feature] Int.t: introducing 64-bit integer type.

This type provides 64-bits (not 63, like int); also on 32-bit
architectures. It should be used instead of int for all values
that may exceed 32-bits to ensure that everything works fine
for the 32-bit port of Opa.
  • Loading branch information...
commit 4c151a992ec84644020fac79f05b80f12c801d92 1 parent 44d0185
Adam Koprowski akoprow authored

Showing 3 changed files with 110 additions and 0 deletions. Show diff stats Hide diff stats

  1. +1 0  libbase/_tags
  2. +59 0 libbase/int.ml
  3. +50 0 libbase/int.mli
1  libbase/_tags
@@ -9,6 +9,7 @@
9 9 <sgzip.ml> : use_zip
10 10 <xml.ml>: with_mlstate_debug
11 11 <time.ml>: with_mlstate_debug
  12 +<int.ml>: with_mlstate_debug
12 13
13 14 # This warnings are generated by the preprocessor : what a shame !
14 15 <indexer.ml>: warn_z
59 libbase/int.ml
... ... @@ -0,0 +1,59 @@
  1 +(*
  2 + Copyright © 2011 MLstate
  3 +
  4 + This file is part of OPA.
  5 +
  6 + OPA is free software: you can redistribute it and/or modify it under the
  7 + terms of the GNU Affero General Public License, version 3, as published by
  8 + the Free Software Foundation.
  9 +
  10 + OPA is distributed in the hope that it will be useful, but WITHOUT ANY
  11 + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12 + FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
  13 + more details.
  14 +
  15 + You should have received a copy of the GNU Affero General Public License
  16 + along with OPA. If not, see <http://www.gnu.org/licenses/>.
  17 +*)
  18 +
  19 +#<Ifstatic:OCAML_WORD_SIZE 64>
  20 +
  21 +type t = Nativeint.t
  22 +
  23 +let ( + ) = Nativeint.add
  24 +let ( - ) = Nativeint.sub
  25 +let ( * ) = Nativeint.mul
  26 +let ( / ) = Nativeint.div
  27 +
  28 +exception Overflow
  29 +let of_int = Nativeint.of_int
  30 +let to_int v =
  31 + let res = Nativeint.to_int v in
  32 + (* make sure that we don't loose the representation during the conversion *)
  33 + if v == of_int res then
  34 + res
  35 + else
  36 + raise Overflow
  37 +
  38 +#<Else>
  39 +
  40 +type t = Int64.t
  41 +
  42 +let ( + ) = Int64.add
  43 +let ( - ) = Int64.sub
  44 +let ( * ) = Int64.mul
  45 +let ( / ) = Int64.div
  46 +
  47 +let of_int v =
  48 + Int64.of_nativeint (Nativeint.of_int v)
  49 +
  50 +exception Overflow
  51 +let to_int v =
  52 + let res = Nativeint.to_int (Int64.to_nativeint v) in
  53 + (* make sure that we don't loose the representation during the conversion *)
  54 + if v == of_int res then
  55 + res
  56 + else
  57 + raise Overflow
  58 +
  59 +#<End>
50 libbase/int.mli
... ... @@ -0,0 +1,50 @@
  1 +(*
  2 + Copyright © 2011 MLstate
  3 +
  4 + This file is part of OPA.
  5 +
  6 + OPA is free software: you can redistribute it and/or modify it under the
  7 + terms of the GNU Affero General Public License, version 3, as published by
  8 + the Free Software Foundation.
  9 +
  10 + OPA is distributed in the hope that it will be useful, but WITHOUT ANY
  11 + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12 + FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
  13 + more details.
  14 +
  15 + You should have received a copy of the GNU Affero General Public License
  16 + along with OPA. If not, see <http://www.gnu.org/licenses/>.
  17 +*)
  18 +
  19 +(**
  20 + * This module provides 64-bit signed integers. It should be used in place
  21 + * of [int] when this extra bit is required (int's are 63-bit) *and* to make
  22 + * sure everything works on 32-bit platforms (where this type will still provide
  23 + * 64-bits).
  24 + * In short this should be used in place of int's when dealing with values
  25 + * that may exceed 31 bits.
  26 + * Performance notice: value of type Int.t will occupy more space and
  27 + * operations on them will generally be slower than over the int type.
  28 + *)
  29 +
  30 + (** A 64-bit integer (also on 32-bit platforms). *)
  31 +type t
  32 +
  33 + (** Addition *)
  34 +val ( + ): t -> t -> t
  35 +
  36 + (** Subtraction *)
  37 +val ( - ): t -> t -> t
  38 +
  39 + (** Multiplication *)
  40 +val ( * ): t -> t -> t
  41 +
  42 + (** Integer division *)
  43 +val ( / ): t -> t -> t
  44 +
  45 + (** Conversion from native integer *)
  46 +val of_int: int -> t
  47 +
  48 +exception Overflow
  49 + (** Conversion to native integer; throws Overflow if the value does not fit *)
  50 +val to_int: t -> int

0 comments on commit 4c151a9

Please sign in to comment.
Something went wrong with that request. Please try again.