package hardcaml

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

Source file fixed_intf.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
(** Fixed point arithmetic.

    {[
      (* select bits type *)
      module F = Fixed.Make (Bits)
      (* select Signed/Unsigned fixed type, and overflow and rounding mode. *)
      module S = F.Signed.Make (struct
          let round = F.Signed.Round.tie_to_nearest_even
          let overflow = F.Signed.Overflow.saturate
        end)
    ]} *)

open! Import

module type Round = sig
  module B : sig type t end
  type t
  val neg_infinity : t
  val pos_infinity : t
  val to_zero : t
  val away_from_zero : t
  val tie_to_neg_infinity : t
  val tie_to_pos_infinity : t
  val tie_to_zero : t
  val tie_away_from_zero : t
  val tie_to_nearest_even : t
  val tie_to_nearest_odd : t
  val generic : B.t -> t
  val eval : t -> int -> B.t -> B.t
end

module type Overflow = sig
  module B : sig type t end
  type t
  val wrap : t
  val saturate : t
  val eval : t -> int -> int -> B.t -> B.t
end

module type Fixed_point = sig
  module B : sig type t end

  type t

  val mk : int -> B.t -> t
  (** create a fixed point value.  [mk f x] will have [f] fractional bits.  [width x -
      f] will be the number of integer bits *)

  val int : t -> B.t
  (** return the integer part of the value *)

  val frac : t -> B.t
  (** return the fractional part of the value *)

  val signal : t -> B.t
  (** return the underlying bits *)

  val width_int : t -> int
  (** number of integer bits *)

  val width_frac : t -> int
  (** number of fractional bits *)

  val to_float : t -> float
  (** convert fixed point value to a float *)

  val select_int : t -> int -> B.t
  (** [select_int f x] extracts the integer part, and resizes it to x bits.  Bits are
      dropped from the msb down, if required. *)

  val select_frac : t -> int -> B.t
  (** [select_frac f x] extracts the fractional part, and resizes it to x bits.  Bits
      are dropped from the lsb up, if required. *)

  val select : t -> int -> int -> t
  (** resizes a fixed type using select_int and select_frac *)

  val norm : t list -> t list
  (** find largest integer and fractional parts in each fixed value, and resize all
      elements to that size *)

  val norm2 : t -> t -> t * t
  (** same as norm, but for 2 values *)

  val const : int -> int -> float -> t
  (** create a fixed value with the given number of integer and fractional bits from the
      floating point value *)

  val (+:) : t -> t -> t
  (** adition *)

  val (-:) : t -> t -> t
  (** subtraction *)

  val ( *: ) : t -> t -> t
  (** multiplication *)

  val (==:) : t -> t -> B.t
  (** equality *)

  val (<>:) : t -> t -> B.t
  (** inequality *)

  val (<:) : t -> t -> B.t
  (** less than *)

  val (<=:) : t -> t -> B.t
  (** less than or equal to *)

  val (>:) : t -> t -> B.t
  (** greater than *)

  val (>=:) : t -> t -> B.t
  (** greater than or equal to *)

  val mux : B.t -> t list -> t
  (** multiplexor *)

  val resize : t -> int -> int -> t
  (** [resize x i f] will resize the integer part to have [i] bits, and fractional part
      to have [f] bits.  Rounding and overflow control is applied *)
end

module type Fixed = sig

  module Make (B : Comb.S) : sig

    type unsigned
    type signed

    type 'a round
    type 'a overflow

    (** various different rounding modes *)
    module type Round = Round with module B := B

    (** overflow control - wrap or saturate *)
    module type Overflow = Overflow with module B := B

    (** fixed point API *)
    module type Fixed = Fixed_point with module B := B

    module Unsigned : sig
      module Round : (Round with type t = unsigned round)
      module Overflow : (Overflow with type t = unsigned overflow)
      module type Spec = sig
        val round : Round.t
        val overflow : Overflow.t
      end
      module Make (S : Spec) : Fixed
    end

    module Signed : sig
      module Round : (Round with type t = signed round)
      module Overflow : (Overflow with type t = signed overflow)
      module type Spec = sig
        val round : Round.t
        val overflow : Overflow.t
      end
      module Make (S : Spec) : Fixed
    end
  end
end
OCaml

Innovation. Community. Security.