Debian Codegraph @

kernel/float64_common.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (* OCaml's float type follows the IEEE 754 Binary64 (double precision)
12 format *)
13 type t = float
14
15 (* The [f : float] type annotation enable the compiler to compile f <> f
16 as comparison on floats rather than the polymorphic OCaml comparison
17 which is much slower. *)
18 let is_nan (f : float) = f <> f
19 let is_infinity f = f = infinity
20 let is_neg_infinity f = f = neg_infinity
21
22 (* OCaml gives a sign to nan values which should not be displayed as
23 all NaNs are considered equal here.
24 OCaml prints infinities as "inf" (resp. "-inf")
25 but we want "infinity" (resp. "neg_infinity"). *)
26 let to_string_raw fmt f =
27 if is_nan f then "nan"
28 else if is_infinity f then "infinity"
29 else if is_neg_infinity f then "neg_infinity"
30 else Printf.sprintf fmt f
31
32 let to_hex_string = to_string_raw "%h"
33
34 (* Printing a binary64 float in 17 decimal places and parsing it again
35 will yield the same float. *)
36 let to_string = to_string_raw "%.17g"
37
38 let of_string = float_of_string
39
40 (* Compiles a float to OCaml code *)
41 let compile f =
42 Printf.sprintf "Float64.of_float (%s)" (to_hex_string f)
43
44 let of_float f = f
45
46 let to_float f = if is_nan f then nan else f
47
48 let sign f = copysign 1. f < 0.
49
50 let opp = ( ~-. )
51 let abs = abs_float
52
53 type float_comparison = FEq | FLt | FGt | FNotComparable
54
55 (* See above comment on [is_nan] for the [float] type annotations. *)
56 let eq (x : float) (y : float) = x = y
57 [@@ocaml.inline always]
58
59 let lt (x : float) (y : float) = x < y
60 [@@ocaml.inline always]
61
62 let le (x : float) (y : float) = x <= y
63 [@@ocaml.inline always]
64
65 (* inspired by lib/util.ml; see also #10471 *)
66 let pervasives_compare (x : float) (y : float) = compare x y
67
68 let compare (x : float) (y : float) =
69 if x < y then FLt
70 else
71 (
72 if x > y then FGt
73 else
74 (
75 if x = y then FEq
76 else FNotComparable (* NaN case *)
77 )
78 )
79 [@@ocaml.inline always]
80
81 type float_class =
82 | PNormal | NNormal | PSubn | NSubn | PZero | NZero | PInf | NInf | NaN
83
84 let classify x =
85 match classify_float x with
86 | FP_normal -> if 0. < x then PNormal else NNormal
87 | FP_subnormal -> if 0. < x then PSubn else NSubn
88 | FP_zero -> if 0. < 1. /. x then PZero else NZero
89 | FP_infinite -> if 0. < x then PInf else NInf
90 | FP_nan -> NaN
91 [@@ocaml.inline always]
92
93 let of_uint63 x = Uint63.to_float x
94 [@@ocaml.inline always]
95
96 let prec = 53
97 let normfr_mantissa f =
98 let f = abs f in
99 if f >= 0.5 && f < 1. then Uint63.of_float (ldexp f prec)
100 else Uint63.zero
101 [@@ocaml.inline always]
102
103 let eshift = 2101 (* 2*emax + prec *)
104
105 (* When calling frexp on a nan or an infinity, the returned value inside
106 the exponent is undefined.
107 Therefore we must always set it to a fixed value (here 0). *)
108 let frshiftexp f =
109 match classify_float f with
110 | FP_zero | FP_infinite | FP_nan -> (f, Uint63.zero)
111 | FP_normal | FP_subnormal ->
112 let (m, e) = frexp f in
113 m, Uint63.of_int (e + eshift)
114 [@@ocaml.inline always]
115
116 let ldshiftexp f e = ldexp f (Uint63.to_int_min e (2 * eshift) - eshift)
117 [@@ocaml.inline always]
118
119 external next_up : float -> float = "rocq_next_up_byte" "rocq_next_up"
120 [@@unboxed] [@@noalloc]
121
122 external next_down : float -> float = "rocq_next_down_byte" "rocq_next_down"
123 [@@unboxed] [@@noalloc]
124
125 let equal f1 f2 =
126 if f1 = f2 then f1 <> 0. || sign f1 = sign f2 (* OCaml consider 0. = -0. *)
127 else is_nan f1 && is_nan f2
128 [@@ocaml.inline always]
129
130 let hash =
131 (* Hashtbl.hash already considers all NaNs as equal,
132 cf. https://github.com/ocaml/ocaml/commit/aea227fdebe0b5361fd3e1d0aaa42cf929052269
133 and http://caml.inria.fr/pub/docs/manual-ocaml/libref/Hashtbl.html *)
134 Hashtbl.hash
135
136 let total_compare f1 f2 =
137 (* pervasives_compare considers all NaNs as equal, which is fine here,
138 but also considers -0. and +0. as equal *)
139 if f1 = 0. && f2 = 0. then pervasives_compare (1. /. f1) (1. /. f2)
140 else pervasives_compare f1 f2
141
142 let is_float64 t =
143 Obj.tag t = Obj.double_tag
144 [@@ocaml.inline always]