-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathint64_overflow.ml
More file actions
28 lines (21 loc) · 762 Bytes
/
int64_overflow.ml
File metadata and controls
28 lines (21 loc) · 762 Bytes
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
26
27
28
open Big_int
type t = { value : int64; overflow : bool }
let ok i = { value = i; overflow = false }
exception Overflow
let with_overflow1 g f i =
let res = f i in
{ value = res
; overflow = not @@ eq_big_int (big_int_of_int64 res) (g @@ big_int_of_int64 i)
}
let with_overflow2 g f i j =
let res = f i j in
{ value = res
; overflow = not @@ eq_big_int (big_int_of_int64 res)
(g (big_int_of_int64 i) (big_int_of_int64 j))
}
let neg = with_overflow1 minus_big_int Int64.neg
let succ = with_overflow1 succ_big_int Int64.succ
let pred = with_overflow1 pred_big_int Int64.pred
let add = with_overflow2 add_big_int Int64.add
let sub = with_overflow2 sub_big_int Int64.sub
let mul = with_overflow2 mult_big_int Int64.mul