Source file nativeint.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
# 2 "nativeint.ml"
open! Stdlib
[@@@ocaml.flambda_o3]
external neg: (nativeint[@local_opt]) -> (nativeint[@local_opt]) @@ portable = "%nativeint_neg"
external add: (nativeint[@local_opt]) -> (nativeint[@local_opt]) -> (nativeint[@local_opt]) @@ portable = "%nativeint_add"
external sub: (nativeint[@local_opt]) -> (nativeint[@local_opt]) -> (nativeint[@local_opt]) @@ portable = "%nativeint_sub"
external mul: (nativeint[@local_opt]) -> (nativeint[@local_opt]) -> (nativeint[@local_opt]) @@ portable = "%nativeint_mul"
external div: (nativeint[@local_opt]) -> (nativeint[@local_opt]) -> (nativeint[@local_opt]) @@ portable = "%nativeint_div"
external rem: (nativeint[@local_opt]) -> (nativeint[@local_opt]) -> (nativeint[@local_opt]) @@ portable = "%nativeint_mod"
external logand: (nativeint[@local_opt]) -> (nativeint[@local_opt]) -> (nativeint[@local_opt]) @@ portable = "%nativeint_and"
external logor: (nativeint[@local_opt]) -> (nativeint[@local_opt]) -> (nativeint[@local_opt]) @@ portable = "%nativeint_or"
external logxor: (nativeint[@local_opt]) -> (nativeint[@local_opt]) -> (nativeint[@local_opt]) @@ portable = "%nativeint_xor"
external shift_left: (nativeint[@local_opt]) -> int -> (nativeint[@local_opt]) @@ portable = "%nativeint_lsl"
external shift_right: (nativeint[@local_opt]) -> int -> (nativeint[@local_opt]) @@ portable = "%nativeint_asr"
external shift_right_logical: (nativeint[@local_opt]) -> int -> (nativeint[@local_opt]) @@ portable = "%nativeint_lsr"
external of_int: int -> (nativeint[@local_opt]) @@ portable = "%nativeint_of_int"
external to_int: (nativeint[@local_opt]) -> int @@ portable = "%nativeint_to_int"
external of_float : float -> nativeint @@ portable
= "caml_nativeint_of_float" "caml_nativeint_of_float_unboxed"
[@@unboxed] [@@noalloc]
external to_float : nativeint -> float @@ portable
= "caml_nativeint_to_float" "caml_nativeint_to_float_unboxed"
[@@unboxed] [@@noalloc]
external of_int32: int32 -> nativeint @@ portable = "%nativeint_of_int32"
external to_int32: nativeint -> int32 @@ portable = "%nativeint_to_int32"
let zero = 0n
let one = 1n
let minus_one = -1n
let[@inline available] succ n = add n 1n
let[@inline available] pred n = sub n 1n
let[@inline available] abs n = if n >= 0n then n else neg n
let size = Sys.word_size
let min_int = shift_left 1n (size - 1)
let max_int = sub min_int 1n
let[@inline available] lognot n = logxor n (-1n)
let unsigned_to_int =
let max_int = of_int Stdlib.max_int in
fun[@inline available] n ->
if n >= 0n && n <= max_int then
Some (to_int n)
else
None
external format : string -> nativeint -> string @@ portable = "caml_nativeint_format"
let[@inline available] to_string n = format "%d" n
external of_string: string -> (nativeint[@unboxed]) @@ portable
= "caml_nativeint_of_string" "caml_nativeint_of_string_unboxed"
let[@inline available] of_string_opt s =
try Some (of_string s)
with Failure _ -> None
type t = nativeint
let[@inline available] compare (x: t) (y: t) = Stdlib.compare x y
let[@inline available] equal (x: t) (y: t) = compare x y = 0
let[@inline available] unsigned_compare n m =
compare (sub n min_int) (sub m min_int)
let unsigned_lt n m =
sub n min_int < sub m min_int
let[@inline available] min x y : t = if x <= y then x else y
let[@inline available] max x y : t = if x >= y then x else y
let[@inline available] unsigned_div n d =
if d < zero then
if unsigned_lt n d then zero else one
else
let q = shift_left (div (shift_right_logical n 1) d) 1 in
let r = sub n (mul q d) in
if unsigned_lt r d then q else succ q
let[@inline available] unsigned_rem n d =
sub n (mul ((unsigned_div[@inlined]) n d) d)
external seeded_hash_param :
int -> int -> int -> nativeint -> int @@ portable = "caml_hash_exn" [@@noalloc]
let seeded_hash seed x = seeded_hash_param 10 100 seed x
let hash x = seeded_hash_param 10 100 0 x