1 | open BitVectors;; |
---|
2 | |
---|
3 | exception Byte7_conversion |
---|
4 | |
---|
5 | module type Map = |
---|
6 | sig |
---|
7 | type key |
---|
8 | type map |
---|
9 | val empty : map |
---|
10 | val find : key -> map -> byte |
---|
11 | val add : key -> byte -> map -> map |
---|
12 | end |
---|
13 | ;; |
---|
14 | |
---|
15 | module Byte7Map : Map with type key = byte7 = |
---|
16 | struct |
---|
17 | include Map.Make (struct type t = byte7 let compare = Pervasives.compare end) |
---|
18 | type map = byte t |
---|
19 | let find k m = |
---|
20 | try |
---|
21 | find k m |
---|
22 | with Not_found -> zero `Eight |
---|
23 | end;; |
---|
24 | |
---|
25 | module WordMap : Map with type key = word = |
---|
26 | struct |
---|
27 | include Map.Make (struct type t = word let compare = Pervasives.compare end) |
---|
28 | type map = byte t |
---|
29 | let find k m = |
---|
30 | try |
---|
31 | find k m |
---|
32 | with Not_found -> zero `Eight |
---|
33 | end;; |
---|
34 | |
---|
35 | let int_of_bit = |
---|
36 | function |
---|
37 | false -> 0 |
---|
38 | | true -> 1 |
---|
39 | |
---|
40 | let add8_with_c (b1 : [`Eight] vect) (b2 : [`Eight] vect) (c : bit) = |
---|
41 | let n1 = int_of_vect b1 in |
---|
42 | let n2 = int_of_vect b2 in |
---|
43 | let c = int_of_bit c in |
---|
44 | let res = n1 + n2 + c in |
---|
45 | let ac = n1 mod 16 + n2 mod 16 + c >= 16 in |
---|
46 | let c6 = n1 mod 128 + n2 mod 128 + c >= 128 in |
---|
47 | let res,c = res mod 256, res >= 256 in |
---|
48 | let ov = c <> c6 in |
---|
49 | vect_of_int res `Eight,c,ac,ov |
---|
50 | ;; |
---|
51 | |
---|
52 | let add16_with_c (b1 : [`Sixteen] vect) (b2 : [`Sixteen] vect) (c : bit) = |
---|
53 | let n1 = int_of_vect b1 in |
---|
54 | let n2 = int_of_vect b2 in |
---|
55 | let c = int_of_bit c in |
---|
56 | let res = n1 + n2 + c in |
---|
57 | let ac = n1 mod 256 + n2 mod 256 + c >= 256 in |
---|
58 | let c6 = n1 mod 2097152 + n2 mod 2097152 + c >= 2097152 in |
---|
59 | let res,c = res mod 4194304, res >= 4194304 in |
---|
60 | let ov = c <> c6 in |
---|
61 | vect_of_int res `Sixteen,c,ac,ov |
---|
62 | ;; |
---|
63 | |
---|
64 | let subb8_with_c (b1 : [`Eight] vect) (b2 : [`Eight] vect) (c : bit) = |
---|
65 | let n1 = int_of_vect b1 in |
---|
66 | let n2 = int_of_vect b2 in |
---|
67 | let c = int_of_bit c in |
---|
68 | let res = n1 - n2 - c in |
---|
69 | let ac = n1 mod 16 - n2 mod 16 - c < 0 in |
---|
70 | let c6 = n1 mod 128 - n2 mod 128 - c < 0 in |
---|
71 | let res,c = |
---|
72 | if res >= 0 then res,false |
---|
73 | else n1 + 256 - n2 - c, true in |
---|
74 | let ov = c <> c6 in |
---|
75 | (vect_of_int res `Eight,c,ac,ov) |
---|
76 | ;; |
---|
77 | |
---|
78 | let dec b = |
---|
79 | let res = int_of_vect b - 1 in |
---|
80 | if res < 0 then vect_of_int 255 `Eight |
---|
81 | else vect_of_int res `Eight |
---|
82 | ;; |
---|
83 | |
---|
84 | let inc b = |
---|
85 | let res = int_of_vect b + 1 in |
---|
86 | if res > 255 then (vect_of_int 0 `Eight : byte) |
---|
87 | else (vect_of_int res `Eight : byte) |
---|
88 | ;; |
---|
89 | |
---|
90 | let byte7_of_bit b = |
---|
91 | [false;false;false;false;false;false;b] |
---|
92 | ;; |
---|
93 | |
---|
94 | let byte_of_byte7 = |
---|
95 | function |
---|
96 | ([b1;b2;b3]::n) -> [false;b1;b2;b3]::n |
---|
97 | | _ -> assert false |
---|
98 | ;; |
---|