4 type 'a vect = bit list
5 type nibble = [`Four] vect
6 type byte7 = [`Seven] vect
7 type byte = [`Eight] vect
8 type word = [`Sixteen] vect
9 type word11 = [`Eleven] vect
11 type sizes = [ `Four | `Seven | `Eight | `Eleven | `Sixteen ]
13 let mk_nibble b1 b2 b3 b4 = [b1; b2; b3; b4]
16 [b1; b2; b3; b4] -> b1,b2,b3,b4
18 let mk_byte n1 n2 = n1 @ n2
19 let mk_byte_from_bits ((b1,b2,b3,b4),(b5,b6,b7,b8)) = ([b1;b2;b3;b4;b5;b6;b7;b8] : [`Eight] vect)
22 b1::b2::b3::b4::r -> [b1;b2;b3;b4],r
26 [b1;b2;b3;b4;b5;b6;b7;b8] -> (b1,b2,b3,b4),(b5,b6,b7,b8)
28 let mk_byte7 b1 b2 b3 n1 = b1::b2::b3::n1
31 b1::b2::b3::r -> b1,b2,b3,r
36 b1::b2::b3::b4::b5::b6::b7::b8::r -> [b1;b2;b3;b4;b5;b6;b7;b8],r
38 let mk_word11 = mk_byte7
39 let from_word11 = from_byte7
43 List.nth (List.rev l) index
44 with (Failure _ | Invalid_argument _) -> assert false
46 let set_bit l index new_val =
50 _, [] -> raise (Invalid_argument "")
51 | 0,_::tl -> new_val::tl
52 | n,hd::tl -> hd::(aux (n-1) tl) in
53 List.rev (aux index (List.rev l))
54 with Invalid_argument "" -> assert false
56 let (-&-) l1 l2 = List.map2 (fun b1 b2 -> b1 & b2) l1 l2
57 let (-|-) l1 l2 = List.map2 (fun b1 b2 -> b1 || b2) l1 l2
58 let xor b1 b2 = b1 <> b2
59 let (-^-) l1 l2 = List.map2 xor l1 l2
60 let complement l1 = List.map (not) l1
62 let iter_bits f v = String.concat "" (List.map f v)
63 let map_bits = List.map
64 let map2_bits = List.map2
66 let string_of_bit = function false -> "0" | true -> "1"
67 let string_of_vect l = String.concat "" (List.map string_of_bit l)
69 let full_add l r c = List.fold_right2 (fun b1 b2 (c,r) -> b1 & b2 || c & (b1 || b2),xor (xor b1 b2) c::r) l r (c,[])
70 let half_add l r = full_add l r false
76 [he;he;he;he;he;he;he;he] @ l
85 let l,res = split_last tl in
91 | _::tl -> tl @ [false]
92 let shift_right l = false :: snd (split_last l)
98 let he,tl = split_last l in
101 (* CSC: can overflow!!! *)
108 pow + (aux (pow * 2) tl)
128 let divide_with_remainder x y = (x / y, x mod y)
132 raise (Invalid_argument "Negative index")
134 let (d, r) = divide_with_remainder i 2 in
135 if (d, r) = (0, 0) then
142 let rec pad i l = if i = 0 then l else false :: (pad (i - 1) l)
144 let vect_of_int i size =
145 let big_list = List.rev (aux i) in
146 if List.length big_list > size_lookup size then
147 raise (Invalid_argument "BitVectors.vect_of_int: size not big enough")
149 let diff = size_lookup size - List.length big_list in
152 let zero size = pad (size_lookup size) []
154 (* CSC: can overflow!!! *)
155 (* CSC: only works properly with bytes!!! *)
156 let hex_string_of_vect v = Printf.sprintf "%0 2X" (int_of_vect v);;
159 Map.Make (struct type t = word let compare = compare end);;