Debian Codegraph @

theories/Corelib/Floats/FloatOps.v

raw
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 From Corelib Require Import BinNums PosDef IntDef Uint63Axioms.
12 From Corelib Require Import FloatClass SpecFloat PrimFloat.
13
14 (** * Derived operations and mapping between primitive [float]s and [spec_float]s *)
15
16 Definition prec := Eval compute in Z.of_nat 53.
17 Definition emax := Eval compute in Z.of_nat 1024.
18 Abbreviation emin := (emin prec emax).
19
20 Definition shift := Eval compute in Z.of_nat 2101. (** [= 2*emax + prec] *)
21
22 Module Z.
23 Definition frexp f :=
24 let (m, se) := frshiftexp f in
25 (m, (Z.sub (to_Z se) shift)).
26
27 Definition ldexp f e :=
28 let e' := Z.max (Z.min e (Z.sub emax emin)) (Z.sub (Z.sub emin emax) (Zpos 1)) in
29 ldshiftexp f (of_Z (Z.add e' shift)).
30 End Z.
31
32 Definition ulp f := Z.ldexp one (fexp prec emax (snd (Z.frexp f))).
33
34 (** [Prim2SF] is an injective function that will be useful to express
35 the properties of the implemented Binary64 format (see [FloatAxioms]).
36 *)
37 Definition Prim2SF f :=
38 if is_nan f then S754_nan
39 else if is_zero f then S754_zero (get_sign f)
40 else if is_infinity f then S754_infinity (get_sign f)
41 else
42 let (r, exp) := Z.frexp f in
43 let e := Z.sub exp prec in
44 let (shr, e') := shr_fexp prec emax (to_Z (normfr_mantissa r))%uint63 e loc_Exact in
45 match shr_m shr with
46 | Zpos p => S754_finite (get_sign f) p e'
47 | Zneg _ | Z0 => S754_zero false (* must never occur *)
48 end.
49
50 Definition SF2Prim ef :=
51 match ef with
52 | S754_nan => nan
53 | S754_zero false => zero
54 | S754_zero true => neg_zero
55 | S754_infinity false => infinity
56 | S754_infinity true => neg_infinity
57 | S754_finite s m e =>
58 let pm := of_uint63 (of_Z (Zpos m)) in
59 let f := Z.ldexp pm e in
60 if s then (-f)%float else f
61 end.