package hardcaml

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file intbitsList.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
open Base

(* Bits API built using lists on ints.  Allows any bit precision and is simple, but
   slow. *)

module Gates = struct
  (* msb first *)
  type t = int list [@@deriving compare]

  let equal = [%compare.equal: t]
  let empty = []
  let is_empty = List.is_empty
  let width x = List.length x
  let of_constant = Constant.to_bit_list
  let to_constant = Constant.of_bit_list
  let concat_msb l = List.concat l

  let select s h l =
    let rec sel b i =
      match b with
      | [] -> []
      | hd :: tl ->
        if i > h then [] else if i >= l then hd :: sel tl (i + 1) else sel tl (i + 1)
    in
    List.rev (sel (List.rev s) 0)
  ;;

  let ( &: ) = List.map2_exn ~f:( land )
  let ( |: ) = List.map2_exn ~f:( lor )
  let ( ^: ) = List.map2_exn ~f:( lxor )
  let ( ~: ) = List.map ~f:(fun x -> if x = 1 then 0 else 1)

  let rec to_string b =
    match b with
    | [] -> ""
    | h :: t -> (if h = 1 then "1" else "0") ^ to_string t
  ;;

  let to_bstr = to_string
  let sexp_of_t s = [%sexp (to_bstr s : string)]
  let ( -- ) a _ = a
end

module Primitives = struct
  include Comb.Make_primitives (Gates)

  (* About 30% faster than the generic implementation, and common enough to care. *)
  let mux sel vals =
    let len = List.length vals in
    let idx = to_constant sel |> Constant.to_int64 |> Int64.to_int_trunc in
    List.nth_exn vals (if idx >= len then len - 1 else idx)
  ;;
end

include Comb.Make (Primitives)
OCaml

Innovation. Community. Security.