Fcore_generic_fmt.v 39 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
(** * 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 27 28 29

Section RND_generic.

Variable beta : radix.

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

Variable fexp : Z -> Z.

34
(** To be a good fexp *)
35 36 37 38 39 40 41 42
Definition valid_exp :=
  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 ).

Variable prop_exp : valid_exp.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
43

44
Definition canonic_exponent x :=
45
  fexp (ln_beta beta x).
46 47 48

Definition canonic (f : float beta) :=
  Fexp f = canonic_exponent (F2R f).
49

50 51 52
Definition scaled_mantissa x :=
  (x * bpow (- canonic_exponent x))%R.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
53
Definition generic_format (x : R) :=
54
  x = F2R (Float beta (Ztrunc (scaled_mantissa x)) (canonic_exponent x)).
55

56
(** Basic facts *)
Guillaume Melquiond's avatar
Guillaume Melquiond committed
57 58 59
Theorem generic_format_0 :
  generic_format 0.
Proof.
60
unfold generic_format, scaled_mantissa.
61 62 63 64 65 66 67 68 69 70 71 72
rewrite Rmult_0_l.
change (Ztrunc 0) with (Ztrunc (Z2R 0)).
now rewrite Ztrunc_Z2R, F2R_0.
Qed.

Theorem canonic_exponent_opp :
  forall x,
  canonic_exponent (-x) = canonic_exponent x.
Proof.
intros x.
unfold canonic_exponent.
now rewrite ln_beta_opp.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
73 74
Qed.

75 76 77 78 79 80 81 82 83
Theorem canonic_exponent_abs :
  forall x,
  canonic_exponent (Rabs x) = canonic_exponent x.
Proof.
intros x.
unfold canonic_exponent.
now rewrite ln_beta_abs.
Qed.

84 85 86 87 88
Theorem generic_format_bpow :
  forall e, (fexp (e + 1) <= e)%Z ->
  generic_format (bpow e).
Proof.
intros e H.
89
unfold generic_format, scaled_mantissa, canonic_exponent.
90
rewrite ln_beta_bpow.
91
rewrite <- bpow_plus.
92 93 94 95 96 97 98 99 100 101 102 103 104 105
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.
omega.
Qed.

Theorem generic_format_canonic_exponent :
  forall m e,
  (canonic_exponent (F2R (Float beta m e)) <= e)%Z ->
  generic_format (F2R (Float beta m e)).
Proof.
intros m e.
106
unfold generic_format, scaled_mantissa.
107 108 109
set (e' := canonic_exponent (F2R (Float beta m e))).
intros He.
unfold F2R at 3. simpl.
110
assert (H: (Z2R m * bpow e * bpow (- e') = Z2R (m * Zpower beta (e + -e')))%R).
111
rewrite Rmult_assoc, <- bpow_plus, Z2R_mult.
112 113 114 115 116 117
rewrite Z2R_Zpower.
apply refl_equal.
now apply Zle_left.
rewrite H, Ztrunc_Z2R.
unfold F2R. simpl.
rewrite <- H.
118
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_l.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
now rewrite Rmult_1_r.
Qed.

Theorem canonic_opp :
  forall m e,
  canonic (Float beta m e) ->
  canonic (Float beta (-m) e).
Proof.
intros m e H.
unfold canonic.
now rewrite <- opp_F2R, canonic_exponent_opp.
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.

149
Theorem scaled_mantissa_generic :
150 151
  forall x,
  generic_format x ->
152
  scaled_mantissa x = Z2R (Ztrunc (scaled_mantissa x)).
153 154
Proof.
intros x Hx.
155
unfold scaled_mantissa.
156 157
pattern x at 1 3 ; rewrite Hx.
unfold F2R. simpl.
158
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_r, Rmult_1_r.
159 160 161
now rewrite Ztrunc_Z2R.
Qed.

162 163 164 165 166 167
Theorem scaled_mantissa_bpow :
  forall x,
  (scaled_mantissa x * bpow (canonic_exponent x))%R = x.
Proof.
intros x.
unfold scaled_mantissa.
168
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_l.
169 170 171
apply Rmult_1_r.
Qed.

172 173 174 175 176 177
Theorem scaled_mantissa_0 :
  scaled_mantissa 0 = R0.
Proof.
apply Rmult_0_l.
Qed.

178 179 180 181 182 183 184 185 186 187
Theorem scaled_mantissa_opp :
  forall x,
  scaled_mantissa (-x) = (-scaled_mantissa x)%R.
Proof.
intros x.
unfold scaled_mantissa.
rewrite canonic_exponent_opp.
now rewrite Ropp_mult_distr_l_reverse.
Qed.

188 189 190 191 192 193 194 195 196 197 198 199 200
Theorem scaled_mantissa_abs :
  forall x,
  scaled_mantissa (Rabs x) = Rabs (scaled_mantissa x).
Proof.
intros x.
unfold scaled_mantissa.
rewrite canonic_exponent_abs, Rabs_mult.
apply f_equal.
apply sym_eq.
apply Rabs_pos_eq.
apply bpow_ge_0.
Qed.

201 202 203 204 205
Theorem generic_format_opp :
  forall x, generic_format x -> generic_format (-x).
Proof.
intros x Hx.
unfold generic_format.
206 207 208 209
rewrite scaled_mantissa_opp, canonic_exponent_opp.
rewrite Ztrunc_opp.
rewrite <- opp_F2R.
now apply f_equal.
210 211
Qed.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
212 213 214 215 216 217 218 219 220 221 222
Theorem generic_format_abs :
  forall x, generic_format x -> generic_format (Rabs x).
Proof.
intros x Hx.
unfold generic_format.
rewrite scaled_mantissa_abs, canonic_exponent_abs.
rewrite Ztrunc_abs.
rewrite <- abs_F2R.
now apply f_equal.
Qed.

223
Theorem canonic_exponent_fexp :
224
  forall x ex,
225
  (bpow (ex - 1) <= Rabs x < bpow ex)%R ->
226 227 228 229 230 231 232
  canonic_exponent x = fexp ex.
Proof.
intros x ex Hx.
unfold canonic_exponent.
now rewrite ln_beta_unique with (1 := Hx).
Qed.

233
Theorem canonic_exponent_fexp_pos :
234
  forall x ex,
235
  (bpow (ex - 1) <= x < bpow ex)%R ->
236 237 238
  canonic_exponent x = fexp ex.
Proof.
intros x ex Hx.
239 240 241
apply canonic_exponent_fexp.
rewrite Rabs_pos_eq.
exact Hx.
242 243 244 245
apply Rle_trans with (2 := proj1 Hx).
apply bpow_ge_0.
Qed.

246
(** Properties when the real number is "small" (kind of subnormal) *)
247 248 249 250 251 252 253
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.
254
split.
255 256 257 258 259 260
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.
261
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_l.
262 263
rewrite Rmult_1_r, Rmult_1_l.
apply Rlt_le_trans with (1 := proj2 Hx).
264
now apply bpow_le.
265 266
Qed.

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
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.
rewrite canonic_exponent_abs.
unfold canonic_exponent.
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).
now rewrite (proj2 (proj2 (prop_exp _) He)).
Qed.

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
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.
314 315
Qed.

316
(** Generic facts about any format *)
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
Theorem generic_format_discrete :
  forall x m,
  let e := canonic_exponent x in
  (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.
now rewrite scaled_mantissa_bpow.
Qed.

336 337 338 339 340 341
Theorem generic_format_canonic :
  forall f, canonic f ->
  generic_format (F2R f).
Proof.
intros (m, e) Hf.
unfold canonic in Hf. simpl in Hf.
342
unfold generic_format, scaled_mantissa.
343
rewrite <- Hf.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
344
apply F2R_eq_compat.
345
unfold F2R. simpl.
346
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_r, Rmult_1_r.
347 348 349
now rewrite Ztrunc_Z2R.
Qed.

350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
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.
apply F2R_gt_0_reg with beta (canonic_exponent x).
now rewrite <- Fx.
Qed.

367 368
Theorem canonic_exp_ge:
  forall prec,
369
  (forall e, (e - fexp e <= prec)%Z) ->
370 371 372 373 374 375 376 377
  (* OK with FLX, FLT and FTZ *)
  forall x, generic_format x ->
  (Rabs x < bpow (prec + canonic_exponent x))%R.
intros prec Hp x Hx.
case (Req_dec x 0); intros Hxz.
rewrite Hxz, Rabs_R0.
apply bpow_gt_0.
unfold canonic_exponent.
378 379 380
destruct (ln_beta beta x) as (ex,Ex) ; simpl.
specialize (Ex Hxz).
apply Rlt_le_trans with (1 := proj2 Ex).
381
apply bpow_le.
382
specialize (Hp ex).
383 384 385
omega.
Qed.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
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.
unfold valid_exp in prop_exp.
assert (e+1 <= fexp (e+1))%Z.
replace (fexp (e+1)) with (fexp e).
omega.
destruct (prop_exp e) as (Y1,Y2).
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.
unfold scaled_mantissa, canonic_exponent.
apply mantissa_DN_small_pos; trivial.
rewrite ln_beta_bpow.
split.
apply Req_le.
apply f_equal.
ring.
414
apply bpow_lt.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
415 416 417 418 419 420
omega.
now rewrite ln_beta_bpow.
unfold scaled_mantissa.
apply Rmult_le_pos; apply bpow_ge_0.
Qed.

421
Section Fcore_generic_round_pos.
422

423
(** * Rounding functions: R -> Z *)
424
Record Zround := mkZround {
425 426 427
  Zrnd : R -> Z ;
  Zrnd_monotone : forall x y, (x <= y)%R -> (Zrnd x <= Zrnd y)%Z ;
  Zrnd_Z2R : forall n, Zrnd (Z2R n) = n
428 429
}.

430
Variable rnd : Zround.
431 432 433
Let Zrnd := Zrnd rnd.
Let Zrnd_monotone := Zrnd_monotone rnd.
Let Zrnd_Z2R := Zrnd_Z2R rnd.
434

435
Theorem Zrnd_DN_or_UP :
436
  forall x, Zrnd x = Zfloor x \/ Zrnd x = Zceil x.
437
Proof.
438 439
intros x.
destruct (Zle_or_lt (Zrnd x) (Zfloor x)) as [Hx|Hx].
440 441
left.
apply Zle_antisym with (1 := Hx).
442
rewrite <- (Zrnd_Z2R (Zfloor x)).
443 444 445 446
apply Zrnd_monotone.
apply Zfloor_lb.
right.
apply Zle_antisym.
447
rewrite <- (Zrnd_Z2R (Zceil x)).
448 449 450 451 452 453 454 455 456 457
apply Zrnd_monotone.
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.

458
(** * the most useful one: R -> F *)
459
Definition round x :=
460
  F2R (Float beta (Zrnd (scaled_mantissa x)) (canonic_exponent x)).
461

462 463
Theorem round_monotone_pos :
  forall x y, (0 < x)%R -> (x <= y)%R -> (round x <= round y)%R.
464
Proof.
465
intros x y Hx Hxy.
466
unfold round, scaled_mantissa, canonic_exponent.
467 468 469 470 471 472 473 474 475 476
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.
477
apply (lt_bpow beta).
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
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].
rewrite (proj2 (proj2 (prop_exp ey) Hy1) ex).
apply F2R_le_compat.
apply Zrnd_monotone.
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].
rewrite (proj2 (proj2 (prop_exp ex) (Zle_trans _ _ _ He Hx2)) ey Hx2).
apply F2R_le_compat.
apply Zrnd_monotone.
apply Rmult_le_compat_r.
apply bpow_ge_0.
exact Hxy.
497
apply Rle_trans with (F2R (Float beta (Zrnd (bpow (ey - 1) * bpow (- fexp ey))) (fexp ey))).
498
rewrite <- bpow_plus.
499 500 501 502 503
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.
504
rewrite <- (Zrnd_Z2R 1).
505 506 507 508 509
apply Zrnd_monotone.
apply Rlt_le.
exact (proj2 (mantissa_small_pos _ _ Hex Hx1)).
unfold F2R. simpl.
rewrite Z2R_Zpower. 2: omega.
510
rewrite <- bpow_plus, Rmult_1_l.
511
apply bpow_le.
512
omega.
513
apply Rle_trans with (F2R (Float beta (Zrnd (bpow ex * bpow (- fexp ex))) (fexp ex))).
514 515 516 517 518 519
apply F2R_le_compat.
apply Zrnd_monotone.
apply Rmult_le_compat_r.
apply bpow_ge_0.
apply Rlt_le.
apply Hex.
520
rewrite <- bpow_plus.
521 522 523 524
rewrite <- Z2R_Zpower. 2: omega.
rewrite Zrnd_Z2R.
unfold F2R. simpl.
rewrite 2!Z2R_Zpower ; try omega.
525
rewrite <- 2!bpow_plus.
526
apply bpow_le.
527 528 529 530 531 532 533 534 535 536 537 538 539 540
omega.
apply F2R_le_compat.
apply Zrnd_monotone.
apply Rmult_le_compat_r.
apply bpow_ge_0.
apply Hey.
rewrite He'.
apply F2R_le_compat.
apply Zrnd_monotone.
apply Rmult_le_compat_r.
apply bpow_ge_0.
exact Hxy.
Qed.

541
Theorem round_generic :
542 543
  forall x,
  generic_format x ->
544
  round x = x.
545 546
Proof.
intros x Hx.
547
unfold round.
548 549 550 551 552
rewrite scaled_mantissa_generic with (1 := Hx).
rewrite Zrnd_Z2R.
now apply sym_eq.
Qed.

553 554
Theorem round_0 :
  round 0 = R0.
555
Proof.
556
unfold round, scaled_mantissa.
557 558 559 560 561 562
rewrite Rmult_0_l.
fold (Z2R 0).
rewrite Zrnd_Z2R.
apply F2R_0.
Qed.

563
Theorem round_bounded_large_pos :
564 565 566
  forall x ex,
  (fexp ex < ex)%Z ->
  (bpow (ex - 1) <= x < bpow ex)%R ->
567
  (bpow (ex - 1) <= round x <= bpow ex)%R.
568 569
Proof.
intros x ex He Hx.
570
unfold round, scaled_mantissa.
571 572
rewrite (canonic_exponent_fexp_pos _ _ Hx).
unfold F2R. simpl.
573
destruct (Zrnd_DN_or_UP (x * bpow (- fexp ex))) as [Hr|Hr] ; rewrite Hr.
574 575 576
(* DN *)
split.
replace (ex - 1)%Z with (ex - 1 + - fexp ex + fexp ex)%Z by ring.
577
rewrite bpow_plus.
578 579
apply Rmult_le_compat_r.
apply bpow_ge_0.
580
assert (Hf: Z2R (Zpower beta (ex - 1 - fexp ex)) = bpow (ex - 1 + - fexp ex)).
581 582 583 584 585 586
apply Z2R_Zpower.
omega.
rewrite <- Hf.
apply Z2R_le.
apply Zfloor_lub.
rewrite Hf.
587
rewrite bpow_plus.
588 589 590 591 592 593
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.
594
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_r, Rmult_1_r.
595 596 597 598 599 600
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.
601
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_r, Rmult_1_r.
602 603
apply Zceil_ub.
pattern ex at 3 ; replace ex with (ex - fexp ex + fexp ex)%Z by ring.
604
rewrite bpow_plus.
605 606
apply Rmult_le_compat_r.
apply bpow_ge_0.
607
assert (Hf: Z2R (Zpower beta (ex - fexp ex)) = bpow (ex - fexp ex)).
608 609 610 611 612 613 614
apply Z2R_Zpower.
omega.
rewrite <- Hf.
apply Z2R_le.
apply Zceil_glb.
rewrite Hf.
unfold Zminus.
615
rewrite bpow_plus.
616 617 618 619 620 621
apply Rmult_le_compat_r.
apply bpow_ge_0.
apply Rlt_le.
apply Hx.
Qed.

622
Theorem round_bounded_small_pos :
623 624 625
  forall x ex,
  (ex <= fexp ex)%Z ->
  (bpow (ex - 1) <= x < bpow ex)%R ->
626
  round x = R0 \/ round x = bpow (fexp ex).
627 628
Proof.
intros x ex He Hx.
629
unfold round, scaled_mantissa.
630 631
rewrite (canonic_exponent_fexp_pos _ _ Hx).
unfold F2R. simpl.
632
destruct (Zrnd_DN_or_UP (x * bpow (-fexp ex))) as [Hr|Hr] ; rewrite Hr.
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
(* 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.

650
Theorem generic_format_round_pos :
651 652
  forall x,
  (0 < x)%R ->
653
  generic_format (round x).
654 655 656 657 658 659 660
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 *)
661
destruct (round_bounded_small_pos _ _ He Hex) as [Hr|Hr] ; rewrite Hr.
662 663 664 665
apply generic_format_0.
apply generic_format_bpow.
now apply (proj2 (prop_exp ex)).
(* large *)
666
generalize (round_bounded_large_pos _ _ He Hex).
667
intros (Hr1, Hr2).
668
destruct (Rle_or_lt (bpow ex) (round x)) as [Hr|Hr].
669 670 671 672 673 674
rewrite <- (Rle_antisym _ _ Hr Hr2).
apply generic_format_bpow.
now apply (proj1 (prop_exp ex)).
assert (Hr' := conj Hr1 Hr).
unfold generic_format, scaled_mantissa.
rewrite (canonic_exponent_fexp_pos _ _ Hr').
675
unfold round, scaled_mantissa.
676 677
rewrite (canonic_exponent_fexp_pos _ _ Hex).
unfold F2R at 3. simpl.
678
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_r, Rmult_1_r.
679 680 681
now rewrite Ztrunc_Z2R.
Qed.

682
End Fcore_generic_round_pos.
683

684
Theorem round_ext :
685
  forall rnd1 rnd2,
686
  ( forall x, Zrnd rnd1 x = Zrnd rnd2 x ) ->
687
  forall x,
688
  round rnd1 x = round rnd2 x.
689 690
Proof.
intros rnd1 rnd2 Hext x.
691
unfold round.
692 693 694
now rewrite Hext.
Qed.

695
Section Zround_opp.
696

697
Variable rnd : Zround.
698

699
Definition Zrnd_opp x := Zopp (Zrnd rnd (-x)).
700 701

Lemma Zrnd_opp_le :
702
  forall x y, (x <= y)%R -> (Zrnd_opp x <= Zrnd_opp y)%Z.
703
Proof.
704
intros x y Hxy.
705
unfold Zrnd_opp.
706 707 708 709
apply Zopp_le_cancel.
rewrite 2!Zopp_involutive.
apply Zrnd_monotone.
now apply Ropp_le_contravar.
710 711
Qed.

712
Lemma Zrnd_Z2R_opp :
713
  forall n, Zrnd_opp (Z2R n) = n.
714
Proof.
715
intros n.
716
unfold Zrnd_opp.
717
rewrite <- Z2R_opp, Zrnd_Z2R.
718 719 720
apply Zopp_involutive.
Qed.

721
Definition Zround_opp := mkZround Zrnd_opp Zrnd_opp_le Zrnd_Z2R_opp.
722

723
Theorem round_opp :
724
  forall x,
725
  round rnd (- x) = Ropp (round Zround_opp x).
726 727
Proof.
intros x.
728
unfold round.
729
rewrite opp_F2R, canonic_exponent_opp, scaled_mantissa_opp.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
730
apply F2R_eq_compat.
731 732 733 734
apply sym_eq.
exact (Zopp_involutive _).
Qed.

735
End Zround_opp.
736

737
(** IEEE-754 roundings: up, down and to zero *)
738 739
Definition rndDN := mkZround Zfloor Zfloor_le Zfloor_Z2R.
Definition rndUP := mkZround Zceil Zceil_le Zceil_Z2R.
740
Definition rndZR := mkZround Ztrunc Ztrunc_le Ztrunc_Z2R.
741

742
Theorem round_DN_or_UP :
743
  forall rnd x,
744
  round rnd x = round rndDN x \/ round rnd x = round rndUP x.
745 746
Proof.
intros rnd x.
747
unfold round.
748
unfold Zrnd at 2 4. simpl.
749
destruct (Zrnd_DN_or_UP rnd (scaled_mantissa x)) as [Hx|Hx].
750 751 752 753
left. now rewrite Hx.
right. now rewrite Hx.
Qed.

754 755
Theorem round_monotone :
  forall rnd x y, (x <= y)%R -> (round rnd x <= round rnd y)%R.
756
Proof.
757
intros rnd x y Hxy.
758
destruct (total_order_T x 0) as [[Hx|Hx]|Hx].
759
3: now apply round_monotone_pos.
760
(* x < 0 *)
761
unfold round.
762 763 764 765 766 767 768
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)).
rewrite (canonic_exponent_opp (-x)), (canonic_exponent_opp (-y)).
apply Ropp_le_cancel.
rewrite 2!opp_F2R.
769
apply (round_monotone_pos (Zround_opp rnd) (-y) (-x)).
770 771 772 773 774 775
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.
776
rewrite <- (Zrnd_Z2R rnd 0).
777 778
apply Zrnd_monotone.
simpl.
779
rewrite <- (Rmult_0_l (bpow (- fexp (ln_beta beta x)))).
780 781 782 783
apply Rmult_le_compat_r.
apply bpow_ge_0.
now apply Rlt_le.
apply F2R_ge_0_compat. simpl.
784
rewrite <- (Zrnd_Z2R rnd 0).
785 786 787 788 789 790
apply Zrnd_monotone.
apply Rmult_le_pos.
exact Hy.
apply bpow_ge_0.
(* x = 0 *)
rewrite Hx.
791
rewrite round_0.
792 793
apply F2R_ge_0_compat.
simpl.
794
rewrite <- (Zrnd_Z2R rnd 0).
795 796 797 798 799 800
apply Zrnd_monotone.
apply Rmult_le_pos.
now rewrite <- Hx.
apply bpow_ge_0.
Qed.

801 802
Theorem round_monotone_l :
  forall rnd x y, generic_format x -> (x <= y)%R -> (x <= round rnd y)%R.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
803
Proof.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
804
intros rnd x y Hx Hxy.
805 806
rewrite <- (round_generic rnd x Hx).
now apply round_monotone.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
807
Qed.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
808

809 810
Theorem round_monotone_r :
  forall rnd x y, generic_format y -> (x <= y)%R -> (round rnd x <= y)%R.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
811
Proof.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
812
intros rnd x y Hy Hxy.
813 814
rewrite <- (round_generic rnd y Hy).
now apply round_monotone.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
815
Qed.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
816

817
Theorem round_abs_abs :
818
  forall P : R -> R -> Prop,
819 820
  ( forall rnd x, P x (round rnd x) ) ->
  forall rnd x, P (Rabs x) (Rabs (round rnd x)).
821 822 823 824 825 826
Proof.
intros P HP rnd x.
destruct (Rle_or_lt 0 x) as [Hx|Hx].
(* . *)
rewrite 2!Rabs_pos_eq.
apply HP.
827 828
rewrite <- (round_0 rnd).
now apply round_monotone.
829 830 831 832 833
exact Hx.
(* . *)
rewrite (Rabs_left _ Hx).
rewrite Rabs_left1.
pattern x at 2 ; rewrite <- Ropp_involutive.
834
rewrite round_opp.
835 836
rewrite Ropp_involutive.
apply HP.
837 838
rewrite <- (round_0 rnd).
apply round_monotone.
839 840 841
now apply Rlt_le.
Qed.

842 843
Theorem round_monotone_abs_l :
  forall rnd x y, generic_format x -> (x <= Rabs y)%R -> (x <= Rabs (round rnd y))%R.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
844 845
Proof.
intros rnd x y.
846
apply round_abs_abs.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
847
clear rnd y; intros rnd y Hy.
848
now apply round_monotone_l.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
849 850
Qed.

851 852
Theorem round_monotone_abs_r :
  forall rnd x y, generic_format y -> (Rabs x <= y)%R -> (Rabs (round rnd x) <= y)%R.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
853 854
Proof.
intros rnd x y.
855
apply round_abs_abs.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
856
clear rnd x; intros rnd x Hx.
857
now apply round_monotone_r.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
858 859
Qed.

860
Theorem round_DN_opp :
861
  forall x,
862
  round rndDN (-x) = (- round rndUP x)%R.
863 864
Proof.
intros x.
865
unfold round.
866 867 868 869 870 871 872 873
rewrite scaled_mantissa_opp.
rewrite opp_F2R.
unfold Zrnd. simpl.
unfold Zceil.
rewrite Zopp_involutive.
now rewrite canonic_exponent_opp.
Qed.

874
Theorem round_UP_opp :
875
  forall x,
876
  round rndUP (-x) = (- round rndDN x)%R.
877 878
Proof.
intros x.
879
unfold round.
880 881 882 883 884 885 886 887
rewrite scaled_mantissa_opp.
rewrite opp_F2R.
unfold Zrnd. simpl.
unfold Zceil.
rewrite Ropp_involutive.
now rewrite canonic_exponent_opp.
Qed.

888
Theorem generic_format_round :
889
  forall Zrnd x,
890
  generic_format (round Zrnd x).
891 892 893 894
Proof.
intros rnd x.
destruct (total_order_T x 0) as [[Hx|Hx]|Hx].
rewrite <- (Ropp_involutive x).
895 896
destruct (round_DN_or_UP rnd (- - x)) as [Hr|Hr] ; rewrite Hr.
rewrite round_DN_opp.
897
apply generic_format_opp.
898
apply generic_format_round_pos.
899
now apply Ropp_0_gt_lt_contravar.
900
rewrite round_UP_opp.
901
apply generic_format_opp.
902
apply generic_format_round_pos.
903 904
now apply Ropp_0_gt_lt_contravar.
rewrite Hx.
905
rewrite round_0.
906
apply generic_format_0.
907
now apply generic_format_round_pos.
908 909
Qed.

910
Theorem round_DN_pt :
911
  forall x,
912
  Rnd_DN_pt generic_format x (round rndDN x).
913 914 915
Proof.
intros x.
split.
916
apply generic_format_round.
917 918
split.
pattern x at 2 ; rewrite <- scaled_mantissa_bpow.
919
unfold round, F2R. simpl.
920 921 922 923
apply Rmult_le_compat_r.
apply bpow_ge_0.
apply Zfloor_lb.
intros g Hg Hgx.
924 925
rewrite <- (round_generic rndDN _ Hg).
now apply round_monotone.
926 927 928 929 930 931 932 933 934
Qed.

Theorem generic_format_satisfies_any :
  satisfies_any generic_format.
Proof.
split.
(* symmetric set *)
exact generic_format_0.
exact generic_format_opp.
935
(* round down *)
936
intros x.
937
exists (round rndDN x).
938
apply round_DN_pt.
939 940
Qed.

941
Theorem round_UP_pt :
942
  forall x,
943
  Rnd_UP_pt generic_format x (round rndUP x).
944 945
Proof.
intros x.
946
rewrite <- (Ropp_involutive x).
947
rewrite round_UP_opp.
948
apply Rnd_DN_UP_pt_sym.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
949
apply generic_format_opp.
950
apply round_DN_pt.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
951 952
Qed.

953
Theorem round_ZR_pt :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
954 955 956 957 958 959 960
  forall x,
  Rnd_ZR_pt generic_format x (round rndZR x).
Proof.
intros x.
split ; intros Hx.
(* *)
replace (round rndZR x) with (round rndDN x).
961
apply round_DN_pt.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
962
apply F2R_eq_compat.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
963 964 965 966 967 968 969
apply sym_eq.
apply Ztrunc_floor.
rewrite <- (Rmult_0_l (bpow (- canonic_exponent x))).
apply Rmult_le_compat_r with (2 := Hx).
apply bpow_ge_0.
(* *)
replace (round rndZR x) with (round rndUP x).
970
apply round_UP_pt.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
971
apply F2R_eq_compat.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
972 973 974 975 976 977 978
apply sym_eq.
apply Ztrunc_ceil.
rewrite <- (Rmult_0_l (bpow (- canonic_exponent x))).
apply Rmult_le_compat_r with (2 := Hx).
apply bpow_ge_0.
Qed.

979
Theorem round_DN_small_pos :
980
  forall x ex,
Guillaume Melquiond's avatar
Guillaume Melquiond committed
981
  (bpow (ex - 1) <= x < bpow ex)%R ->
982
  (ex <= fexp ex)%Z ->
983
  round rndDN x = R0.
984 985
Proof.
intros x ex Hx He.
986 987
rewrite <- (F2R_0 beta (canonic_exponent x)).
rewrite <- mantissa_DN_small_pos with (1 := Hx) (2 := He).
988
now rewrite <- canonic_exponent_fexp_pos with (1 := Hx).
989 990
Qed.

991
Theorem round_UP_small_pos :
992
  forall x ex,
Guillaume Melquiond's avatar
Guillaume Melquiond committed
993
  (bpow (ex - 1) <= x < bpow ex)%R ->
994
  (ex <= fexp ex)%Z ->
995
  round rndUP x = (bpow (fexp ex)).