library colibrilib.fp_dyadic — module Rulesmodule Rules10use int.Int11use int.Power12use option.Option13use real.RealInfix14use colibrilib.pow2r.Pow2r15use colibrilib.fp.Ereal16use colibrilib.fbin.Fp17use colibrilib.fp_facts.Facts18use colibrilib.dyadic.Dyadic as D1920predicate finite (x: t) =21is_not_nan x /\ not is_plus_infinity x /\ not is_minus_infinity x22
The dyadic domain of a float node: the real of the float, when it is finite
predicate fmem (x: t) (d: D.t) = finite x -> D.mem (to_real x) d26
The floats of a format: multiples of 2^umin with psb bits
let function float_dom (umin psb: int) : D.t = D.mk (Some umin) None (Some psb)29Valid (49ms) (colibri2)30let ghost float_mem (x: t) : unit31ensures { fmem x (float_dom (ulp_min (fmt x)) (sb (fmt x))) }32= {proof…} if pure { finite x } then float_dyadic x {qed}33
lo (rnd r) >= max (umin, min (lo r, umax)) (the rounding keeps the multiples of 2^e, e <= umax), prec (rnd r) <= sb.
Valid (402ms) (colibri2)let round_fwd0 (ghost fm: format) (umin umax psb: int) (d: D.t) : D.t39requires { umin = ulp_min fm /\ umax = ulp_max fm /\ psb = sb fm }40ensures { forall m z r. D.mem r d -> finite (round_f fm m z (Fin r)) ->41D.mem (to_real (round_f fm m z (Fin r))) result }42= let l = match d.D.lo with43| None -> umin44| Some k ->45let k = if k <= umax then k else umax in46if k <= umin then umin else k47end in48{proof…}49let lemma fwd (m: mode) (z: bool) (r: real)50requires { D.mem r d /\ finite (round_f fm m z (Fin r)) }51ensures { D.mem (to_real (round_f fm m z (Fin r))) (D.mk (Some l) None (Some psb)) }52= let x = round_f fm m z (Fin r) in53float_dyadic x;54match d.D.lo with55| None -> ()56| Some k ->57let k' = if k <= umax then k else umax in58D.fix_mono r k' k;59round_fix fm m z r k'60end in61{qed}62D.mk (Some l) None (Some psb)63
A nonzero r that rounds to zero is below 2^umin, so not a multiple of 2^umin; one that rounds to a nonzero float keeps the multiples of 2^e, e <= umax: hi r <= max (hi (rnd r), umin - 1) when this bound plus one is at most umax.
Valid (221ms) (colibri2)let round_back (ghost fm: format) (umin umax: int) (dr: D.t) : D.t71requires { umin = ulp_min fm /\ umax = ulp_max fm }72ensures { forall m z r. finite (round_f fm m z (Fin r)) ->73D.mem (to_real (round_f fm m z (Fin r))) dr -> D.mem r result }74= match dr.D.hi with75| None -> D.top76| Some h ->77let k = if h <= umin - 1 then umin - 1 else h in78if k + 1 <= umax then begin79{proof…}80let lemma back (m: mode) (z: bool) (r: real)81requires { finite (round_f fm m z (Fin r)) }82requires { D.mem (to_real (round_f fm m z (Fin r))) dr }83ensures { D.mem_hi r (Some k) }84= let x = round_f fm m z (Fin r) in85if pure { r <> 0.0 /\ D.fix r (k + 1) } then begin86if is_zero x then begin87round_zero_small fm m z r;88D.fix_mono r umin (k + 1);89D.fix_nz_ge r umin;90absurd91end else begin92assert { ext x = Fin (to_real x) };93assert { to_real x <> 0.0 };94round_fix fm m z r (k + 1);95D.fix_mono (to_real x) (h + 1) (k + 1);96absurd97end98end in99{qed}100D.mk None (Some k) None101end else D.top102end103
The values of d are multiples of 2^umin with psb bits
Valid (127ms) (colibri2)let is_exact (umin psb: int) (d: D.t) : bool108ensures { result -> forall r. D.mem r d -> D.fix r umin /\ D.flt r psb }109= match d.D.lo, d.D.prec with110| Some l, Some p ->111if umin <= l && 0 <= p && p <= psb then begin112{proof…} D.fix_mono_all umin l; D.flt_mono_all p psb; {qed}113true114end else false115| _ -> false116end117
Such a real, at most the largest finite float, rounds to itself (Flocq's round_generic, src/Core/Generic_fmt.v)
Valid (745ms) (colibri2)let ghost round_exact (fm: format) (m: mode) (z: bool) (r: real) : unit121requires { D.fix r (ulp_min fm) /\ D.flt r (sb fm) }122requires { -. to_real (of_index fm (nb_sign fm - 2)) <=. r <=.123to_real (of_index fm (nb_sign fm - 2)) }124ensures { finite (round_f fm m z (Fin r)) }125ensures { to_real (round_f fm m z (Fin r)) = r }126= {proof…}127let y = dyadic_exact fm r in128let zy = if is_positive y then false else true in129assert { round_f fm m zy (ext y) = y };130round_f_val fm m z zy (Fin r);131assert { ext (round_f fm m z (Fin r)) = Fin r }132{qed}133
The values of d are floats of the format below 2^(umax+sb-1): the rounding is exact (Gappa's EQL(rnd(a), a)), the result keeps the domain of d
Valid (83ms) (cvc5)let round_fwd (ghost fm: format) (umin umax psb: int) (d: D.t) : D.t138requires { umin = ulp_min fm /\ umax = ulp_max fm /\ psb = sb fm }139ensures { forall m z r. D.mem r d -> finite (round_f fm m z (Fin r)) ->140D.mem (to_real (round_f fm m z (Fin r))) result }141= let b = round_fwd0 fm umin umax psb d in142let small = match d.D.hi, d.D.prec with143| Some h, Some p -> 0 <= p && h + p <= umax + psb - 1144| _ -> false end in145if small && is_exact umin psb d then begin146{proof…}147let lemma ex (m: mode) (z: bool) (r: real)148requires { D.mem r d /\ finite (round_f fm m z (Fin r)) }149ensures { to_real (round_f fm m z (Fin r)) = r }150= max_float_ge fm;151if pure { r <> 0.0 } then begin152D.mag r d;153match d.D.hi, d.D.prec with154| Some h, Some p -> pow2r_le (h + p) (umax + psb - 1)155| _ -> absurd156end157end;158round_exact fm m z r in159{qed}160D.inter b d161end else b162end163