| 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 |
From Corelib Require Import BinNums PosDef IntDef FloatClass. |
| 12 |
|
| 13 |
(** * Specification of floating-point arithmetic |
| 14 |
|
| 15 |
This specification is mostly borrowed from the [IEEE754.Binary] module |
| 16 |
of the Flocq library (see {{http://flocq.gforge.inria.fr/}}) *) |
| 17 |
|
| 18 |
(** ** Inductive specification of floating-point numbers |
| 19 |
|
| 20 |
Similar to [Flocq.IEEE754.Binary.full_float], but with no NaN payload. *) |
| 21 |
Variant spec_float := |
| 22 |
| S754_zero (s : bool) |
| 23 |
| S754_infinity (s : bool) |
| 24 |
| S754_nan |
| 25 |
| S754_finite (s : bool) (m : positive) (e : Z). |
| 26 |
|
| 27 |
(** ** Parameterized definitions |
| 28 |
|
| 29 |
[prec] is the number of bits of the mantissa including the implicit one; |
| 30 |
[emax] is the exponent of the infinities. |
| 31 |
|
| 32 |
For instance, Binary64 is defined by [prec = 53] and [emax = 1024]. *) |
| 33 |
Section FloatOps. |
| 34 |
Variable prec emax : Z. |
| 35 |
|
| 36 |
Definition emin := Z.sub (Z.sub (Zpos 3) emax) prec. |
| 37 |
Definition fexp e := Z.max (Z.sub e prec) emin. |
| 38 |
|
| 39 |
Section Zdigits2. |
| 40 |
Fixpoint digits2_pos (n : positive) : positive := |
| 41 |
match n with |
| 42 |
| xH => xH |
| 43 |
| xO p => Pos.succ (digits2_pos p) |
| 44 |
| xI p => Pos.succ (digits2_pos p) |
| 45 |
end. |
| 46 |
|
| 47 |
Definition Zdigits2 n := |
| 48 |
match n with |
| 49 |
| Z0 => n |
| 50 |
| Zpos p => Zpos (digits2_pos p) |
| 51 |
| Zneg p => Zpos (digits2_pos p) |
| 52 |
end. |
| 53 |
End Zdigits2. |
| 54 |
|
| 55 |
Section ValidBinary. |
| 56 |
Definition canonical_mantissa m e := |
| 57 |
Z.eqb (fexp (Z.add (Zpos (digits2_pos m)) e)) e. |
| 58 |
|
| 59 |
Definition bounded m e := |
| 60 |
andb (canonical_mantissa m e) (Z.leb e (Z.sub emax prec)). |
| 61 |
|
| 62 |
Definition valid_binary x := |
| 63 |
match x with |
| 64 |
| S754_finite _ m e => bounded m e |
| 65 |
| _ => true |
| 66 |
end. |
| 67 |
End ValidBinary. |
| 68 |
|
| 69 |
Section Iter. |
| 70 |
Context {A : Type}. |
| 71 |
Variable (f : A -> A). |
| 72 |
|
| 73 |
Fixpoint iter_pos (n : positive) (x : A) {struct n} : A := |
| 74 |
match n with |
| 75 |
| xI n' => iter_pos n' (iter_pos n' (f x)) |
| 76 |
| xO n' => iter_pos n' (iter_pos n' x) |
| 77 |
| xH => f x |
| 78 |
end. |
| 79 |
End Iter. |
| 80 |
|
| 81 |
Section Rounding. |
| 82 |
Inductive location := loc_Exact | loc_Inexact : comparison -> location. |
| 83 |
|
| 84 |
Record shr_record := { shr_m : Z ; shr_r : bool ; shr_s : bool }. |
| 85 |
|
| 86 |
Definition shr_1 mrs := |
| 87 |
let '(Build_shr_record m r s) := mrs in |
| 88 |
let s := orb r s in |
| 89 |
match m with |
| 90 |
| Z0 => Build_shr_record Z0 false s |
| 91 |
| Zpos xH => Build_shr_record Z0 true s |
| 92 |
| Zpos (xO p) => Build_shr_record (Zpos p) false s |
| 93 |
| Zpos (xI p) => Build_shr_record (Zpos p) true s |
| 94 |
| Zneg xH => Build_shr_record Z0 true s |
| 95 |
| Zneg (xO p) => Build_shr_record (Zneg p) false s |
| 96 |
| Zneg (xI p) => Build_shr_record (Zneg p) true s |
| 97 |
end. |
| 98 |
|
| 99 |
Definition loc_of_shr_record mrs := |
| 100 |
match mrs with |
| 101 |
| Build_shr_record _ false false => loc_Exact |
| 102 |
| Build_shr_record _ false true => loc_Inexact Lt |
| 103 |
| Build_shr_record _ true false => loc_Inexact Eq |
| 104 |
| Build_shr_record _ true true => loc_Inexact Gt |
| 105 |
end. |
| 106 |
|
| 107 |
Definition shr_record_of_loc m l := |
| 108 |
match l with |
| 109 |
| loc_Exact => Build_shr_record m false false |
| 110 |
| loc_Inexact Lt => Build_shr_record m false true |
| 111 |
| loc_Inexact Eq => Build_shr_record m true false |
| 112 |
| loc_Inexact Gt => Build_shr_record m true true |
| 113 |
end. |
| 114 |
|
| 115 |
Definition shr mrs e n := |
| 116 |
match n with |
| 117 |
| Zpos p => (iter_pos shr_1 p mrs, Z.add e n) |
| 118 |
| _ => (mrs, e) |
| 119 |
end. |
| 120 |
|
| 121 |
Definition shr_fexp m e l := |
| 122 |
shr (shr_record_of_loc m l) e (Z.sub (fexp (Z.add (Zdigits2 m) e)) e). |
| 123 |
|
| 124 |
Definition round_nearest_even mx lx := |
| 125 |
match lx with |
| 126 |
| loc_Exact => mx |
| 127 |
| loc_Inexact Lt => mx |
| 128 |
| loc_Inexact Eq => if Z.even mx then mx else Z.add mx (Zpos 1) |
| 129 |
| loc_Inexact Gt => Z.add mx (Zpos 1) |
| 130 |
end. |
| 131 |
|
| 132 |
Definition binary_round_aux sx mx ex lx := |
| 133 |
let '(mrs', e') := shr_fexp mx ex lx in |
| 134 |
let '(mrs'', e'') := shr_fexp (round_nearest_even (shr_m mrs') (loc_of_shr_record mrs')) e' loc_Exact in |
| 135 |
match shr_m mrs'' with |
| 136 |
| Z0 => S754_zero sx |
| 137 |
| Zpos m => if Z.leb e'' (Z.sub emax prec) then S754_finite sx m e'' else S754_infinity sx |
| 138 |
| _ => S754_nan |
| 139 |
end. |
| 140 |
|
| 141 |
Definition shl_align mx ex ex' := |
| 142 |
match Z.sub ex' ex with |
| 143 |
| Zneg d => (Pos.iter xO mx d, ex') |
| 144 |
| _ => (mx, ex) |
| 145 |
end. |
| 146 |
|
| 147 |
Definition binary_round sx mx ex := |
| 148 |
let '(mz, ez) := shl_align mx ex (fexp (Z.add (Zpos (digits2_pos mx)) ex))in |
| 149 |
binary_round_aux sx (Zpos mz) ez loc_Exact. |
| 150 |
|
| 151 |
Definition binary_normalize m e szero := |
| 152 |
match m with |
| 153 |
| Z0 => S754_zero szero |
| 154 |
| Zpos m => binary_round false m e |
| 155 |
| Zneg m => binary_round true m e |
| 156 |
end. |
| 157 |
End Rounding. |
| 158 |
|
| 159 |
(** ** Define operations *) |
| 160 |
|
| 161 |
Definition SFopp x := |
| 162 |
match x with |
| 163 |
| S754_nan => S754_nan |
| 164 |
| S754_infinity sx => S754_infinity (negb sx) |
| 165 |
| S754_finite sx mx ex => S754_finite (negb sx) mx ex |
| 166 |
| S754_zero sx => S754_zero (negb sx) |
| 167 |
end. |
| 168 |
|
| 169 |
Definition SFabs x := |
| 170 |
match x with |
| 171 |
| S754_nan => S754_nan |
| 172 |
| S754_infinity sx => S754_infinity false |
| 173 |
| S754_finite sx mx ex => S754_finite false mx ex |
| 174 |
| S754_zero sx => S754_zero false |
| 175 |
end. |
| 176 |
|
| 177 |
Definition SFcompare f1 f2 := |
| 178 |
match f1, f2 with |
| 179 |
| S754_nan , _ | _, S754_nan => None |
| 180 |
| S754_infinity s1, S754_infinity s2 => |
| 181 |
Some match s1, s2 with |
| 182 |
| true, true => Eq |
| 183 |
| false, false => Eq |
| 184 |
| true, false => Lt |
| 185 |
| false, true => Gt |
| 186 |
end |
| 187 |
| S754_infinity s, _ => Some (if s then Lt else Gt) |
| 188 |
| _, S754_infinity s => Some (if s then Gt else Lt) |
| 189 |
| S754_finite s _ _, S754_zero _ => Some (if s then Lt else Gt) |
| 190 |
| S754_zero _, S754_finite s _ _ => Some (if s then Gt else Lt) |
| 191 |
| S754_zero _, S754_zero _ => Some Eq |
| 192 |
| S754_finite s1 m1 e1, S754_finite s2 m2 e2 => |
| 193 |
Some match s1, s2 with |
| 194 |
| true, false => Lt |
| 195 |
| false, true => Gt |
| 196 |
| false, false => |
| 197 |
match Z.compare e1 e2 with |
| 198 |
| Lt => Lt |
| 199 |
| Gt => Gt |
| 200 |
| Eq => Pos.compare_cont Eq m1 m2 |
| 201 |
end |
| 202 |
| true, true => |
| 203 |
match Z.compare e1 e2 with |
| 204 |
| Lt => Gt |
| 205 |
| Gt => Lt |
| 206 |
| Eq => CompOpp (Pos.compare_cont Eq m1 m2) |
| 207 |
end |
| 208 |
end |
| 209 |
end. |
| 210 |
|
| 211 |
Definition SFeqb f1 f2 := |
| 212 |
match SFcompare f1 f2 with |
| 213 |
| Some Eq => true |
| 214 |
| _ => false |
| 215 |
end. |
| 216 |
|
| 217 |
Definition SFltb f1 f2 := |
| 218 |
match SFcompare f1 f2 with |
| 219 |
| Some Lt => true |
| 220 |
| _ => false |
| 221 |
end. |
| 222 |
|
| 223 |
Definition SFleb f1 f2 := |
| 224 |
match SFcompare f1 f2 with |
| 225 |
| Some (Lt | Eq) => true |
| 226 |
| _ => false |
| 227 |
end. |
| 228 |
|
| 229 |
Definition SFclassify f := |
| 230 |
match f with |
| 231 |
| S754_nan => NaN |
| 232 |
| S754_infinity false => PInf |
| 233 |
| S754_infinity true => NInf |
| 234 |
| S754_zero false => PZero |
| 235 |
| S754_zero true => NZero |
| 236 |
| S754_finite false m _ => |
| 237 |
if Z.eqb (Zpos (digits2_pos m)) prec then PNormal |
| 238 |
else PSubn |
| 239 |
| S754_finite true m _ => |
| 240 |
if Z.eqb (Zpos (digits2_pos m)) prec then NNormal |
| 241 |
else NSubn |
| 242 |
end. |
| 243 |
|
| 244 |
Definition SFmul x y := |
| 245 |
match x, y with |
| 246 |
| S754_nan, _ | _, S754_nan => S754_nan |
| 247 |
| S754_infinity sx, S754_infinity sy => S754_infinity (xorb sx sy) |
| 248 |
| S754_infinity sx, S754_finite sy _ _ => S754_infinity (xorb sx sy) |
| 249 |
| S754_finite sx _ _, S754_infinity sy => S754_infinity (xorb sx sy) |
| 250 |
| S754_infinity _, S754_zero _ => S754_nan |
| 251 |
| S754_zero _, S754_infinity _ => S754_nan |
| 252 |
| S754_finite sx _ _, S754_zero sy => S754_zero (xorb sx sy) |
| 253 |
| S754_zero sx, S754_finite sy _ _ => S754_zero (xorb sx sy) |
| 254 |
| S754_zero sx, S754_zero sy => S754_zero (xorb sx sy) |
| 255 |
| S754_finite sx mx ex, S754_finite sy my ey => |
| 256 |
binary_round_aux (xorb sx sy) (Zpos (Pos.mul mx my)) (Z.add ex ey) loc_Exact |
| 257 |
end. |
| 258 |
|
| 259 |
Definition cond_Zopp (b : bool) m := if b then Z.opp m else m. |
| 260 |
|
| 261 |
Definition SFadd x y := |
| 262 |
match x, y with |
| 263 |
| S754_nan, _ | _, S754_nan => S754_nan |
| 264 |
| S754_infinity sx, S754_infinity sy => |
| 265 |
match sx, sy with true, true | false, false => x | _, _ => S754_nan end |
| 266 |
| S754_infinity _, _ => x |
| 267 |
| _, S754_infinity _ => y |
| 268 |
| S754_zero sx, S754_zero sy => |
| 269 |
match sx, sy with true, true | false, false => x | _, _ => S754_zero false end |
| 270 |
| S754_zero _, _ => y |
| 271 |
| _, S754_zero _ => x |
| 272 |
| S754_finite sx mx ex, S754_finite sy my ey => |
| 273 |
let ez := Z.min ex ey in |
| 274 |
binary_normalize (Z.add (cond_Zopp sx (Zpos (fst (shl_align mx ex ez)))) (cond_Zopp sy (Zpos (fst (shl_align my ey ez))))) |
| 275 |
ez false |
| 276 |
end. |
| 277 |
|
| 278 |
Definition SFsub x y := |
| 279 |
match x, y with |
| 280 |
| S754_nan, _ | _, S754_nan => S754_nan |
| 281 |
| S754_infinity sx, S754_infinity sy => |
| 282 |
match sx, sy with true, false | false, true => x | _, _ => S754_nan end |
| 283 |
| S754_infinity _, _ => x |
| 284 |
| _, S754_infinity sy => S754_infinity (negb sy) |
| 285 |
| S754_zero sx, S754_zero sy => |
| 286 |
match sx, sy with true, false | false, true => x | _, _ => S754_zero false end |
| 287 |
| S754_zero _, S754_finite sy my ey => S754_finite (negb sy) my ey |
| 288 |
| _, S754_zero _ => x |
| 289 |
| S754_finite sx mx ex, S754_finite sy my ey => |
| 290 |
let ez := Z.min ex ey in |
| 291 |
binary_normalize (Z.sub (cond_Zopp sx (Zpos (fst (shl_align mx ex ez)))) (cond_Zopp sy (Zpos (fst (shl_align my ey ez))))) |
| 292 |
ez false |
| 293 |
end. |
| 294 |
|
| 295 |
Definition new_location_even nb_steps k := |
| 296 |
if Z.eqb k Z0 then loc_Exact |
| 297 |
else loc_Inexact (Z.compare (Z.mul (Zpos 2) k) nb_steps). |
| 298 |
|
| 299 |
Definition new_location_odd nb_steps k := |
| 300 |
if Z.eqb k Z0 then loc_Exact |
| 301 |
else |
| 302 |
loc_Inexact |
| 303 |
match Z.compare (Z.add (Z.mul (Zpos 2) k) (Zpos 1)) nb_steps with |
| 304 |
| Lt => Lt |
| 305 |
| Eq => Lt |
| 306 |
| Gt => Gt |
| 307 |
end. |
| 308 |
|
| 309 |
Definition new_location nb_steps := |
| 310 |
if Z.even nb_steps then new_location_even nb_steps else new_location_odd nb_steps. |
| 311 |
|
| 312 |
Definition SFdiv_core_binary m1 e1 m2 e2 := |
| 313 |
let d1 := Zdigits2 m1 in |
| 314 |
let d2 := Zdigits2 m2 in |
| 315 |
let e' := Z.min (fexp (Z.sub (Z.add d1 e1) (Z.add d2 e2))) (Z.sub e1 e2) in |
| 316 |
let s := Z.sub (Z.sub e1 e2) e' in |
| 317 |
let m' := |
| 318 |
match s with |
| 319 |
| Zpos _ => Z.shiftl m1 s |
| 320 |
| Z0 => m1 |
| 321 |
| Zneg _ => Z0 |
| 322 |
end in |
| 323 |
let '(q, r) := Z.div_eucl m' m2 in |
| 324 |
(q, e', new_location m2 r). |
| 325 |
|
| 326 |
Definition SFdiv x y := |
| 327 |
match x, y with |
| 328 |
| S754_nan, _ | _, S754_nan => S754_nan |
| 329 |
| S754_infinity sx, S754_infinity sy => S754_nan |
| 330 |
| S754_infinity sx, S754_finite sy _ _ => S754_infinity (xorb sx sy) |
| 331 |
| S754_finite sx _ _, S754_infinity sy => S754_zero (xorb sx sy) |
| 332 |
| S754_infinity sx, S754_zero sy => S754_infinity (xorb sx sy) |
| 333 |
| S754_zero sx, S754_infinity sy => S754_zero (xorb sx sy) |
| 334 |
| S754_finite sx _ _, S754_zero sy => S754_infinity (xorb sx sy) |
| 335 |
| S754_zero sx, S754_finite sy _ _ => S754_zero (xorb sx sy) |
| 336 |
| S754_zero sx, S754_zero sy => S754_nan |
| 337 |
| S754_finite sx mx ex, S754_finite sy my ey => |
| 338 |
let '(mz, ez, lz) := SFdiv_core_binary (Zpos mx) ex (Zpos my) ey in |
| 339 |
binary_round_aux (xorb sx sy) mz ez lz |
| 340 |
end. |
| 341 |
|
| 342 |
Definition SFsqrt_core_binary m e := |
| 343 |
let d := Zdigits2 m in |
| 344 |
let e' := Z.min (fexp (Z.div2 (Z.add (Z.add d e) (Zpos 1)))) (Z.div2 e) in |
| 345 |
let s := Z.sub e (Z.mul (Zpos 2) e') in |
| 346 |
let m' := |
| 347 |
match s with |
| 348 |
| Zpos p => Z.shiftl m s |
| 349 |
| Z0 => m |
| 350 |
| Zneg _ => Z0 |
| 351 |
end in |
| 352 |
let (q, r) := Z.sqrtrem m' in |
| 353 |
let l := |
| 354 |
if Z.eqb r Z0 then loc_Exact |
| 355 |
else loc_Inexact (if Z.leb r q then Lt else Gt) in |
| 356 |
(q, e', l). |
| 357 |
|
| 358 |
Definition SFsqrt x := |
| 359 |
match x with |
| 360 |
| S754_nan => S754_nan |
| 361 |
| S754_infinity false => x |
| 362 |
| S754_infinity true => S754_nan |
| 363 |
| S754_finite true _ _ => S754_nan |
| 364 |
| S754_zero _ => x |
| 365 |
| S754_finite false mx ex => |
| 366 |
let '(mz, ez, lz) := SFsqrt_core_binary (Zpos mx) ex in |
| 367 |
binary_round_aux false mz ez lz |
| 368 |
end. |
| 369 |
|
| 370 |
Definition SFnormfr_mantissa f := |
| 371 |
match f with |
| 372 |
| S754_finite _ mx ex => |
| 373 |
if Z.eqb ex (Z.opp prec) then Npos mx else N0 |
| 374 |
| _ => N0 |
| 375 |
end. |
| 376 |
|
| 377 |
Definition SFldexp f e := |
| 378 |
match f with |
| 379 |
| S754_finite sx mx ex => binary_round sx mx (Z.add ex e) |
| 380 |
| _ => f |
| 381 |
end. |
| 382 |
|
| 383 |
Definition SFfrexp f := |
| 384 |
match f with |
| 385 |
| S754_finite sx mx ex => |
| 386 |
if Z.leb prec (Zpos (digits2_pos mx)) then |
| 387 |
(S754_finite sx mx (Z.opp prec), Z.add ex prec) |
| 388 |
else |
| 389 |
let d := Z.sub prec (Zpos (digits2_pos mx)) in |
| 390 |
(S754_finite sx (Pos.iter xO mx (Z.to_pos d)) (Z.opp prec), Z.sub (Z.add ex prec) d) |
| 391 |
| _ => (f, Z.sub (Z.mul (Zneg 2) emax) prec) |
| 392 |
end. |
| 393 |
|
| 394 |
Definition SFone := binary_round false 1 Z0. |
| 395 |
|
| 396 |
Definition SFulp x := SFldexp SFone (fexp (snd (SFfrexp x))). |
| 397 |
|
| 398 |
Definition SFpred_pos x := |
| 399 |
match x with |
| 400 |
| S754_finite _ mx _ => |
| 401 |
let d := |
| 402 |
if Pos.eqb mx~0 (Pos.iter xO xH (Z.to_pos prec)) then |
| 403 |
SFldexp SFone (fexp (Z.sub (snd (SFfrexp x)) (Zpos 1))) |
| 404 |
else |
| 405 |
SFulp x in |
| 406 |
SFsub x d |
| 407 |
| _ => x |
| 408 |
end. |
| 409 |
|
| 410 |
Definition SFmax_float := |
| 411 |
S754_finite false (Pos.sub (Pos.iter xO xH (Z.to_pos prec)) 1) (Z.sub emax prec). |
| 412 |
|
| 413 |
Definition SFsucc x := |
| 414 |
match x with |
| 415 |
| S754_zero _ => SFldexp SFone emin |
| 416 |
| S754_infinity false => x |
| 417 |
| S754_infinity true => SFopp SFmax_float |
| 418 |
| S754_nan => x |
| 419 |
| S754_finite false _ _ => SFadd x (SFulp x) |
| 420 |
| S754_finite true _ _ => SFopp (SFpred_pos (SFopp x)) |
| 421 |
end. |
| 422 |
|
| 423 |
Definition SFpred f := SFopp (SFsucc (SFopp f)). |
| 424 |
End FloatOps. |