Fcore_generic_fmt.v 33.3 KB
Newer Older
1 2 3 4
Require Import Fcore_Raux.
Require Import Fcore_defs.
Require Import Fcore_rnd.
Require Import Fcore_float_prop.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
5 6 7 8 9

Section RND_generic.

Variable beta : radix.

10
Notation bpow e := (bpow beta e).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
11 12 13

Variable fexp : Z -> Z.

14 15 16 17 18 19 20 21
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
22

23
Definition canonic_exponent x :=
24
  fexp (ln_beta beta x).
25 26 27

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

29 30 31
Definition scaled_mantissa x :=
  (x * bpow (- canonic_exponent x))%R.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
32
Definition generic_format (x : R) :=
33
  x = F2R (Float beta (Ztrunc (scaled_mantissa x)) (canonic_exponent x)).
34

Guillaume Melquiond's avatar
Guillaume Melquiond committed
35 36 37
Theorem generic_format_0 :
  generic_format 0.
Proof.
38
unfold generic_format, scaled_mantissa.
39 40 41 42 43 44 45 46 47 48 49 50
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
51 52
Qed.

53 54 55 56 57 58 59 60 61
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.

62 63 64 65 66
Theorem generic_format_bpow :
  forall e, (fexp (e + 1) <= e)%Z ->
  generic_format (bpow e).
Proof.
intros e H.
67
unfold generic_format, scaled_mantissa, canonic_exponent.
68
rewrite ln_beta_bpow.
69
rewrite <- bpow_plus.
70 71 72 73 74 75 76 77 78 79 80 81 82 83
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.
84
unfold generic_format, scaled_mantissa.
85 86 87
set (e' := canonic_exponent (F2R (Float beta m e))).
intros He.
unfold F2R at 3. simpl.
88
assert (H: (Z2R m * bpow e * bpow (- e') = Z2R (m * Zpower beta (e + -e')))%R).
89
rewrite Rmult_assoc, <- bpow_plus, Z2R_mult.
90 91 92 93 94 95
rewrite Z2R_Zpower.
apply refl_equal.
now apply Zle_left.
rewrite H, Ztrunc_Z2R.
unfold F2R. simpl.
rewrite <- H.
96
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_l.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
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.

127
Theorem scaled_mantissa_generic :
128 129
  forall x,
  generic_format x ->
130
  scaled_mantissa x = Z2R (Ztrunc (scaled_mantissa x)).
131 132
Proof.
intros x Hx.
133
unfold scaled_mantissa.
134 135
pattern x at 1 3 ; rewrite Hx.
unfold F2R. simpl.
136
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_r, Rmult_1_r.
137 138 139
now rewrite Ztrunc_Z2R.
Qed.

140 141 142 143 144 145
Theorem scaled_mantissa_bpow :
  forall x,
  (scaled_mantissa x * bpow (canonic_exponent x))%R = x.
Proof.
intros x.
unfold scaled_mantissa.
146
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_l.
147 148 149
apply Rmult_1_r.
Qed.

150 151 152 153 154 155
Theorem scaled_mantissa_0 :
  scaled_mantissa 0 = R0.
Proof.
apply Rmult_0_l.
Qed.

156 157 158 159 160 161 162 163 164 165
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.

166 167 168 169 170 171 172 173 174 175 176 177 178
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.

179 180 181 182 183
Theorem generic_format_opp :
  forall x, generic_format x -> generic_format (-x).
Proof.
intros x Hx.
unfold generic_format.
184 185 186 187
rewrite scaled_mantissa_opp, canonic_exponent_opp.
rewrite Ztrunc_opp.
rewrite <- opp_F2R.
now apply f_equal.
188 189
Qed.

190
Theorem canonic_exponent_fexp :
191
  forall x ex,
192
  (bpow (ex - 1) <= Rabs x < bpow ex)%R ->
193 194 195 196 197 198 199
  canonic_exponent x = fexp ex.
Proof.
intros x ex Hx.
unfold canonic_exponent.
now rewrite ln_beta_unique with (1 := Hx).
Qed.

200
Theorem canonic_exponent_fexp_pos :
201
  forall x ex,
202
  (bpow (ex - 1) <= x < bpow ex)%R ->
203 204 205
  canonic_exponent x = fexp ex.
Proof.
intros x ex Hx.
206 207 208
apply canonic_exponent_fexp.
rewrite Rabs_pos_eq.
exact Hx.
209 210 211 212 213 214 215 216 217 218 219
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.
220
split.
221 222 223 224 225 226
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.
227
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_l.
228 229 230 231 232
rewrite Rmult_1_r, Rmult_1_l.
apply Rlt_le_trans with (1 := proj2 Hx).
now apply -> bpow_le.
Qed.

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
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.

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
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.
280 281
Qed.

282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
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.

301 302 303 304 305 306
Theorem generic_format_canonic :
  forall f, canonic f ->
  generic_format (F2R f).
Proof.
intros (m, e) Hf.
unfold canonic in Hf. simpl in Hf.
307
unfold generic_format, scaled_mantissa.
308 309 310
rewrite <- Hf.
apply (f_equal (fun m => F2R (Float beta m e))).
unfold F2R. simpl.
311
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_r, Rmult_1_r.
312 313 314
now rewrite Ztrunc_Z2R.
Qed.

315 316
Theorem canonic_exp_ge:
  forall prec,
317
  (forall e, (e - fexp e <= prec)%Z) ->
318 319 320 321 322 323 324 325
  (* 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.
326 327 328
destruct (ln_beta beta x) as (ex,Ex) ; simpl.
specialize (Ex Hxz).
apply Rlt_le_trans with (1 := proj2 Ex).
329
apply -> bpow_le.
330
specialize (Hp ex).
331 332 333
omega.
Qed.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
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.

369
Section Fcore_generic_round_pos.
370

371
Record Zround := mkZround {
372 373 374
  Zrnd : R -> Z ;
  Zrnd_monotone : forall x y, (x <= y)%R -> (Zrnd x <= Zrnd y)%Z ;
  Zrnd_Z2R : forall n, Zrnd (Z2R n) = n
375 376
}.

377
Variable rnd : Zround.
378 379 380
Let Zrnd := Zrnd rnd.
Let Zrnd_monotone := Zrnd_monotone rnd.
Let Zrnd_Z2R := Zrnd_Z2R rnd.
381

382
Theorem Zrnd_DN_or_UP :
383
  forall x, Zrnd x = Zfloor x \/ Zrnd x = Zceil x.
384
Proof.
385 386
intros x.
destruct (Zle_or_lt (Zrnd x) (Zfloor x)) as [Hx|Hx].
387 388
left.
apply Zle_antisym with (1 := Hx).
389
rewrite <- (Zrnd_Z2R (Zfloor x)).
390 391 392 393
apply Zrnd_monotone.
apply Zfloor_lb.
right.
apply Zle_antisym.
394
rewrite <- (Zrnd_Z2R (Zceil x)).
395 396 397 398 399 400 401 402 403 404
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.

405
Definition round x :=
406
  F2R (Float beta (Zrnd (scaled_mantissa x)) (canonic_exponent x)).
407

408 409
Theorem round_monotone_pos :
  forall x y, (0 < x)%R -> (x <= y)%R -> (round x <= round y)%R.
410
Proof.
411
intros x y Hx Hxy.
412
unfold round, scaled_mantissa, canonic_exponent.
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
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.
443
apply Rle_trans with (F2R (Float beta (Zrnd (bpow (ey - 1) * bpow (- fexp ey))) (fexp ey))).
444
rewrite <- bpow_plus.
445 446 447 448 449
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.
450
rewrite <- (Zrnd_Z2R 1).
451 452 453 454 455
apply Zrnd_monotone.
apply Rlt_le.
exact (proj2 (mantissa_small_pos _ _ Hex Hx1)).
unfold F2R. simpl.
rewrite Z2R_Zpower. 2: omega.
456
rewrite <- bpow_plus, Rmult_1_l.
457 458
apply -> bpow_le.
omega.
459
apply Rle_trans with (F2R (Float beta (Zrnd (bpow ex * bpow (- fexp ex))) (fexp ex))).
460 461 462 463 464 465
apply F2R_le_compat.
apply Zrnd_monotone.
apply Rmult_le_compat_r.
apply bpow_ge_0.
apply Rlt_le.
apply Hex.
466
rewrite <- bpow_plus.
467 468 469 470
rewrite <- Z2R_Zpower. 2: omega.
rewrite Zrnd_Z2R.
unfold F2R. simpl.
rewrite 2!Z2R_Zpower ; try omega.
471
rewrite <- 2!bpow_plus.
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
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.

487
Theorem round_generic :
488 489
  forall x,
  generic_format x ->
490
  round x = x.
491 492
Proof.
intros x Hx.
493
unfold round.
494 495 496 497 498
rewrite scaled_mantissa_generic with (1 := Hx).
rewrite Zrnd_Z2R.
now apply sym_eq.
Qed.

499 500
Theorem round_0 :
  round 0 = R0.
501
Proof.
502
unfold round, scaled_mantissa.
503 504 505 506 507 508
rewrite Rmult_0_l.
fold (Z2R 0).
rewrite Zrnd_Z2R.
apply F2R_0.
Qed.

509
Theorem round_bounded_large_pos :
510 511 512
  forall x ex,
  (fexp ex < ex)%Z ->
  (bpow (ex - 1) <= x < bpow ex)%R ->
513
  (bpow (ex - 1) <= round x <= bpow ex)%R.
514 515
Proof.
intros x ex He Hx.
516
unfold round, scaled_mantissa.
517 518
rewrite (canonic_exponent_fexp_pos _ _ Hx).
unfold F2R. simpl.
519
destruct (Zrnd_DN_or_UP (x * bpow (- fexp ex))) as [Hr|Hr] ; rewrite Hr.
520 521 522
(* DN *)
split.
replace (ex - 1)%Z with (ex - 1 + - fexp ex + fexp ex)%Z by ring.
523
rewrite bpow_plus.
524 525
apply Rmult_le_compat_r.
apply bpow_ge_0.
526
assert (Hf: Z2R (Zpower beta (ex - 1 - fexp ex)) = bpow (ex - 1 + - fexp ex)).
527 528 529 530 531 532
apply Z2R_Zpower.
omega.
rewrite <- Hf.
apply Z2R_le.
apply Zfloor_lub.
rewrite Hf.
533
rewrite bpow_plus.
534 535 536 537 538 539
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.
540
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_r, Rmult_1_r.
541 542 543 544 545 546
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.
547
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_r, Rmult_1_r.
548 549
apply Zceil_ub.
pattern ex at 3 ; replace ex with (ex - fexp ex + fexp ex)%Z by ring.
550
rewrite bpow_plus.
551 552
apply Rmult_le_compat_r.
apply bpow_ge_0.
553
assert (Hf: Z2R (Zpower beta (ex - fexp ex)) = bpow (ex - fexp ex)).
554 555 556 557 558 559 560
apply Z2R_Zpower.
omega.
rewrite <- Hf.
apply Z2R_le.
apply Zceil_glb.
rewrite Hf.
unfold Zminus.
561
rewrite bpow_plus.
562 563 564 565 566 567
apply Rmult_le_compat_r.
apply bpow_ge_0.
apply Rlt_le.
apply Hx.
Qed.

568
Theorem round_bounded_small_pos :
569 570 571
  forall x ex,
  (ex <= fexp ex)%Z ->
  (bpow (ex - 1) <= x < bpow ex)%R ->
572
  round x = R0 \/ round x = bpow (fexp ex).
573 574
Proof.
intros x ex He Hx.
575
unfold round, scaled_mantissa.
576 577
rewrite (canonic_exponent_fexp_pos _ _ Hx).
unfold F2R. simpl.
578
destruct (Zrnd_DN_or_UP (x * bpow (-fexp ex))) as [Hr|Hr] ; rewrite Hr.
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
(* 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.

596
Theorem generic_format_round_pos :
597 598
  forall x,
  (0 < x)%R ->
599
  generic_format (round x).
600 601 602 603 604 605 606
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 *)
607
destruct (round_bounded_small_pos _ _ He Hex) as [Hr|Hr] ; rewrite Hr.
608 609 610 611
apply generic_format_0.
apply generic_format_bpow.
now apply (proj2 (prop_exp ex)).
(* large *)
612
generalize (round_bounded_large_pos _ _ He Hex).
613
intros (Hr1, Hr2).
614
destruct (Rle_or_lt (bpow ex) (round x)) as [Hr|Hr].
615 616 617 618 619 620
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').
621
unfold round, scaled_mantissa.
622 623
rewrite (canonic_exponent_fexp_pos _ _ Hex).
unfold F2R at 3. simpl.
624
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_r, Rmult_1_r.
625 626 627
now rewrite Ztrunc_Z2R.
Qed.

628
End Fcore_generic_round_pos.
629

630
Theorem round_ext :
631
  forall rnd1 rnd2,
632
  ( forall x, Zrnd rnd1 x = Zrnd rnd2 x ) ->
633
  forall x,
634
  round rnd1 x = round rnd2 x.
635 636
Proof.
intros rnd1 rnd2 Hext x.
637
unfold round.
638 639 640
now rewrite Hext.
Qed.

641
Section Zround_opp.
642

643
Variable rnd : Zround.
644

645
Definition Zrnd_opp x := Zopp (Zrnd rnd (-x)).
646 647

Lemma Zrnd_opp_le :
648
  forall x y, (x <= y)%R -> (Zrnd_opp x <= Zrnd_opp y)%Z.
649
Proof.
650
intros x y Hxy.
651
unfold Zrnd_opp.
652 653 654 655
apply Zopp_le_cancel.
rewrite 2!Zopp_involutive.
apply Zrnd_monotone.
now apply Ropp_le_contravar.
656 657
Qed.

658
Lemma Zrnd_Z2R_opp :
659
  forall n, Zrnd_opp (Z2R n) = n.
660
Proof.
661
intros n.
662
unfold Zrnd_opp.
663
rewrite <- Z2R_opp, Zrnd_Z2R.
664 665 666
apply Zopp_involutive.
Qed.

667
Definition Zround_opp := mkZround Zrnd_opp Zrnd_opp_le Zrnd_Z2R_opp.
668

669
Theorem round_opp :
670
  forall x,
671
  round rnd (- x) = Ropp (round Zround_opp x).
672 673
Proof.
intros x.
674
unfold round.
675 676 677 678 679 680
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.

681
End Zround_opp.
682

683 684 685
Definition rndDN := mkZround Zfloor Zfloor_le Zfloor_Z2R.
Definition rndUP := mkZround Zceil Zceil_le Zceil_Z2R.
Definition ZrndTZ := mkZround Ztrunc Ztrunc_le Ztrunc_Z2R.
686

687
Theorem round_DN_or_UP :
688
  forall rnd x,
689
  round rnd x = round rndDN x \/ round rnd x = round rndUP x.
690 691
Proof.
intros rnd x.
692
unfold round.
693
unfold Zrnd at 2 4. simpl.
694
destruct (Zrnd_DN_or_UP rnd (scaled_mantissa x)) as [Hx|Hx].
695 696 697 698
left. now rewrite Hx.
right. now rewrite Hx.
Qed.

699 700
Theorem round_monotone :
  forall rnd x y, (x <= y)%R -> (round rnd x <= round rnd y)%R.
701
Proof.
702
intros rnd x y Hxy.
703
destruct (total_order_T x 0) as [[Hx|Hx]|Hx].
704
3: now apply round_monotone_pos.
705
(* x < 0 *)
706
unfold round.
707 708 709 710 711 712 713
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.
714
apply (round_monotone_pos (Zround_opp rnd) (-y) (-x)).
715 716 717 718 719 720
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.
721
rewrite <- (Zrnd_Z2R rnd 0).
722 723
apply Zrnd_monotone.
simpl.
724
rewrite <- (Rmult_0_l (bpow (- fexp (ln_beta beta x)))).
725 726 727 728
apply Rmult_le_compat_r.
apply bpow_ge_0.
now apply Rlt_le.
apply F2R_ge_0_compat. simpl.
729
rewrite <- (Zrnd_Z2R rnd 0).
730 731 732 733 734 735
apply Zrnd_monotone.
apply Rmult_le_pos.
exact Hy.
apply bpow_ge_0.
(* x = 0 *)
rewrite Hx.
736
rewrite round_0.
737 738
apply F2R_ge_0_compat.
simpl.
739
rewrite <- (Zrnd_Z2R rnd 0).
740 741 742 743 744 745
apply Zrnd_monotone.
apply Rmult_le_pos.
now rewrite <- Hx.
apply bpow_ge_0.
Qed.

746 747
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
748
Proof.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
749
intros rnd x y Hx Hxy.
750 751
rewrite <- (round_generic rnd x Hx).
now apply round_monotone.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
752
Qed.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
753

754 755
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
756
Proof.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
757
intros rnd x y Hy Hxy.
758 759
rewrite <- (round_generic rnd y Hy).
now apply round_monotone.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
760
Qed.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
761

762
Theorem round_abs_abs :
763
  forall P : R -> R -> Prop,
764 765
  ( forall rnd x, P x (round rnd x) ) ->
  forall rnd x, P (Rabs x) (Rabs (round rnd x)).
766 767 768 769 770 771
Proof.
intros P HP rnd x.
destruct (Rle_or_lt 0 x) as [Hx|Hx].
(* . *)
rewrite 2!Rabs_pos_eq.
apply HP.
772 773
rewrite <- (round_0 rnd).
now apply round_monotone.
774 775 776 777 778
exact Hx.
(* . *)
rewrite (Rabs_left _ Hx).
rewrite Rabs_left1.
pattern x at 2 ; rewrite <- Ropp_involutive.
779
rewrite round_opp.
780 781
rewrite Ropp_involutive.
apply HP.
782 783
rewrite <- (round_0 rnd).
apply round_monotone.
784 785 786
now apply Rlt_le.
Qed.

787 788
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
789 790
Proof.
intros rnd x y.
791
apply round_abs_abs.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
792
clear rnd y; intros rnd y Hy.
793
now apply round_monotone_l.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
794 795
Qed.

796 797
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
798 799
Proof.
intros rnd x y.
800
apply round_abs_abs.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
801
clear rnd x; intros rnd x Hx.
802
now apply round_monotone_r.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
803 804
Qed.

805
Theorem round_DN_opp :
806
  forall x,
807
  round rndDN (-x) = (- round rndUP x)%R.
808 809
Proof.
intros x.
810
unfold round.
811 812 813 814 815 816 817 818
rewrite scaled_mantissa_opp.
rewrite opp_F2R.
unfold Zrnd. simpl.
unfold Zceil.
rewrite Zopp_involutive.
now rewrite canonic_exponent_opp.
Qed.

819
Theorem round_UP_opp :
820
  forall x,
821
  round rndUP (-x) = (- round rndDN x)%R.
822 823
Proof.
intros x.
824
unfold round.
825 826 827 828 829 830 831 832
rewrite scaled_mantissa_opp.
rewrite opp_F2R.
unfold Zrnd. simpl.
unfold Zceil.
rewrite Ropp_involutive.
now rewrite canonic_exponent_opp.
Qed.

833
Theorem generic_format_round :
834
  forall Zrnd x,
835
  generic_format (round Zrnd x).
836 837 838 839
Proof.
intros rnd x.
destruct (total_order_T x 0) as [[Hx|Hx]|Hx].
rewrite <- (Ropp_involutive x).
840 841
destruct (round_DN_or_UP rnd (- - x)) as [Hr|Hr] ; rewrite Hr.
rewrite round_DN_opp.
842
apply generic_format_opp.
843
apply generic_format_round_pos.
844
now apply Ropp_0_gt_lt_contravar.
845
rewrite round_UP_opp.
846
apply generic_format_opp.
847
apply generic_format_round_pos.
848 849
now apply Ropp_0_gt_lt_contravar.
rewrite Hx.
850
rewrite round_0.
851
apply generic_format_0.
852
now apply generic_format_round_pos.
853 854 855 856
Qed.

Theorem generic_DN_pt :
  forall x,
857
  Rnd_DN_pt generic_format x (round rndDN x).
858 859 860
Proof.
intros x.
split.
861
apply generic_format_round.
862 863
split.
pattern x at 2 ; rewrite <- scaled_mantissa_bpow.
864
unfold round, F2R. simpl.
865 866 867 868
apply Rmult_le_compat_r.
apply bpow_ge_0.
apply Zfloor_lb.
intros g Hg Hgx.
869 870
rewrite <- (round_generic rndDN _ Hg).
now apply round_monotone.
871 872 873 874 875 876 877 878 879
Qed.

Theorem generic_format_satisfies_any :
  satisfies_any generic_format.
Proof.
split.
(* symmetric set *)
exact generic_format_0.
exact generic_format_opp.
880
(* round down *)
881
intros x.
882
exists (round rndDN x).
883 884 885
apply generic_DN_pt.
Qed.

886 887
Theorem generic_UP_pt :
  forall x,
888
  Rnd_UP_pt generic_format x (round rndUP x).
889 890
Proof.
intros x.
891
rewrite <- (Ropp_involutive x).
892
rewrite round_UP_opp.
893 894 895
apply Rnd_DN_UP_pt_sym.
apply generic_format_satisfies_any.
apply generic_DN_pt.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
896 897
Qed.

898
Theorem round_DN_small_pos :
899
  forall x ex,
Guillaume Melquiond's avatar
Guillaume Melquiond committed
900
  (bpow (ex - 1) <= x < bpow ex)%R ->
901
  (ex <= fexp ex)%Z ->
902
  round rndDN x = R0.
903 904
Proof.
intros x ex Hx He.
905 906
rewrite <- (F2R_0 beta (canonic_exponent x)).
rewrite <- mantissa_DN_small_pos with (1 := Hx) (2 := He).
907
now rewrite <- canonic_exponent_fexp_pos with (1 := Hx).
908 909
Qed.

910
Theorem round_UP_small_pos :
911
  forall x ex,
Guillaume Melquiond's avatar
Guillaume Melquiond committed
912
  (bpow (ex - 1) <= x < bpow ex)%R ->
913
  (ex <= fexp ex)%Z ->
914
  round rndUP x = (bpow (fexp ex)).
915 916
Proof.
intros x ex Hx He.
917 918
rewrite <- F2R_bpow.
rewrite <- mantissa_UP_small_pos with (1 := Hx) (2 := He).
919
now rewrite <- canonic_exponent_fexp_pos with (1 := Hx).
920 921
Qed.

922 923 924 925 926
Theorem generic_format_EM :
  forall x,
  generic_format x \/ ~generic_format x.
Proof.
intros x.
927
destruct (Req_dec (round rndDN x) x) as [Hx|Hx].
928
left.
929
rewrite <- Hx.
930
apply generic_format_round.
931 932 933
right.
intros H.
apply Hx.
934
now apply round_generic.
935 936
Qed.

937
Theorem round_large_pos_ge_pow :
938
  forall rnd x e,
939
  (0 < round rnd x)%R ->
Guillaume Melquiond's avatar
Guillaume Melquiond committed
940
  (bpow e <= x)%R ->
941
  (bpow e <= round rnd x)%R.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
942
Proof.
943
intros rnd x e Hd Hex.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
944 945
destruct (ln_beta beta x) as (ex, He).
assert (Hx: (0 < x)%R).
946 947
apply Rlt_le_trans with (2 := Hex).
apply bpow_gt_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
948 949 950 951 952 953 954 955
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)).
956
destruct (round_bounded_small_pos rnd x ex H He) as [Hr|Hr].
957 958 959 960
rewrite Hr in Hd.
elim Rlt_irrefl with (1 := Hd).
rewrite Hr.
apply -> bpow_le.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
961
omega.
962
apply (round_bounded_large_pos rnd x ex H He).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
963 964
Qed.

965 966
Theorem canonic_exponent_DN :
  forall x,
967 968
  (0 < round rndDN x)%R ->
  canonic_exponent (round rndDN x) = canonic_exponent x.
969
Proof.
970
intros x Hd.
971 972 973
unfold canonic_exponent.
apply f_equal.
apply ln_beta_unique.
974
rewrite (Rabs_pos_eq (round rndDN x)). 2: now apply Rlt_le.
975 976 977 978
destruct (ln_beta beta x) as (ex, He).
simpl.
assert (Hx: (0 < x)%R).
apply Rlt_le_trans with (1 := Hd).
979
apply (generic_DN_pt x).
980 981 982
specialize (He (Rgt_not_eq _ _ Hx)).
rewrite Rabs_pos_eq in He. 2: now apply Rlt_le.
split.
983
apply round_large_pos_ge_pow with (1 := Hd).
984
apply He.
985
apply Rle_lt_trans with (2 := proj2 He).
986 987 988
apply (generic_DN_pt x).
Qed.

989 990
Theorem scaled_mantissa_DN :
  forall x,
991 992
  (0 < round rndDN x)%R ->
  scaled_mantissa (round rndDN x) = Z2R (Zfloor (scaled_mantissa x)).