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

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 99 100 101 102 103 104 105 106 107
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.
108
unfold generic_format, scaled_mantissa.
109 110 111
set (e' := canonic_exponent (F2R (Float beta m e))).
intros He.
unfold F2R at 3. simpl.
112
assert (H: (Z2R m * bpow e * bpow (- e') = Z2R (m * Zpower beta (e + -e')))%R).
113
rewrite Rmult_assoc, <- bpow_plus, Z2R_mult.
114 115 116 117 118 119
rewrite Z2R_Zpower.
apply refl_equal.
now apply Zle_left.
rewrite H, Ztrunc_Z2R.
unfold F2R. simpl.
rewrite <- H.
120
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_l.
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 149 150
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.

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

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

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

180 181 182 183 184 185 186 187 188 189
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.

190 191 192 193 194 195 196 197 198 199 200 201 202
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.

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

Guillaume Melquiond's avatar
Guillaume Melquiond committed
214 215 216 217 218 219 220 221 222 223 224
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.

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

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

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

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

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

318
(** Generic facts about any format *)
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
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.

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

352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
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.

369 370
Theorem canonic_exp_ge:
  forall prec,
371
  (forall e, (e - fexp e <= prec)%Z) ->
372 373 374 375 376 377 378 379
  (* 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.
380 381 382
destruct (ln_beta beta x) as (ex,Ex) ; simpl.
specialize (Ex Hxz).
apply Rlt_le_trans with (1 := proj2 Ex).
383
apply bpow_le.
384
specialize (Hp ex).
385 386 387
omega.
Qed.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
388 389 390 391 392 393 394 395 396 397
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.
398
destruct (valid_exp e) as (Y1,Y2).
BOLDO Sylvie's avatar
BOLDO Sylvie committed
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
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.
415
apply bpow_lt.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
416 417 418 419 420 421
omega.
now rewrite ln_beta_bpow.
unfold scaled_mantissa.
apply Rmult_le_pos; apply bpow_ge_0.
Qed.

422
Section Fcore_generic_round_pos.
423

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

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

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

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

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

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

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

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

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

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

683
End Fcore_generic_round_pos.
684

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

696
Section Zround_opp.
697

698
Variable rnd : Zround.
699

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

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

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

722
Definition Zround_opp := mkZround Zrnd_opp Zrnd_opp_le Zrnd_Z2R_opp.
723

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

736
End Zround_opp.
737

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

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

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

802 803
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
804
Proof.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
805
intros rnd x y Hx Hxy.
806 807
rewrite <- (round_generic rnd x Hx).
now apply round_monotone.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
808
Qed.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
809

810 811
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
812
Proof.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
813
intros rnd x y Hy Hxy.
814 815
rewrite <- (round_generic rnd y Hy).
now apply round_monotone.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
816
Qed.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
817

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

843 844
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
845 846
Proof.
intros rnd x y.
847
apply round_abs_abs.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
848
clear rnd y; intros rnd y Hy.
849
now apply round_monotone_l.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
850 851
Qed.

852 853
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
854 855
Proof.
intros rnd x y.
856
apply round_abs_abs.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
857
clear rnd x; intros rnd x Hx.
858
now apply round_monotone_r.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
859 860
Qed.

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

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

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

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

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

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

954
Theorem round_ZR_pt :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
955 956 957 958 959 960 961
  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).
962
apply round_DN_pt.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
963
apply F2R_eq_compat.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
964 965 966 967 968 969 970
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).
971
apply round_UP_pt.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
972
apply F2R_eq_compat.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
973 974 975 976 977 978 979
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.

980
Theorem round_DN_small_pos :
981
  forall x ex,
Guillaume Melquiond's avatar
Guillaume Melquiond committed
982
  (bpow (ex - 1) <= x < bpow ex)%R ->
983
  (ex <= fexp ex)%Z ->