[120] | 1 | open BitVectors;; |
---|
| 2 | |
---|
[44] | 3 | exception Byte7_conversion |
---|
[28] | 4 | |
---|
[120] | 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 | ;; |
---|
[28] | 14 | |
---|
[120] | 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 | |
---|
[91] | 35 | let int_of_bit = |
---|
| 36 | function |
---|
| 37 | false -> 0 |
---|
| 38 | | true -> 1 |
---|
[28] | 39 | |
---|
[91] | 40 | let byte7_of_byte b = |
---|
[97] | 41 | let n1,n2 = from_byte b in |
---|
| 42 | match from_nibble n1 with |
---|
| 43 | (false,b1,b2,b3) -> mk_byte7 b1 b2 b3 n2 |
---|
[91] | 44 | | _ -> raise Byte7_conversion |
---|
| 45 | |
---|
[92] | 46 | let add8_with_c (b1 : [`Eight] vect) (b2 : [`Eight] vect) (c : bit) = |
---|
[90] | 47 | let n1 = int_of_vect b1 in |
---|
| 48 | let n2 = int_of_vect b2 in |
---|
[92] | 49 | let c = int_of_bit c in |
---|
[28] | 50 | let res = n1 + n2 + c in |
---|
| 51 | let ac = n1 mod 16 + n2 mod 16 + c >= 16 in |
---|
| 52 | let c6 = n1 mod 128 + n2 mod 128 + c >= 128 in |
---|
| 53 | let res,c = res mod 256, res >= 256 in |
---|
| 54 | let ov = c <> c6 in |
---|
[92] | 55 | vect_of_int res `Eight,c,ac,ov |
---|
[28] | 56 | ;; |
---|
| 57 | |
---|
[91] | 58 | let subb8_with_c (b1 : [`Eight] vect) (b2 : [`Eight] vect) (c : bit) = |
---|
[90] | 59 | let n1 = int_of_vect b1 in |
---|
| 60 | let n2 = int_of_vect b2 in |
---|
[91] | 61 | let c = int_of_bit c in |
---|
[28] | 62 | let res = n1 - n2 - c in |
---|
| 63 | let ac = n1 mod 16 - n2 mod 16 - c < 0 in |
---|
| 64 | let c6 = n1 mod 128 - n2 mod 128 - c < 0 in |
---|
| 65 | let res,c = |
---|
| 66 | if res >= 0 then res,false |
---|
| 67 | else n1 + 256 - n2 - c, true in |
---|
| 68 | let ov = c <> c6 in |
---|
[90] | 69 | (vect_of_int res `Eight,c,ac,ov) |
---|
[28] | 70 | ;; |
---|
| 71 | |
---|
| 72 | let dec b = |
---|
[90] | 73 | let res = int_of_vect b - 1 in |
---|
| 74 | if res < 0 then vect_of_int 255 `Eight |
---|
| 75 | else vect_of_int res `Eight |
---|
[28] | 76 | ;; |
---|
| 77 | |
---|
| 78 | let inc b = |
---|
[90] | 79 | let res = int_of_vect b + 1 in |
---|
[92] | 80 | if res > 255 then (vect_of_int 0 `Eight : byte) |
---|
| 81 | else (vect_of_int res `Eight : byte) |
---|
[28] | 82 | ;; |
---|
[41] | 83 | |
---|
[42] | 84 | let byte7_of_bit b = |
---|
[91] | 85 | [false;false;false;false;false;false;b] |
---|
[41] | 86 | ;; |
---|
[42] | 87 | |
---|
[97] | 88 | let byte_of_byte7 = |
---|
| 89 | function |
---|
| 90 | ([b1;b2;b3]::n) -> [false;b1;b2;b3]::n |
---|
| 91 | | _ -> assert false |
---|
[88] | 92 | ;; |
---|