Source file type.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
open Bigarray

type ('a, 'b) kind = ('a, 'b) Bigarray.kind

exception Unsupported

type u8 = int8_unsigned_elt

type u16 = int16_unsigned_elt

type i32 = int32_elt

type i64 = int64_elt

type f32 = float32_elt

type f64 = float64_elt

type c32 = complex32_elt

type c64 = complex64_elt

let u8 = Int8_unsigned

let u16 = Int16_unsigned

let i32 = Int32

let i64 = Int64

let f32 = Float32

let f64 = Float64

let c32 = Complex32

let c64 = Complex64

module Kind = struct
  let[@inline] max : type a b. (a, b) kind -> a = function
    | Int8_unsigned ->
        255
    | Int16_unsigned ->
        65535
    | Int32 ->
        Int32.max_int
    | Int64 ->
        Int64.max_int
    | Float32 ->
        1.0
    | Float64 ->
        1.0
    | Complex32 ->
        Complex.one
    | Complex64 ->
        Complex.one
    | _ ->
        raise Unsupported


  let[@inline] min : type a b. (a, b) kind -> a = function
    | Int8_unsigned ->
        0
    | Int16_unsigned ->
        0
    | Int32 ->
        Int32.min_int
    | Int64 ->
        Int64.min_int
    | Float32 ->
        0.0
    | Float64 ->
        0.0
    | Complex32 ->
        Complex.zero
    | Complex64 ->
        Complex.zero
    | _ ->
        raise Unsupported


  let[@inline] max_f : type a b. (a, b) kind -> float = function
    | Int8_unsigned ->
        255.
    | Int16_unsigned ->
        65535.
    | Int32 ->
        Int32.max_int |> Int32.to_float
    | Int64 ->
        Int64.max_int |> Int64.to_float
    | Float32 ->
        1.0
    | Float64 ->
        1.0
    | Complex32 ->
        1.0
    | Complex64 ->
        1.0
    | _ ->
        raise Unsupported


  let[@inline] min_f : type a b. (a, b) kind -> float = function
    | Int8_unsigned ->
        0.0
    | Int16_unsigned ->
        0.0
    | Int32 ->
        Int32.min_int |> Int32.to_float
    | Int64 ->
        Int64.min_int |> Int64.to_float
    | Float32 ->
        0.0
    | Float64 ->
        0.0
    | Complex32 ->
        0.0
    | Complex64 ->
        0.0
    | _ ->
        raise Unsupported


  let[@inline] to_float : type a b. (a, b) kind -> a -> float =
   fun kind v ->
    match kind with
    | Int8_unsigned ->
        float_of_int v
    | Int16_unsigned ->
        float_of_int v
    | Int32 ->
        Int32.to_float v
    | Int64 ->
        Int64.to_float v
    | Float32 ->
        v
    | Float64 ->
        v
    | Complex32 ->
        Complex.norm v
    | _ ->
        raise Unsupported


  let[@inline] of_float : type a b. (a, b) kind -> float -> a =
   fun kind v ->
    match kind with
    | Int8_unsigned ->
        int_of_float v
    | Int16_unsigned ->
        int_of_float v
    | Int32 ->
        Int32.of_float v
    | Int64 ->
        Int64.of_float v
    | Float32 ->
        v
    | Float64 ->
        v
    | Complex32 ->
        Complex.{re = v; im = 0.}
    | _ ->
        raise Unsupported


  let clamp kind f =
    let min = min_f kind in
    let max = max_f kind in
    if f < min then min else if f > max then max else f


  let normalize kind f =
    let min = min_f kind in
    let max = max_f kind in
    (f -. min) /. (max -. min)


  let denormalize kind f =
    let min = min_f kind in
    let max = max_f kind in
    f *. (max -. min)


  let convert ~from to_kind f =
    to_float from f
    |> normalize from
    |> denormalize to_kind
    |> of_float to_kind
end