Common — library colibrilib.fp — module Intf

The interface

243
module Intf
244
use int.Int
245
use int.Power
246
use real.RealInfix
247
use real.FromInt
248
use colibrilib.pow2r.Pow2r
249
use colibrilib.dyadic.Dyadic as D
250
use int.EuclideanDivision as ED
251
use Ereal
252
use export Widen
253
use export Dir
254
use option.Option

The format: eb bits of exponent, sb bits of significand (the hidden bit included), as in ieee_float. It is data, as in Farith: each float carries its format.

259
type
format
260
function
eb
format : int
261
function
sb
format : int
262
axiom
eb_gt_1
: forall fm. 1 < eb fm
263
axiom
sb_gt_1
: forall fm. 1 < sb fm
264
265
type
t
266
function
fmt
t : format

The format of a float, as data

269
val
format_of
(x: t) : format
270
ensures { result = fmt x }

Equality of formats, as data

273
val
eq_format
(a b: format) : bool
274
ensures { result <-> a = b }

The format of e bits of exponent and s of significand, when it has two bits of each

278
val
mk_format_o
(e s: int) : option format
279
ensures { match result with
280
| Some fm -> eb fm = e /\ sb fm = s
281
| None -> not (1 < e /\ 1 < s) end }

One more bit of significand

The format fm' with the exponent of fm and one more bit of significand contains the floats of fm and the midpoints between two neighbours (ext_widen): the float of index i of fm is at windex i in fm', and windex i + 1 is the midpoint between i and i + 1.

290
predicate
widens
(fm fm': format) = eb fm' = eb fm /\ sb fm' = sb fm + 1

emax of ieee_float: the formats have sb < emax

293
function
emax
(fm: format) : int = power 2 (eb fm - 1)

The format with one more bit, when it is a format

296
val
widen
(fm: format) : option format
297
ensures { match result with
298
| None -> emax fm <= sb fm + 1
299
| Some fm' -> widens fm fm' end }
300
301
302
val predicate
is_nan
t
303
predicate
is_not_nan
(x: t) = not (is_nan x)
304
predicate
is_plus_infinity
t
305
predicate
is_minus_infinity
t
306
307
function
not_a_number
format : t
308
axiom
not_a_number_spec
: forall fm [not_a_number fm].
309
is_nan (not_a_number fm) /\ fmt (not_a_number fm) = fm

One NaN per format, as in SMT–LIB

311
axiom
nan_unique
: forall x [is_nan x]. is_nan x -> x = not_a_number (fmt x)
312
function
plus_infinity
format : t
313
axiom
plus_infinity_spec
: forall fm [plus_infinity fm].
314
is_plus_infinity (plus_infinity fm) /\ fmt (plus_infinity fm) = fm
315
function
minus_infinity
format : t
316
axiom
minus_infinity_spec
: forall fm [minus_infinity fm].
317
is_minus_infinity (minus_infinity fm) /\ fmt (minus_infinity fm) = fm

The order where –0 < +0, and its strict version, on the floats of one format

321
predicate
le_dz
t t
322
predicate
lt_dz
t t
323
324
axiom
le_not_nan
: forall x y [le_dz x y]. le_dz x y ->
325
is_not_nan x /\ is_not_nan y /\ fmt x = fmt y
326
axiom
lt_not_nan
: forall x y [lt_dz x y]. lt_dz x y ->
327
is_not_nan x /\ is_not_nan y /\ fmt x = fmt y
328
329
axiom
lt_plus_infinity
: forall x [lt_dz x (plus_infinity (fmt x))].
330
is_not_nan x -> not (is_plus_infinity x) -> lt_dz x (plus_infinity (fmt x))
331
axiom
minus_infinity_lt
: forall x [lt_dz (minus_infinity (fmt x)) x].
332
is_not_nan x -> not (is_minus_infinity x) -> lt_dz (minus_infinity (fmt x)) x

One +inf and one –inf per format, not NaN and different

335
axiom
plus_infinity_unique
: forall x [is_plus_infinity x].
336
is_plus_infinity x -> x = plus_infinity (fmt x)
337
axiom
minus_infinity_unique
: forall x [is_minus_infinity x].
338
is_minus_infinity x -> x = minus_infinity (fmt x)
339
axiom
infinities_not_nan
: forall fm.
340
is_not_nan (plus_infinity fm) /\ is_not_nan (minus_infinity fm)
341
axiom
infinities_differ
: forall fm.
342
not is_minus_infinity (plus_infinity fm) /\
343
not is_plus_infinity (minus_infinity fm)

Nothing is above +inf, nothing below –inf

346
axiom
lt_not_plus_infinity
: forall x y [lt_dz x y].
347
lt_dz x y -> not (is_plus_infinity x)
348
axiom
lt_not_minus_infinity
: forall x y [lt_dz x y].
349
lt_dz x y -> not (is_minus_infinity y)
350
351
val
le_diff_zero
(x y: t) : bool
352
ensures { result <-> le_dz x y }
353
354
val
lt_diff_zero
(x y: t) : bool
355
ensures { result <-> lt_dz x y }

The index of a float

The floats that are not NaN, ordered by le_dz, are in bijection with [-nb_sign, nb_sign), as in the semantics of SMT–LIB (Brain, Tinelli, Rümmer, Wahl, "An Automatable Formal Semantics for IEEE–754 Floating–Point Arithmetic", 2015, §V.A), with +0 at 0 and –0 at –1. nb_sign is the number of floats of each sign: the zero, the subnormals, the normals and the infinity. On the floats that are not negative, the index is the IEEE binary encoding.

367
function
nb_sign
(fm: format) : int =
368
(power 2 (eb fm) - 1) * power 2 (sb fm - 1) + 1
369
function
index
(x: t) : int
370
function
of_index
(fm: format) (i: int) : t
371
372
axiom
index_le
: forall x y [le_dz x y | index x, index y].
373
is_not_nan x -> is_not_nan y -> fmt x = fmt y ->
374
(le_dz x y <-> index x <= index y)
375
376
axiom
index_lt
: forall x y [lt_dz x y | index x, index y].
377
is_not_nan x -> is_not_nan y -> fmt x = fmt y ->
378
(lt_dz x y <-> index x < index y)

Leibniz equality for all the floats that are not NaN, zeros and infinities included

382
axiom
index_inj
: forall x [index x].
383
is_not_nan x -> of_index (fmt x) (index x) = x
384
385
val ghost
index_range
(x: t) : unit
386
requires { is_not_nan x }
387
ensures { - nb_sign (fmt x) <= index x < nb_sign (fmt x) }
388
389
val ghost
of_index_range
(fm: format) (i: int) : unit
390
requires { - nb_sign fm <= i < nb_sign fm }
391
ensures { is_not_nan (of_index fm i) /\ index (of_index fm i) = i }
392
ensures { fmt (of_index fm i) = fm }

The index and the float of an index, as data: nb_sign, the number of significands in a binade, and of_index

396
val
nb_sign_v
(fm: format) : int
397
ensures { result = nb_sign fm }
398
val
binade_v
(fm: format) : int
399
ensures { result = power 2 (sb fm - 1) }
400
val
of_index_v
(fm: format) (i: int) : t
401
ensures { result = of_index fm i }

The index of a float and the widths of a format, as data

404
val
index_v
(x: t) : int
405
requires { is_not_nan x }
406
ensures { result = index x }
407
val
eb_v
(fm: format) : int
408
ensures { result = eb fm }
409
val
sb_v
(fm: format) : int
410
ensures { result = sb fm }

The successor and the predecessor, and the float of index the middle of the indices of x and y (rounded down); the properties are the let ghost facts of fp_facts.Facts

415
function
succ_dz
(x: t) : t = of_index (fmt x) (index x + 1)
416
function
pred_dz
(x: t) : t = of_index (fmt x) (index x - 1)
417
418
val
succ_diff_zero
(x: t) : t
419
requires { is_not_nan x /\ not is_plus_infinity x }
420
ensures { result = succ_dz x }
421
422
val
pred_diff_zero
(x: t) : t
423
requires { is_not_nan x /\ not is_minus_infinity x }
424
ensures { result = pred_dz x }
425
426
val
mid_index
(x y: t) : t
427
requires { is_not_nan x /\ is_not_nan y /\ fmt x = fmt y }
428
ensures { result = of_index (fmt x) (ED.div (index x + index y) 2) }

The zeros and the signs

431
predicate
is_zero
t
432
predicate
is_positive
t
433
434
axiom
index_zero
: forall x [is_zero x].
435
is_zero x <-> is_not_nan x /\ (index x = 0 \/ index x = -1)
436
axiom
index_positive
: forall x [is_positive x].
437
is_not_nan x -> (is_positive x <-> 0 <= index x)

The classes by the magnitude: up to the sign bit, the IEEE binary encoding. Subnormal: exponent 0 and significand not 0; normal: exponent neither 0 nor all ones; infinite: the last one.

442
function
magnitude
(x: t) : int = if 0 <= index x then index x else -1 - index x
443
444
predicate
is_subnormal
t
445
predicate
is_normal
t
446
447
axiom
index_subnormal
: forall x [is_subnormal x].
448
is_subnormal x <-> is_not_nan x /\ 0 < magnitude x < power 2 (sb (fmt x) - 1)
449
axiom
index_normal
: forall x [is_normal x].
450
is_normal x <->
451
is_not_nan x /\ power 2 (sb (fmt x) - 1) <= magnitude x < nb_sign (fmt x) - 1
452
axiom
index_plus_infinity
: forall x [is_plus_infinity x].
453
is_plus_infinity x <-> is_not_nan x /\ index x = nb_sign (fmt x) - 1
454
axiom
index_minus_infinity
: forall x [is_minus_infinity x].
455
is_minus_infinity x <-> is_not_nan x /\ index x = - nb_sign (fmt x)

Negation and absolute value: on the index, the reflection i -> -1 - i and the magnitude

459
val function
neg
t : t
460
val function
abs
t : t
461
462
axiom
neg_fmt
: forall x [neg x]. fmt (neg x) = fmt x
463
axiom
abs_fmt
: forall x [abs x]. fmt (abs x) = fmt x
464
axiom
neg_nan
: forall x [neg x]. is_nan (neg x) <-> is_nan x
465
axiom
abs_nan
: forall x [abs x]. is_nan (abs x) <-> is_nan x
466
axiom
index_neg
: forall x [neg x].
467
is_not_nan x -> index (neg x) = -1 - index x
468
axiom
index_abs
: forall x [abs x].
469
is_not_nan x -> index (abs x) = magnitude x

The comparisons of IEEE 754 on the floats of one format: –0 and +0 are equal, NaN is unordered

473
predicate
fp_le
(x y: t) = le_dz x y \/ (fmt x = fmt y /\ is_zero x /\ is_zero y)
474
predicate
fp_lt
(x y: t) = lt_dz x y /\ not (is_zero x /\ is_zero y)
475
predicate
fp_eq
(x y: t) =
476
(is_not_nan x /\ x = y) \/ (fmt x = fmt y /\ is_zero x /\ is_zero y)

The minimum and the maximum of SMT–LIB: one of the arguments (the other one if one is NaN; either one for –0 and +0)

480
function
min
t t : t
481
function
max
t t : t
482
axiom
min_choice
: forall x y [min x y]. min x y = x \/ min x y = y
483
axiom
max_choice
: forall x y [max x y]. max x y = x \/ max x y = y

Rounding

As BTRW15 §V: the operations round the exact result, an extended real (ext: the value of a float that is not NaN), to a float; round_f m zneg e is –0 if zneg and +0 otherwise when e rounds to 0. The rounding modes are abstract, but for RTN (the sign of zero sums).

491
function
to_real
t : real
492
function
ext
t : ereal
493
axiom
ext_def
: forall x [ext x]. ext x =
494
if is_plus_infinity x then PInf
495
else if is_minus_infinity x then MInf
496
else Fin (to_real x)
497
axiom
ext_le
: forall x y [le_dz x y]. le_dz x y -> ele (ext x) (ext y)
498
axiom
ext_zero
: forall x [ext x]. is_not_nan x -> (is_zero x <-> ext x = Fin 0.0)
499
axiom
ext_neg
: forall x [ext (neg x)]. is_not_nan x -> ext (neg x) = eneg (ext x)

Strictly increasing, but on the zeros

501
axiom
ext_lt
: forall x y [lt_dz x y]. lt_dz x y -> not (is_zero x /\ is_zero y) ->
502
not ele (ext y) (ext x)

The modes of ieee_float (Widen, extracted with fbin.mlw)

505
predicate
is_rtn
(m: mode) = m = RTN
506
function
round_f
format mode bool ereal : t
507
508
axiom
round_f_not_nan
: forall fm m z e [round_f fm m z e].
509
is_not_nan (round_f fm m z e) /\ fmt (round_f fm m z e) = fm
510
axiom
round_f_infinities
: forall fm m z.
511
round_f fm m z PInf = plus_infinity fm /\
512
round_f fm m z MInf = minus_infinity fm
513
axiom
round_f_fin_zero
: forall fm m z. is_zero (round_f fm m z (Fin 0.0))
514
axiom
round_f_zero
: forall fm m z e [round_f fm m z e].
515
is_zero (round_f fm m z e) -> (is_positive (round_f fm m z e) <-> not z)

Increasing; when both results are zeros, their signs must be in order

517
axiom
round_f_le
: forall fm m z1 z2 e1 e2
518
[round_f fm m z1 e1, round_f fm m z2 e2].
519
ele e1 e2 ->
520
(is_zero (round_f fm m z1 e1) /\ is_zero (round_f fm m z2 e2) -> z2 -> z1) ->
521
le_dz (round_f fm m z1 e1) (round_f fm m z2 e2)

Exact on the floats of the format, the zeros with their sign

523
axiom
round_f_exact
: forall m x
524
[round_f (fmt x) m (if is_positive x then false else true) (ext x)].
525
is_not_nan x ->
526
round_f (fmt x) m (if is_positive x then false else true) (ext x) = x

The rounding of an integer, as data (z: the sign of a zero)

529
val
of_int
(fm: format) (m: mode) (z: bool) (i: int) : t
530
ensures { result = round_f fm m z (Fin (from_int i)) }

The integers of magnitude at most 2^sb and at most the largest finite float are floats (as in ieee_float)

534
val ghost
int_exact
(fm: format) (i: int) : t
535
requires { - power 2 (sb fm) <= i <= power 2 (sb fm) }
536
requires { -. to_real (of_index fm (nb_sign fm - 2)) <=. from_int i <=.
537
to_real (of_index fm (nb_sign fm - 2)) }
538
ensures { fmt result = fm /\ is_not_nan result /\ ext result = Fin (from_int i) }

The finite floats are floats of the formats with at least as many bits of exponent and of significand

542
val ghost
widen_exact
(fm': format) (x: t) : t
543
requires { eb (fmt x) <= eb fm' /\ sb (fmt x) <= sb fm' }
544
requires { is_not_nan x /\ not is_plus_infinity x /\ not is_minus_infinity x }
545
ensures { fmt result = fm' /\ is_not_nan result /\ ext result = ext x }

The powers of 2 dividing the floats (colibrilib/dyadic.mlw)

ulp_min fm: the exponent of the least ulp (of the subnormals); ulp_max fm: the exponent of the ulp of the largest finite float.

551
function
ulp_min
(fm: format) : int = 3 - emax fm - sb fm
552
function
ulp_max
(fm: format) : int = ulp_min fm + power 2 (eb fm) - 3

A finite float is a multiple of 2^ulp_min with sb bits (FIX and FLT of Gappa)

556
val ghost
float_dyadic
(x: t) : unit
557
requires { is_not_nan x /\ not is_plus_infinity x /\ not is_minus_infinity x }
558
ensures { D.fix (to_real x) (ulp_min (fmt x)) /\ D.flt (to_real x) (sb (fmt x)) }

The rounding keeps the multiples of 2^e, e <= ulp_max

561
val ghost
round_fix
(fm: format) (m: mode) (z: bool) (r: real) (e: int) : unit
562
requires { D.fix r e /\ e <= ulp_max fm }
563
requires { not is_plus_infinity (round_f fm m z (Fin r)) }
564
requires { not is_minus_infinity (round_f fm m z (Fin r)) }
565
ensures { D.fix (to_real (round_f fm m z (Fin r))) e }

A nonzero real that rounds to zero is below 2^ulp_min

568
val ghost
round_zero_small
(fm: format) (m: mode) (z: bool) (r: real) : unit
569
requires { r <> 0.0 /\ is_zero (round_f fm m z (Fin r)) }
570
ensures { -. pow2r (ulp_min fm) <. r <. pow2r (ulp_min fm) }

A multiple of 2^ulp_min with sb bits, at most the largest finite float, is a float (Flocq's generic_format_FLT, src/Core/FLT.v)

574
val ghost
dyadic_exact
(fm: format) (r: real) : t
575
requires { D.fix r (ulp_min fm) /\ D.flt r (sb fm) }
576
requires { -. to_real (of_index fm (nb_sign fm - 2)) <=. r <=.
577
to_real (of_index fm (nb_sign fm - 2)) }
578
ensures { fmt result = fm /\ is_not_nan result /\ ext result = Fin r }

The largest finite float is at least 2^(ulp_max + sb - 1) (its significand has sb bits)

582
val ghost
max_float_ge
(fm: format) : unit
583
ensures { pow2r (ulp_max fm + sb fm - 1) <=. to_real (of_index fm (nb_sign fm - 2)) }

In round to nearest, a real at least 2 x that rounds below the infinity bounds 2 x by the largest finite float (the gap between it and the overflow threshold), and the same on the negative side (average_2 of QF_FP/schanda/spark)

589
val ghost
near_double
(m: mode) (z: bool) (x: t) (r: real) : unit
590
requires { m = RNE \/ m = RNA }
591
requires { is_not_nan x /\ not is_plus_infinity x /\ not is_minus_infinity x }
592
ensures { 2.0 *. to_real x <=. r ->
593
not is_plus_infinity (round_f (fmt x) m z (Fin r)) ->
594
2.0 *. to_real x <=. to_real (of_index (fmt x) (nb_sign (fmt x) - 2)) }
595
ensures { r <=. 2.0 *. to_real x ->
596
not is_minus_infinity (round_f (fmt x) m z (Fin r)) ->
597
-. to_real (of_index (fmt x) (nb_sign (fmt x) - 2)) <=. 2.0 *. to_real x }

The significand and the exponent of a finite float: n 2^e (the others give any pair)

601
val
to_dyadic
(x: t) : (int, int)
602
ensures { let (n, e) = result in
603
is_not_nan x -> not is_plus_infinity x -> not is_minus_infinity x ->
604
to_real x = from_int n *. pow2r e }

The linear bounds a r + b <= f <= c r + d of a rounding f to nearest of a real r, when f is in [lo, hi] (fbin.Nearest.nearest_bounds): `b = ((am, ae), ad, (bm, be), (cm, ce), cd, (dm, de)) gives a = am 2^ae / ad, b = bm 2^be`, c = cm 2^ce / cd, d = dm 2^de

611
val
nearest_bounds
(lo hi: t) : option ((int, int), int, (int, int), (int, int), int, (int, int))
612
ensures { match result with
613
| Some b ->
614
let ((am, ae), ad, (bm, be), (cm, ce), cd, (dm, de)) = b in
615
0 < ad /\ 0 < cd /\
616
forall m z r. (m = RNE \/ m = RNA) ->
617
le_dz lo (round_f (fmt lo) m z (Fin r)) ->
618
le_dz (round_f (fmt lo) m z (Fin r)) hi ->
619
from_int am *. pow2r ae *. r +. from_int ad *. (from_int bm *. pow2r be)
620
<=. from_int ad *. to_real (round_f (fmt lo) m z (Fin r)) /\
621
from_int cd *. to_real (round_f (fmt lo) m z (Fin r))
622
<=. from_int cm *. pow2r ce *. r +. from_int cd *. (from_int dm *. pow2r de)
623
| None -> true end }
624
625
axiom
round_f_down
: forall fm z e [round_f fm rtn z e].
626
ele (ext (round_f fm rtn z e)) e
627
axiom
round_f_up
: forall fm z e [round_f fm rtp z e].
628
ele e (ext (round_f fm rtp z e))

The reals that round above or below a float

In the format fm' with one more bit, the floats of fm are at windex (ext_widen). The reals that round to a or above are at or above a (RTN, RTZ above 0), its predecessor (RTP, RTZ below 0), or the midpoint between them (nearest); a is not NaN, not –inf and not +0 (–0 instead). Whether the bound itself rounds to a or above is found by rounding it.

638
axiom
ext_widen
: forall fm fm' i. widens fm fm' -> - nb_sign fm <= i < nb_sign fm ->
639
ext (of_index fm' (windex i)) = ext (of_index fm i)
640
641
642
axiom
round_f_ge_thr
: forall fm fm' m z e a [widens fm fm', le_dz a (round_f fm m z e)].
643
widens fm fm' -> fmt a = fm -> is_not_nan a -> not is_minus_infinity a ->
644
index a <> 0 -> le_dz a (round_f fm m z e) ->
645
ele (ext (of_index fm' (windex (index a) + lo_off m (index a)))) e

The reals that round to b or below, b not NaN, not +inf, not –0

648
axiom
round_f_le_thr
: forall fm fm' m z e b [widens fm fm', le_dz (round_f fm m z e) b].
649
widens fm fm' -> fmt b = fm -> is_not_nan b -> not is_plus_infinity b ->
650
index b <> -1 -> le_dz (round_f fm m z e) b ->
651
ele e (ext (of_index fm' (windex (index b) + hi_off m (index b))))

Addition

The sign of a zero sum (IEEE 754 §6.3): the sign of the arguments when they have the same sign, else –0 in RTN only

657
function
add_zneg
(m: mode) (x y: t) : bool =
658
if is_positive x <-> is_positive y then (if is_positive x then false else true)
659
else (if is_rtn m then true else false)

The addition of BTRW15 §V: NaN, or the rounding of the sum of the extended reals, in the format of x

663
function
fp_add
(m: mode) (x y: t) : t =
664
if is_nan x \/ is_nan y \/ opposite (ext x) (ext y) then not_a_number (fmt x)
665
else round_f (fmt x) m (add_zneg m x y) (eadd (ext x) (ext y))

Implemented by F.add of Farith, for two floats of the same format

668
val
add
(m: mode) (x y: t) : t
669
requires { fmt x = fmt y }
670
ensures { result = fp_add m x y }

Multiplication

674
predicate
is_infinity
(x: t) = is_plus_infinity x \/ is_minus_infinity x

The least (resp. greatest) finite float above (resp. below) a real r, strictly when strict, from the rounding f of r up (resp. down) and exact, whether f is finite of value r

679
val
float_lo
(f: t) (strict exact: bool) : t
680
ensures { fmt result = fmt f }
681
ensures { forall r. f = round_f (fmt f) RTP (r <. 0.0) (Fin r) ->
682
(exact <-> is_not_nan f /\ not is_infinity f /\ to_real f = r) ->
683
forall v. fmt v = fmt f -> is_not_nan v -> not is_infinity v ->
684
(if strict then r <. to_real v else r <=. to_real v) -> le_dz result v }
685
val
float_hi
(f: t) (strict exact: bool) : t
686
ensures { fmt result = fmt f }
687
ensures { forall r. f = round_f (fmt f) RTN (r <. 0.0) (Fin r) ->
688
(exact <-> is_not_nan f /\ not is_infinity f /\ to_real f = r) ->
689
forall v. fmt v = fmt f -> is_not_nan v -> not is_infinity v ->
690
(if strict then to_real v <. r else to_real v <=. r) -> le_dz v result }

The bounds of x when x + y rounds into [lo, hi], in any mode (None: no bound)

fp_add, as a symbol that the implementation instantiates: the refinement of add_bounds then matches its specification syntactically (the axiom is a goal of the refinement)

697
function
fp_add_i
(m: mode) (x y: t) : t
698
axiom
fp_add_i_def
: forall m x y [fp_add_i m x y]. fp_add_i m x y = fp_add m x y
699
700
val
add_bounds
(lo hi: t) : option (t, t)
701
ensures { match result with
702
| Some (a, b) -> fmt a = fmt lo /\ fmt b = fmt lo /\
703
forall m x y. fmt x = fmt lo -> fmt y = fmt lo ->
704
le_dz lo (fp_add_i m x y) -> le_dz (fp_add_i m x y) hi ->
705
le_dz a x /\ le_dz x b
706
| None -> true
707
end }

The sign of a product (IEEE 754 §6.3), zeros included: negative when the signs differ

711
function
mul_zneg
(x y: t) : bool =
712
if is_positive x <-> is_positive y then false else true

The multiplication of BTRW15 §V

715
function
fp_mul
(m: mode) (x y: t) : t =
716
if is_nan x \/ is_nan y \/ (is_zero x /\ is_infinity y)
717
\/ (is_infinity x /\ is_zero y) then not_a_number (fmt x)
718
else round_f (fmt x) m (mul_zneg x y) (emul (ext x) (ext y))

Implemented by F.mul of Farith, for two floats of the same format

721
val
mul
(m: mode) (x y: t) : t
722
requires { fmt x = fmt y }
723
ensures { result = fp_mul m x y }

Division

The division of BTRW15 §V; the sign of the quotient is the sign of the product, also for a zero divisor

729
function
fp_div
(m: mode) (x y: t) : t =
730
if is_nan x \/ is_nan y \/ (is_zero x /\ is_zero y)
731
\/ (is_infinity x /\ is_infinity y) then not_a_number (fmt x)
732
else if is_zero y then
733
(if mul_zneg x y then minus_infinity (fmt x) else plus_infinity (fmt x))
734
else round_f (fmt x) m (mul_zneg x y) (ediv (ext x) (ext y))

Implemented by F.div of Farith, for two floats of the same format

737
val
div
(m: mode) (x y: t) : t
738
requires { fmt x = fmt y }
739
ensures { result = fp_div m x y }

Square root

The square root of BTRW15 §V: NaN below –0, the zeros keep their sign

745
function
fp_sqrt
(m: mode) (x: t) : t =
746
if is_nan x \/ (not is_positive x /\ not is_zero x) then not_a_number (fmt x)
747
else round_f (fmt x) m (if is_positive x then false else true) (esqrt (ext x))

Implemented by F.sqrt of Farith

750
val
sqrt
(m: mode) (x: t) : t
751
ensures { result = fp_sqrt m x }

Conversion

The conversion to the format fm (to_fp of SMT–LIB from a float): NaN to NaN, a zero result has the sign of the argument

757
function
fp_convert
(fm: format) (m: mode) (x: t) : t =
758
if is_nan x then not_a_number fm
759
else round_f fm m (if is_positive x then false else true) (ext x)

Implemented by F.round of Farith

762
val
convert
(fm: format) (m: mode) (x: t) : t
763
ensures { result = fp_convert fm m x }

Rounding to an integral float

The rounding of a real to an integer in the mode

768
function
round_int
mode real : int
769
axiom
round_int_monotonic
: forall m x y. x <=. y -> round_int m x <= round_int m y
770
771
function
eround_int
(m: mode) (e: ereal) : ereal =
772
match e with Fin r -> Fin (from_int (round_int m r)) | _ -> e end

roundToIntegral: NaN to NaN, a zero result has the sign of the argument

776
function
fp_rti
(m: mode) (x: t) : t =
777
if is_nan x then not_a_number (fmt x)
778
else round_f (fmt x) m (if is_positive x then false else true) (eround_int m (ext x))

Implemented by F.round_to_integral of colibri2

781
val
round_to_integral
(m: mode) (x: t) : t
782
ensures { result = fp_rti m x }

Backward rounding to an integer

The reals r with round_int m r >= n are at or above int_thr_lo m n, the ones with round_int m r <= n at or below int_thr_hi m n (the ties and the strict bounds of the directed modes are included)

789
function
int_thr_lo
(m: mode) (n: int) : real =
790
match m with
791
| RTN -> from_int n
792
| RTP -> from_int n -. 1.0
793
| RTZ -> if 0 < n then from_int n else from_int n -. 1.0
794
| RNE | RNA -> from_int n -. 0.5
795
end
796
797
function
int_thr_hi
(m: mode) (n: int) : real =
798
match m with
799
| RTN -> from_int n +. 1.0
800
| RTP -> from_int n
801
| RTZ -> if n < 0 then from_int n else from_int n +. 1.0
802
| RNE | RNA -> from_int n +. 0.5
803
end
804
805
val ghost
round_int_ge
(m: mode) (r: real) (n: int) : unit
806
requires { n <= round_int m r }
807
ensures { int_thr_lo m n <=. r }
808
809
val ghost
round_int_le
(m: mode) (r: real) (n: int) : unit
810
requires { round_int m r <= n }
811
ensures { r <=. int_thr_hi m n }

round_int RTP r is the least integer at or above r, round_int RTN r the greatest at or below

815
val ghost
round_int_ceil
(r: real) (k: int) : unit
816
requires { r <=. from_int k }
817
ensures { round_int RTP r <= k }
818
819
val ghost
round_int_floor
(r: real) (k: int) : unit
820
requires { from_int k <=. r }
821
ensures { k <= round_int RTN r }

round_int of a finite float, as data

824
val
round_int_v
(m: mode) (x: t) : int
825
requires { is_not_nan x /\ not is_plus_infinity x /\ not is_minus_infinity x }
826
ensures { result = round_int m (to_real x) }

The rounding of the rational n / d

829
val
of_q
(fm: format) (m: mode) (z: bool) (n d: int) : t
830
requires { 0 < d }
831
ensures { result = round_f fm m z (Fin (from_int n /. from_int d)) }

The float to which every real of [n1/d1, n2/d2] rounds, when the two ends round to the same float (a zero result is –0 when the real is negative)

836
val
round_bracket
(fm: format) (m: mode) (n1 d1 n2 d2: int) : option t
837
ensures { match result with
838
| None -> d1 <= 0 \/ d2 <= 0 \/
839
round_f fm m (n1 < 0) (Fin (from_int n1 /. from_int d1))
840
<> round_f fm m (n2 < 0) (Fin (from_int n2 /. from_int d2))
841
| Some x -> forall r. from_int n1 /. from_int d1 <=. r <=. from_int n2 /. from_int d2 ->
842
x = round_f fm m (r <. 0.0) (Fin r)
843
end }

The float to which every real strictly between n/2^k and (n+1)/2^k rounds, when 2^-k is at most half the least positive float (emax fm + sb fm - 2 = 1 - qmin): no float nor midpoint of two floats is strictly between them

849
val
round_between
(fm: format) (m: mode) (n k: int) : t
850
requires { emax fm + sb fm - 2 <= k }
851
ensures { forall r. from_int n /. from_int (power 2 k) <. r <. from_int (n + 1) /. from_int (power 2 k) ->
852
result = round_f fm m (r <. 0.0) (Fin r) }
853
end