Fcore_FLT.v 7.29 KB
Newer Older
1
(**
2 3 4 5
This file is part of the Flocq formalization of floating-point
arithmetic in Coq: http://flocq.gforge.inria.fr/

Copyright (C) 2010 Sylvie Boldo
6
#<br />#
7 8 9 10 11 12 13 14 15 16 17 18 19
Copyright (C) 2010 Guillaume Melquiond

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
COPYING file for more details.
*)

20
(** * Floating-point format with gradual underflow *)
21 22 23 24 25 26 27 28
Require Import Fcore_Raux.
Require Import Fcore_defs.
Require Import Fcore_rnd.
Require Import Fcore_generic_fmt.
Require Import Fcore_float_prop.
Require Import Fcore_FLX.
Require Import Fcore_FIX.
Require Import Fcore_rnd_ne.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
29 30 31 32 33

Section RND_FLT.

Variable beta : radix.

34
Notation bpow e := (bpow beta e).
BOLDO Sylvie's avatar
BOLDO Sylvie committed
35 36

Variable emin prec : Z.
37 38

Context { prec_gt_0_ : Prec_gt_0 prec }.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
39

40 41 42
(* floating-point format with gradual underflow *)
Definition FLT_format (x : R) :=
  exists f : float beta,
43
  x = F2R f /\ (Zabs (Fnum f) < Zpower beta prec)%Z /\ (emin <= Fexp f)%Z.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
44

45 46
Definition FLT_exp e := Zmax (e - prec) emin.

47
(** Properties of the FLT format *)
48
Global Instance FLT_exp_valid : Valid_exp FLT_exp.
49 50 51
Proof.
intros k.
unfold FLT_exp.
52 53 54
generalize (prec_gt_0 prec).
repeat split ;
  intros ; zify ; omega.
55 56 57
Qed.

Theorem FLT_format_generic :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
58
  forall x : R, FLT_format x <-> generic_format beta FLT_exp x.
59
Proof.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
60
split.
61
(* . *)
Guillaume Melquiond's avatar
Guillaume Melquiond committed
62 63 64 65 66 67
intros ((xm, xe), (Hx1, (Hx2, Hx3))).
destruct (Req_dec x 0) as [Hx4|Hx4].
rewrite Hx4.
apply generic_format_0.
destruct (ln_beta beta x) as (ex, Hx5).
specialize (Hx5 Hx4).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
68
rewrite Hx1.
69 70 71 72 73 74
apply generic_format_canonic_exponent.
rewrite <- Hx1.
rewrite canonic_exponent_fexp with (1 := Hx5).
unfold FLT_exp.
apply Zmax_lub. 2: exact Hx3.
cut (ex -1 < prec + xe)%Z. omega.
75
apply (lt_bpow beta).
76 77 78 79 80 81 82 83
apply Rle_lt_trans with (1 := proj1 Hx5).
rewrite Hx1.
apply F2R_lt_bpow.
simpl.
now ring_simplify (prec + xe - xe)%Z.
(* . *)
unfold generic_format.
set (ex := canonic_exponent beta FLT_exp x).
84
set (mx := Ztrunc (scaled_mantissa beta FLT_exp x)).
85 86 87
intros Hx.
rewrite Hx.
eexists ; repeat split ; simpl.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
88
apply lt_Z2R.
89 90
rewrite Z2R_Zpower. 2: now apply Zlt_le_weak.
apply Rmult_lt_reg_r with (bpow ex).
91
apply bpow_gt_0.
92
rewrite <- bpow_plus.
93 94 95 96 97 98 99 100 101 102 103
change (F2R (Float beta (Zabs mx) ex) < bpow (prec + ex))%R.
rewrite <- abs_F2R.
rewrite <- Hx.
destruct (Req_dec x 0) as [Hx0|Hx0].
rewrite Hx0, Rabs_R0.
apply bpow_gt_0.
unfold canonic_exponent in ex.
destruct (ln_beta beta x) as (ex', He).
simpl in ex.
specialize (He Hx0).
apply Rlt_le_trans with (1 := proj2 He).
104
apply bpow_le.
105 106
cut (ex' - prec <= ex)%Z. omega.
unfold ex, FLT_exp.
107 108
apply Zle_max_l.
apply Zle_max_r.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
109 110
Qed.

111 112 113
Theorem FLT_format_satisfies_any :
  satisfies_any FLT_format.
Proof.
114
refine (satisfies_any_eq _ _ _ (generic_format_satisfies_any beta FLT_exp)).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
115 116 117
intros x.
apply iff_sym.
apply FLT_format_generic.
118
Qed.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
119

120
Theorem FLT_canonic_FLX :
121
  forall x, x <> R0 ->
122
  (bpow (emin + prec - 1) <= Rabs x)%R ->
123
  canonic_exponent beta FLT_exp x = canonic_exponent beta (FLX_exp prec) x.
124
Proof.
125 126
intros x Hx0 Hx.
unfold canonic_exponent.
127 128
apply Zmax_left.
destruct (ln_beta beta x) as (ex, He).
129 130 131
unfold FLX_exp. simpl.
specialize (He Hx0).
cut (emin + prec - 1 < ex)%Z. omega.
132
apply (lt_bpow beta).
133 134 135 136
apply Rle_lt_trans with (1 := Hx).
apply He.
Qed.

137
(** Links between FLT and FLX *)
Guillaume Melquiond's avatar
Guillaume Melquiond committed
138 139 140 141 142 143
Theorem FLT_generic_format_FLX :
  forall x : R,
  (bpow (emin + prec - 1) <= Rabs x)%R ->
  ( generic_format beta FLT_exp x <-> generic_format beta (FLX_exp prec) x ).
Proof.
intros x Hx.
144 145 146
destruct (Req_dec x 0) as [Hx0|Hx0].
rewrite Hx0.
split ; intros _ ;  apply generic_format_0.
147
unfold generic_format, scaled_mantissa.
148
now rewrite (FLT_canonic_FLX x Hx0 Hx).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
149 150
Qed.

151 152 153 154 155 156 157 158 159 160 161 162 163 164

Theorem FLX_generic_format_FLT :
  forall x : R,
  generic_format beta FLT_exp x -> generic_format beta (FLX_exp prec) x.
Proof.
intros x Hx.
unfold generic_format in Hx; rewrite Hx.
apply generic_format_canonic_exponent.
rewrite <- Hx.
unfold canonic_exponent, FLX_exp, FLT_exp.
apply Zle_max_l.
Qed.


Guillaume Melquiond's avatar
Guillaume Melquiond committed
165 166 167 168 169 170 171 172
Theorem FLT_format_FLX :
  forall x : R,
  (bpow (emin + prec - 1) <= Rabs x)%R ->
  ( FLT_format x <-> FLX_format beta prec x ).
Proof.
intros x Hx1.
apply iff_trans with (1 := FLT_format_generic x).
apply iff_trans with (1 := FLT_generic_format_FLX x Hx1).
173
split.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
174
now apply FLX_format_generic.
175
now apply generic_format_FLX.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
176 177
Qed.

178
Theorem FLT_round_FLX : forall rnd x,
179
  (bpow (emin + prec - 1) <= Rabs x)%R ->
180
  round beta FLT_exp rnd x = round beta (FLX_exp prec) rnd x.
181
intros rnd x Hx.
182
unfold round, scaled_mantissa.
183 184 185 186 187 188 189
rewrite ->FLT_canonic_FLX; trivial.
intros H; contradict Hx.
rewrite H, Rabs_R0; apply Rlt_not_le.
apply bpow_gt_0.
Qed.


190
(** Links between FLT and FIX (underflow) *)
191
Theorem FLT_canonic_FIX :
192 193
  forall x, x <> R0 ->
  (Rabs x < bpow (emin + prec))%R ->
194
  canonic_exponent beta FLT_exp x = canonic_exponent beta (FIX_exp emin) x.
195 196
Proof.
intros x Hx0 Hx.
197 198 199
unfold canonic_exponent.
apply Zmax_right.
unfold FIX_exp.
200 201 202
destruct (ln_beta beta x) as (ex, Hex).
simpl.
cut (ex - 1 < emin + prec)%Z. omega.
203
apply (lt_bpow beta).
204 205 206 207
apply Rle_lt_trans with (2 := Hx).
now apply Hex.
Qed.

208 209 210 211
Theorem FLT_generic_format_FIX :
  forall x : R,
  (Rabs x <= bpow (emin + prec))%R ->
  ( generic_format beta FLT_exp x <-> generic_format beta (FIX_exp emin) x ).
212
Proof.
213 214 215 216 217
intros x Hx.
destruct (Req_dec x 0) as [Hx0|Hx0].
rewrite Hx0.
split ; intros _ ;  apply generic_format_0.
destruct Hx as [Hx|Hx].
218
unfold generic_format, scaled_mantissa.
219 220 221 222 223 224 225 226
now rewrite (FLT_canonic_FIX x Hx0 Hx).
(* extra case *)
rewrite <- (Rabs_pos_eq (bpow (emin + prec))) in Hx. 2: apply bpow_ge_0.
assert (H1: generic_format beta FLT_exp (bpow (emin + prec))).
rewrite <- F2R_bpow.
apply generic_format_canonic_exponent.
unfold generic_format, canonic_exponent, FLT_exp.
rewrite F2R_bpow, ln_beta_bpow.
227
assert (Hp := prec_gt_0 prec).
228 229 230 231 232
apply Zmax_lub ; omega.
assert (H2: generic_format beta (FIX_exp emin) (bpow (emin + prec))).
rewrite <- F2R_bpow.
apply generic_format_canonic_exponent.
unfold generic_format, canonic_exponent, FIX_exp.
233 234
generalize (prec_gt_0 prec).
clear ; omega.
235 236 237 238
destruct Rabs_eq_Rabs with (1 := Hx) as [H|H] ;
  rewrite H ; clear H ;
  split ; intros _ ;
  try apply generic_format_opp ; easy.
239 240
Qed.

241 242 243 244 245
Theorem FLT_format_FIX :
  forall x,
  (Rabs x <= bpow (emin + prec))%R ->
  ( FLT_format x <-> FIX_format beta emin x ).
Proof.
246 247 248
intros x Hx1.
apply iff_trans with (1 := FLT_format_generic x).
apply iff_trans with (1 := FLT_generic_format_FIX x Hx1).
249 250 251
split.
apply FIX_format_generic.
apply generic_format_FIX.
252 253
Qed.

254
(** FLT is a nice format: it has a monotone exponent... *)
255
Global Instance FLT_exp_monotone : Monotone_exp FLT_exp.
256
Proof.
257
intros ex ey.
258
unfold FLT_exp.
259
zify ; omega.
260 261
Qed.

262
(** and it allows a rounding to nearest, ties to even. *)
263
Hypothesis NE_prop : Zeven beta = false \/ (1 < prec)%Z.
264

265
Global Instance exists_NE_FLT : Exists_NE beta FLT_exp.
266
Proof.
267
destruct NE_prop as [H|H].
268 269 270 271
now left.
right.
intros e.
unfold FLT_exp.
272 273
destruct (Zmax_spec (e - prec) emin) as [(H1,H2)|(H1,H2)] ;
  rewrite H2 ; clear H2.
274 275 276 277 278 279 280 281
generalize (Zmax_spec (e + 1 - prec) emin).
generalize (Zmax_spec (e - prec + 1 - prec) emin).
omega.
generalize (Zmax_spec (e + 1 - prec) emin).
generalize (Zmax_spec (emin + 1 - prec) emin).
omega.
Qed.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
282
End RND_FLT.