Source file int_conversions.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
open! Import
module Int = Int0
module Sys = Sys0
let convert_failure x a b to_string =
Printf.failwithf
"conversion from %s to %s failed: %s is out of range"
a
b
(to_string x)
()
[@@cold]
;;
let num_bits_int = Sys.int_size_in_bits
let num_bits_int32 = 32
let num_bits_int64 = 64
let num_bits_nativeint = Word_size.num_bits Word_size.word_size
let () = assert (num_bits_int = 63 || num_bits_int = 31 || num_bits_int = 32)
let min_int32 = Stdlib.Int32.min_int
let max_int32 = Stdlib.Int32.max_int
let min_int64 = Stdlib.Int64.min_int
let max_int64 = Stdlib.Int64.max_int
let min_nativeint = Stdlib.Nativeint.min_int
let max_nativeint = Stdlib.Nativeint.max_int
include Integer_to_string
let int_to_int32_failure x = convert_failure x "int" "int32" int_to_string
let int32_to_int_failure x =
convert_failure (globalize_int32 x) "int32" "int" int32_to_string
;;
external int32_to_int_trunc : local_ int32 -> int @@ portable = "%int32_to_int"
external int_to_int32_trunc
: local_ int
-> (int32[@local_opt])
@@ portable
= "%int32_of_int"
let int_is_representable_as_int32 @ portable =
if num_bits_int <= num_bits_int32
then fun _ -> true
else (
let min = int32_to_int_trunc min_int32 in
let max = int32_to_int_trunc max_int32 in
fun x -> compare_int min x <= 0 && compare_int x max <= 0)
;;
let%template int32_is_representable_as_int =
if num_bits_int32 <= num_bits_int
then fun _ -> true
else (
let min = int_to_int32_trunc Int.min_value in
let max = int_to_int32_trunc Int.max_value in
fun x ->
(compare_int32 [@mode local]) min x <= 0 && (compare_int32 [@mode local]) x max <= 0)
;;
let int_to_int32 x =
if int_is_representable_as_int32 x then Some (int_to_int32_trunc x) else None
;;
let int32_to_int x =
if int32_is_representable_as_int x then Some (int32_to_int_trunc x) else None
;;
let int_to_int32_exn x = exclave_
if int_is_representable_as_int32 x then int_to_int32_trunc x else int_to_int32_failure x
;;
let int32_to_int_exn x =
if int32_is_representable_as_int x then int32_to_int_trunc x else int32_to_int_failure x
;;
let[@cold] [@zero_alloc] int64_to_int_failure x =
convert_failure x "int64" "int" int64_to_string
;;
let () = assert (num_bits_int < num_bits_int64)
external int_to_int64 : local_ int -> (int64[@local_opt]) @@ portable = "%int64_of_int"
external int64_to_int_trunc : local_ int64 -> int @@ portable = "%int64_to_int"
let%template int64_is_representable_as_int =
let min = int_to_int64 Int.min_value in
let max = int_to_int64 Int.max_value in
fun x ->
(compare_int64 [@mode local]) min x <= 0 && (compare_int64 [@mode local]) x max <= 0
;;
let int64_to_int x =
if int64_is_representable_as_int x then Some (int64_to_int_trunc x) else None
;;
let int64_to_int_exn x =
if int64_is_representable_as_int x
then int64_to_int_trunc x
else (
let x =
Stdlib.Int64.add
(globalize_int64 x)
0L
in
int64_to_int_failure x)
;;
let nativeint_to_int_failure x =
convert_failure (globalize_nativeint x) "nativeint" "int" nativeint_to_string
;;
let () = assert (num_bits_int <= num_bits_nativeint)
external int_to_nativeint
: local_ int
-> (nativeint[@local_opt])
@@ portable
= "%nativeint_of_int"
external nativeint_to_int_trunc
: local_ nativeint
-> int
@@ portable
= "%nativeint_to_int"
let%template nativeint_is_representable_as_int =
if num_bits_nativeint <= num_bits_int
then fun _ -> true
else (
let min = int_to_nativeint Int.min_value in
let max = int_to_nativeint Int.max_value in
fun x ->
(compare_nativeint [@mode local]) min x <= 0
&& (compare_nativeint [@mode local]) x max <= 0)
;;
let nativeint_to_int x =
if nativeint_is_representable_as_int x then Some (nativeint_to_int_trunc x) else None
;;
let nativeint_to_int_exn x =
if nativeint_is_representable_as_int x
then nativeint_to_int_trunc x
else nativeint_to_int_failure x
;;
let int64_to_int32_failure x =
convert_failure (globalize_int64 x) "int64" "int32" int64_to_string
;;
let () = assert (num_bits_int32 < num_bits_int64)
external int32_to_int64
: local_ int32
-> (int64[@local_opt])
@@ portable
= "%int64_of_int32"
external int64_to_int32_trunc
: local_ int64
-> (int32[@local_opt])
@@ portable
= "%int64_to_int32"
include struct
open struct
let min = int32_to_int64 min_int32
let max = int32_to_int64 max_int32
end
let%template int64_is_representable_as_int32 x =
(compare_int64 [@mode local]) min x <= 0 && (compare_int64 [@mode local]) x max <= 0
[@@zero_alloc]
;;
end
let int64_to_int32 x =
if int64_is_representable_as_int32 x then Some (int64_to_int32_trunc x) else None
;;
let int64_to_int32_exn x = exclave_
if int64_is_representable_as_int32 x
then int64_to_int32_trunc x
else int64_to_int32_failure x
;;
let nativeint_to_int32_failure x =
convert_failure (globalize_nativeint x) "nativeint" "int32" nativeint_to_string
;;
let () = assert (num_bits_int32 <= num_bits_nativeint)
external int32_to_nativeint
: local_ int32
-> (nativeint[@local_opt])
@@ portable
= "%nativeint_of_int32"
external nativeint_to_int32_trunc
: local_ nativeint
-> (int32[@local_opt])
@@ portable
= "%nativeint_to_int32"
let%template nativeint_is_representable_as_int32 =
if num_bits_nativeint <= num_bits_int32
then fun _ -> true
else (
let min = int32_to_nativeint min_int32 in
let max = int32_to_nativeint max_int32 in
fun x ->
(compare_nativeint [@mode local]) min x <= 0
&& (compare_nativeint [@mode local]) x max <= 0)
;;
let nativeint_to_int32 x =
if nativeint_is_representable_as_int32 x
then Some (nativeint_to_int32_trunc x)
else None
;;
let nativeint_to_int32_exn x = exclave_
if nativeint_is_representable_as_int32 x
then nativeint_to_int32_trunc x
else nativeint_to_int32_failure x
;;
let int64_to_nativeint_failure x =
convert_failure (globalize_int64 x) "int64" "nativeint" int64_to_string
;;
let () = assert (num_bits_int64 >= num_bits_nativeint)
external int64_to_nativeint_trunc
: local_ int64
-> (nativeint[@local_opt])
@@ portable
= "%int64_to_nativeint"
external nativeint_to_int64
: local_ nativeint
-> (int64[@local_opt])
@@ portable
= "%int64_of_nativeint"
let%template int64_is_representable_as_nativeint =
if num_bits_int64 <= num_bits_nativeint
then fun _ -> true
else (
let min = nativeint_to_int64 min_nativeint in
let max = nativeint_to_int64 max_nativeint in
fun x ->
(compare_int64 [@mode local]) min x <= 0 && (compare_int64 [@mode local]) x max <= 0)
;;
let int64_to_nativeint x =
if int64_is_representable_as_nativeint x
then Some (int64_to_nativeint_trunc x)
else None
;;
let int64_to_nativeint_exn x = exclave_
if int64_is_representable_as_nativeint x
then int64_to_nativeint_trunc x
else int64_to_nativeint_failure x
;;
let int64_to_int63_failure x =
convert_failure (globalize_int64 x) "int64" "int63" int64_to_string
;;
let%template int64_is_representable_as_int63 =
let min = Stdlib.Int64.shift_right min_int64 1 in
let max = Stdlib.Int64.shift_right max_int64 1 in
fun x ->
(compare_int64 [@mode local]) min x <= 0 && (compare_int64 [@mode local]) x max <= 0
;;
let int64_fit_on_int63_exn x =
if int64_is_representable_as_int63 x then () else int64_to_int63_failure x
;;