Fcore_generic_fmt.v 40.2 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

Class Valid_exp :=
  valid_exp :
38 39 40 41 42 43
  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 ).

44
Context { valid_exp_ : Valid_exp }.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
45

46
Definition canonic_exponent x :=
47
  fexp (ln_beta beta x).
48 49 50

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

52 53 54
Definition scaled_mantissa x :=
  (x * bpow (- canonic_exponent x))%R.

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

58
(** Basic facts *)
Guillaume Melquiond's avatar
Guillaume Melquiond committed
59 60 61
Theorem generic_format_0 :
  generic_format 0.
Proof.
62
unfold generic_format, scaled_mantissa.
63 64 65 66 67 68 69 70 71 72 73 74
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
75 76
Qed.

77 78 79 80 81 82 83 84 85
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.

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

116
Theorem generic_format_F2R :
117
  forall m e,
118
  ( m <> 0 -> canonic_exponent (F2R (Float beta m e)) <= e )%Z ->
119 120 121
  generic_format (F2R (Float beta m e)).
Proof.
intros m e.
122 123 124 125
destruct (Z_eq_dec m 0) as [Zm|Zm].
intros _.
rewrite Zm, F2R_0.
apply generic_format_0.
126
unfold generic_format, scaled_mantissa.
127 128
set (e' := canonic_exponent (F2R (Float beta m e))).
intros He.
129
specialize (He Zm).
130
unfold F2R at 3. simpl.
131 132 133 134
rewrite  F2R_change_exp with (1 := He).
apply F2R_eq_compat.
rewrite Rmult_assoc, <- bpow_plus, <- Z2R_Zpower, <- Z2R_mult.
now rewrite Ztrunc_Z2R.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
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.
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.

165
Theorem scaled_mantissa_generic :
166 167
  forall x,
  generic_format x ->
168
  scaled_mantissa x = Z2R (Ztrunc (scaled_mantissa x)).
169 170
Proof.
intros x Hx.
171
unfold scaled_mantissa.
172 173
pattern x at 1 3 ; rewrite Hx.
unfold F2R. simpl.
174
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_r, Rmult_1_r.
175 176 177
now rewrite Ztrunc_Z2R.
Qed.

178 179 180 181 182 183
Theorem scaled_mantissa_bpow :
  forall x,
  (scaled_mantissa x * bpow (canonic_exponent x))%R = x.
Proof.
intros x.
unfold scaled_mantissa.
184
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_l.
185 186 187
apply Rmult_1_r.
Qed.

188 189 190 191 192 193
Theorem scaled_mantissa_0 :
  scaled_mantissa 0 = R0.
Proof.
apply Rmult_0_l.
Qed.

194 195 196 197 198 199 200 201 202 203
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.

204 205 206 207 208 209 210 211 212 213 214 215 216
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.

217 218 219 220 221
Theorem generic_format_opp :
  forall x, generic_format x -> generic_format (-x).
Proof.
intros x Hx.
unfold generic_format.
222 223 224 225
rewrite scaled_mantissa_opp, canonic_exponent_opp.
rewrite Ztrunc_opp.
rewrite <- opp_F2R.
now apply f_equal.
226 227
Qed.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
228 229 230 231 232 233 234 235 236 237 238
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.

239 240 241 242 243 244 245 246 247 248 249 250 251 252
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 _.
rewrite scaled_mantissa_opp, canonic_exponent_opp, Ztrunc_opp.
intros H.
rewrite <- (Ropp_involutive x) at 1.
rewrite H, <- opp_F2R.
apply Ropp_involutive.
easy.
Qed.

253
Theorem canonic_exponent_fexp :
254
  forall x ex,
255
  (bpow (ex - 1) <= Rabs x < bpow ex)%R ->
256 257 258 259 260 261 262
  canonic_exponent x = fexp ex.
Proof.
intros x ex Hx.
unfold canonic_exponent.
now rewrite ln_beta_unique with (1 := Hx).
Qed.

263
Theorem canonic_exponent_fexp_pos :
264
  forall x ex,
265
  (bpow (ex - 1) <= x < bpow ex)%R ->
266 267 268
  canonic_exponent x = fexp ex.
Proof.
intros x ex Hx.
269 270 271
apply canonic_exponent_fexp.
rewrite Rabs_pos_eq.
exact Hx.
272 273 274 275
apply Rle_trans with (2 := proj1 Hx).
apply bpow_ge_0.
Qed.

276
(** Properties when the real number is "small" (kind of subnormal) *)
277 278 279 280 281 282 283
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.
284
split.
285 286 287 288 289 290
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.
291
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_l.
292 293
rewrite Rmult_1_r, Rmult_1_l.
apply Rlt_le_trans with (1 := proj2 Hx).
294
now apply bpow_le.
295 296
Qed.

297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
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).
319
now rewrite (proj2 (proj2 (valid_exp _) He)).
320 321
Qed.

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
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.
344 345
Qed.

346
(** Generic facts about any format *)
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
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.

366 367 368 369 370 371
Theorem generic_format_canonic :
  forall f, canonic f ->
  generic_format (F2R f).
Proof.
intros (m, e) Hf.
unfold canonic in Hf. simpl in Hf.
372
unfold generic_format, scaled_mantissa.
373
rewrite <- Hf.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
374
apply F2R_eq_compat.
375
unfold F2R. simpl.
376
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_r, Rmult_1_r.
377 378 379
now rewrite Ztrunc_Z2R.
Qed.

380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
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.

397 398
Theorem canonic_exp_ge:
  forall prec,
399
  (forall e, (e - fexp e <= prec)%Z) ->
400 401 402 403 404 405 406 407
  (* 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.
408 409 410
destruct (ln_beta beta x) as (ex,Ex) ; simpl.
specialize (Ex Hxz).
apply Rlt_le_trans with (1 := proj2 Ex).
411
apply bpow_le.
412
specialize (Hp ex).
413 414 415
omega.
Qed.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
416 417 418 419 420 421 422 423 424 425
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.
426
destruct (valid_exp e) as (Y1,Y2).
BOLDO Sylvie's avatar
BOLDO Sylvie committed
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
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.
443
apply bpow_lt.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
444 445 446 447 448 449
omega.
now rewrite ln_beta_bpow.
unfold scaled_mantissa.
apply Rmult_le_pos; apply bpow_ge_0.
Qed.

450
Section Fcore_generic_round_pos.
451

452
(** * Rounding functions: R -> Z *)
453 454 455 456 457 458

Variable rnd : R -> Z.

Class Valid_rnd := {
  Zrnd_monotone : forall x y, (x <= y)%R -> (rnd x <= rnd y)%Z ;
  Zrnd_Z2R : forall n, rnd (Z2R n) = n
459 460
}.

461
Context { valid_rnd : Valid_rnd }.
462

463
Theorem Zrnd_DN_or_UP :
464
  forall x, rnd x = Zfloor x \/ rnd x = Zceil x.
465
Proof.
466
intros x.
467
destruct (Zle_or_lt (rnd x) (Zfloor x)) as [Hx|Hx].
468 469
left.
apply Zle_antisym with (1 := Hx).
470
rewrite <- (Zrnd_Z2R (Zfloor x)).
471 472 473 474
apply Zrnd_monotone.
apply Zfloor_lb.
right.
apply Zle_antisym.
475
rewrite <- (Zrnd_Z2R (Zceil x)).
476 477 478 479 480 481 482 483 484 485
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.

486
(** * the most useful one: R -> F *)
487
Definition round x :=
488
  F2R (Float beta (rnd (scaled_mantissa x)) (canonic_exponent x)).
489

490 491
Theorem round_monotone_pos :
  forall x y, (0 < x)%R -> (x <= y)%R -> (round x <= round y)%R.
492
Proof.
493
intros x y Hx Hxy.
494
unfold round, scaled_mantissa, canonic_exponent.
495 496 497 498 499 500 501 502 503 504
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.
505
apply (lt_bpow beta).
506 507 508 509
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].
510
rewrite (proj2 (proj2 (valid_exp ey) Hy1) ex).
511 512 513 514 515 516 517 518
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].
519
rewrite (proj2 (proj2 (valid_exp ex) (Zle_trans _ _ _ He Hx2)) ey Hx2).
520 521 522 523 524
apply F2R_le_compat.
apply Zrnd_monotone.
apply Rmult_le_compat_r.
apply bpow_ge_0.
exact Hxy.
525
apply Rle_trans with (F2R (Float beta (rnd (bpow (ey - 1) * bpow (- fexp ey))) (fexp ey))).
526
rewrite <- bpow_plus.
527 528 529 530 531
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.
532
rewrite <- (Zrnd_Z2R 1).
533 534 535 536 537
apply Zrnd_monotone.
apply Rlt_le.
exact (proj2 (mantissa_small_pos _ _ Hex Hx1)).
unfold F2R. simpl.
rewrite Z2R_Zpower. 2: omega.
538
rewrite <- bpow_plus, Rmult_1_l.
539
apply bpow_le.
540
omega.
541
apply Rle_trans with (F2R (Float beta (rnd (bpow ex * bpow (- fexp ex))) (fexp ex))).
542 543 544 545 546 547
apply F2R_le_compat.
apply Zrnd_monotone.
apply Rmult_le_compat_r.
apply bpow_ge_0.
apply Rlt_le.
apply Hex.
548
rewrite <- bpow_plus.
549 550 551 552
rewrite <- Z2R_Zpower. 2: omega.
rewrite Zrnd_Z2R.
unfold F2R. simpl.
rewrite 2!Z2R_Zpower ; try omega.
553
rewrite <- 2!bpow_plus.
554
apply bpow_le.
555 556 557 558 559 560 561 562 563 564 565 566 567 568
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.

569
Theorem round_generic :
570 571
  forall x,
  generic_format x ->
572
  round x = x.
573 574
Proof.
intros x Hx.
575
unfold round.
576 577 578 579 580
rewrite scaled_mantissa_generic with (1 := Hx).
rewrite Zrnd_Z2R.
now apply sym_eq.
Qed.

581 582
Theorem round_0 :
  round 0 = R0.
583
Proof.
584
unfold round, scaled_mantissa.
585 586 587 588 589 590
rewrite Rmult_0_l.
fold (Z2R 0).
rewrite Zrnd_Z2R.
apply F2R_0.
Qed.

591
Theorem round_bounded_large_pos :
592 593 594
  forall x ex,
  (fexp ex < ex)%Z ->
  (bpow (ex - 1) <= x < bpow ex)%R ->
595
  (bpow (ex - 1) <= round x <= bpow ex)%R.
596 597
Proof.
intros x ex He Hx.
598
unfold round, scaled_mantissa.
599 600
rewrite (canonic_exponent_fexp_pos _ _ Hx).
unfold F2R. simpl.
601
destruct (Zrnd_DN_or_UP (x * bpow (- fexp ex))) as [Hr|Hr] ; rewrite Hr.
602 603 604
(* DN *)
split.
replace (ex - 1)%Z with (ex - 1 + - fexp ex + fexp ex)%Z by ring.
605
rewrite bpow_plus.
606 607
apply Rmult_le_compat_r.
apply bpow_ge_0.
608
assert (Hf: Z2R (Zpower beta (ex - 1 - fexp ex)) = bpow (ex - 1 + - fexp ex)).
609 610 611 612 613 614
apply Z2R_Zpower.
omega.
rewrite <- Hf.
apply Z2R_le.
apply Zfloor_lub.
rewrite Hf.
615
rewrite bpow_plus.
616 617 618 619 620 621
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.
622
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_r, Rmult_1_r.
623 624 625 626 627 628
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.
629
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_r, Rmult_1_r.
630 631
apply Zceil_ub.
pattern ex at 3 ; replace ex with (ex - fexp ex + fexp ex)%Z by ring.
632
rewrite bpow_plus.
633 634
apply Rmult_le_compat_r.
apply bpow_ge_0.
635
assert (Hf: Z2R (Zpower beta (ex - fexp ex)) = bpow (ex - fexp ex)).
636 637 638 639 640 641 642
apply Z2R_Zpower.
omega.
rewrite <- Hf.
apply Z2R_le.
apply Zceil_glb.
rewrite Hf.
unfold Zminus.
643
rewrite bpow_plus.
644 645 646 647 648 649
apply Rmult_le_compat_r.
apply bpow_ge_0.
apply Rlt_le.
apply Hx.
Qed.

650
Theorem round_bounded_small_pos :
651 652 653
  forall x ex,
  (ex <= fexp ex)%Z ->
  (bpow (ex - 1) <= x < bpow ex)%R ->
654
  round x = R0 \/ round x = bpow (fexp ex).
655 656
Proof.
intros x ex He Hx.
657
unfold round, scaled_mantissa.
658 659
rewrite (canonic_exponent_fexp_pos _ _ Hx).
unfold F2R. simpl.
660
destruct (Zrnd_DN_or_UP (x * bpow (-fexp ex))) as [Hr|Hr] ; rewrite Hr.
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
(* 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.

678
Theorem generic_format_round_pos :
679 680
  forall x,
  (0 < x)%R ->
681
  generic_format (round x).
682 683 684 685 686 687 688
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 *)
689
destruct (round_bounded_small_pos _ _ He Hex) as [Hr|Hr] ; rewrite Hr.
690 691
apply generic_format_0.
apply generic_format_bpow.
692
now apply valid_exp.
693
(* large *)
694
generalize (round_bounded_large_pos _ _ He Hex).
695
intros (Hr1, Hr2).
696
destruct (Rle_or_lt (bpow ex) (round x)) as [Hr|Hr].
697 698
rewrite <- (Rle_antisym _ _ Hr Hr2).
apply generic_format_bpow.
699
now apply valid_exp.
700 701 702
assert (Hr' := conj Hr1 Hr).
unfold generic_format, scaled_mantissa.
rewrite (canonic_exponent_fexp_pos _ _ Hr').
703
unfold round, scaled_mantissa.
704 705
rewrite (canonic_exponent_fexp_pos _ _ Hex).
unfold F2R at 3. simpl.
706
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_r, Rmult_1_r.
707 708 709
now rewrite Ztrunc_Z2R.
Qed.

710
End Fcore_generic_round_pos.
711

712
Theorem round_ext :
713
  forall rnd1 rnd2,
714
  ( forall x, rnd1 x = rnd2 x ) ->
715
  forall x,
716
  round rnd1 x = round rnd2 x.
717 718
Proof.
intros rnd1 rnd2 Hext x.
719
unfold round.
720 721 722
now rewrite Hext.
Qed.

723
Section Zround_opp.
724

725 726
Variable rnd : R -> Z.
Context { valid_rnd : Valid_rnd rnd }.
727

728
Definition Zrnd_opp x := Zopp (rnd (-x)).
729

730 731 732 733
Global Instance valid_rnd_opp : Valid_rnd Zrnd_opp.
Proof with auto with typeclass_instances.
split.
(* *)
734
intros x y Hxy.
735
unfold Zrnd_opp.
736 737
apply Zopp_le_cancel.
rewrite 2!Zopp_involutive.
738
apply Zrnd_monotone...
739
now apply Ropp_le_contravar.
740
(* *)
741
intros n.
742
unfold Zrnd_opp.
743
rewrite <- Z2R_opp, Zrnd_Z2R...
744 745 746
apply Zopp_involutive.
Qed.

747
Theorem round_opp :
748
  forall x,
749
  round rnd (- x) = Ropp (round Zrnd_opp x).
750 751
Proof.
intros x.
752
unfold round.
753
rewrite opp_F2R, canonic_exponent_opp, scaled_mantissa_opp.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
754
apply F2R_eq_compat.
755 756 757 758
apply sym_eq.
exact (Zopp_involutive _).
Qed.

759
End Zround_opp.
760

761
(** IEEE-754 roundings: up, down and to zero *)
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787

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.

Section monotone.

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

789
Theorem round_DN_or_UP :
790 791
  forall x,
  round rnd x = round Zfloor x \/ round rnd x = round Zceil x.
792
Proof.
793
intros x.
794
unfold round.
795
destruct (Zrnd_DN_or_UP rnd (scaled_mantissa x)) as [Hx|Hx].
796 797 798 799
left. now rewrite Hx.
right. now rewrite Hx.
Qed.

800
Theorem round_monotone :
801 802 803
  forall x y, (x <= y)%R -> (round rnd x <= round rnd y)%R.
Proof with auto with typeclass_instances.
intros x y Hxy.
804
destruct (total_order_T x 0) as [[Hx|Hx]|Hx].
805
3: now apply round_monotone_pos.
806
(* x < 0 *)
807
unfold round.
808 809 810 811 812 813 814
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.
815
apply (round_monotone_pos (Zrnd_opp rnd) (-y) (-x)).
816 817 818 819 820 821
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.
822
rewrite <- (Zrnd_Z2R rnd 0).
823
apply Zrnd_monotone...
824
simpl.
825
rewrite <- (Rmult_0_l (bpow (- fexp (ln_beta beta x)))).
826 827 828 829
apply Rmult_le_compat_r.
apply bpow_ge_0.
now apply Rlt_le.
apply F2R_ge_0_compat. simpl.
830
rewrite <- (Zrnd_Z2R rnd 0).
831
apply Zrnd_monotone...
832 833 834 835 836
apply Rmult_le_pos.
exact Hy.
apply bpow_ge_0.
(* x = 0 *)
rewrite Hx.
837
rewrite round_0...
838 839
apply F2R_ge_0_compat.
simpl.
840
rewrite <- (Zrnd_Z2R rnd 0).
841
apply Zrnd_monotone...
842 843 844 845 846
apply Rmult_le_pos.
now rewrite <- Hx.
apply bpow_ge_0.
Qed.

847
Theorem round_monotone_l :
848
  forall x y, generic_format x -> (x <= y)%R -> (x <= round rnd y)%R.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
849
Proof.
850
intros x y Hx Hxy.
851 852
rewrite <- (round_generic rnd x Hx).
now apply round_monotone.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
853
Qed.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
854

855
Theorem round_monotone_r :
856
  forall x y, generic_format y -> (x <= y)%R -> (round rnd x <= y)%R.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
857
Proof.
858
intros x y Hy Hxy.
859 860
rewrite <- (round_generic rnd y Hy).
now apply round_monotone.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
861
Qed.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
862

863 864
End monotone.

865
Theorem round_abs_abs :
866
  forall P : R -> R -> Prop,
867 868 869 870
  ( forall rnd (Hr : Valid_rnd rnd) x, P x (round rnd x) ) ->
  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.
871 872 873
destruct (Rle_or_lt 0 x) as [Hx|Hx].
(* . *)
rewrite 2!Rabs_pos_eq.
874
now apply HP.
875 876
rewrite <- (round_0 rnd).
now apply round_monotone.
877 878 879 880 881
exact Hx.
(* . *)
rewrite (Rabs_left _ Hx).
rewrite Rabs_left1.
pattern x at 2 ; rewrite <- Ropp_involutive.
882
rewrite round_opp.
883
rewrite Ropp_involutive.
884
apply HP...
885
rewrite <- (round_0 rnd).
886
apply round_monotone...
887 888 889
now apply Rlt_le.
Qed.

890 891 892 893 894
Section monotone_abs.

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

895
Theorem round_monotone_abs_l :
896 897 898 899 900 901 902
  forall x y, generic_format x -> (x <= Rabs y)%R -> (x <= Rabs (round rnd y))%R.
Proof with auto with typeclass_instances.
intros x y.
apply round_abs_abs...
clear rnd valid_rnd y.
intros rnd' Hrnd y Hy.
apply round_monotone_l...
BOLDO Sylvie's avatar
BOLDO Sylvie committed
903 904
Qed.

905
Theorem round_monotone_abs_r :
906 907 908 909 910 911 912
  forall x y, generic_format y -> (Rabs x <= y)%R -> (Rabs (round rnd x) <= y)%R.
Proof with auto with typeclass_instances.
intros x y.
apply round_abs_abs...
clear rnd valid_rnd x.
intros rnd' Hrnd x Hx.
apply round_monotone_r...
BOLDO Sylvie's avatar
BOLDO Sylvie committed
913 914
Qed.

915 916
End monotone_abs.

917
Theorem round_DN_opp :
918
  forall x,
919
  round Zfloor (-x) = (- round Zceil x)%R.
920 921
Proof.
intros x.
922
unfold round.
923 924 925 926 927 928 929
rewrite scaled_mantissa_opp.
rewrite opp_F2R.
unfold Zceil.
rewrite Zopp_involutive.
now rewrite canonic_exponent_opp.
Qed.

930
Theorem round_UP_opp :
931
  forall x,
932
  round Zceil (-x) = (- round Zfloor x)%R.
933 934
Proof.
intros x.
935
unfold round.
936 937 938 939 940 941 942
rewrite scaled_mantissa_opp.
rewrite opp_F2R.
unfold Zceil.
rewrite Ropp_involutive.
now rewrite canonic_exponent_opp.
Qed.

943
Theorem generic_format_round :
944 945 946 947
  forall rnd { Hr : Valid_rnd rnd } x,
  generic_format (round rnd x).
Proof with auto with typeclass_instances.
intros rnd Zrnd x.
948 949
destruct (total_order_T x 0) as [[Hx|Hx]|Hx].
rewrite <- (Ropp_involutive x).
950 951
destruct (round_DN_or_UP rnd (- - x)) as [Hr|Hr] ; rewrite Hr.
rewrite round_DN_opp.
952
apply generic_format_opp.
953
apply generic_format_round_pos...
954
now apply Ropp_0_gt_lt_contravar.
955
rewrite round_UP_opp.
956
apply generic_format_opp.
957
apply generic_format_round_pos...
958 959
now apply Ropp_0_gt_lt_contravar.
rewrite Hx.
960
rewrite round_0...
961
apply generic_format_0.
962
now apply generic_format_round_pos.
963 964
Qed.

965
Theorem round_DN_pt :
966
  forall x,
967 968
  Rnd_DN_pt generic_format x (round Zfloor x).
Proof with auto with typeclass_instances.
969 970
intros x.
split.
971
apply generic_format_round...
972 973
split.
pattern x at 2 ; rewrite <- scaled_mantissa_bpow.
974
unfold round, F2R. simpl.
975 976 977 978
apply Rmult_le_compat_r.
apply bpow_ge_0.
apply Zfloor_lb.
intros g Hg Hgx.
979
apply round_monotone_l...
980 981 982 983 984 985 986 987 988
Qed.

Theorem generic_format_satisfies_any :
  satisfies_any generic_format.
Proof.
split.
(* symmetric set *)
exact generic_format_0.
exact generic_format_opp.
989
(* round down *)
990
intros x.
991
eexists.
992
apply round_DN_pt.
993 994
Qed.

995
Theorem round_UP_pt :
996
  forall x,
997
  Rnd_UP_pt generic_format x (round Zceil x).
998 999
Proof.
intros x.
1000
rewrite <- (Ropp_involutive x).
1001
rewrite round_UP_opp.
1002
apply Rnd_DN_UP_pt_sym.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
1003
apply generic_format_opp.
1004
apply round_DN_pt.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
1005 1006
Qed.

1007
Theorem round_ZR_pt :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
1008
  forall x,
1009
  Rnd_ZR_pt generic_format x (round Ztrunc x).