Common — library colibrilib.flocq — module Round

The rounding of Flocq

The rounding of the reals to the floats of prec bits of significand and least exponent emin, with an unbounded exponent above: Flocq's round radix2 (FLT_exp emin prec) (Flocq.Core.Generic_fmt), for the modes of IEEE 754.

This theory is realized in Rocq in common/rocq/flocq/Round.v: the functions are defined by Flocq's round, and the axioms are proved there from Flocq's lemmas (round_DN_pt, round_UP_pt, round_ZR_DN, round_ZR_UP, round_NE_pt, round_NA_pt). Build: common/rocq/build.sh. It uses only the theories of the standard library, so the realization rests only on the realizations of Why3 and on Flocq.

colibrilib.fbin_ieee proves that the rounding of fbin.mlw is this one.

17
theory Round
18
use int.Int
19
use int.Power
20
use real.RealInfix
21
use real.FromInt
22
use real.Abs

2^e for every integer e (Flocq's bpow radix2 e)

25
function
pow2
(e: int) : real =
26
if 0 <= e then from_int (power 2 e) else 1.0 /. from_int (power 2 (-e))

The floats of the format (Flocq's FLT_format, equivalent to generic_format radix2 (FLT_exp emin prec))

30
predicate
generic
(prec emin: int) (x: real) =
31
exists m e. emin <= e /\ - power 2 prec < m < power 2 prec /\
32
x = from_int m *. pow2 e

x has a representation with an even significand (Flocq's NE_prop gives the canonical one)

36
predicate
even_repr
(prec emin: int) (x: real) =
37
exists m e. emin <= e /\ - power 2 prec < m < power 2 prec /\
38
x = from_int m *. pow2 e /\ exists q. m = 2 * q

f is a float nearest to r (Flocq's Rnd_N_pt)

41
predicate
nearest
(prec emin: int) (r f: real) =
42
generic prec emin f /\
43
forall g. generic prec emin g -> abs (f -. r) <=. abs (g -. r)

The roundings: toward minus infinity (Zfloor), toward plus infinity (Zceil), toward zero (Ztrunc), to nearest ties to even (ZnearestE), to nearest ties away from zero (ZnearestA)

48
function
rnd_dn
(prec emin: int) (r: real) : real
49
function
rnd_up
(prec emin: int) (r: real) : real
50
function
rnd_zr
(prec emin: int) (r: real) : real
51
function
rnd_ne
(prec emin: int) (r: real) : real
52
function
rnd_na
(prec emin: int) (r: real) : real

Flocq's round_DN_pt: the greatest float below

55
axiom
rnd_dn_spec
: forall prec emin r. 0 < prec ->
56
generic prec emin (rnd_dn prec emin r) /\ rnd_dn prec emin r <=. r /\
57
forall g. generic prec emin g -> g <=. r -> g <=. rnd_dn prec emin r

Flocq's round_UP_pt: the least float above

60
axiom
rnd_up_spec
: forall prec emin r. 0 < prec ->
61
generic prec emin (rnd_up prec emin r) /\ r <=. rnd_up prec emin r /\
62
forall g. generic prec emin g -> r <=. g -> rnd_up prec emin r <=. g

Flocq's round_ZR_DN and round_ZR_UP

65
axiom
rnd_zr_spec
: forall prec emin r. 0 < prec ->
66
rnd_zr prec emin r =
67
if 0.0 <=. r then rnd_dn prec emin r else rnd_up prec emin r

Flocq's round_NE_pt: a nearest float, the even one on a tie

70
axiom
rnd_ne_spec
: forall prec emin r. 1 < prec ->
71
nearest prec emin r (rnd_ne prec emin r) /\
72
forall g. nearest prec emin r g -> g <> rnd_ne prec emin r ->
73
even_repr prec emin (rnd_ne prec emin r)

Flocq's round_NA_pt: a nearest float, the larger in magnitude on a tie

77
axiom
rnd_na_spec
: forall prec emin r. 0 < prec ->
78
nearest prec emin r (rnd_na prec emin r) /\
79
forall g. nearest prec emin r g -> abs g <=. abs (rnd_na prec emin r)
80
end