open Printf let iterations = 10000;; (*===================================================================================*) let rec read_loop str x acc intbits bitind = (*1*) if x <= intbits (*2*) then (acc lsr (intbits-x)) (*3*) else read_loop str x ((int_of_char (str.[bitind]) ) lor (acc lsl 8)) (*4*) (intbits+8) (bitind+1);; (*5*) let get_xbits str x inoff = (*6*) let bitoff = (inoff land 7) (*7*) and bitind = (inoff lsr 3) (*8*) in let intbits = 8-bitoff in (*9*) let res = (int_of_char (str.[bitind])) land ((1 lsl intbits)-1) in (*10*) read_loop str x res intbits (bitind+1);; (*11*) let skip_bits nofchannels = (*12*) (((5 + (nofchannels * 11) - 1) lsr 3)+1) * 8;; (*13*) let rec getchannels str offset nofchannels = (*14*) if nofchannels > 0 (*15*) then (get_xbits str 11 offset) :: getchannels str (offset+11) (nofchannels-1) (*16*) else [];; (*17*) let rec decode_five_11 str nofpackets offset = (*18*) if nofpackets > 0 (*19*) then let nofchannels = get_xbits str 5 offset (*20*) in let list = getchannels str (offset+5) nofchannels in (*21*) list :: (decode_five_11 str (nofpackets-1) (offset+(skip_bits nofchannels))) (*22*) else [];; (*23*) (*===================================================================================*) let rec sum l = match l with (a::b) -> a + (sum b) | [] -> 0;; let rec sumlists l = match l with (a::b) -> ((sum a) + (sumlists b)) | [] -> 0;; let get_file fname = let handle = open_in fname in let len = (in_channel_length handle); in let s = String.create len in (really_input handle s 0 len; s);; let rec iter n f = if n>1 then (f(); iter (n-1) f) else f();; let main fname = let str = get_file fname in let packets=(get_xbits str 16 0) in let start = Sys.time() in let sumval = (iter iterations (function () -> sumlists (decode_five_11 str packets 16))) in let stop = Sys.time () in let time = (stop -. start) in let outfile = open_out (fname ^ ".ocaml") in (output_string outfile (string_of_int sumval); printf "%.3f" time; flush stdout);; let () = let w = (Array.get Sys.argv 1) in main w;;