Changeset 88


Ignore:
Timestamp:
Sep 20, 2010, 10:35:53 AM (9 years ago)
Author:
mulligan
Message:

int_of_vect implemented.

Location:
Deliverables/D4.1
Files:
4 edited
2 moved

Legend:

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

    r87 r88  
    6363let half_add l r = full_add l r false
    6464
    65 let rec split_last = function [] -> assert false | [he] -> he,[] | he::tl -> let l,res = split_last tl in l,he::res
     65let rec split_last =
     66  function
     67    [] -> assert false
     68  | [he] -> he,[]
     69  | he::tl ->
     70      let l,res = split_last tl in
     71        l,he::res
    6672
    67 let shift_left = function [] -> assert false | _::tl -> tl @ [false]
     73let shift_left =
     74  function
     75    [] -> assert false
     76  | _::tl -> tl @ [false]
    6877let shift_right l = false :: snd (split_last l)
    69 let rotate_left = function [] -> assert false | he::tl -> tl @ [he]
    70 let rotate_right l = let he,tl = split_last l in he::tl
     78let rotate_left =
     79  function
     80    [] -> assert false
     81  | he::tl -> tl @ [he]
     82let rotate_right l =
     83  let he,tl = split_last l in
     84    he::tl
     85
     86let int_of_vect v =
     87  let rec aux pow v =
     88    match v with
     89      [] -> 0
     90    | hd::tl ->
     91        if hd = true then
     92          pow + (aux (pow * 2) tl)
     93        else
     94          aux (pow * 2) tl
     95  in
     96    aux 1 (List.rev v)
  • Deliverables/D4.1/BitVectors.mli

    r87 r88  
    2424val set_bit: 'a vect -> int -> bit -> 'a vect option
    2525
     26val int_of_vect: 'a vect -> int
     27val vect_of_int: int -> 'a -> 'a vect
     28
    2629val (-&-): 'a vect -> 'a vect -> 'a vect
    2730val (-|-): 'a vect -> 'a vect -> 'a vect
  • Deliverables/D4.1/Byte.ml

    r86 r88  
    6161  type bit = Bit.bit
    6262  type nibble = Nibble.nibble
    63   type byte = nibble * nibble
     63  type byte = nibble * nibble(l -&- r) -|- (c -&- (l -^- r)
    6464
    6565  let from_bits b1 b2 b3 b4 b5 b6 b7 b8 =
     
    138138  let to_int (n1, n2) =
    139139    let [b1; b2; b3; b4] = Nibble.to_bits n1 in
    140     let [b5; b6; b7; b8] = Nibble.to_bits n2 in
     140    let [b5; b6; b7;h b8] = Nibble.to_bits n2 in
    141141      (128 * Bit.to_int b8) + (64 * Bit.to_int b8) +
    142142      (32 * Bit.to_int b8) + (16 * Bit.to_int b8) +
  • Deliverables/D4.1/Nibble.ml

    r86 r88  
    6666    | 8 -> Some (Bit.from_int 1, Bit.from_int 0, Bit.from_int 0, Bit.from_int 0)
    6767    | 9 -> Some (Bit.from_int 1, Bit.from_int 0, Bit.from_int 0, Bit.from_int 1)
    68     | 10 -> Some (Bit.from_int 1, Bit.from_int 0, Bit.from_int 1, Bit.from_int 0)
     68    | 10 -> Some (Bit.from_int 1, B(l -&- r) -|- (c -&- (l -^- r)it.from_int 0, Bit.from_int 1, Bit.from_int 0)
    6969    | 11 -> Some (Bit.from_int 1, Bit.from_int 0, Bit.from_int 1, Bit.from_int 1)
    7070    | 12 -> Some (Bit.from_int 1, Bit.from_int 1, Bit.from_int 0, Bit.from_int 0)
     
    8888    | 1 -> Some b3
    8989    | 2 -> Some b2
    90     | 3 -> Some b1
     90    | 3 -> Some b1(l -&- r) -|- (c -&- (l -^- r)
    9191    | _ -> None
    9292
  • Deliverables/D4.1/physical.ml

    r58 r88  
     1open Bit_vectors
     2
    13exception Byte7_conversion
    2 
    3 type bit = bool
    4 type nibble = bit * bit * bit * bit
    5 type byte = nibble * nibble
    6 type byte7 = bit * bit * bit * nibble
    7 type word = byte * byte
    8 type word11 = bit * bit * bit * byte
    94
    105module Byte7Map =
     
    1712    (false,b2,b3,b4),n -> (b2,b3,b4,n)
    1813  | _ -> raise Byte7_conversion
    19 
    20 let int_of_bit b = if b then 0 else 1
    21 
    22 let int_of_nibble (b1,b2,b3,b4) =
    23  int_of_bit b4 + int_of_bit b3 * 2 + int_of_bit b2 * 4 + int_of_bit b1 * 8
    24 
    25 let int_of_byte (n1,n2) = int_of_nibble n2 + int_of_nibble n1 * 16
    26 
    27 let int_of_byte7 (b1,b2,b3,n2) = int_of_nibble n2 + int_of_bit b3 * 16 +
    28                                  int_of_bit b2 * 32 + int_of_bit b1 * 64
    29 
    30 let int_of_word (b1,b2) = int_of_byte b2 + int_of_byte b1 * 256
    31 
    32 let bit_of_int n =
    33  if n >= 2 then assert false
    34  else
    35   if n = 0 then false
    36   else true
    37 ;;
    38 
    39 let nibble_of_int n =
    40  if n >= 16 then assert false
    41  else
    42   let b1 = n / 8 in
    43   let b2 = (n mod 8) / 4 in
    44   let b3 = (n mod 4) / 2 in
    45   let b4 = n mod 2 in
    46    bit_of_int b1, bit_of_int b2, bit_of_int b3, bit_of_int b4
    47 ;;
    48 
    49 let byte_of_int n =
    50  if n >= 256 then assert false
    51  else
    52   let b1 = n / 16 in
    53   let b2 = n mod 16 in
    54    nibble_of_int b1, nibble_of_int b2
    55 ;;
    56 
    57 let byte7_of_int n =
    58  if n >= 128 then assert false
    59  else
    60   let (_,b1,b2,b3),n = byte_of_int n in
    61    (b1,b2,b3,n)
    62 ;;
    63 
    64 let word_of_int n =
    65  if n >= 256 * 256 then assert false
    66  else
    67   let b1 = n / 256 in
    68   let b2 = n mod 256 in
    69    byte_of_int b1, byte_of_int b2
    70 ;;
    71 
    72 let complement ((b1,b2,b3,b4),(b5,b6,b7,b8)) =
    73   (not b1,not b2,not b3,not b4),(not b5,not b6,not b7,not b8)
    74 
    75 let (++) w n = word_of_int (int_of_word w + n);;
    7614
    7715let add8_with_c b1 b2 c =
     
    11957let byte_of_byte7 (b1,b2,b3,n) =
    12058        (false,b1,b2,b3),n
    121 
    122 let nth_bit pos ((b1,b2,b3,b4),(b5,b6,b7,b8)) =
    123   match pos with
    124     0 -> b1
    125   | 1 -> b2
    126   | 2 -> b3
    127   | 3 -> b4
    128   | 4 -> b5
    129   | 5 -> b6
    130   | 6 -> b7
    131   | 7 -> b8
    132 
    133 let set_nth_bit pos v ((b1,b2,b3,b4) as n1,((b5,b6,b7,b8) as n2)) =
    134   match pos with
    135     0 -> ((v,b2,b3,b4),n2)
    136   | 1 -> ((b1,v,b3,b4),n2)
    137   | 2 -> ((b1,b2,v,b4),n2)
    138   | 3 -> ((b1,b2,b3,v),n2)
    139   | 4 -> (n1,(v,b6,b7,b8))
    140   | 5 -> (n1,(b5,v,b7,b8))
    141   | 6 -> (n1,(b5,b6,v,b8))
    142   | 7 -> (n1,(b5,b6,b7,v))
     59;;
  • Deliverables/D4.1/physical.mli

    r72 r88  
     1open Bit_vectors
     2
    13exception Byte7_conversion
    2 
    3 type bit = bool
    4 type nibble = bit * bit * bit * bit
    5 type byte = nibble * nibble
    6 type byte7 = bit * bit * bit * nibble
    7 type word = byte * byte
    8 type word11 = bit * bit * bit * byte
    94
    105module Byte7Map : Map.S with type key = byte7
     
    138val byte7_of_byte: byte -> byte7
    149
    15 val word_of_int: int -> word
    16 val int_of_word: word -> int
    17 val byte7_of_int: int -> byte7
    18 val byte_of_int: int -> byte
    19 val int_of_bit: bit -> int
    20 val int_of_byte: byte -> int
    21 val int_of_byte7: byte7 -> int
    22 val int_of_nibble: nibble -> int
    23 val nibble_of_int: int -> nibble
    24 val byte7_of_bit: bit -> byte7
    25 val byte_of_byte7: byte7 -> byte
    26 
    27 val complement: byte -> byte
    28 
    29 val (++): word -> int -> word
    30 
    31 val add8_with_c:
    32  byte -> byte -> bit -> byte * bit * bit * bit (* +, c, ac, ov *)
    3310val subb8_with_c:
    3411 byte -> byte -> bit -> byte * bit * bit * bit (* -, c, ac, ov *)
    3512val dec: byte -> byte (* with roll-over *)
    3613val inc: byte -> byte (* with roll-over *)
    37 
    38 val nth_bit: int -> byte -> bit
    39 val set_nth_bit: int -> bit -> byte -> byte
Note: See TracChangeset for help on using the changeset viewer.