Changeset 64


Ignore:
Timestamp:
Sep 13, 2010, 12:59:27 PM (9 years ago)
Author:
mulligan
Message:

More work on bits, bytes and nibbles.

Location:
Deliverables/D4.1
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • Deliverables/D4.1/Bit.ml

    r63 r64  
    2222  val (-^-): bit -> bit -> bit
    2323  val not: bit -> bit
    24 
    2524end;;
    2625
  • Deliverables/D4.1/Byte.ml

    r63 r64  
    3030  val get_bit_at: int -> byte -> bit
    3131  val set_bit_at: int -> bit -> byte -> byte option
     32*)
    3233
    3334  val (-&-): byte -> byte -> byte
    3435  val (-|-): byte -> byte -> byte
    3536  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*)
    3643  val not: byte -> byte
    3744
     45(*
    3846  val map_nibble: (nibble -> nibble) -> byte -> byte
    3947  val map_bit: (bit -> bit) -> byte -> byte
     
    4250  val zip_nibble: (nibble -> nibble -> nibble) -> byte -> byte -> byte
    4351  val zip_bit: (bit -> bit -> bit) -> byte -> byte -> byte
    44   val pretty: byte -> string
     52  val to_string: byte -> string
    4553*)
    4654end
    4755
    48 module Byte(Nibble: NIBBLE): BYTE
    49   with type bit = Nibble.bit and type nibble = Nibble.nibble =
     56module Byte(Bit: BIT) (Nibble: NIBBLE): BYTE
     57  with type Nibble.bit = Bit.bit
     58  and  type nibble = Nibble.nibble =
    5059struct
    51   type bit = Nibble.bit
     60  type bit = Bit.bit
    5261  type nibble = Nibble.nibble
    5362  type byte = nibble * nibble
    5463
    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)
    5794end
  • Deliverables/D4.1/Nibble.ml

    r63 r64  
    1717  val from_bit: bit -> nibble
    1818  val from_int: int -> nibble option
    19   val from_string: string -> nibble option
     19  (* val from_string: string -> nibble option *)
    2020
    2121  val to_bits: nibble -> (bit * bit * bit * bit)
     
    3030  val (-|-): nibble -> nibble -> nibble
    3131  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
    3236  val not: nibble -> nibble
    33 
    34   val swap_bits: nibble -> nibble
    3537
    3638  val map_bit: (bit -> bit) -> nibble -> nibble
     
    6567    | 15 -> Some (Bit.from_int 1, Bit.from_int 1, Bit.from_int 1, Bit.from_int 1)
    6668    | _ -> None
    67   let from_string
    6869
    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
    75116  let to_string = iter_bit Bit.to_string
    76 
    77   let get_bit_at index (l, h) =
    78     if index = 0 then
    79       Some l
    80     else if index = 1 then
    81       Some h
    82     else
    83       None
    84 
    85   let set_bit_at index new_bit (l, h) =
    86     if index = 0 then
    87       Some (new_bit, h)
    88     else if index = 1 then
    89       Some (l, new_bit)
    90     else
    91       None
    92 
    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 in
    107     let str_h = f h in
    108       str_l ^ str_h
    109   let zip_bit f (l1, h1) (l2, h2) = (f l1 l2, f l2 h2)
    110117end
    111118
Note: See TracChangeset for help on using the changeset viewer.