Source file onehot_clean.ml
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
open! Base
open! Hardcaml
type 'a t =
{ any_bit_set : 'a
; data : 'a
}
[@@deriving hardcaml]
let scan_from_msb (type t) (module Bits : Comb.S with type t = t) (x : t) =
let open Bits in
let rec f x =
if width x = 1
then x, x
else (
let top, bot = split_in_half_msb x in
let c1, top = f top in
let c2, bot = f bot in
c1 |: c2, top @: (bot &: sresize ~:c1 (width bot)))
in
let any_bit_set, data = f x in
{ any_bit_set; data }
;;
let scan_from_lsb (type t) (module Bits : Comb.S with type t = t) (x : t) =
let open Bits in
let rec f x =
if width x = 1
then x, x
else (
let top, bot = split_in_half_msb x in
let c1, top = f top in
let c2, bot = f bot in
c1 |: c2, (top &: sresize ~:c2 (width top)) @: bot)
in
let any_bit_set, data = f x in
{ any_bit_set; data }
;;