Fcore_generic_fmt.v 34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
(*
This file is part of the Flocq formalization of floating-point
arithmetic in Coq: http://flocq.gforge.inria.fr/

Copyright (C) 2010 Sylvie Boldo
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.
*)

19 20 21 22
Require Import Fcore_Raux.
Require Import Fcore_defs.
Require Import Fcore_rnd.
Require Import Fcore_float_prop.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
23 24 25 26 27

Section RND_generic.

Variable beta : radix.

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

Variable fexp : Z -> Z.

32 33 34 35 36 37 38 39
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
40

41
Definition canonic_exponent x :=
42
  fexp (ln_beta beta x).
43 44 45

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

47 48 49
Definition scaled_mantissa x :=
  (x * bpow (- canonic_exponent x))%R.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
50
Definition generic_format (x : R) :=
51
  x = F2R (Float beta (Ztrunc (scaled_mantissa x)) (canonic_exponent x)).
52

Guillaume Melquiond's avatar
Guillaume Melquiond committed
53 54 55
Theorem generic_format_0 :
  generic_format 0.
Proof.
56
unfold generic_format, scaled_mantissa.
57 58 59 60 61 62 63 64 65 66 67 68
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
69 70
Qed.

71 72 73 74 75 76 77 78 79
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.

80 81 82 83 84
Theorem generic_format_bpow :
  forall e, (fexp (e + 1) <= e)%Z ->
  generic_format (bpow e).
Proof.
intros e H.
85
unfold generic_format, scaled_mantissa, canonic_exponent.
86
rewrite ln_beta_bpow.
87
rewrite <- bpow_plus.
88 89 90 91 92 93 94 95 96 97 98 99 100 101
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.
102
unfold generic_format, scaled_mantissa.
103 104 105
set (e' := canonic_exponent (F2R (Float beta m e))).
intros He.
unfold F2R at 3. simpl.
106
assert (H: (Z2R m * bpow e * bpow (- e') = Z2R (m * Zpower beta (e + -e')))%R).
107
rewrite Rmult_assoc, <- bpow_plus, Z2R_mult.
108 109 110 111 112 113
rewrite Z2R_Zpower.
apply refl_equal.
now apply Zle_left.
rewrite H, Ztrunc_Z2R.
unfold F2R. simpl.
rewrite <- H.
114
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_l.
115 116 117 118 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
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.

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

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

168 169 170 171 172 173
Theorem scaled_mantissa_0 :
  scaled_mantissa 0 = R0.
Proof.
apply Rmult_0_l.
Qed.

174 175 176 177 178 179 180 181 182 183
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.

184 185 186 187 188 189 190 191 192 193 194 195 196
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.

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

208
Theorem canonic_exponent_fexp :
209
  forall x ex,
210
  (bpow (ex - 1) <= Rabs x < bpow ex)%R ->
211 212 213 214 215 216 217
  canonic_exponent x = fexp ex.
Proof.
intros x ex Hx.
unfold canonic_exponent.
now rewrite ln_beta_unique with (1 := Hx).
Qed.

218
Theorem canonic_exponent_fexp_pos :
219
  forall x ex,
220
  (bpow (ex - 1) <= x < bpow ex)%R ->
221 222 223
  canonic_exponent x = fexp ex.
Proof.
intros x ex Hx.
224 225 226
apply canonic_exponent_fexp.
rewrite Rabs_pos_eq.
exact Hx.
227 228 229 230 231 232 233 234 235 236 237
apply Rle_trans with (2 := proj1 Hx).
apply bpow_ge_0.
Qed.

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.
238
split.
239 240 241 242 243 244
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.
245
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_l.
246 247 248 249 250
rewrite Rmult_1_r, Rmult_1_l.
apply Rlt_le_trans with (1 := proj2 Hx).
now apply -> bpow_le.
Qed.

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
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.

276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
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.
298 299
Qed.

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
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.

319 320 321 322 323 324
Theorem generic_format_canonic :
  forall f, canonic f ->
  generic_format (F2R f).
Proof.
intros (m, e) Hf.
unfold canonic in Hf. simpl in Hf.
325
unfold generic_format, scaled_mantissa.
326 327 328
rewrite <- Hf.
apply (f_equal (fun m => F2R (Float beta m e))).
unfold F2R. simpl.
329
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_r, Rmult_1_r.
330 331 332
now rewrite Ztrunc_Z2R.
Qed.

333 334
Theorem canonic_exp_ge:
  forall prec,
335
  (forall e, (e - fexp e <= prec)%Z) ->
336 337 338 339 340 341 342 343
  (* 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.
344 345 346
destruct (ln_beta beta x) as (ex,Ex) ; simpl.
specialize (Ex Hxz).
apply Rlt_le_trans with (1 := proj2 Ex).
347
apply -> bpow_le.
348
specialize (Hp ex).
349 350 351
omega.
Qed.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
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.
apply -> bpow_lt.
omega.
now rewrite ln_beta_bpow.
unfold scaled_mantissa.
apply Rmult_le_pos; apply bpow_ge_0.
Qed.

387
Section Fcore_generic_round_pos.
388

389
Record Zround := mkZround {
390 391 392
  Zrnd : R -> Z ;
  Zrnd_monotone : forall x y, (x <= y)%R -> (Zrnd x <= Zrnd y)%Z ;
  Zrnd_Z2R : forall n, Zrnd (Z2R n) = n
393 394
}.

395
Variable rnd : Zround.
396 397 398
Let Zrnd := Zrnd rnd.
Let Zrnd_monotone := Zrnd_monotone rnd.
Let Zrnd_Z2R := Zrnd_Z2R rnd.
399

400
Theorem Zrnd_DN_or_UP :
401
  forall x, Zrnd x = Zfloor x \/ Zrnd x = Zceil x.
402
Proof.
403 404
intros x.
destruct (Zle_or_lt (Zrnd x) (Zfloor x)) as [Hx|Hx].
405 406
left.
apply Zle_antisym with (1 := Hx).
407
rewrite <- (Zrnd_Z2R (Zfloor x)).
408 409 410 411
apply Zrnd_monotone.
apply Zfloor_lb.
right.
apply Zle_antisym.
412
rewrite <- (Zrnd_Z2R (Zceil x)).
413 414 415 416 417 418 419 420 421 422
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.

423
Definition round x :=
424
  F2R (Float beta (Zrnd (scaled_mantissa x)) (canonic_exponent x)).
425

426 427
Theorem round_monotone_pos :
  forall x y, (0 < x)%R -> (x <= y)%R -> (round x <= round y)%R.
428
Proof.
429
intros x y Hx Hxy.
430
unfold round, scaled_mantissa, canonic_exponent.
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
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.
apply <- bpow_lt.
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.
461
apply Rle_trans with (F2R (Float beta (Zrnd (bpow (ey - 1) * bpow (- fexp ey))) (fexp ey))).
462
rewrite <- bpow_plus.
463 464 465 466 467
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.
468
rewrite <- (Zrnd_Z2R 1).
469 470 471 472 473
apply Zrnd_monotone.
apply Rlt_le.
exact (proj2 (mantissa_small_pos _ _ Hex Hx1)).
unfold F2R. simpl.
rewrite Z2R_Zpower. 2: omega.
474
rewrite <- bpow_plus, Rmult_1_l.
475 476
apply -> bpow_le.
omega.
477
apply Rle_trans with (F2R (Float beta (Zrnd (bpow ex * bpow (- fexp ex))) (fexp ex))).
478 479 480 481 482 483
apply F2R_le_compat.
apply Zrnd_monotone.
apply Rmult_le_compat_r.
apply bpow_ge_0.
apply Rlt_le.
apply Hex.
484
rewrite <- bpow_plus.
485 486 487 488
rewrite <- Z2R_Zpower. 2: omega.
rewrite Zrnd_Z2R.
unfold F2R. simpl.
rewrite 2!Z2R_Zpower ; try omega.
489
rewrite <- 2!bpow_plus.
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
apply -> bpow_le.
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.

505
Theorem round_generic :
506 507
  forall x,
  generic_format x ->
508
  round x = x.
509 510
Proof.
intros x Hx.
511
unfold round.
512 513 514 515 516
rewrite scaled_mantissa_generic with (1 := Hx).
rewrite Zrnd_Z2R.
now apply sym_eq.
Qed.

517 518
Theorem round_0 :
  round 0 = R0.
519
Proof.
520
unfold round, scaled_mantissa.
521 522 523 524 525 526
rewrite Rmult_0_l.
fold (Z2R 0).
rewrite Zrnd_Z2R.
apply F2R_0.
Qed.

527
Theorem round_bounded_large_pos :
528 529 530
  forall x ex,
  (fexp ex < ex)%Z ->
  (bpow (ex - 1) <= x < bpow ex)%R ->
531
  (bpow (ex - 1) <= round x <= bpow ex)%R.
532 533
Proof.
intros x ex He Hx.
534
unfold round, scaled_mantissa.
535 536
rewrite (canonic_exponent_fexp_pos _ _ Hx).
unfold F2R. simpl.
537
destruct (Zrnd_DN_or_UP (x * bpow (- fexp ex))) as [Hr|Hr] ; rewrite Hr.
538 539 540
(* DN *)
split.
replace (ex - 1)%Z with (ex - 1 + - fexp ex + fexp ex)%Z by ring.
541
rewrite bpow_plus.
542 543
apply Rmult_le_compat_r.
apply bpow_ge_0.
544
assert (Hf: Z2R (Zpower beta (ex - 1 - fexp ex)) = bpow (ex - 1 + - fexp ex)).
545 546 547 548 549 550
apply Z2R_Zpower.
omega.
rewrite <- Hf.
apply Z2R_le.
apply Zfloor_lub.
rewrite Hf.
551
rewrite bpow_plus.
552 553 554 555 556 557
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.
558
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_r, Rmult_1_r.
559 560 561 562 563 564
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.
565
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_r, Rmult_1_r.
566 567
apply Zceil_ub.
pattern ex at 3 ; replace ex with (ex - fexp ex + fexp ex)%Z by ring.
568
rewrite bpow_plus.
569 570
apply Rmult_le_compat_r.
apply bpow_ge_0.
571
assert (Hf: Z2R (Zpower beta (ex - fexp ex)) = bpow (ex - fexp ex)).
572 573 574 575 576 577 578
apply Z2R_Zpower.
omega.
rewrite <- Hf.
apply Z2R_le.
apply Zceil_glb.
rewrite Hf.
unfold Zminus.
579
rewrite bpow_plus.
580 581 582 583 584 585
apply Rmult_le_compat_r.
apply bpow_ge_0.
apply Rlt_le.
apply Hx.
Qed.

586
Theorem round_bounded_small_pos :
587 588 589
  forall x ex,
  (ex <= fexp ex)%Z ->
  (bpow (ex - 1) <= x < bpow ex)%R ->
590
  round x = R0 \/ round x = bpow (fexp ex).
591 592
Proof.
intros x ex He Hx.
593
unfold round, scaled_mantissa.
594 595
rewrite (canonic_exponent_fexp_pos _ _ Hx).
unfold F2R. simpl.
596
destruct (Zrnd_DN_or_UP (x * bpow (-fexp ex))) as [Hr|Hr] ; rewrite Hr.
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
(* 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.

614
Theorem generic_format_round_pos :
615 616
  forall x,
  (0 < x)%R ->
617
  generic_format (round x).
618 619 620 621 622 623 624
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 *)
625
destruct (round_bounded_small_pos _ _ He Hex) as [Hr|Hr] ; rewrite Hr.
626 627 628 629
apply generic_format_0.
apply generic_format_bpow.
now apply (proj2 (prop_exp ex)).
(* large *)
630
generalize (round_bounded_large_pos _ _ He Hex).
631
intros (Hr1, Hr2).
632
destruct (Rle_or_lt (bpow ex) (round x)) as [Hr|Hr].
633 634 635 636 637 638
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').
639
unfold round, scaled_mantissa.
640 641
rewrite (canonic_exponent_fexp_pos _ _ Hex).
unfold F2R at 3. simpl.
642
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_r, Rmult_1_r.
643 644 645
now rewrite Ztrunc_Z2R.
Qed.

646
End Fcore_generic_round_pos.
647

648
Theorem round_ext :
649
  forall rnd1 rnd2,
650
  ( forall x, Zrnd rnd1 x = Zrnd rnd2 x ) ->
651
  forall x,
652
  round rnd1 x = round rnd2 x.
653 654
Proof.
intros rnd1 rnd2 Hext x.
655
unfold round.
656 657 658
now rewrite Hext.
Qed.

659
Section Zround_opp.
660

661
Variable rnd : Zround.
662

663
Definition Zrnd_opp x := Zopp (Zrnd rnd (-x)).
664 665

Lemma Zrnd_opp_le :
666
  forall x y, (x <= y)%R -> (Zrnd_opp x <= Zrnd_opp y)%Z.
667
Proof.
668
intros x y Hxy.
669
unfold Zrnd_opp.
670 671 672 673
apply Zopp_le_cancel.
rewrite 2!Zopp_involutive.
apply Zrnd_monotone.
now apply Ropp_le_contravar.
674 675
Qed.

676
Lemma Zrnd_Z2R_opp :
677
  forall n, Zrnd_opp (Z2R n) = n.
678
Proof.
679
intros n.
680
unfold Zrnd_opp.
681
rewrite <- Z2R_opp, Zrnd_Z2R.
682 683 684
apply Zopp_involutive.
Qed.

685
Definition Zround_opp := mkZround Zrnd_opp Zrnd_opp_le Zrnd_Z2R_opp.
686

687
Theorem round_opp :
688
  forall x,
689
  round rnd (- x) = Ropp (round Zround_opp x).
690 691
Proof.
intros x.
692
unfold round.
693 694 695 696 697 698
rewrite opp_F2R, canonic_exponent_opp, scaled_mantissa_opp.
apply (f_equal (fun m => F2R (Float beta m _))).
apply sym_eq.
exact (Zopp_involutive _).
Qed.

699
End Zround_opp.
700

701 702
Definition rndDN := mkZround Zfloor Zfloor_le Zfloor_Z2R.
Definition rndUP := mkZround Zceil Zceil_le Zceil_Z2R.
703
Definition rndZR := mkZround Ztrunc Ztrunc_le Ztrunc_Z2R.
704

705
Theorem round_DN_or_UP :
706
  forall rnd x,
707
  round rnd x = round rndDN x \/ round rnd x = round rndUP x.
708 709
Proof.
intros rnd x.
710
unfold round.
711
unfold Zrnd at 2 4. simpl.
712
destruct (Zrnd_DN_or_UP rnd (scaled_mantissa x)) as [Hx|Hx].
713 714 715 716
left. now rewrite Hx.
right. now rewrite Hx.
Qed.

717 718
Theorem round_monotone :
  forall rnd x y, (x <= y)%R -> (round rnd x <= round rnd y)%R.
719
Proof.
720
intros rnd x y Hxy.
721
destruct (total_order_T x 0) as [[Hx|Hx]|Hx].
722
3: now apply round_monotone_pos.
723
(* x < 0 *)
724
unfold round.
725 726 727 728 729 730 731
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.
732
apply (round_monotone_pos (Zround_opp rnd) (-y) (-x)).
733 734 735 736 737 738
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.
739
rewrite <- (Zrnd_Z2R rnd 0).
740 741
apply Zrnd_monotone.
simpl.
742
rewrite <- (Rmult_0_l (bpow (- fexp (ln_beta beta x)))).
743 744 745 746
apply Rmult_le_compat_r.
apply bpow_ge_0.
now apply Rlt_le.
apply F2R_ge_0_compat. simpl.
747
rewrite <- (Zrnd_Z2R rnd 0).
748 749 750 751 752 753
apply Zrnd_monotone.
apply Rmult_le_pos.
exact Hy.
apply bpow_ge_0.
(* x = 0 *)
rewrite Hx.
754
rewrite round_0.
755 756
apply F2R_ge_0_compat.
simpl.
757
rewrite <- (Zrnd_Z2R rnd 0).
758 759 760 761 762 763
apply Zrnd_monotone.
apply Rmult_le_pos.
now rewrite <- Hx.
apply bpow_ge_0.
Qed.

764 765
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
766
Proof.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
767
intros rnd x y Hx Hxy.
768 769
rewrite <- (round_generic rnd x Hx).
now apply round_monotone.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
770
Qed.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
771

772 773
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
774
Proof.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
775
intros rnd x y Hy Hxy.
776 777
rewrite <- (round_generic rnd y Hy).
now apply round_monotone.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
778
Qed.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
779

780
Theorem round_abs_abs :
781
  forall P : R -> R -> Prop,
782 783
  ( forall rnd x, P x (round rnd x) ) ->
  forall rnd x, P (Rabs x) (Rabs (round rnd x)).
784 785 786 787 788 789
Proof.
intros P HP rnd x.
destruct (Rle_or_lt 0 x) as [Hx|Hx].
(* . *)
rewrite 2!Rabs_pos_eq.
apply HP.
790 791
rewrite <- (round_0 rnd).
now apply round_monotone.
792 793 794 795 796
exact Hx.
(* . *)
rewrite (Rabs_left _ Hx).
rewrite Rabs_left1.
pattern x at 2 ; rewrite <- Ropp_involutive.
797
rewrite round_opp.
798 799
rewrite Ropp_involutive.
apply HP.
800 801
rewrite <- (round_0 rnd).
apply round_monotone.
802 803 804
now apply Rlt_le.
Qed.

805 806
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
807 808
Proof.
intros rnd x y.
809
apply round_abs_abs.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
810
clear rnd y; intros rnd y Hy.
811
now apply round_monotone_l.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
812 813
Qed.

814 815
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
816 817
Proof.
intros rnd x y.
818
apply round_abs_abs.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
819
clear rnd x; intros rnd x Hx.
820
now apply round_monotone_r.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
821 822
Qed.

823
Theorem round_DN_opp :
824
  forall x,
825
  round rndDN (-x) = (- round rndUP x)%R.
826 827
Proof.
intros x.
828
unfold round.
829 830 831 832 833 834 835 836
rewrite scaled_mantissa_opp.
rewrite opp_F2R.
unfold Zrnd. simpl.
unfold Zceil.
rewrite Zopp_involutive.
now rewrite canonic_exponent_opp.
Qed.

837
Theorem round_UP_opp :
838
  forall x,
839
  round rndUP (-x) = (- round rndDN x)%R.
840 841
Proof.
intros x.
842
unfold round.
843 844 845 846 847 848 849 850
rewrite scaled_mantissa_opp.
rewrite opp_F2R.
unfold Zrnd. simpl.
unfold Zceil.
rewrite Ropp_involutive.
now rewrite canonic_exponent_opp.
Qed.

851
Theorem generic_format_round :
852
  forall Zrnd x,
853
  generic_format (round Zrnd x).
854 855 856 857
Proof.
intros rnd x.
destruct (total_order_T x 0) as [[Hx|Hx]|Hx].
rewrite <- (Ropp_involutive x).
858 859
destruct (round_DN_or_UP rnd (- - x)) as [Hr|Hr] ; rewrite Hr.
rewrite round_DN_opp.
860
apply generic_format_opp.
861
apply generic_format_round_pos.
862
now apply Ropp_0_gt_lt_contravar.
863
rewrite round_UP_opp.
864
apply generic_format_opp.
865
apply generic_format_round_pos.
866 867
now apply Ropp_0_gt_lt_contravar.
rewrite Hx.
868
rewrite round_0.
869
apply generic_format_0.
870
now apply generic_format_round_pos.
871 872 873 874
Qed.

Theorem generic_DN_pt :
  forall x,
875
  Rnd_DN_pt generic_format x (round rndDN x).
876 877 878
Proof.
intros x.
split.
879
apply generic_format_round.
880 881
split.
pattern x at 2 ; rewrite <- scaled_mantissa_bpow.
882
unfold round, F2R. simpl.
883 884 885 886
apply Rmult_le_compat_r.
apply bpow_ge_0.
apply Zfloor_lb.
intros g Hg Hgx.
887 888
rewrite <- (round_generic rndDN _ Hg).
now apply round_monotone.
889 890 891 892 893 894 895 896 897
Qed.

Theorem generic_format_satisfies_any :
  satisfies_any generic_format.
Proof.
split.
(* symmetric set *)
exact generic_format_0.
exact generic_format_opp.
898
(* round down *)
899
intros x.
900
exists (round rndDN x).
901 902 903
apply generic_DN_pt.
Qed.

904 905
Theorem generic_UP_pt :
  forall x,
906
  Rnd_UP_pt generic_format x (round rndUP x).
907 908
Proof.
intros x.
909
rewrite <- (Ropp_involutive x).
910
rewrite round_UP_opp.
911 912 913
apply Rnd_DN_UP_pt_sym.
apply generic_format_satisfies_any.
apply generic_DN_pt.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
914 915
Qed.

916
Theorem round_DN_small_pos :
917
  forall x ex,
Guillaume Melquiond's avatar
Guillaume Melquiond committed
918
  (bpow (ex - 1) <= x < bpow ex)%R ->
919
  (ex <= fexp ex)%Z ->
920
  round rndDN x = R0.
921 922
Proof.
intros x ex Hx He.
923 924
rewrite <- (F2R_0 beta (canonic_exponent x)).
rewrite <- mantissa_DN_small_pos with (1 := Hx) (2 := He).
925
now rewrite <- canonic_exponent_fexp_pos with (1 := Hx).
926 927
Qed.

928
Theorem round_UP_small_pos :
929
  forall x ex,
Guillaume Melquiond's avatar
Guillaume Melquiond committed
930
  (bpow (ex - 1) <= x < bpow ex)%R ->
931
  (ex <= fexp ex)%Z ->
932
  round rndUP x = (bpow (fexp ex)).
933 934
Proof.
intros x ex Hx He.
935 936
rewrite <- F2R_bpow.
rewrite <- mantissa_UP_small_pos with (1 := Hx) (2 := He).
937
now rewrite <- canonic_exponent_fexp_pos with (1 := Hx).
938 939
Qed.

940 941 942 943 944
Theorem generic_format_EM :
  forall x,
  generic_format x \/ ~generic_format x.
Proof.
intros x.
945
destruct (Req_dec (round rndDN x) x) as [Hx|Hx].
946
left.
947
rewrite <- Hx.
948
apply generic_format_round.
949 950 951
right.
intros H.
apply Hx.
952
now apply round_generic.
953 954
Qed.

955
Theorem round_large_pos_ge_pow :
956
  forall rnd x e,
957
  (0 < round rnd x)%R ->
Guillaume Melquiond's avatar
Guillaume Melquiond committed
958
  (bpow e <= x)%R ->
959
  (bpow e <= round rnd x)%R.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
960
Proof.
961
intros rnd x e Hd Hex.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
962 963
destruct (ln_beta beta x) as (ex, He).
assert (Hx: (0 < x)%R).
964 965
apply Rlt_le_trans with (2 := Hex).
apply bpow_gt_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
966 967 968 969 970 971 972 973
specialize (He (Rgt_not_eq _ _ Hx)).
rewrite Rabs_pos_eq in He. 2: now apply Rlt_le.
apply Rle_trans with (bpow (ex - 1)).
apply -> bpow_le.
cut (e < ex)%Z. omega.
apply <- bpow_lt.
now apply Rle_lt_trans with (2 := proj2 He).
destruct (Zle_or_lt ex (fexp ex)).
974
destruct (round_bounded_small_pos rnd x ex H He) as [Hr|Hr].
975 976 977 978
rewrite Hr in Hd.
elim Rlt_irrefl with (1 := Hd).
rewrite Hr.
apply -> bpow_le.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
979
omega.
980
apply (round_bounded_large_pos rnd x ex H He).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
981 982
Qed.

983 984
Theorem canonic_exponent_DN :
  forall x,
985 986
  (0 < round rndDN x)%R ->
  canonic_exponent (round rndDN x) = canonic_exponent x.
987
Proof.
988
intros x Hd.
989 990 991
unfold canonic_exponent.
apply f_equal.
apply ln_beta_unique.
992
rewrite (Rabs_pos_eq (round rndDN x)). 2: now apply Rlt_le.
993 994 995 996
destruct (ln_beta beta x) as (ex, He).
simpl.
assert (Hx: (0 < x)%R).
apply Rlt_le_trans with (1 := Hd).
997
apply (generic_DN_pt x).
998 999 1000
specialize (He (Rgt_not_eq _ _ Hx)).
rewrite Rabs_pos_eq in He. 2: now apply Rlt_le.
split.
1001
apply round_large_pos_ge_pow with (1 := Hd).