| 1 |
(************************************************************************) |
| 2 |
(* * The Rocq Prover / The Rocq Development Team *) |
| 3 |
(* v * Copyright INRIA, CNRS and contributors *) |
| 4 |
(* <O___,, * (see version control and CREDITS file for authors & dates) *) |
| 5 |
(* \VV/ **************************************************************) |
| 6 |
(* // * This file is distributed under the terms of the *) |
| 7 |
(* * GNU Lesser General Public License Version 2.1 *) |
| 8 |
(* * (see LICENSE file for the text of the license) *) |
| 9 |
(************************************************************************) |
| 10 |
|
| 11 |
Require Import PrimInt63 FloatClass. |
| 12 |
|
| 13 |
(** * Definition of the interface for primitive floating-point arithmetic |
| 14 |
|
| 15 |
This interface provides processor operators for the Binary64 format of the |
| 16 |
IEEE 754-2008 standard. *) |
| 17 |
|
| 18 |
(** ** Type definition for the co-domain of [compare] *) |
| 19 |
Variant float_comparison : Set := FEq | FLt | FGt | FNotComparable. |
| 20 |
|
| 21 |
Register float_comparison as kernel.ind_f_cmp. |
| 22 |
|
| 23 |
Register float_class as kernel.ind_f_class. |
| 24 |
|
| 25 |
(** ** The main type *) |
| 26 |
(** [float]: primitive type for Binary64 floating-point numbers. *) |
| 27 |
Primitive float := #float64_type. |
| 28 |
Register float as num.float.type. |
| 29 |
|
| 30 |
Record float_wrapper := wrap_float { float_wrap : float }. |
| 31 |
Register float_wrapper as num.float.float_wrapper. |
| 32 |
Register wrap_float as num.float.wrap_float. |
| 33 |
Definition printer (x : float_wrapper) : float := float_wrap x. |
| 34 |
Definition parser (x : float) : float := x. |
| 35 |
|
| 36 |
(** ** Syntax support *) |
| 37 |
Module Import PrimFloatNotationsInternalA. |
| 38 |
Declare Scope float_scope. |
| 39 |
Delimit Scope float_scope with float. |
| 40 |
Bind Scope float_scope with float. |
| 41 |
End PrimFloatNotationsInternalA. |
| 42 |
Number Notation float parser printer : float_scope. |
| 43 |
|
| 44 |
(** ** Floating-point operators *) |
| 45 |
Primitive classify := #float64_classify. |
| 46 |
|
| 47 |
Primitive abs := #float64_abs. |
| 48 |
|
| 49 |
Primitive sqrt := #float64_sqrt. |
| 50 |
|
| 51 |
Primitive opp := #float64_opp. |
| 52 |
|
| 53 |
(** For the record: this is the IEEE754 equality |
| 54 |
(eqb nan nan = false and eqb +0 -0 = true) *) |
| 55 |
Primitive eqb := #float64_eq. |
| 56 |
|
| 57 |
Primitive ltb := #float64_lt. |
| 58 |
|
| 59 |
Primitive leb := #float64_le. |
| 60 |
|
| 61 |
Primitive compare := #float64_compare. |
| 62 |
|
| 63 |
(** Boolean Leibniz equality *) |
| 64 |
Module Leibniz. |
| 65 |
Primitive eqb := #float64_equal. |
| 66 |
End Leibniz. |
| 67 |
|
| 68 |
Register Leibniz.eqb as num.float.leibniz.eqb. |
| 69 |
|
| 70 |
Primitive mul := #float64_mul. |
| 71 |
|
| 72 |
Primitive add := #float64_add. |
| 73 |
|
| 74 |
Primitive sub := #float64_sub. |
| 75 |
|
| 76 |
Primitive div := #float64_div. |
| 77 |
|
| 78 |
Module Import PrimFloatNotationsInternalB. |
| 79 |
Notation "- x" := (opp x) : float_scope. |
| 80 |
Notation "x =? y" := (eqb x y) (at level 70, no associativity) : float_scope. |
| 81 |
Notation "x <? y" := (ltb x y) (at level 70, no associativity) : float_scope. |
| 82 |
Notation "x <=? y" := (leb x y) (at level 70, no associativity) : float_scope. |
| 83 |
Notation "x ?= y" := (compare x y) (at level 70, no associativity) : float_scope. |
| 84 |
Notation "x * y" := (mul x y) : float_scope. |
| 85 |
Notation "x + y" := (add x y) : float_scope. |
| 86 |
Notation "x - y" := (sub x y) : float_scope. |
| 87 |
Notation "x / y" := (div x y) : float_scope. |
| 88 |
End PrimFloatNotationsInternalB. |
| 89 |
|
| 90 |
(** ** Conversions *) |
| 91 |
|
| 92 |
(** [of_uint63]: convert a primitive unsigned integer into a float value. |
| 93 |
The value is rounded if need be. *) |
| 94 |
Primitive of_uint63 := #float64_of_uint63. |
| 95 |
|
| 96 |
(** Specification of [normfr_mantissa]: |
| 97 |
- If the input is a float value with an absolute value inside $[0.5, 1.)$#[0.5, 1.)#; |
| 98 |
- Then return its mantissa as a primitive integer. |
| 99 |
The mantissa will be a 53-bit integer with its most significant bit set to 1; |
| 100 |
- Else return zero. |
| 101 |
|
| 102 |
The sign bit is always ignored. *) |
| 103 |
Primitive normfr_mantissa := #float64_normfr_mantissa. |
| 104 |
|
| 105 |
(** ** Exponent manipulation functions *) |
| 106 |
(** [frshiftexp]: convert a float to fractional part in $[0.5, 1.)$#[0.5, 1.)# |
| 107 |
and integer part. *) |
| 108 |
Primitive frshiftexp := #float64_frshiftexp. |
| 109 |
|
| 110 |
(** [ldshiftexp]: multiply a float by an integral power of 2. *) |
| 111 |
Primitive ldshiftexp := #float64_ldshiftexp. |
| 112 |
|
| 113 |
(** ** Predecesor/Successor functions *) |
| 114 |
|
| 115 |
(** [next_up]: return the next float towards positive infinity. *) |
| 116 |
Primitive next_up := #float64_next_up. |
| 117 |
|
| 118 |
(** [next_down]: return the next float towards negative infinity. *) |
| 119 |
Primitive next_down := #float64_next_down. |
| 120 |
|
| 121 |
(** ** Special values (needed for pretty-printing) *) |
| 122 |
Definition infinity := Eval compute in div (of_uint63 1) (of_uint63 0). |
| 123 |
Definition neg_infinity := Eval compute in opp infinity. |
| 124 |
Definition nan := Eval compute in div (of_uint63 0) (of_uint63 0). |
| 125 |
|
| 126 |
Register infinity as num.float.infinity. |
| 127 |
Register neg_infinity as num.float.neg_infinity. |
| 128 |
Register nan as num.float.nan. |
| 129 |
|
| 130 |
(** ** Other special values *) |
| 131 |
Definition one := Eval compute in (of_uint63 1). |
| 132 |
Definition zero := Eval compute in (of_uint63 0). |
| 133 |
Definition neg_zero := Eval compute in (-zero)%float. |
| 134 |
Definition two := Eval compute in (of_uint63 2). |
| 135 |
|
| 136 |
(** ** Predicates and helper functions *) |
| 137 |
Definition is_nan f := negb (f =? f)%float. |
| 138 |
|
| 139 |
Definition is_zero f := (f =? zero)%float. (* note: 0 =? -0 with floats *) |
| 140 |
|
| 141 |
Definition is_infinity f := (abs f =? infinity)%float. |
| 142 |
|
| 143 |
Definition is_finite (x : float) := negb (is_nan x || is_infinity x). |
| 144 |
|
| 145 |
(** [get_sign]: return [true] for [-] sign, [false] for [+] sign. *) |
| 146 |
Definition get_sign f := |
| 147 |
let f := if is_zero f then (one / f)%float else f in |
| 148 |
(f <? zero)%float. |
| 149 |
|
| 150 |
Module Export PrimFloatNotations. |
| 151 |
Local Open Scope float_scope. |
| 152 |
Export PrimFloatNotationsInternalA. |
| 153 |
Export PrimFloatNotationsInternalB. |
| 154 |
End PrimFloatNotations. |