Fcore_FLT.v 6.65 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
Qed.

57 58
Theorem generic_format_FLT :
  forall x, FLT_format x -> generic_format beta FLT_exp x.
59
Proof.
60
clear prec_gt_0_.
61 62 63 64 65
intros x ((mx, ex), (H1, (H2, H3))).
simpl in H2, H3.
rewrite H1.
destruct (Z_eq_dec mx 0) as [Zmx|Zmx].
rewrite Zmx, F2R_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
66
apply generic_format_0.
67
apply generic_format_canonic_exponent.
68 69 70 71 72 73
unfold canonic_exponent, FLT_exp.
rewrite ln_beta_F2R with (1 := Zmx).
apply Zmax_lub with (2 := H3).
apply Zplus_le_reg_r with (prec - ex)%Z.
ring_simplify.
now apply ln_beta_Z2R_le.
74 75 76 77 78 79
Qed.

Theorem FLT_format_generic :
  forall x, generic_format beta FLT_exp x -> FLT_format x.
Proof.
intros x.
80 81
unfold generic_format.
set (ex := canonic_exponent beta FLT_exp x).
82
set (mx := Ztrunc (scaled_mantissa beta FLT_exp x)).
83 84 85
intros Hx.
rewrite Hx.
eexists ; repeat split ; simpl.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
86
apply lt_Z2R.
87 88
rewrite Z2R_Zpower. 2: now apply Zlt_le_weak.
apply Rmult_lt_reg_r with (bpow ex).
89
apply bpow_gt_0.
90
rewrite <- bpow_plus.
91 92 93 94 95 96 97 98 99 100 101
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).
102
apply bpow_le.
103 104
cut (ex' - prec <= ex)%Z. omega.
unfold ex, FLT_exp.
105 106
apply Zle_max_l.
apply Zle_max_r.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
107 108
Qed.

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

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

136
(** Links between FLT and FLX *)
Guillaume Melquiond's avatar
Guillaume Melquiond committed
137 138 139
Theorem FLT_generic_format_FLX :
  forall x : R,
  (bpow (emin + prec - 1) <= Rabs x)%R ->
140 141
  generic_format beta (FLX_exp prec) x ->
  generic_format beta FLT_exp x.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
142
Proof.
143
intros x Hx H.
144 145
destruct (Req_dec x 0) as [Hx0|Hx0].
rewrite Hx0.
146
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
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.

163
Theorem FLT_round_FLX : forall rnd x,
164
  (bpow (emin + prec - 1) <= Rabs x)%R ->
165
  round beta FLT_exp rnd x = round beta (FLX_exp prec) rnd x.
166
intros rnd x Hx.
167
unfold round, scaled_mantissa.
168 169 170 171 172 173
rewrite ->FLT_canonic_FLX; trivial.
intros H; contradict Hx.
rewrite H, Rabs_R0; apply Rlt_not_le.
apply bpow_gt_0.
Qed.

174
(** Links between FLT and FIX (underflow) *)
175
Theorem FLT_canonic_FIX :
176 177
  forall x, x <> R0 ->
  (Rabs x < bpow (emin + prec))%R ->
178
  canonic_exponent beta FLT_exp x = canonic_exponent beta (FIX_exp emin) x.
179 180
Proof.
intros x Hx0 Hx.
181 182 183
unfold canonic_exponent.
apply Zmax_right.
unfold FIX_exp.
184 185 186
destruct (ln_beta beta x) as (ex, Hex).
simpl.
cut (ex - 1 < emin + prec)%Z. omega.
187
apply (lt_bpow beta).
188 189 190 191
apply Rle_lt_trans with (2 := Hx).
now apply Hex.
Qed.

192 193 194 195 196 197 198 199 200 201 202 203
Theorem FIX_generic_format_FLT :
  forall x : R,
  generic_format beta FLT_exp x ->
  generic_format beta (FIX_exp emin) x.
Proof.
intros x Hx.
rewrite Hx.
apply generic_format_canonic_exponent.
rewrite <- Hx.
apply Zle_max_r.
Qed.

204 205 206
Theorem FLT_generic_format_FIX :
  forall x : R,
  (Rabs x <= bpow (emin + prec))%R ->
207 208
  generic_format beta (FIX_exp emin) x ->
  generic_format beta FLT_exp x.
209
Proof.
210
intros x Hx H'.
211 212
destruct (Req_dec x 0) as [Hx0|Hx0].
rewrite Hx0.
213
apply generic_format_0.
214
destruct Hx as [Hx|Hx].
215
unfold generic_format, scaled_mantissa.
216 217 218 219
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))).
220 221
apply generic_format_bpow.
unfold FLT_exp.
222
assert (Hp := prec_gt_0 prec).
223
apply Zmax_lub ; clear -Hp ; omega.
224
assert (H2: generic_format beta (FIX_exp emin) (bpow (emin + prec))).
225 226
apply generic_format_bpow.
unfold FIX_exp.
227 228
generalize (prec_gt_0 prec).
clear ; omega.
229 230 231
destruct Rabs_eq_Rabs with (1 := Hx) as [H|H] ;
  rewrite H ; clear H ;
  try apply generic_format_opp ; easy.
232 233
Qed.

234
(** FLT is a nice format: it has a monotone exponent... *)
235
Global Instance FLT_exp_monotone : Monotone_exp FLT_exp.
236
Proof.
237
intros ex ey.
238
unfold FLT_exp.
239
zify ; omega.
240 241
Qed.

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

245
Global Instance exists_NE_FLT : Exists_NE beta FLT_exp.
246
Proof.
247
destruct NE_prop as [H|H].
248 249 250 251
now left.
right.
intros e.
unfold FLT_exp.
252 253
destruct (Zmax_spec (e - prec) emin) as [(H1,H2)|(H1,H2)] ;
  rewrite H2 ; clear H2.
254 255 256 257 258 259 260 261
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
262
End RND_FLT.