blob: efcd3bd6cac017ec278e7ca0a7f95e0ad0c68815 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
(* "Hacker's Delight", section 2.12 *)
let ( + ) x y =
let z = x + y in
(* Overflow occurs iff x and y have same sign and z's sign is different *)
if (z lxor x) land (z lxor y) < 0
then raise Exc.IntOverflow
else z
let ( - ) x y =
let z = x - y in
(* Overflow occurs iff x and y have opposite signs and z and x have
opposite signs *)
if (x lxor y) land (z lxor x) < 0
then raise Exc.IntOverflow
else z
let ( * ) x y =
let z = x * y in
if (x = min_int && y < 0) || (y <> 0 && z / y <> x)
then raise Exc.IntOverflow
else z
let of_int32 = Safe32.to_int
let to_int32 = Safe32.of_int
|