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

BOLDO Sylvie's avatar
BOLDO Sylvie committed
5
Copyright (C) 2010-2011 Sylvie Boldo
6
#<br />#
BOLDO Sylvie's avatar
BOLDO Sylvie committed
7
Copyright (C) 2010-2011 Guillaume Melquiond
8 9 10 11 12 13 14 15 16 17 18 19

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
(** * What is a real number belonging to a format, and many properties. *)
21 22 23 24
Require Import Fcore_Raux.
Require Import Fcore_defs.
Require Import Fcore_rnd.
Require Import Fcore_float_prop.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
25

26
Section Generic.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
27 28 29

Variable beta : radix.

30
Notation bpow e := (bpow beta e).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
31

32 33
Section Format.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
34 35
Variable fexp : Z -> Z.

36
(** To be a good fexp *)
37 38 39

Class Valid_exp :=
  valid_exp :
40 41 42 43 44 45
  forall k : Z,
  ( (fexp k < k)%Z -> (fexp (k + 1) <= k)%Z ) /\
  ( (k <= fexp k)%Z ->
    (fexp (fexp k + 1) <= fexp k)%Z /\
    forall l : Z, (l <= fexp k)%Z -> fexp l = fexp k ).

46
Context { valid_exp_ : Valid_exp }.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
47

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
Theorem valid_exp_large :
  forall k l,
  (fexp k < k)%Z -> (k <= l)%Z ->
  (fexp l < l)%Z.
Proof.
intros k l Hk H.
replace l with (k + Z_of_nat (Zabs_nat (l - k)))%Z.
induction (Zabs_nat (l - k)).
now rewrite Zplus_0_r.
rewrite inj_S, <- Zplus_succ_r_reverse.
apply Zle_lt_succ.
now apply valid_exp_.
rewrite inj_Zabs_nat, Zabs_eq.
ring.
now apply Zle_minus_le_0.
Qed.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
65
Definition canonic_exp x :=
66
  fexp (ln_beta beta x).
67 68

Definition canonic (f : float beta) :=
BOLDO Sylvie's avatar
BOLDO Sylvie committed
69
  Fexp f = canonic_exp (F2R f).
70

71
Definition scaled_mantissa x :=
BOLDO Sylvie's avatar
BOLDO Sylvie committed
72
  (x * bpow (- canonic_exp x))%R.
73

Guillaume Melquiond's avatar
Guillaume Melquiond committed
74
Definition generic_format (x : R) :=
BOLDO Sylvie's avatar
BOLDO Sylvie committed
75
  x = F2R (Float beta (Ztrunc (scaled_mantissa x)) (canonic_exp x)).
76

77
(** Basic facts *)
Guillaume Melquiond's avatar
Guillaume Melquiond committed
78 79 80
Theorem generic_format_0 :
  generic_format 0.
Proof.
81
unfold generic_format, scaled_mantissa.
82 83 84 85 86
rewrite Rmult_0_l.
change (Ztrunc 0) with (Ztrunc (Z2R 0)).
now rewrite Ztrunc_Z2R, F2R_0.
Qed.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
87
Theorem canonic_exp_opp :
88
  forall x,
BOLDO Sylvie's avatar
BOLDO Sylvie committed
89
  canonic_exp (-x) = canonic_exp x.
90 91
Proof.
intros x.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
92
unfold canonic_exp.
93
now rewrite ln_beta_opp.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
94 95
Qed.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
96
Theorem canonic_exp_abs :
97
  forall x,
BOLDO Sylvie's avatar
BOLDO Sylvie committed
98
  canonic_exp (Rabs x) = canonic_exp x.
99 100
Proof.
intros x.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
101
unfold canonic_exp.
102 103 104
now rewrite ln_beta_abs.
Qed.

105 106 107 108 109
Theorem generic_format_bpow :
  forall e, (fexp (e + 1) <= e)%Z ->
  generic_format (bpow e).
Proof.
intros e H.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
110
unfold generic_format, scaled_mantissa, canonic_exp.
111
rewrite ln_beta_bpow.
112
rewrite <- bpow_plus.
113 114 115 116 117
rewrite <- (Z2R_Zpower beta (e + - fexp (e + 1))).
rewrite Ztrunc_Z2R.
rewrite <- F2R_bpow.
rewrite F2R_change_exp with (1 := H).
now rewrite Zmult_1_l.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
now apply Zle_minus_le_0.
Qed.

Theorem generic_format_bpow' :
  forall e, (fexp e <= e)%Z ->
  generic_format (bpow e).
Proof.
intros e He.
apply generic_format_bpow.
destruct (Zle_lt_or_eq _ _ He).
now apply valid_exp.
rewrite <- H.
apply valid_exp_.
rewrite H.
apply Zle_refl.
133 134
Qed.

135
Theorem generic_format_F2R :
136
  forall m e,
BOLDO Sylvie's avatar
BOLDO Sylvie committed
137
  ( m <> 0 -> canonic_exp (F2R (Float beta m e)) <= e )%Z ->
138 139 140
  generic_format (F2R (Float beta m e)).
Proof.
intros m e.
141 142 143 144
destruct (Z_eq_dec m 0) as [Zm|Zm].
intros _.
rewrite Zm, F2R_0.
apply generic_format_0.
145
unfold generic_format, scaled_mantissa.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
146
set (e' := canonic_exp (F2R (Float beta m e))).
147
intros He.
148
specialize (He Zm).
149
unfold F2R at 3. simpl.
150 151 152 153
rewrite  F2R_change_exp with (1 := He).
apply F2R_eq_compat.
rewrite Rmult_assoc, <- bpow_plus, <- Z2R_Zpower, <- Z2R_mult.
now rewrite Ztrunc_Z2R.
154 155 156 157 158 159 160 161 162 163
now apply Zle_left.
Qed.

Theorem canonic_opp :
  forall m e,
  canonic (Float beta m e) ->
  canonic (Float beta (-m) e).
Proof.
intros m e H.
unfold canonic.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
164
now rewrite F2R_Zopp, canonic_exp_opp.
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
Qed.

Theorem canonic_unicity :
  forall f1 f2,
  canonic f1 ->
  canonic f2 ->
  F2R f1 = F2R f2 ->
  f1 = f2.
Proof.
intros (m1, e1) (m2, e2).
unfold canonic. simpl.
intros H1 H2 H.
rewrite H in H1.
rewrite <- H2 in H1. clear H2.
rewrite H1 in H |- *.
apply (f_equal (fun m => Float beta m e2)).
apply F2R_eq_reg with (1 := H).
Qed.

184
Theorem scaled_mantissa_generic :
185 186
  forall x,
  generic_format x ->
187
  scaled_mantissa x = Z2R (Ztrunc (scaled_mantissa x)).
188 189
Proof.
intros x Hx.
190
unfold scaled_mantissa.
191 192
pattern x at 1 3 ; rewrite Hx.
unfold F2R. simpl.
193
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_r, Rmult_1_r.
194 195 196
now rewrite Ztrunc_Z2R.
Qed.

197
Theorem scaled_mantissa_mult_bpow :
198
  forall x,
BOLDO Sylvie's avatar
BOLDO Sylvie committed
199
  (scaled_mantissa x * bpow (canonic_exp x))%R = x.
200 201 202
Proof.
intros x.
unfold scaled_mantissa.
203
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_l.
204 205 206
apply Rmult_1_r.
Qed.

207 208 209 210 211 212
Theorem scaled_mantissa_0 :
  scaled_mantissa 0 = R0.
Proof.
apply Rmult_0_l.
Qed.

213 214 215 216 217 218
Theorem scaled_mantissa_opp :
  forall x,
  scaled_mantissa (-x) = (-scaled_mantissa x)%R.
Proof.
intros x.
unfold scaled_mantissa.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
219
rewrite canonic_exp_opp.
220 221 222
now rewrite Ropp_mult_distr_l_reverse.
Qed.

223 224 225 226 227 228
Theorem scaled_mantissa_abs :
  forall x,
  scaled_mantissa (Rabs x) = Rabs (scaled_mantissa x).
Proof.
intros x.
unfold scaled_mantissa.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
229
rewrite canonic_exp_abs, Rabs_mult.
230 231 232 233 234 235
apply f_equal.
apply sym_eq.
apply Rabs_pos_eq.
apply bpow_ge_0.
Qed.

236 237 238 239 240
Theorem generic_format_opp :
  forall x, generic_format x -> generic_format (-x).
Proof.
intros x Hx.
unfold generic_format.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
241
rewrite scaled_mantissa_opp, canonic_exp_opp.
242
rewrite Ztrunc_opp.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
243
rewrite F2R_Zopp.
244
now apply f_equal.
245 246
Qed.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
247 248 249 250 251
Theorem generic_format_abs :
  forall x, generic_format x -> generic_format (Rabs x).
Proof.
intros x Hx.
unfold generic_format.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
252
rewrite scaled_mantissa_abs, canonic_exp_abs.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
253
rewrite Ztrunc_abs.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
254
rewrite F2R_Zabs.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
255 256 257
now apply f_equal.
Qed.

258 259 260 261 262 263
Theorem generic_format_abs_inv :
  forall x, generic_format (Rabs x) -> generic_format x.
Proof.
intros x.
unfold generic_format, Rabs.
case Rcase_abs ; intros _.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
264
rewrite scaled_mantissa_opp, canonic_exp_opp, Ztrunc_opp.
265 266
intros H.
rewrite <- (Ropp_involutive x) at 1.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
267
rewrite H, F2R_Zopp.
268 269 270 271
apply Ropp_involutive.
easy.
Qed.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
272
Theorem canonic_exp_fexp :
273
  forall x ex,
274
  (bpow (ex - 1) <= Rabs x < bpow ex)%R ->
BOLDO Sylvie's avatar
BOLDO Sylvie committed
275
  canonic_exp x = fexp ex.
276 277
Proof.
intros x ex Hx.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
278
unfold canonic_exp.
279 280 281
now rewrite ln_beta_unique with (1 := Hx).
Qed.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
282
Theorem canonic_exp_fexp_pos :
283
  forall x ex,
284
  (bpow (ex - 1) <= x < bpow ex)%R ->
BOLDO Sylvie's avatar
BOLDO Sylvie committed
285
  canonic_exp x = fexp ex.
286 287
Proof.
intros x ex Hx.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
288
apply canonic_exp_fexp.
289 290
rewrite Rabs_pos_eq.
exact Hx.
291 292 293 294
apply Rle_trans with (2 := proj1 Hx).
apply bpow_ge_0.
Qed.

295
(** Properties when the real number is "small" (kind of subnormal) *)
296 297 298 299 300 301 302
Theorem mantissa_small_pos :
  forall x ex,
  (bpow (ex - 1) <= x < bpow ex)%R ->
  (ex <= fexp ex)%Z ->
  (0 < x * bpow (- fexp ex) < 1)%R.
Proof.
intros x ex Hx He.
303
split.
304 305 306 307 308 309
apply Rmult_lt_0_compat.
apply Rlt_le_trans with (2 := proj1 Hx).
apply bpow_gt_0.
apply bpow_gt_0.
apply Rmult_lt_reg_r with (bpow (fexp ex)).
apply bpow_gt_0.
310
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_l.
311 312
rewrite Rmult_1_r, Rmult_1_l.
apply Rlt_le_trans with (1 := proj2 Hx).
313
now apply bpow_le.
314 315
Qed.

316 317 318 319 320 321 322 323 324 325 326 327
Theorem scaled_mantissa_small :
  forall x ex,
  (Rabs x < bpow ex)%R ->
  (ex <= fexp ex)%Z ->
  (Rabs (scaled_mantissa x) < 1)%R.
Proof.
intros x ex Ex He.
destruct (Req_dec x 0) as [Zx|Zx].
rewrite Zx, scaled_mantissa_0, Rabs_R0.
now apply (Z2R_lt 0 1).
rewrite <- scaled_mantissa_abs.
unfold scaled_mantissa.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
328 329
rewrite canonic_exp_abs.
unfold canonic_exp.
330 331 332 333 334 335 336 337
destruct (ln_beta beta x) as (ex', Ex').
simpl.
specialize (Ex' Zx).
apply (mantissa_small_pos _ _ Ex').
assert (ex' <= fexp ex)%Z.
apply Zle_trans with (2 := He).
apply bpow_lt_bpow with beta.
now apply Rle_lt_trans with (2 := Ex).
338
now rewrite (proj2 (proj2 (valid_exp _) He)).
339 340
Qed.

341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
Theorem mantissa_DN_small_pos :
  forall x ex,
  (bpow (ex - 1) <= x < bpow ex)%R ->
  (ex <= fexp ex)%Z ->
  Zfloor (x * bpow (- fexp ex)) = Z0.
Proof.
intros x ex Hx He.
apply Zfloor_imp. simpl.
assert (H := mantissa_small_pos x ex Hx He).
split ; try apply Rlt_le ; apply H.
Qed.

Theorem mantissa_UP_small_pos :
  forall x ex,
  (bpow (ex - 1) <= x < bpow ex)%R ->
  (ex <= fexp ex)%Z ->
  Zceil (x * bpow (- fexp ex)) = 1%Z.
Proof.
intros x ex Hx He.
apply Zceil_imp. simpl.
assert (H := mantissa_small_pos x ex Hx He).
split ; try apply Rlt_le ; apply H.
363 364
Qed.

365
(** Generic facts about any format *)
366 367
Theorem generic_format_discrete :
  forall x m,
BOLDO Sylvie's avatar
BOLDO Sylvie committed
368
  let e := canonic_exp x in
369 370 371 372 373 374 375 376 377 378 379 380 381
  (F2R (Float beta m e) < x < F2R (Float beta (m + 1) e))%R ->
  ~ generic_format x.
Proof.
intros x m e (Hx,Hx2) Hf.
apply Rlt_not_le with (1 := Hx2). clear Hx2.
rewrite Hf.
fold e.
apply F2R_le_compat.
apply Zlt_le_succ.
apply lt_Z2R.
rewrite <- scaled_mantissa_generic with (1 := Hf).
apply Rmult_lt_reg_r with (bpow e).
apply bpow_gt_0.
382
now rewrite scaled_mantissa_mult_bpow.
383 384
Qed.

385 386 387 388 389 390
Theorem generic_format_canonic :
  forall f, canonic f ->
  generic_format (F2R f).
Proof.
intros (m, e) Hf.
unfold canonic in Hf. simpl in Hf.
391
unfold generic_format, scaled_mantissa.
392
rewrite <- Hf.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
393
apply F2R_eq_compat.
394
unfold F2R. simpl.
395
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_r, Rmult_1_r.
396 397 398
now rewrite Ztrunc_Z2R.
Qed.

399 400 401 402 403 404 405 406 407 408 409 410 411
Theorem generic_format_ge_bpow :
  forall emin,
  ( forall e, (emin <= fexp e)%Z ) ->
  forall x,
  (0 < x)%R ->
  generic_format x ->
  (bpow emin <= x)%R.
Proof.
intros emin Emin x Hx Fx.
rewrite Fx.
apply Rle_trans with (bpow (fexp (ln_beta beta x))).
now apply bpow_le.
apply bpow_le_F2R.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
412
apply F2R_gt_0_reg with beta (canonic_exp x).
413 414 415
now rewrite <- Fx.
Qed.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
416
Theorem abs_lt_bpow_prec:
417
  forall prec,
BOLDO Sylvie's avatar
BOLDO Sylvie committed
418
  (forall e, (e - prec <= fexp e)%Z) ->
419
  (* OK with FLX, FLT and FTZ *)
420
  forall x,
BOLDO Sylvie's avatar
BOLDO Sylvie committed
421 422
  (Rabs x < bpow (prec + canonic_exp x))%R.
intros prec Hp x.
423 424 425
case (Req_dec x 0); intros Hxz.
rewrite Hxz, Rabs_R0.
apply bpow_gt_0.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
426
unfold canonic_exp.
427 428 429
destruct (ln_beta beta x) as (ex,Ex) ; simpl.
specialize (Ex Hxz).
apply Rlt_le_trans with (1 := proj2 Ex).
430
apply bpow_le.
431
specialize (Hp ex).
432 433 434
omega.
Qed.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
435 436 437 438 439 440 441 442 443 444
Theorem generic_format_bpow_inv :
  forall e,
    generic_format (bpow e) ->
   (fexp e <= e)%Z.
Proof.
intros e He.
apply Znot_gt_le; intros He2.
assert (e+1 <= fexp (e+1))%Z.
replace (fexp (e+1)) with (fexp e).
omega.
445
destruct (valid_exp e) as (Y1,Y2).
BOLDO Sylvie's avatar
BOLDO Sylvie committed
446 447 448 449 450 451 452 453 454
apply sym_eq; apply Y2; omega.
absurd (bpow e=0)%R.
apply sym_not_eq; apply Rlt_not_eq.
apply bpow_gt_0.
rewrite He.
replace (Ztrunc (scaled_mantissa (bpow e))) with 0%Z.
apply F2R_0.
apply sym_eq.
rewrite Ztrunc_floor.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
455
unfold scaled_mantissa, canonic_exp.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
456 457 458 459 460 461
apply mantissa_DN_small_pos; trivial.
rewrite ln_beta_bpow.
split.
apply Req_le.
apply f_equal.
ring.
462
apply bpow_lt.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
463 464 465 466 467 468
omega.
now rewrite ln_beta_bpow.
unfold scaled_mantissa.
apply Rmult_le_pos; apply bpow_ge_0.
Qed.

469
Section Fcore_generic_round_pos.
470

BOLDO Sylvie's avatar
BOLDO Sylvie committed
471
(** Rounding functions: R -> Z *)
472 473 474 475

Variable rnd : R -> Z.

Class Valid_rnd := {
476
  Zrnd_le : forall x y, (x <= y)%R -> (rnd x <= rnd y)%Z ;
477
  Zrnd_Z2R : forall n, rnd (Z2R n) = n
478 479
}.

480
Context { valid_rnd : Valid_rnd }.
481

482
Theorem Zrnd_DN_or_UP :
483
  forall x, rnd x = Zfloor x \/ rnd x = Zceil x.
484
Proof.
485
intros x.
486
destruct (Zle_or_lt (rnd x) (Zfloor x)) as [Hx|Hx].
487 488
left.
apply Zle_antisym with (1 := Hx).
489
rewrite <- (Zrnd_Z2R (Zfloor x)).
490
apply Zrnd_le.
491 492 493
apply Zfloor_lb.
right.
apply Zle_antisym.
494
rewrite <- (Zrnd_Z2R (Zceil x)).
495
apply Zrnd_le.
496 497 498 499 500 501 502 503 504
apply Zceil_ub.
rewrite Zceil_floor_neq.
omega.
intros H.
rewrite <- H in Hx.
rewrite Zfloor_Z2R, Zrnd_Z2R in Hx.
apply Zlt_irrefl with (1 := Hx).
Qed.

505 506 507 508 509 510 511 512 513 514 515 516 517
Theorem Zrnd_ZR_or_AW :
  forall x, rnd x = Ztrunc x \/ rnd x = Zaway x.
Proof.
intros x.
unfold Ztrunc, Zaway.
destruct (Zrnd_DN_or_UP x) as [Hx|Hx] ;
  case Rlt_bool.
now right.
now left.
now left.
now right.
Qed.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
518
(** the most useful one: R -> F *)
519
Definition round x :=
BOLDO Sylvie's avatar
BOLDO Sylvie committed
520
  F2R (Float beta (rnd (scaled_mantissa x)) (canonic_exp x)).
521

522
Theorem round_le_pos :
523
  forall x y, (0 < x)%R -> (x <= y)%R -> (round x <= round y)%R.
524
Proof.
525
intros x y Hx Hxy.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
526
unfold round, scaled_mantissa, canonic_exp.
527 528 529 530 531 532 533 534 535 536
destruct (ln_beta beta x) as (ex, Hex). simpl.
destruct (ln_beta beta y) as (ey, Hey). simpl.
specialize (Hex (Rgt_not_eq _ _ Hx)).
specialize (Hey (Rgt_not_eq _ _ (Rlt_le_trans _ _ _ Hx Hxy))).
rewrite Rabs_pos_eq in Hex.
2: now apply Rlt_le.
rewrite Rabs_pos_eq in Hey.
2: apply Rle_trans with (2:=Hxy); now apply Rlt_le.
assert (He: (ex <= ey)%Z).
cut (ex - 1 < ey)%Z. omega.
537
apply (lt_bpow beta).
538 539 540 541
apply Rle_lt_trans with (1 := proj1 Hex).
apply Rle_lt_trans with (1 := Hxy).
apply Hey.
destruct (Zle_or_lt ey (fexp ey)) as [Hy1|Hy1].
542
rewrite (proj2 (proj2 (valid_exp ey) Hy1) ex).
543
apply F2R_le_compat.
544
apply Zrnd_le.
545 546 547 548 549 550
apply Rmult_le_compat_r.
apply bpow_ge_0.
exact Hxy.
now apply Zle_trans with ey.
destruct (Zle_lt_or_eq _ _ He) as [He'|He'].
destruct (Zle_or_lt ey (fexp ex)) as [Hx2|Hx2].
551
rewrite (proj2 (proj2 (valid_exp ex) (Zle_trans _ _ _ He Hx2)) ey Hx2).
552
apply F2R_le_compat.
553
apply Zrnd_le.
554 555 556
apply Rmult_le_compat_r.
apply bpow_ge_0.
exact Hxy.
557
apply Rle_trans with (F2R (Float beta (rnd (bpow (ey - 1) * bpow (- fexp ey))) (fexp ey))).
558
rewrite <- bpow_plus.
559 560 561 562 563
rewrite <- (Z2R_Zpower beta (ey - 1 + -fexp ey)). 2: omega.
rewrite Zrnd_Z2R.
destruct (Zle_or_lt ex (fexp ex)) as [Hx1|Hx1].
apply Rle_trans with (F2R (Float beta 1 (fexp ex))).
apply F2R_le_compat.
564
rewrite <- (Zrnd_Z2R 1).
565
apply Zrnd_le.
566 567 568 569
apply Rlt_le.
exact (proj2 (mantissa_small_pos _ _ Hex Hx1)).
unfold F2R. simpl.
rewrite Z2R_Zpower. 2: omega.
570
rewrite <- bpow_plus, Rmult_1_l.
571
apply bpow_le.
572
omega.
573
apply Rle_trans with (F2R (Float beta (rnd (bpow ex * bpow (- fexp ex))) (fexp ex))).
574
apply F2R_le_compat.
575
apply Zrnd_le.
576 577 578 579
apply Rmult_le_compat_r.
apply bpow_ge_0.
apply Rlt_le.
apply Hex.
580
rewrite <- bpow_plus.
581 582 583 584
rewrite <- Z2R_Zpower. 2: omega.
rewrite Zrnd_Z2R.
unfold F2R. simpl.
rewrite 2!Z2R_Zpower ; try omega.
585
rewrite <- 2!bpow_plus.
586
apply bpow_le.
587 588
omega.
apply F2R_le_compat.
589
apply Zrnd_le.
590 591 592 593 594
apply Rmult_le_compat_r.
apply bpow_ge_0.
apply Hey.
rewrite He'.
apply F2R_le_compat.
595
apply Zrnd_le.
596 597 598 599 600
apply Rmult_le_compat_r.
apply bpow_ge_0.
exact Hxy.
Qed.

601
Theorem round_generic :
602 603
  forall x,
  generic_format x ->
604
  round x = x.
605 606
Proof.
intros x Hx.
607
unfold round.
608 609 610 611 612
rewrite scaled_mantissa_generic with (1 := Hx).
rewrite Zrnd_Z2R.
now apply sym_eq.
Qed.

613 614
Theorem round_0 :
  round 0 = R0.
615
Proof.
616
unfold round, scaled_mantissa.
617 618 619 620 621 622
rewrite Rmult_0_l.
fold (Z2R 0).
rewrite Zrnd_Z2R.
apply F2R_0.
Qed.

623
Theorem round_bounded_large_pos :
624 625 626
  forall x ex,
  (fexp ex < ex)%Z ->
  (bpow (ex - 1) <= x < bpow ex)%R ->
627
  (bpow (ex - 1) <= round x <= bpow ex)%R.
628 629
Proof.
intros x ex He Hx.
630
unfold round, scaled_mantissa.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
631
rewrite (canonic_exp_fexp_pos _ _ Hx).
632
unfold F2R. simpl.
633
destruct (Zrnd_DN_or_UP (x * bpow (- fexp ex))) as [Hr|Hr] ; rewrite Hr.
634 635 636
(* DN *)
split.
replace (ex - 1)%Z with (ex - 1 + - fexp ex + fexp ex)%Z by ring.
637
rewrite bpow_plus.
638 639
apply Rmult_le_compat_r.
apply bpow_ge_0.
640
assert (Hf: Z2R (Zpower beta (ex - 1 - fexp ex)) = bpow (ex - 1 + - fexp ex)).
641 642 643 644 645 646
apply Z2R_Zpower.
omega.
rewrite <- Hf.
apply Z2R_le.
apply Zfloor_lub.
rewrite Hf.
647
rewrite bpow_plus.
648 649 650 651 652 653
apply Rmult_le_compat_r.
apply bpow_ge_0.
apply Hx.
apply Rle_trans with (2 := Rlt_le _ _ (proj2 Hx)).
apply Rmult_le_reg_r with (bpow (- fexp ex)).
apply bpow_gt_0.
654
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_r, Rmult_1_r.
655 656 657 658 659 660
apply Zfloor_lb.
(* UP *)
split.
apply Rle_trans with (1 := proj1 Hx).
apply Rmult_le_reg_r with (bpow (- fexp ex)).
apply bpow_gt_0.
661
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_r, Rmult_1_r.
662 663
apply Zceil_ub.
pattern ex at 3 ; replace ex with (ex - fexp ex + fexp ex)%Z by ring.
664
rewrite bpow_plus.
665 666
apply Rmult_le_compat_r.
apply bpow_ge_0.
667
assert (Hf: Z2R (Zpower beta (ex - fexp ex)) = bpow (ex - fexp ex)).
668 669 670 671 672 673 674
apply Z2R_Zpower.
omega.
rewrite <- Hf.
apply Z2R_le.
apply Zceil_glb.
rewrite Hf.
unfold Zminus.
675
rewrite bpow_plus.
676 677 678 679 680 681
apply Rmult_le_compat_r.
apply bpow_ge_0.
apply Rlt_le.
apply Hx.
Qed.

682
Theorem round_bounded_small_pos :
683 684 685
  forall x ex,
  (ex <= fexp ex)%Z ->
  (bpow (ex - 1) <= x < bpow ex)%R ->
686
  round x = R0 \/ round x = bpow (fexp ex).
687 688
Proof.
intros x ex He Hx.
689
unfold round, scaled_mantissa.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
690
rewrite (canonic_exp_fexp_pos _ _ Hx).
691
unfold F2R. simpl.
692
destruct (Zrnd_DN_or_UP (x * bpow (-fexp ex))) as [Hr|Hr] ; rewrite Hr.
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
(* DN *)
left.
apply Rmult_eq_0_compat_r.
apply (@f_equal _ _ Z2R _ Z0).
apply Zfloor_imp.
refine (let H := _ in conj (Rlt_le _ _ (proj1 H)) (proj2 H)).
now apply mantissa_small_pos.
(* UP *)
right.
pattern (bpow (fexp ex)) at 2 ; rewrite <- Rmult_1_l.
apply (f_equal (fun m => (m * bpow (fexp ex))%R)).
apply (@f_equal _ _ Z2R _ 1%Z).
apply Zceil_imp.
refine (let H := _ in conj (proj1 H) (Rlt_le _ _ (proj2 H))).
now apply mantissa_small_pos.
Qed.

710
Theorem generic_format_round_pos :
711 712
  forall x,
  (0 < x)%R ->
713
  generic_format (round x).
714 715 716 717 718 719 720
Proof.
intros x Hx0.
destruct (ln_beta beta x) as (ex, Hex).
specialize (Hex (Rgt_not_eq _ _ Hx0)).
rewrite Rabs_pos_eq in Hex. 2: now apply Rlt_le.
destruct (Zle_or_lt ex (fexp ex)) as [He|He].
(* small *)
721
destruct (round_bounded_small_pos _ _ He Hex) as [Hr|Hr] ; rewrite Hr.
722 723
apply generic_format_0.
apply generic_format_bpow.
724
now apply valid_exp.
725
(* large *)
726
generalize (round_bounded_large_pos _ _ He Hex).
727
intros (Hr1, Hr2).
728
destruct (Rle_or_lt (bpow ex) (round x)) as [Hr|Hr].
729 730
rewrite <- (Rle_antisym _ _ Hr Hr2).
apply generic_format_bpow.
731
now apply valid_exp.
732 733
assert (Hr' := conj Hr1 Hr).
unfold generic_format, scaled_mantissa.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
734
rewrite (canonic_exp_fexp_pos _ _ Hr').
735
unfold round, scaled_mantissa.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
736
rewrite (canonic_exp_fexp_pos _ _ Hex).
737
unfold F2R at 3. simpl.
738
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_r, Rmult_1_r.
739 740 741
now rewrite Ztrunc_Z2R.
Qed.

742
End Fcore_generic_round_pos.
743

744
Theorem round_ext :
745
  forall rnd1 rnd2,
746
  ( forall x, rnd1 x = rnd2 x ) ->
747
  forall x,
748
  round rnd1 x = round rnd2 x.
749 750
Proof.
intros rnd1 rnd2 Hext x.
751
unfold round.
752 753 754
now rewrite Hext.
Qed.

755
Section Zround_opp.
756

757 758
Variable rnd : R -> Z.
Context { valid_rnd : Valid_rnd rnd }.
759

760
Definition Zrnd_opp x := Zopp (rnd (-x)).
761

762 763 764 765
Global Instance valid_rnd_opp : Valid_rnd Zrnd_opp.
Proof with auto with typeclass_instances.
split.
(* *)
766
intros x y Hxy.
767
unfold Zrnd_opp.
768 769
apply Zopp_le_cancel.
rewrite 2!Zopp_involutive.
770
apply Zrnd_le...
771
now apply Ropp_le_contravar.
772
(* *)
773
intros n.
774
unfold Zrnd_opp.
775
rewrite <- Z2R_opp, Zrnd_Z2R...
776 777 778
apply Zopp_involutive.
Qed.

779
Theorem round_opp :
780
  forall x,
781
  round rnd (- x) = Ropp (round Zrnd_opp x).
782 783
Proof.
intros x.
784
unfold round.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
785
rewrite <- F2R_Zopp, canonic_exp_opp, scaled_mantissa_opp.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
786
apply F2R_eq_compat.
787 788 789 790
apply sym_eq.
exact (Zopp_involutive _).
Qed.

791
End Zround_opp.
792

793
(** IEEE-754 roundings: up, down and to zero *)
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815

Global Instance valid_rnd_DN : Valid_rnd Zfloor.
Proof.
split.
apply Zfloor_le.
apply Zfloor_Z2R.
Qed.

Global Instance valid_rnd_UP : Valid_rnd Zceil.
Proof.
split.
apply Zceil_le.
apply Zceil_Z2R.
Qed.

Global Instance valid_rnd_ZR : Valid_rnd Ztrunc.
Proof.
split.
apply Ztrunc_le.
apply Ztrunc_Z2R.
Qed.

816 817 818 819 820 821 822
Global Instance valid_rnd_AW : Valid_rnd Zaway.
Proof.
split.
apply Zaway_le.
apply Zaway_Z2R.
Qed.

823 824 825 826
Section monotone.

Variable rnd : R -> Z.
Context { valid_rnd : Valid_rnd rnd }.
827

828
Theorem round_DN_or_UP :
829 830
  forall x,
  round rnd x = round Zfloor x \/ round rnd x = round Zceil x.
831
Proof.
832
intros x.
833
unfold round.
834
destruct (Zrnd_DN_or_UP rnd (scaled_mantissa x)) as [Hx|Hx].
835 836 837 838
left. now rewrite Hx.
right. now rewrite Hx.
Qed.

839 840 841 842 843 844 845 846 847 848 849
Theorem round_ZR_or_AW :
  forall x,
  round rnd x = round Ztrunc x \/ round rnd x = round Zaway x.
Proof.
intros x.
unfold round.
destruct (Zrnd_ZR_or_AW rnd (scaled_mantissa x)) as [Hx|Hx].
left. now rewrite Hx.
right. now rewrite Hx.
Qed.

850
Theorem round_le :
851 852 853
  forall x y, (x <= y)%R -> (round rnd x <= round rnd y)%R.
Proof with auto with typeclass_instances.
intros x y Hxy.
854
destruct (total_order_T x 0) as [[Hx|Hx]|Hx].
855
3: now apply round_le_pos.
856
(* x < 0 *)
857
unfold round.
858 859 860 861
destruct (Rlt_or_le y 0) as [Hy|Hy].
(* . y < 0 *)
rewrite <- (Ropp_involutive x), <- (Ropp_involutive y).
rewrite (scaled_mantissa_opp (-x)), (scaled_mantissa_opp (-y)).
BOLDO Sylvie's avatar
BOLDO Sylvie committed
862
rewrite (canonic_exp_opp (-x)), (canonic_exp_opp (-y)).
863
apply Ropp_le_cancel.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
864
rewrite <- 2!F2R_Zopp.
865
apply (round_le_pos (Zrnd_opp rnd) (-y) (-x)).
866 867 868 869 870 871
rewrite <- Ropp_0.
now apply Ropp_lt_contravar.
now apply Ropp_le_contravar.
(* . 0 <= y *)
apply Rle_trans with R0.
apply F2R_le_0_compat. simpl.
872
rewrite <- (Zrnd_Z2R rnd 0).
873
apply Zrnd_le...
874
simpl.
875
rewrite <- (Rmult_0_l (bpow (- fexp (ln_beta beta x)))).
876 877 878 879
apply Rmult_le_compat_r.
apply bpow_ge_0.
now apply Rlt_le.
apply F2R_ge_0_compat. simpl.
880
rewrite <- (Zrnd_Z2R rnd 0).
881
apply Zrnd_le...
882 883 884 885 886
apply Rmult_le_pos.
exact Hy.
apply bpow_ge_0.
(* x = 0 *)
rewrite Hx.
887
rewrite round_0...
888 889
apply F2R_ge_0_compat.
simpl.
890
rewrite <- (Zrnd_Z2R rnd 0).
891
apply Zrnd_le...
892 893 894 895 896
apply Rmult_le_pos.
now rewrite <- Hx.
apply bpow_ge_0.
Qed.

897
Theorem round_ge_generic :
898
  forall x y, generic_format x -> (x <= y)%R -> (x <= round rnd y)%R.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
899
Proof.
900
intros x y Hx Hxy.
901
rewrite <- (round_generic rnd x Hx).
902
now apply round_le.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
903
Qed.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
904

905
Theorem round_le_generic :
906
  forall x y, generic_format y -> (x <= y)%R -> (round rnd x <= y)%R.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
907
Proof.
908
intros x y Hy Hxy.
909
rewrite <- (round_generic rnd y Hy).
910
now apply round_le.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
911
Qed.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
912

913 914
End monotone.

915
Theorem round_abs_abs' :
916
  forall P : R -> R -> Prop,
917
  ( forall rnd (Hr : Valid_rnd rnd) x, (0 <= x)%R -> P x (round rnd x) ) ->
918 919 920
  forall rnd {Hr : Valid_rnd rnd} x, P (Rabs x) (Rabs (round rnd x)).
Proof with auto with typeclass_instances.
intros P HP rnd Hr x.
921 922 923
destruct (Rle_or_lt 0 x) as [Hx|Hx].
(* . *)
rewrite 2!Rabs_pos_eq.
924
now apply HP.
925
rewrite <- (round_0 rnd).
926
now apply round_le.
927 928 929 930 931
exact Hx.
(* . *)
rewrite (Rabs_left _ Hx).