Changeset 64
- Timestamp:
- Sep 13, 2010, 12:59:27 PM (10 years ago)
- Location:
- Deliverables/D4.1
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
Deliverables/D4.1/Bit.ml
r63 r64 22 22 val (-^-): bit -> bit -> bit 23 23 val not: bit -> bit 24 25 24 end;; 26 25 -
Deliverables/D4.1/Byte.ml
r63 r64 30 30 val get_bit_at: int -> byte -> bit 31 31 val set_bit_at: int -> bit -> byte -> byte option 32 *) 32 33 33 34 val (-&-): byte -> byte -> byte 34 35 val (-|-): byte -> byte -> byte 35 36 val (-^-): byte -> byte -> byte 37 (* 38 val rotate_right: byte -> byte 39 val rotate_left: byte -> byte 40 val shift_right: byte -> byte 41 val shift_left: byte -> byte 42 *) 36 43 val not: byte -> byte 37 44 45 (* 38 46 val map_nibble: (nibble -> nibble) -> byte -> byte 39 47 val map_bit: (bit -> bit) -> byte -> byte … … 42 50 val zip_nibble: (nibble -> nibble -> nibble) -> byte -> byte -> byte 43 51 val zip_bit: (bit -> bit -> bit) -> byte -> byte -> byte 44 val pretty: byte -> string52 val to_string: byte -> string 45 53 *) 46 54 end 47 55 48 module Byte(Nibble: NIBBLE): BYTE 49 with type bit = Nibble.bit and type nibble = Nibble.nibble = 56 module Byte(Bit: BIT) (Nibble: NIBBLE): BYTE 57 with type Nibble.bit = Bit.bit 58 and type nibble = Nibble.nibble = 50 59 struct 51 type bit = Nibble.bit60 type bit = Bit.bit 52 61 type nibble = Nibble.nibble 53 62 type byte = nibble * nibble 54 63 55 let from_bits b1 b2 b3 b4 b5 b6 b7 b8 = 56 let nibble 64 let (-&-) (l1, l2) (r1, r2) = (Nibble.(-&-) l1 r1, Nibble.(-&-) l2 r2) 65 let (-|-) (l1, l2) (r1, r2) = (Nibble.(-|-) l1 r1, Nibble.(-|-) l2 r2) 66 let (-^-) (l1, l2) (r1, r2) = (Nibble.(-^-) l1 r1, Nibble.(-^-) l2 r2) 67 (* 68 let shift_right (n1, n2) = 69 let (b1, b2, b3, b4) = Nibble.to_bits n1 in 70 let (b5, b6, b7, b8) = Nibble.to_bits n2 in 71 let new_n1 = Nibble.from_bits (Bit.from_bool false) b1 b2 b3 in 72 let new_n2 = Nibble.from_bits b4 b5 b6 b7 in 73 (new_n1, new_n2) 74 let shift_left (n1, n2) = 75 let (b1, b2, b3, b4) = Nibble.to_bits n1 in 76 let (b5, b6, b7, b8) = Nibble.to_bits n2 in 77 let new_n1 = Nibble.from_bits b1 b2 b3 b4 in 78 let new_n2 = Nibble.from_bits b5 b6 b7 (Bit.from_bool false) in 79 (new_n1, new_n2) 80 let rotate_right (n1, n2) = 81 let (b1, b2, b3, b4) = Nibble.to_bits n1 in 82 let (b5, b6, b7, b8) = Nibble.to_bits n2 in 83 let new_n1 = Nibble.from_bits b8 b1 b2 b3 in 84 let new_n2 = Nibble.from_bits b4 b5 b6 b7 in 85 (new_n1, new_n2) 86 let rotate_left (n1, n2) = 87 let (b1, b2, b3, b4) = Nibble.to_bits n1 in 88 let (b5, b6, b7, b8) = Nibble.to_bits n2 in 89 let new_n1 = Nibble.from_bits b2 b3 b4 b5 in 90 let new_n2 = Nibble.from_bits b5 b6 b7 b1 in 91 (new_n1, new_n2) 92 *) 93 let not (n1, n2) = (Nibble.not n1, Nibble.not n2) 57 94 end -
Deliverables/D4.1/Nibble.ml
r63 r64 17 17 val from_bit: bit -> nibble 18 18 val from_int: int -> nibble option 19 val from_string: string -> nibble option19 (* val from_string: string -> nibble option *) 20 20 21 21 val to_bits: nibble -> (bit * bit * bit * bit) … … 30 30 val (-|-): nibble -> nibble -> nibble 31 31 val (-^-): nibble -> nibble -> nibble 32 val rotate_right: nibble -> nibble 33 val rotate_left: nibble -> nibble 34 val shift_right: nibble -> nibble 35 val shift_left: nibble -> nibble 32 36 val not: nibble -> nibble 33 34 val swap_bits: nibble -> nibble35 37 36 38 val map_bit: (bit -> bit) -> nibble -> nibble … … 65 67 | 15 -> Some (Bit.from_int 1, Bit.from_int 1, Bit.from_int 1, Bit.from_int 1) 66 68 | _ -> None 67 let from_string68 69 69 let to_bit (l, h) = 70 let (bl, bh) = Bit.to_bool l, Bit.to_bool h in 71 match (bl, bh) with 72 (false, b) -> Some (Bit.from_bool b) 73 | (true, b) -> None 74 let to_int (l, h) = (2 * Bit.to_int l) + Bit.to_int h 70 let to_bit (b1, b2, b3, b4) = 71 let (d1, d2, d3, d4) = Bit.to_bool b1, Bit.to_bool b2, Bit.to_bool b3, Bit.to_bool b4 in 72 match (d1, d2, d3, d4) with 73 (false, false, false, b) -> Some (Bit.from_bool b) 74 | _ -> None 75 let to_bits (b1, b2, b3, b4) = (b1, b2, b3, b4) 76 let to_int (b1, b2, b3, b4) = (8 * Bit.to_int b1) + (4 * Bit.to_int b2) + 77 (2 * Bit.to_int b3) + Bit.to_int b4 78 79 let get_bit_at index (b1, b2, b3, b4) = 80 match index with 81 0 -> Some b4 82 | 1 -> Some b3 83 | 2 -> Some b2 84 | 3 -> Some b1 85 | _ -> None 86 87 let set_bit_at index new_bit (b1, b2, b3, b4) = 88 match index with 89 0 -> Some (b1, b2, b3, new_bit) 90 | 1 -> Some (b1, b2, new_bit, b4) 91 | 2 -> Some (b1, new_bit, b3, b4) 92 | 3 -> Some (new_bit, b2, b3, b4) 93 | _ -> None 94 95 let (-&-) (l1, l2, l3, l4) (r1, r2, r3, r4) = 96 (Bit.(-&-) l1 r1, Bit.(-&-) l2 r2, Bit.(-&-) l3 r3, Bit.(-&-) l4 r4) 97 let (-|-) (l1, l2, l3, l4) (r1, r2, r3, r4) = 98 (Bit.(-|-) l1 r1, Bit.(-|-) l2 r2, Bit.(-|-) l3 r3, Bit.(-|-) l4 r4) 99 let (-^-) (l1, l2, l3, l4) (r1, r2, r3, r4) = 100 (Bit.(-^-) l1 r1, Bit.(-^-) l2 r2, Bit.(-^-) l3 r3, Bit.(-^-) l4 r4) 101 let rotate_right (b1, b2, b3, b4) = (b4, b1, b2, b3) 102 let rotate_left (b1, b2, b3, b4) = (b2, b3, b4, b1) 103 let shift_right (b1, b2, b3, b4) = (Bit.from_bool false, b1, b2, b3) 104 let shift_left (b1, b2, b3, b4) = (b2, b3, b4, Bit.from_bool false) 105 let not (b1, b2, b3, b4) = (Bit.not b1, Bit.not b2, Bit.not b3, Bit.not b4) 106 107 let map_bit f (b1, b2, b3, b4) = (f b1, f b2, f b3, f b4) 108 let iter_bit f (b1, b2, b3, b4) = 109 let str_b1 = f b1 in 110 let str_b2 = f b2 in 111 let str_b3 = f b3 in 112 let str_b4 = f b4 in 113 str_b1 ^ str_b2 ^ str_b3 ^ str_b4 114 let zip_bit f (l1, l2, l3, l4) (r1, r2, r3, r4) = (f l1 r1, f l2 r2, f l3 r3, f l4 r4) 115 75 116 let to_string = iter_bit Bit.to_string 76 77 let get_bit_at index (l, h) =78 if index = 0 then79 Some l80 else if index = 1 then81 Some h82 else83 None84 85 let set_bit_at index new_bit (l, h) =86 if index = 0 then87 Some (new_bit, h)88 else if index = 1 then89 Some (l, new_bit)90 else91 None92 93 let (-&-) (l1,h1) (l2, h2) =94 (Bit.(-&-) l1 l2, Bit.(-&-) h1 h2)95 let (-|-) (l1,h1) (l2, h2) =96 (Bit.(-|-) l1 l2, Bit.(-|-) h1 h2)97 let (-^-) (l1,h1) (l2, h2) =98 (Bit.(-^-) l1 l2, Bit.(-^-) h1 h2)99 100 let not (l, h) = (Bit.not l, Bit.not h)101 102 let swap_bits (l, h) = (h, l)103 104 let map_bit f (l, h) = (f l, f h)105 let iter_bit f (l, h) =106 let str_l = f l in107 let str_h = f h in108 str_l ^ str_h109 let zip_bit f (l1, h1) (l2, h2) = (f l1 l2, f l2 h2)110 117 end 111 118
Note: See TracChangeset
for help on using the changeset viewer.