Float32.v 37.7 KB
Newer Older
1 2 3
(********************************************************************)
(*                                                                  *)
(*  The Why3 Verification Platform   /   The Why3 Development Team  *)
Guillaume Melquiond's avatar
Guillaume Melquiond committed
4
(*  Copyright 2010-2018   --   Inria - CNRS - Paris-Sud University  *)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
(*                                                                  *)
(*  This software is distributed under the terms of the GNU Lesser  *)
(*  General Public License version 2.1, with the special exception  *)
(*  on linking described in file LICENSE.                           *)
(*                                                                  *)
(********************************************************************)

(* This file is generated by Why3's Coq-realize driver *)
(* Beware! Only edit allowed sections below    *)
Require Import BuiltIn.
Require Reals.Rbasic_fun.
Require Reals.R_sqrt.
Require BuiltIn.
Require int.Int.
Require real.Real.
Require real.RealInfix.
Require real.Abs.
Require real.FromInt.
Require real.Truncate.
Require real.Square.
Require bv.Pow2int.
Require ieee_float.RoundingMode.
Require ieee_float.GenericFloat.

Import Flocq.Core.Fcore.
Import Flocq.Appli.Fappli_IEEE.
Import ieee_float.RoundingMode.
Import ieee_float.GenericFloat.

(* Why3 goal *)
Definition t : Type.
Proof.
  exact (t 8 24).
Defined.

(* Why3 goal *)
41
Definition t'real : t -> R.
42 43 44 45 46
Proof.
  apply B2R.
Defined.

(* Why3 goal *)
47
Definition t'isFinite : t -> Prop.
48 49 50 51 52
Proof.
  apply is_finite.
Defined.

(* Why3 goal *)
53 54 55 56
Lemma t'axiom :
  forall (x:t), (t'isFinite x) ->
  ((-(16777215 * 20282409603651670423947251286016)%R)%R <= (t'real x))%R /\
  ((t'real x) <= (16777215 * 20282409603651670423947251286016)%R)%R.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
Proof.
intros x _.
apply Rabs_le_inv.
change (Rabs (B2R _ _ x) <= F2R (Float radix2 (Zpower radix2 24 - 1) (127 - 23)))%R.
destruct x as [s|s|s|s m e H] ;
  try (simpl ; rewrite Rabs_R0 ; now apply F2R_ge_0_compat).
simpl.
rewrite <- F2R_Zabs.
rewrite abs_cond_Zopp.
apply andb_prop in H.
destruct H as [H1 H2].
apply Zeq_bool_eq in H1.
apply Zle_bool_imp_le in H2.
rewrite Fcore_digits.Zpos_digits2_pos in H1.
apply Rmult_le_compat.
now apply (Z2R_le 0).
apply bpow_ge_0.
apply Z2R_le.
apply (Z.lt_le_pred (Zabs (Zpos m)) (Zpower radix2 24)).
apply Fcore_digits.Zpower_gt_Zdigits.
revert H1.
generalize (Fcore_digits.Zdigits radix2 (Z.pos m)).
unfold FLT_exp, sb.
intros ; zify ; omega.
now apply bpow_le.
Qed.

(* Why3 goal *)
85
Definition zeroF : t.
86 87 88 89 90
Proof.
  apply zeroF.
Defined.

(* Why3 goal *)
91
Definition add : ieee_float.RoundingMode.mode -> t -> t -> t.
92 93 94 95 96
Proof.
  now apply add.
Defined.

(* Why3 goal *)
97
Definition sub : ieee_float.RoundingMode.mode -> t -> t -> t.
98 99 100 101 102
Proof.
  now apply sub.
Defined.

(* Why3 goal *)
103
Definition mul : ieee_float.RoundingMode.mode -> t -> t -> t.
104 105 106 107 108
Proof.
  now apply mul.
Defined.

(* Why3 goal *)
109
Definition div : ieee_float.RoundingMode.mode -> t -> t -> t.
110 111 112 113 114
Proof.
  now apply div.
Defined.

(* Why3 goal *)
115
Definition abs : t -> t.
116 117 118 119 120
Proof.
  apply abs.
Defined.

(* Why3 goal *)
121
Definition neg : t -> t.
122 123 124 125 126
Proof.
  apply neg.
Defined.

(* Why3 goal *)
127
Definition fma : ieee_float.RoundingMode.mode -> t -> t -> t -> t.
128 129 130 131 132
Proof.
  now apply fma.
Defined.

(* Why3 goal *)
133
Definition sqrt : ieee_float.RoundingMode.mode -> t -> t.
134 135 136 137 138
Proof.
  now apply GenericFloat.sqrt.
Defined.

(* Why3 goal *)
139
Definition roundToIntegral : ieee_float.RoundingMode.mode -> t -> t.
140 141 142 143 144
Proof.
  now apply roundToIntegral.
Defined.

(* Why3 goal *)
145
Definition min : t -> t -> t.
146 147 148 149 150
Proof.
  now apply min.
Defined.

(* Why3 goal *)
151
Definition max : t -> t -> t.
152 153 154 155 156
Proof.
  now apply max.
Defined.

(* Why3 goal *)
157
Definition le : t -> t -> Prop.
158 159 160 161 162
Proof.
  apply le.
Defined.

(* Why3 goal *)
163
Definition lt : t -> t -> Prop.
164 165 166 167 168
Proof.
  apply lt.
Defined.

(* Why3 goal *)
169
Definition eq : t -> t -> Prop.
170 171 172 173 174
Proof.
  apply eq.
Defined.

(* Why3 goal *)
175
Definition is_normal : t -> Prop.
176 177 178 179 180
Proof.
  apply is_normal.
Defined.

(* Why3 goal *)
181
Definition is_subnormal : t -> Prop.
182 183 184 185 186
Proof.
  apply is_subnormal.
Defined.

(* Why3 goal *)
187
Definition is_zero : t -> Prop.
188 189 190 191 192
Proof.
  apply is_zero.
Defined.

(* Why3 goal *)
193
Definition is_infinite : t -> Prop.
194 195 196 197 198
Proof.
  apply is_infinite.
Defined.

(* Why3 goal *)
199
Definition is_nan : t -> Prop.
200 201 202 203 204
Proof.
  apply is_nan.
Defined.

(* Why3 goal *)
205
Definition is_positive : t -> Prop.
206 207 208 209 210
Proof.
  apply is_positive.
Defined.

(* Why3 goal *)
211
Definition is_negative : t -> Prop.
212 213 214 215 216
Proof.
  apply is_negative.
Defined.

(* Why3 assumption *)
217 218
Definition is_plus_infinity (x:t) : Prop :=
  (is_infinite x) /\ (is_positive x).
219 220

(* Why3 assumption *)
221 222
Definition is_minus_infinity (x:t) : Prop :=
  (is_infinite x) /\ (is_negative x).
223 224

(* Why3 assumption *)
225
Definition is_plus_zero (x:t) : Prop := (is_zero x) /\ (is_positive x).
226 227

(* Why3 assumption *)
228
Definition is_minus_zero (x:t) : Prop := (is_zero x) /\ (is_negative x).
229 230

(* Why3 assumption *)
231
Definition is_not_nan (x:t) : Prop := (t'isFinite x) \/ (is_infinite x).
232 233

(* Why3 goal *)
234
Lemma is_not_nan1 : forall (x:t), (is_not_nan x) <-> ~ (is_nan x).
235 236 237 238 239
Proof.
  apply is_not_nan1.
Qed.

(* Why3 goal *)
240
Lemma is_not_finite :
241
  forall (x:t), ~ (t'isFinite x) <-> ((is_infinite x) \/ (is_nan x)).
242 243 244 245 246
Proof.
  apply is_not_finite.
Qed.

(* Why3 goal *)
247
Lemma zeroF_is_positive : is_positive zeroF.
248 249 250 251 252
Proof.
  apply zeroF_is_positive.
Qed.

(* Why3 goal *)
253
Lemma zeroF_is_zero : is_zero zeroF.
254 255 256 257 258
Proof.
  apply zeroF_is_zero.
Qed.

(* Why3 goal *)
259 260
Lemma zero_to_real :
  forall (x:t), (is_zero x) <-> ((t'isFinite x) /\ ((t'real x) = 0%R)).
261 262 263 264 265
Proof.
  apply zero_to_real.
Qed.

(* Why3 goal *)
266
Definition of_int : ieee_float.RoundingMode.mode -> Z -> t.
267 268 269 270 271
Proof.
  now apply z_to_fp.
Defined.

(* Why3 goal *)
272
Definition to_int : ieee_float.RoundingMode.mode -> t -> Z.
273 274 275 276 277
Proof.
  now apply fp_to_z.
Defined.

(* Why3 goal *)
278 279
Lemma zero_of_int :
  forall (m:ieee_float.RoundingMode.mode), (zeroF = (of_int m 0%Z)).
280 281 282 283 284
Proof.
  apply zero_of_int.
Qed.

(* Why3 goal *)
285
Definition round : ieee_float.RoundingMode.mode -> R -> R.
286 287 288 289 290 291 292 293 294 295 296 297 298 299
Proof.
  apply (round 8 24).
Defined.

Lemma max_real_cst :
  max_real 8 24 = (33554430 * 10141204801825835211973625643008)%R.
Proof.
  change (33554430 * 10141204801825835211973625643008)%R
    with (F2R (Float radix2 (16777215 * Zpower radix2 (104 - 103)) 103)).
  rewrite <- F2R_change_exp by easy.
  now rewrite <- max_real_is_F2R.
Qed.

(* Why3 goal *)
300
Definition max_int : Z.
301 302 303 304 305
Proof.
  exact (33554430 * 10141204801825835211973625643008)%Z.
Defined.

(* Why3 goal *)
306 307
Lemma max_real_int :
  ((33554430 * 10141204801825835211973625643008)%R = (BuiltIn.IZR max_int)).
308 309 310 311 312 313
Proof.
  unfold max_int.
  now rewrite mult_IZR, <- !Z2R_IZR.
Qed.

(* Why3 assumption *)
314
Definition in_range (x:R) : Prop :=
315 316
  ((-(33554430 * 10141204801825835211973625643008)%R)%R <= x)%R /\
  (x <= (33554430 * 10141204801825835211973625643008)%R)%R.
317 318

(* Why3 assumption *)
319 320
Definition in_int_range (i:Z) : Prop :=
  ((-max_int)%Z <= i)%Z /\ (i <= max_int)%Z.
321 322

(* Why3 goal *)
323
Lemma is_finite : forall (x:t), (t'isFinite x) -> in_range (t'real x).
324 325 326 327 328 329 330 331
Proof.
  unfold t'isFinite, in_range.
  intros x Hx.
  rewrite <- max_real_cst.
  now apply is_finite1.
Qed.

(* Why3 assumption *)
332
Definition no_overflow (m:ieee_float.RoundingMode.mode) (x:R) : Prop :=
333
  in_range (round m x).
334 335

(* Why3 goal *)
336
Lemma Bounded_real_no_overflow :
337 338
  forall (m:ieee_float.RoundingMode.mode) (x:R), (in_range x) ->
  no_overflow m x.
339 340 341 342 343 344 345
Proof.
  unfold no_overflow, in_range.
  rewrite <- max_real_cst.
  now apply (Bounded_real_no_overflow 8 24).
Qed.

(* Why3 goal *)
346
Lemma Round_monotonic :
347 348
  forall (m:ieee_float.RoundingMode.mode) (x:R) (y:R), (x <= y)%R ->
  ((round m x) <= (round m y))%R.
349 350 351 352 353
Proof.
  apply Round_monotonic.
Qed.

(* Why3 goal *)
354 355 356 357
Lemma Round_idempotent :
  forall (m1:ieee_float.RoundingMode.mode) (m2:ieee_float.RoundingMode.mode)
    (x:R),
  ((round m1 (round m2 x)) = (round m2 x)).
358 359 360 361 362
Proof.
  apply Round_idempotent.
Qed.

(* Why3 goal *)
363
Lemma Round_to_real :
364 365
  forall (m:ieee_float.RoundingMode.mode) (x:t), (t'isFinite x) ->
  ((round m (t'real x)) = (t'real x)).
366 367 368 369 370
Proof.
  apply Round_to_real.
Qed.

(* Why3 goal *)
371 372
Lemma Round_down_le :
  forall (x:R), ((round ieee_float.RoundingMode.RTN x) <= x)%R.
373 374 375 376 377
Proof.
  apply Round_down_le.
Qed.

(* Why3 goal *)
378 379
Lemma Round_up_ge :
  forall (x:R), (x <= (round ieee_float.RoundingMode.RTP x))%R.
380 381 382 383 384
Proof.
  apply Round_up_ge.
Qed.

(* Why3 goal *)
385 386 387 388
Lemma Round_down_neg :
  forall (x:R),
  ((round ieee_float.RoundingMode.RTN (-x)%R) =
   (-(round ieee_float.RoundingMode.RTP x))%R).
389 390 391 392 393
Proof.
  apply Round_down_neg.
Qed.

(* Why3 goal *)
394 395 396 397
Lemma Round_up_neg :
  forall (x:R),
  ((round ieee_float.RoundingMode.RTP (-x)%R) =
   (-(round ieee_float.RoundingMode.RTN x))%R).
398 399 400 401 402
Proof.
  apply Round_up_neg.
Qed.

(* Why3 assumption *)
403 404
Definition in_safe_int_range (i:Z) : Prop :=
  ((-16777216%Z)%Z <= i)%Z /\ (i <= 16777216%Z)%Z.
405 406

(* Why3 goal *)
407
Lemma Exact_rounding_for_integers :
408 409
  forall (m:ieee_float.RoundingMode.mode) (i:Z), (in_safe_int_range i) ->
  ((round m (BuiltIn.IZR i)) = (BuiltIn.IZR i)).
410 411 412 413 414 415
Proof.
  intros m i h1.
  now apply Exact_rounding_for_integers.
Qed.

(* Why3 assumption *)
416 417 418
Definition same_sign (x:t) (y:t) : Prop :=
  ((is_positive x) /\ (is_positive y)) \/
  ((is_negative x) /\ (is_negative y)).
419 420

(* Why3 assumption *)
421 422 423
Definition diff_sign (x:t) (y:t) : Prop :=
  ((is_positive x) /\ (is_negative y)) \/
  ((is_negative x) /\ (is_positive y)).
424 425

(* Why3 goal *)
426
Lemma feq_eq :
427 428
  forall (x:t) (y:t), (t'isFinite x) -> (t'isFinite y) -> ~ (is_zero x) ->
  (eq x y) -> (x = y).
429 430 431 432 433
Proof.
  apply feq_eq.
Qed.

(* Why3 goal *)
434
Lemma eq_feq :
435
  forall (x:t) (y:t), (t'isFinite x) -> (t'isFinite y) -> (x = y) -> eq x y.
436 437 438 439 440
Proof.
  apply eq_feq.
Qed.

(* Why3 goal *)
441
Lemma eq_refl : forall (x:t), (t'isFinite x) -> eq x x.
442 443 444 445 446
Proof.
  apply eq_refl.
Qed.

(* Why3 goal *)
447
Lemma eq_sym : forall (x:t) (y:t), (eq x y) -> eq y x.
448 449 450 451 452
Proof.
  apply eq_sym.
Qed.

(* Why3 goal *)
453
Lemma eq_trans : forall (x:t) (y:t) (z:t), (eq x y) -> (eq y z) -> eq x z.
454 455 456 457 458
Proof.
  apply eq_trans.
Qed.

(* Why3 goal *)
459
Lemma eq_zero : eq zeroF (neg zeroF).
460 461 462 463 464
Proof.
  apply eq_zero.
Qed.

(* Why3 goal *)
465
Lemma eq_to_real_finite :
466
  forall (x:t) (y:t), ((t'isFinite x) /\ (t'isFinite y)) ->
467
  (eq x y) <-> ((t'real x) = (t'real y)).
468 469 470 471 472
Proof.
  apply eq_to_real_finite.
Qed.

(* Why3 goal *)
473 474 475 476 477 478
Lemma eq_special :
  forall (x:t) (y:t), (eq x y) ->
  (is_not_nan x) /\
  ((is_not_nan y) /\
   (((t'isFinite x) /\ (t'isFinite y)) \/
    ((is_infinite x) /\ ((is_infinite y) /\ (same_sign x y))))).
479 480 481 482 483
Proof.
  apply eq_special.
Qed.

(* Why3 goal *)
484
Lemma lt_finite :
485
  forall (x:t) (y:t), ((t'isFinite x) /\ (t'isFinite y)) ->
486
  (lt x y) <-> ((t'real x) < (t'real y))%R.
487 488 489 490 491
Proof.
  apply lt_finite.
Qed.

(* Why3 goal *)
492
Lemma le_finite :
493
  forall (x:t) (y:t), ((t'isFinite x) /\ (t'isFinite y)) ->
494
  (le x y) <-> ((t'real x) <= (t'real y))%R.
495 496 497 498 499
Proof.
  apply le_finite.
Qed.

(* Why3 goal *)
500
Lemma le_lt_trans :
501
  forall (x:t) (y:t) (z:t), ((le x y) /\ (lt y z)) -> lt x z.
502 503 504 505 506
Proof.
  apply le_lt_trans.
Qed.

(* Why3 goal *)
507
Lemma lt_le_trans :
508
  forall (x:t) (y:t) (z:t), ((lt x y) /\ (le y z)) -> lt x z.
509 510 511 512 513
Proof.
  apply lt_le_trans.
Qed.

(* Why3 goal *)
514
Lemma le_ge_asym : forall (x:t) (y:t), ((le x y) /\ (le y x)) -> eq x y.
515 516 517 518 519
Proof.
  apply le_ge_asym.
Qed.

(* Why3 goal *)
520
Lemma not_lt_ge :
521 522
  forall (x:t) (y:t), (~ (lt x y) /\ ((is_not_nan x) /\ (is_not_nan y))) ->
  le y x.
523 524 525 526 527
Proof.
  apply not_lt_ge.
Qed.

(* Why3 goal *)
528
Lemma not_gt_le :
529 530
  forall (x:t) (y:t), (~ (lt y x) /\ ((is_not_nan x) /\ (is_not_nan y))) ->
  le x y.
531 532 533 534 535
Proof.
 apply not_gt_le.
Qed.

(* Why3 goal *)
536 537 538 539 540
Lemma le_special :
  forall (x:t) (y:t), (le x y) ->
  ((t'isFinite x) /\ (t'isFinite y)) \/
  (((is_minus_infinity x) /\ (is_not_nan y)) \/
   ((is_not_nan x) /\ (is_plus_infinity y))).
541 542 543 544 545
Proof.
  apply le_special.
Qed.

(* Why3 goal *)
546 547 548 549 550
Lemma lt_special :
  forall (x:t) (y:t), (lt x y) ->
  ((t'isFinite x) /\ (t'isFinite y)) \/
  (((is_minus_infinity x) /\ ((is_not_nan y) /\ ~ (is_minus_infinity y))) \/
   ((is_not_nan x) /\ (~ (is_plus_infinity x) /\ (is_plus_infinity y)))).
551 552 553 554 555
Proof.
  apply lt_special.
Qed.

(* Why3 goal *)
556
Lemma lt_lt_finite :
557
  forall (x:t) (y:t) (z:t), (lt x y) -> (lt y z) -> t'isFinite y.
558 559 560 561 562
Proof.
  apply lt_lt_finite.
Qed.

(* Why3 goal *)
563
Lemma positive_to_real :
564
  forall (x:t), (t'isFinite x) -> (is_positive x) -> (0%R <= (t'real x))%R.
565 566 567 568 569
Proof.
  apply positive_to_real.
Qed.

(* Why3 goal *)
570
Lemma to_real_positive :
571
  forall (x:t), (t'isFinite x) -> (0%R < (t'real x))%R -> is_positive x.
572 573 574 575 576
Proof.
  apply to_real_positive.
Qed.

(* Why3 goal *)
577
Lemma negative_to_real :
578
  forall (x:t), (t'isFinite x) -> (is_negative x) -> ((t'real x) <= 0%R)%R.
579 580 581 582 583
Proof.
  apply negative_to_real.
Qed.

(* Why3 goal *)
584
Lemma to_real_negative :
585
  forall (x:t), (t'isFinite x) -> ((t'real x) < 0%R)%R -> is_negative x.
586 587 588 589 590
Proof.
  apply to_real_negative.
Qed.

(* Why3 goal *)
591 592
Lemma negative_xor_positive :
  forall (x:t), ~ ((is_positive x) /\ (is_negative x)).
593 594 595 596 597
Proof.
  apply negative_xor_positive.
Qed.

(* Why3 goal *)
598
Lemma negative_or_positive :
599
  forall (x:t), (is_not_nan x) -> (is_positive x) \/ (is_negative x).
600 601 602 603 604
Proof.
  apply negative_or_positive.
Qed.

(* Why3 goal *)
605
Lemma diff_sign_trans :
606 607
  forall (x:t) (y:t) (z:t), ((diff_sign x y) /\ (diff_sign y z)) ->
  same_sign x z.
608 609 610 611 612
Proof.
  apply diff_sign_trans.
Qed.

(* Why3 goal *)
613 614 615 616 617
Lemma diff_sign_product :
  forall (x:t) (y:t),
  ((t'isFinite x) /\
   ((t'isFinite y) /\ (((t'real x) * (t'real y))%R < 0%R)%R)) ->
  diff_sign x y.
618 619 620 621 622
Proof.
  apply diff_sign_product.
Qed.

(* Why3 goal *)
623 624 625 626
Lemma same_sign_product :
  forall (x:t) (y:t),
  ((t'isFinite x) /\ ((t'isFinite y) /\ (same_sign x y))) ->
  (0%R <= ((t'real x) * (t'real y))%R)%R.
627 628 629 630 631
Proof.
  apply same_sign_product.
Qed.

(* Why3 assumption *)
632
Definition product_sign (z:t) (x:t) (y:t) : Prop :=
633
  ((same_sign x y) -> is_positive z) /\ ((diff_sign x y) -> is_negative z).
634 635

(* Why3 assumption *)
636
Definition overflow_value (m:ieee_float.RoundingMode.mode) (x:t) : Prop :=
637
  match m with
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
  | ieee_float.RoundingMode.RTN =>
      ((is_positive x) ->
       (t'isFinite x) /\
       ((t'real x) = (33554430 * 10141204801825835211973625643008)%R)) /\
      (~ (is_positive x) -> is_infinite x)
  | ieee_float.RoundingMode.RTP =>
      ((is_positive x) -> is_infinite x) /\
      (~ (is_positive x) ->
       (t'isFinite x) /\
       ((t'real x) = (-(33554430 * 10141204801825835211973625643008)%R)%R))
  | ieee_float.RoundingMode.RTZ =>
      ((is_positive x) ->
       (t'isFinite x) /\
       ((t'real x) = (33554430 * 10141204801825835211973625643008)%R)) /\
      (~ (is_positive x) ->
       (t'isFinite x) /\
       ((t'real x) = (-(33554430 * 10141204801825835211973625643008)%R)%R))
  | (ieee_float.RoundingMode.RNA|ieee_float.RoundingMode.RNE) =>
      is_infinite x
657 658 659
  end.

(* Why3 assumption *)
660
Definition sign_zero_result (m:ieee_float.RoundingMode.mode) (x:t) : Prop :=
661 662
  (is_zero x) ->
  match m with
663 664
  | ieee_float.RoundingMode.RTN => is_negative x
  | _ => is_positive x
665 666 667
  end.

(* Why3 goal *)
668 669 670 671 672
Lemma add_finite :
  forall (m:ieee_float.RoundingMode.mode) (x:t) (y:t), (t'isFinite x) ->
  (t'isFinite y) -> (no_overflow m ((t'real x) + (t'real y))%R) ->
  (t'isFinite (add m x y)) /\
  ((t'real (add m x y)) = (round m ((t'real x) + (t'real y))%R)).
673 674 675 676 677 678 679 680
Proof.
  intros m x y h1 h2 h3.
  apply add_finite ; try easy.
  unfold no_overflow, in_range in h3.
  now rewrite <- max_real_cst in h3.
Qed.

(* Why3 goal *)
681 682
Lemma add_finite_rev :
  forall (m:ieee_float.RoundingMode.mode) (x:t) (y:t),
683
  (t'isFinite (add m x y)) -> (t'isFinite x) /\ (t'isFinite y).
684 685 686 687 688
Proof.
  apply add_finite_rev.
Qed.

(* Why3 goal *)
689 690 691 692 693
Lemma add_finite_rev_n :
  forall (m:ieee_float.RoundingMode.mode) (x:t) (y:t),
  (ieee_float.RoundingMode.to_nearest m) -> (t'isFinite (add m x y)) ->
  (no_overflow m ((t'real x) + (t'real y))%R) /\
  ((t'real (add m x y)) = (round m ((t'real x) + (t'real y))%R)).
694 695 696 697 698 699 700 701
Proof.
  intros m x y h1 h2.
  unfold no_overflow, in_range.
  rewrite <- max_real_cst.
  now apply add_finite_rev_n.
Qed.

(* Why3 goal *)
702 703 704 705 706
Lemma sub_finite :
  forall (m:ieee_float.RoundingMode.mode) (x:t) (y:t), (t'isFinite x) ->
  (t'isFinite y) -> (no_overflow m ((t'real x) - (t'real y))%R) ->
  (t'isFinite (sub m x y)) /\
  ((t'real (sub m x y)) = (round m ((t'real x) - (t'real y))%R)).
707 708 709 710 711 712 713 714
Proof.
  intros m x y h1 h2 h3.
  apply sub_finite ; try easy.
  unfold no_overflow, in_range in h3.
  now rewrite <- max_real_cst in h3.
Qed.

(* Why3 goal *)
715 716
Lemma sub_finite_rev :
  forall (m:ieee_float.RoundingMode.mode) (x:t) (y:t),
717
  (t'isFinite (sub m x y)) -> (t'isFinite x) /\ (t'isFinite y).
718 719 720 721 722
Proof.
  apply sub_finite_rev.
Qed.

(* Why3 goal *)
723 724 725 726 727
Lemma sub_finite_rev_n :
  forall (m:ieee_float.RoundingMode.mode) (x:t) (y:t),
  (ieee_float.RoundingMode.to_nearest m) -> (t'isFinite (sub m x y)) ->
  (no_overflow m ((t'real x) - (t'real y))%R) /\
  ((t'real (sub m x y)) = (round m ((t'real x) - (t'real y))%R)).
728 729 730 731 732 733 734 735
Proof.
  intros m x y h1 h2.
  unfold no_overflow, in_range.
  rewrite <- max_real_cst.
  now apply sub_finite_rev_n.
Qed.

(* Why3 goal *)
736 737 738 739 740
Lemma mul_finite :
  forall (m:ieee_float.RoundingMode.mode) (x:t) (y:t), (t'isFinite x) ->
  (t'isFinite y) -> (no_overflow m ((t'real x) * (t'real y))%R) ->
  (t'isFinite (mul m x y)) /\
  ((t'real (mul m x y)) = (round m ((t'real x) * (t'real y))%R)).
741 742 743 744 745 746 747 748
Proof.
  intros m x y h1 h2 h3.
  apply mul_finite ; try easy.
  unfold no_overflow, in_range in h3.
  now rewrite <- max_real_cst in h3.
Qed.

(* Why3 goal *)
749 750
Lemma mul_finite_rev :
  forall (m:ieee_float.RoundingMode.mode) (x:t) (y:t),
751
  (t'isFinite (mul m x y)) -> (t'isFinite x) /\ (t'isFinite y).
752 753 754 755 756
Proof.
  apply mul_finite_rev.
Qed.

(* Why3 goal *)
757 758 759 760 761
Lemma mul_finite_rev_n :
  forall (m:ieee_float.RoundingMode.mode) (x:t) (y:t),
  (ieee_float.RoundingMode.to_nearest m) -> (t'isFinite (mul m x y)) ->
  (no_overflow m ((t'real x) * (t'real y))%R) /\
  ((t'real (mul m x y)) = (round m ((t'real x) * (t'real y))%R)).
762 763 764 765 766 767 768 769
Proof.
  intros m x y h1 h2.
  unfold no_overflow, in_range.
  rewrite <- max_real_cst.
  now apply mul_finite_rev_n.
Qed.

(* Why3 goal *)
770 771 772 773 774 775
Lemma div_finite :
  forall (m:ieee_float.RoundingMode.mode) (x:t) (y:t), (t'isFinite x) ->
  (t'isFinite y) -> ~ (is_zero y) ->
  (no_overflow m ((t'real x) / (t'real y))%R) ->
  (t'isFinite (div m x y)) /\
  ((t'real (div m x y)) = (round m ((t'real x) / (t'real y))%R)).
776 777 778 779 780 781 782 783
Proof.
  intros m x y h1 h2 h3 h4.
  apply div_finite ; try easy.
  unfold no_overflow, in_range in h4.
  now rewrite <- max_real_cst in h4.
Qed.

(* Why3 goal *)
784 785 786
Lemma div_finite_rev :
  forall (m:ieee_float.RoundingMode.mode) (x:t) (y:t),
  (t'isFinite (div m x y)) ->
787 788
  ((t'isFinite x) /\ ((t'isFinite y) /\ ~ (is_zero y))) \/
  ((t'isFinite x) /\ ((is_infinite y) /\ ((t'real (div m x y)) = 0%R))).
789 790 791 792 793
Proof.
  apply div_finite_rev.
Qed.

(* Why3 goal *)
794 795 796 797 798 799
Lemma div_finite_rev_n :
  forall (m:ieee_float.RoundingMode.mode) (x:t) (y:t),
  (ieee_float.RoundingMode.to_nearest m) -> (t'isFinite (div m x y)) ->
  (t'isFinite y) ->
  (no_overflow m ((t'real x) / (t'real y))%R) /\
  ((t'real (div m x y)) = (round m ((t'real x) / (t'real y))%R)).
800 801 802 803 804 805 806 807
Proof.
  intros m x y h1 h2 h3.
  unfold no_overflow, in_range.
  rewrite <- max_real_cst.
  now apply div_finite_rev_n.
Qed.

(* Why3 goal *)
808
Lemma neg_finite :
809
  forall (x:t), (t'isFinite x) ->
810
  (t'isFinite (neg x)) /\ ((t'real (neg x)) = (-(t'real x))%R).
811 812 813 814 815
Proof.
  apply neg_finite.
Qed.

(* Why3 goal *)
816
Lemma neg_finite_rev :
817
  forall (x:t), (t'isFinite (neg x)) ->
818
  (t'isFinite x) /\ ((t'real (neg x)) = (-(t'real x))%R).
819 820 821 822 823
Proof.
  apply neg_finite_rev.
Qed.

(* Why3 goal *)
824 825 826 827 828
Lemma abs_finite :
  forall (x:t), (t'isFinite x) ->
  (t'isFinite (abs x)) /\
  (((t'real (abs x)) = (Reals.Rbasic_fun.Rabs (t'real x))) /\
   (is_positive (abs x))).
829 830 831 832 833
Proof.
  apply abs_finite.
Qed.

(* Why3 goal *)
834
Lemma abs_finite_rev :
835
  forall (x:t), (t'isFinite (abs x)) ->
836
  (t'isFinite x) /\ ((t'real (abs x)) = (Reals.Rbasic_fun.Rabs (t'real x))).
837 838 839 840 841
Proof.
  apply abs_finite_rev.
Qed.

(* Why3 goal *)
842
Lemma abs_universal : forall (x:t), ~ (is_negative (abs x)).
843 844 845 846 847
Proof.
  apply abs_universal.
Qed.

(* Why3 goal *)
848 849 850 851 852 853 854
Lemma fma_finite :
  forall (m:ieee_float.RoundingMode.mode) (x:t) (y:t) (z:t),
  (t'isFinite x) -> (t'isFinite y) -> (t'isFinite z) ->
  (no_overflow m (((t'real x) * (t'real y))%R + (t'real z))%R) ->
  (t'isFinite (fma m x y z)) /\
  ((t'real (fma m x y z)) =
   (round m (((t'real x) * (t'real y))%R + (t'real z))%R)).
855 856 857 858 859 860 861 862
Proof.
  intros m x y z h1 h2 h3 h4.
  apply fma_finite ; try easy.
  unfold no_overflow, in_range in h4.
  now rewrite <- max_real_cst in h4.
Qed.

(* Why3 goal *)
863 864 865
Lemma fma_finite_rev :
  forall (m:ieee_float.RoundingMode.mode) (x:t) (y:t) (z:t),
  (t'isFinite (fma m x y z)) ->
866
  (t'isFinite x) /\ ((t'isFinite y) /\ (t'isFinite z)).
867 868 869 870 871
Proof.
  apply fma_finite_rev.
Qed.

(* Why3 goal *)
872 873 874 875 876 877
Lemma fma_finite_rev_n :
  forall (m:ieee_float.RoundingMode.mode) (x:t) (y:t) (z:t),
  (ieee_float.RoundingMode.to_nearest m) -> (t'isFinite (fma m x y z)) ->
  (no_overflow m (((t'real x) * (t'real y))%R + (t'real z))%R) /\
  ((t'real (fma m x y z)) =
   (round m (((t'real x) * (t'real y))%R + (t'real z))%R)).
878 879 880 881 882 883 884 885
Proof.
  intros m x y z h1 h2.
  unfold no_overflow, in_range.
  rewrite <- max_real_cst.
  now apply fma_finite_rev_n.
Qed.

(* Why3 goal *)
886 887 888 889 890
Lemma sqrt_finite :
  forall (m:ieee_float.RoundingMode.mode) (x:t), (t'isFinite x) ->
  (0%R <= (t'real x))%R ->
  (t'isFinite (sqrt m x)) /\
  ((t'real (sqrt m x)) = (round m (Reals.R_sqrt.sqrt (t'real x)))).
891 892 893 894 895
Proof.
  apply sqrt_finite.
Qed.

(* Why3 goal *)
896 897 898 899 900
Lemma sqrt_finite_rev :
  forall (m:ieee_float.RoundingMode.mode) (x:t), (t'isFinite (sqrt m x)) ->
  (t'isFinite x) /\
  ((0%R <= (t'real x))%R /\
   ((t'real (sqrt m x)) = (round m (Reals.R_sqrt.sqrt (t'real x))))).
901 902 903 904 905
Proof.
  apply sqrt_finite_rev.
Qed.

(* Why3 assumption *)
906 907
Definition same_sign_real (x:t) (r:R) : Prop :=
  ((is_positive x) /\ (0%R < r)%R) \/ ((is_negative x) /\ (r < 0%R)%R).
908 909

(* Why3 goal *)
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
Lemma add_special :
  forall (m:ieee_float.RoundingMode.mode) (x:t) (y:t),
  let r := add m x y in
  (((is_nan x) \/ (is_nan y)) -> is_nan r) /\
  ((((t'isFinite x) /\ (is_infinite y)) ->
    (is_infinite r) /\ (same_sign r y)) /\
   ((((is_infinite x) /\ (t'isFinite y)) ->
     (is_infinite r) /\ (same_sign r x)) /\
    ((((is_infinite x) /\ ((is_infinite y) /\ (same_sign x y))) ->
      (is_infinite r) /\ (same_sign r x)) /\
     ((((is_infinite x) /\ ((is_infinite y) /\ (diff_sign x y))) -> is_nan r) /\
      ((((t'isFinite x) /\
         ((t'isFinite y) /\ ~ (no_overflow m ((t'real x) + (t'real y))%R))) ->
        (same_sign_real r ((t'real x) + (t'real y))%R) /\
        (overflow_value m r)) /\
       (((t'isFinite x) /\ (t'isFinite y)) ->
        ((same_sign x y) -> same_sign r x) /\
        (~ (same_sign x y) -> sign_zero_result m r))))))).
928 929 930 931 932 933 934 935
Proof.
  intros m x y r.
  unfold no_overflow, in_range, overflow_value.
  rewrite <- max_real_cst.
  apply add_special.
Qed.

(* Why3 goal *)
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
Lemma sub_special :
  forall (m:ieee_float.RoundingMode.mode) (x:t) (y:t),
  let r := sub m x y in
  (((is_nan x) \/ (is_nan y)) -> is_nan r) /\
  ((((t'isFinite x) /\ (is_infinite y)) ->
    (is_infinite r) /\ (diff_sign r y)) /\
   ((((is_infinite x) /\ (t'isFinite y)) ->
     (is_infinite r) /\ (same_sign r x)) /\
    ((((is_infinite x) /\ ((is_infinite y) /\ (same_sign x y))) -> is_nan r) /\
     ((((is_infinite x) /\ ((is_infinite y) /\ (diff_sign x y))) ->
       (is_infinite r) /\ (same_sign r x)) /\
      ((((t'isFinite x) /\
         ((t'isFinite y) /\ ~ (no_overflow m ((t'real x) - (t'real y))%R))) ->
        (same_sign_real r ((t'real x) - (t'real y))%R) /\
        (overflow_value m r)) /\
       (((t'isFinite x) /\ (t'isFinite y)) ->
        ((diff_sign x y) -> same_sign r x) /\
        (~ (diff_sign x y) -> sign_zero_result m r))))))).
954 955 956 957 958 959 960 961
Proof.
  intros m x y r.
  unfold no_overflow, in_range, overflow_value.
  rewrite <- max_real_cst.
  apply sub_special.
Qed.

(* Why3 goal *)
962 963 964 965 966 967 968 969 970 971 972 973 974 975
Lemma mul_special :
  forall (m:ieee_float.RoundingMode.mode) (x:t) (y:t),
  let r := mul m x y in
  (((is_nan x) \/ (is_nan y)) -> is_nan r) /\
  ((((is_zero x) /\ (is_infinite y)) -> is_nan r) /\
   ((((t'isFinite x) /\ ((is_infinite y) /\ ~ (is_zero x))) -> is_infinite r) /\
    ((((is_infinite x) /\ (is_zero y)) -> is_nan r) /\
     ((((is_infinite x) /\ ((t'isFinite y) /\ ~ (is_zero y))) ->
       is_infinite r) /\
      ((((is_infinite x) /\ (is_infinite y)) -> is_infinite r) /\
       ((((t'isFinite x) /\
          ((t'isFinite y) /\ ~ (no_overflow m ((t'real x) * (t'real y))%R))) ->
         overflow_value m r) /\
        (~ (is_nan r) -> product_sign r x y))))))).
976 977 978 979 980 981 982 983
Proof.
  intros m x y r.
  unfold no_overflow, in_range, overflow_value.
  rewrite <- max_real_cst.
  apply mul_special.
Qed.

(* Why3 goal *)
984 985 986 987 988 989 990 991 992 993 994 995 996 997
Lemma div_special :
  forall (m:ieee_float.RoundingMode.mode) (x:t) (y:t),
  let r := div m x y in
  (((is_nan x) \/ (is_nan y)) -> is_nan r) /\
  ((((t'isFinite x) /\ (is_infinite y)) -> is_zero r) /\
   ((((is_infinite x) /\ (t'isFinite y)) -> is_infinite r) /\
    ((((is_infinite x) /\ (is_infinite y)) -> is_nan r) /\
     ((((t'isFinite x) /\
        ((t'isFinite y) /\
         (~ (is_zero y) /\ ~ (no_overflow m ((t'real x) / (t'real y))%R)))) ->
       overflow_value m r) /\
      ((((t'isFinite x) /\ ((is_zero y) /\ ~ (is_zero x))) -> is_infinite r) /\
       ((((is_zero x) /\ (is_zero y)) -> is_nan r) /\
        (~ (is_nan r) -> product_sign r x y))))))).
998 999 1000 1001 1002 1003 1004 1005
Proof.
  intros m x y r.
  unfold no_overflow, in_range, overflow_value.
  rewrite <- max_real_cst.
  apply div_special.
Qed.

(* Why3 goal *)
1006 1007
Lemma neg_special :
  forall (x:t),
1008 1009 1010
  ((is_nan x) -> is_nan (neg x)) /\
  (((is_infinite x) -> is_infinite (neg x)) /\
   (~ (is_nan x) -> diff_sign x (neg x))).
1011 1012 1013 1014 1015
Proof.
  apply neg_special.
Qed.

(* Why3 goal *)
1016 1017
Lemma abs_special :
  forall (x:t),
1018 1019 1020
  ((is_nan x) -> is_nan (abs x)) /\
  (((is_infinite x) -> is_infinite (abs x)) /\
   (~ (is_nan x) -> is_positive (abs x))).
1021 1022 1023 1024 1025
Proof.
  apply abs_special.
Qed.

(* Why3 goal *)
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
Lemma fma_special :
  forall (m:ieee_float.RoundingMode.mode) (x:t) (y:t) (z:t),
  let r := fma m x y z in
  (((is_nan x) \/ ((is_nan y) \/ (is_nan z))) -> is_nan r) /\
  ((((is_zero x) /\ (is_infinite y)) -> is_nan r) /\
   ((((is_infinite x) /\ (is_zero y)) -> is_nan r) /\
    ((((t'isFinite x) /\
       (~ (is_zero x) /\ ((is_infinite y) /\ (t'isFinite z)))) ->
      (is_infinite r) /\ (product_sign r x y)) /\
     ((((t'isFinite x) /\
        (~ (is_zero x) /\ ((is_infinite y) /\ (is_infinite z)))) ->
       ((product_sign z x y) -> (is_infinite r) /\ (same_sign r z)) /\
       (~ (product_sign z x y) -> is_nan r)) /\
      ((((is_infinite x) /\
         ((t'isFinite y) /\ (~ (is_zero y) /\ (t'isFinite z)))) ->
        (is_infinite r) /\ (product_sign r x y)) /\
       ((((is_infinite x) /\
          ((t'isFinite y) /\ (~ (is_zero y) /\ (is_infinite z)))) ->
         ((product_sign z x y) -> (is_infinite r) /\ (same_sign r z)) /\
         (~ (product_sign z x y) -> is_nan r)) /\
        ((((is_infinite x) /\ ((is_infinite y) /\ (t'isFinite z))) ->
          (is_infinite r) /\ (product_sign r x y)) /\
         ((((t'isFinite x) /\ ((t'isFinite y) /\ (is_infinite z))) ->
           (is_infinite r) /\ (same_sign r z)) /\
          ((((is_infinite x) /\ ((is_infinite y) /\ (is_infinite z))) ->
            ((product_sign z x y) -> (is_infinite r) /\ (same_sign r z)) /\
            (~ (product_sign z x y) -> is_nan r)) /\
           ((((t'isFinite x) /\
              ((t'isFinite y) /\
               ((t'isFinite z) /\
                ~ (no_overflow m
                   (((t'real x) * (t'real y))%R + (t'real z))%R)))) ->
             (same_sign_real r (((t'real x) * (t'real y))%R + (t'real z))%R) /\
             (overflow_value m r)) /\
            (((t'isFinite x) /\ ((t'isFinite y) /\ (t'isFinite z))) ->
             ((product_sign z x y) -> same_sign r z) /\
             (~ (product_sign z x y) ->
              ((((t'real x) * (t'real y))%R + (t'real z))%R = 0%R) ->
              ((m = ieee_float.RoundingMode.RTN) -> is_negative r) /\
              (~ (m = ieee_float.RoundingMode.RTN) -> is_positive r))))))))))))).
1066 1067 1068 1069 1070 1071 1072 1073
Proof.
  intros m x y z r.
  unfold no_overflow, in_range, overflow_value.
  rewrite <- max_real_cst.
  apply fma_special.
Qed.

(* Why3 goal *)
1074 1075 1076 1077 1078 1079 1080 1081 1082
Lemma sqrt_special :
  forall (m:ieee_float.RoundingMode.mode) (x:t),
  let r := sqrt m x in
  ((is_nan x) -> is_nan r) /\
  (((is_plus_infinity x) -> is_plus_infinity r) /\
   (((is_minus_infinity x) -> is_nan r) /\
    ((((t'isFinite x) /\ ((t'real x) < 0%R)%R) -> is_nan r) /\
     (((is_zero x) -> same_sign r x) /\
      (((t'isFinite x) /\ (0%R < (t'real x))%R) -> is_positive r))))).
1083 1084 1085 1086 1087
Proof.
  apply sqrt_special.
Qed.

(* Why3 goal *)
1088 1089 1090 1091 1092 1093
Lemma of_int_add_exact :
  forall (m:ieee_float.RoundingMode.mode) (n:ieee_float.RoundingMode.mode)
    (i:Z) (j:Z),
  (in_safe_int_range i) -> (in_safe_int_range j) ->
  (in_safe_int_range (i + j)%Z) ->
  eq (of_int m (i + j)%Z) (add n (of_int m i) (of_int m j)).
1094 1095 1096 1097 1098 1099
Proof.
  intros m n i j h1 h2 h3.
  now apply of_int_add_exact.
Qed.

(* Why3 goal *)
1100 1101 1102 1103 1104 1105
Lemma of_int_sub_exact :
  forall (m:ieee_float.RoundingMode.mode) (n:ieee_float.RoundingMode.mode)
    (i:Z) (j:Z),
  (in_safe_int_range i) -> (in_safe_int_range j) ->
  (in_safe_int_range (i - j)%Z) ->
  eq (of_int m (i - j)%Z) (sub n (of_int m i) (of_int m j)).
1106 1107 1108 1109 1110 1111
Proof.
  intros m n i j h1 h2 h3.
  now apply of_int_sub_exact.
Qed.

(* Why3 goal *)
1112 1113 1114 1115 1116 1117
Lemma of_int_mul_exact :
  forall (m:ieee_float.RoundingMode.mode) (n:ieee_float.RoundingMode.mode)
    (i:Z) (j:Z),
  (in_safe_int_range i) -> (in_safe_int_range j) ->
  (in_safe_int_range (i * j)%Z) ->
  eq (of_int m (i * j)%Z) (mul n (of_int m i) (of_int m j)).
1118 1119 1120 1121 1122 1123
Proof.
  intros m n i j h1 h2 h3.
  now apply of_int_mul_exact.
Qed.

(* Why3 goal *)
1124
Lemma Min_r : forall (x:t) (y:t), (le y x) -> eq (min x y) y.
1125 1126 1127 1128 1129
Proof.
  apply Min_r.
Qed.

(* Why3 goal *)
1130
Lemma Min_l : forall (x:t) (y:t), (le x y) -> eq (min x y) x.
1131 1132 1133 1134 1135
Proof.
  apply Min_l.
Qed.

(* Why3 goal *)
1136
Lemma Max_r : forall (x:t) (y:t), (le y x) -> eq (max x y) x.