[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 | |
---|
[92] | 40 | let add8_with_c (b1 : [`Eight] vect) (b2 : [`Eight] vect) (c : bit) = |
---|
[90] | 41 | let n1 = int_of_vect b1 in |
---|
| 42 | let n2 = int_of_vect b2 in |
---|
[92] | 43 | let c = int_of_bit c in |
---|
[28] | 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 |
---|
[92] | 49 | vect_of_int res `Eight,c,ac,ov |
---|
[28] | 50 | ;; |
---|
| 51 | |
---|
[91] | 52 | let subb8_with_c (b1 : [`Eight] vect) (b2 : [`Eight] vect) (c : bit) = |
---|
[90] | 53 | let n1 = int_of_vect b1 in |
---|
| 54 | let n2 = int_of_vect b2 in |
---|
[91] | 55 | let c = int_of_bit c in |
---|
[28] | 56 | let res = n1 - n2 - c in |
---|
| 57 | let ac = n1 mod 16 - n2 mod 16 - c < 0 in |
---|
| 58 | let c6 = n1 mod 128 - n2 mod 128 - c < 0 in |
---|
| 59 | let res,c = |
---|
| 60 | if res >= 0 then res,false |
---|
| 61 | else n1 + 256 - n2 - c, true in |
---|
| 62 | let ov = c <> c6 in |
---|
[90] | 63 | (vect_of_int res `Eight,c,ac,ov) |
---|
[28] | 64 | ;; |
---|
| 65 | |
---|
| 66 | let dec b = |
---|
[90] | 67 | let res = int_of_vect b - 1 in |
---|
| 68 | if res < 0 then vect_of_int 255 `Eight |
---|
| 69 | else vect_of_int res `Eight |
---|
[28] | 70 | ;; |
---|
| 71 | |
---|
| 72 | let inc b = |
---|
[90] | 73 | let res = int_of_vect b + 1 in |
---|
[92] | 74 | if res > 255 then (vect_of_int 0 `Eight : byte) |
---|
| 75 | else (vect_of_int res `Eight : byte) |
---|
[28] | 76 | ;; |
---|
[41] | 77 | |
---|
[42] | 78 | let byte7_of_bit b = |
---|
[91] | 79 | [false;false;false;false;false;false;b] |
---|
[41] | 80 | ;; |
---|
[42] | 81 | |
---|
[97] | 82 | let byte_of_byte7 = |
---|
| 83 | function |
---|
| 84 | ([b1;b2;b3]::n) -> [false;b1;b2;b3]::n |
---|
| 85 | | _ -> assert false |
---|
[88] | 86 | ;; |
---|