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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
type t = Bits of int [@@unboxed]
let[@inline] compare (Bits x) (Bits y) =
Int.compare x y
let get k (Bits n) =
let two_to_k = Int.shift_left Int.one (k - 1) in
if Int.equal (Int.logand two_to_k n) 0 then
false
else
true
let get_lane ~lane:l ~k bv =
get (k + 1 - l) bv
let get_right_of_lane ~lane:l ~k ~m bv =
get_lane ~lane:(l + m) ~k bv
let get_left_of_lane ~lane:l ~k ~m bv =
get_lane ~lane:(l - m) ~k bv
let rec pos_fold ~f ~init n =
match n with
| 0 -> init
| n -> pos_fold ~f ~init:(f n init) (n - 1)
let snoc_one (Bits n) =
let n' = Int.logor Int.one (Int.shift_left n 1) in
Bits n'
let snoc_ones (Bits n) ~m =
if m >= Sys.int_size then
Bits (Int.minus_one)
else
let one_m_zeros = Int.shift_left Int.one m in
let m_ones = one_m_zeros - 1 in
let n' = Int.logor m_ones (Int.shift_left n m) in
Bits n'
let ones ~m =
snoc_ones (Bits Int.zero) ~m
let snoc_zero (Bits n) =
Bits (Int.shift_left n 1)
let snoc_zeros (Bits n) ~m =
if m >= Sys.int_size then
Bits (Int.zero)
else
let n' = Int.shift_left n m in
Bits n'
let zero = (Bits Int.zero)
let one = (Bits Int.one)
let non_zero (Bits x) = not (Int.equal Int.zero x)
let logor (Bits x) (Bits y) = Bits (Int.logor x y)
let logand (Bits x) (Bits y) = Bits (Int.logand x y)
let shift_right_logical (Bits x) n = Bits (Int.shift_right_logical x n)
let shift_left (Bits x) n = Bits (Int.shift_left x n)
let pp_bv ppf (Bits n)=
Format.fprintf ppf "%o" n