Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/int63_emul.ml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ let to_unsigned_int x =
then to_int x
else invalid_arg "Int63.to_unsigned_int: %Lx can not fit into a 31 bits unsigned integer" x

let without_bit_sign (x:int) = if x >= 0 then x else x land (lnot 0x40000000)
(* [0x40000000] does not fit a 31-bit [int], where it is the sign bit; build it
from an [int64] so that nothing is truncated at compile time. *)
let sign_bit = Int64.to_int 0x40000000L

let without_bit_sign (x:int) = if x >= 0 then x else x land (lnot sign_bit)

let of_unsigned_int x =
if x < 0
Expand Down
6 changes: 5 additions & 1 deletion src/optint_emul.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ let of_int64 = Int64.to_int32

let pp ppf (x:t) = Format.fprintf ppf "%ld" x

let without_bit_sign (x:int) = if x >= 0 then x else x land (lnot 0x40000000)
(* [0x40000000] does not fit a 31-bit [int], where it is the sign bit; build it
from an [int32] so that nothing is truncated at compile time. *)
let sign_bit = Int32.to_int 0x40000000l

let without_bit_sign (x:int) = if x >= 0 then x else x land (lnot sign_bit)

let invalid_arg fmt = Format.kasprintf invalid_arg fmt

Expand Down
6 changes: 5 additions & 1 deletion src/optint_native.ml
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,15 @@ let equal : int -> int -> bool = fun a b -> a = b

let invalid_arg fmt = Format.kasprintf invalid_arg fmt

(* Reached only when [Sys.word_size = 64], so these masks assume an [int] wider
than 32 bits: [int32_sign_mask] and the [lsl 32] in [of_int32] are already
unspecified on a narrower one. They are computed rather than written as
literals so that a 31-bit backend has nothing to truncate. *)
let uint32_max = (0xffff lsl 16) lor 0xffff
let int32_sign_maskl = 0x80000000l
let int32_sign_mask = 1 lsl 31
let int32_maxl = 0x7fffffffl
let int32_max = 0x7fffffff
let int32_max = uint32_max lsr 1

let to_int32 x =
let truncated = x land uint32_max in
Expand Down
Loading