Common — library colibrilib.fbin — module Export

The interface of OCaml

The functions that hand–written OCaml calls (Colibrilib.Fbin_gen), their contracts restated; the types are the ones of Format, Float, Round, Nearest and Special. The functions with a precondition are total here: they raise an exception exactly when it does not hold.

4636
module Export
4637
use int.Int
4638
use int.Power
4639
use int.EuclideanDivision
4640
use real.RealInfix
4641
use real.FromInt
4642
use option.Option
4643
use colibrilib.fp.Widen
4644
use colibrilib.fp.Ereal
4645
use Pow2r
4646
use Format
4647
use Float
4648
use Round
4649
use Impl
4650
use Nearest
4651
use BitsIv
4652
use Special
4653
use Integral
4654
use AddBound
4655
use Exact

Raised by mk_format on a format without two bits of exponent and two of significand

4659
exception
InvalidFormat

The format of eb bits of exponent and sb of significand

  Valid (227ms) (colibri2)
4662
let
mk_format
(eb sb: int) : format
4663
ensures { result.eb = eb /\ result.sb = sb }
4664
raises { InvalidFormat -> not (1 < eb /\ 1 < sb) }
4665
= if 1 < eb && 1 < sb then { eb = eb; sb = sb } else raise InvalidFormat

The NaN, the infinities and the zeros of a format

  Valid (198ms) (colibri2)
4668
let
nan
(fm: format) : t
4669
ensures { result.fmt = fm /\ result.value = NaN }
4670
= Float.mk fm NaN
4671
Valid (211ms) (colibri2)
4672
let
inf
(fm: format) (neg: bool) : t
4673
ensures { result.fmt = fm /\ result.value = if neg then MinusInf else PlusInf }
4674
= Float.mk fm (if neg then MinusInf else PlusInf)
4675
Valid (269ms) (colibri2)
4676
let
zero
(fm: format) (neg: bool) : t
4677
ensures { result.fmt = fm /\ result.value = Finite neg 0 }
4678
= {proof…}
4679
assert { power 2 fm.eb >= 2 && power 2 (fm.sb - 1) >= 1
4680
by power 2 fm.eb = 2 * power 2 (fm.eb - 1) };
4681
assert { 1 < nb_sign fm };
4682
{qed}
4683
Float.mk fm (Finite neg 0)
4684
Valid (187ms) (cvc5)
4685
let
abs
(x: t) : t ensures { result = Impl.abs x } = Impl.abs x Valid (173ms) (colibri2)
4686
let
neg
(x: t) : t ensures { result = Impl.neg x } = Impl.neg x Valid (195ms) (colibri2)
4687
let
is_nan
(x: t) : bool ensures { result <-> Impl.is_nan x } = Impl.is_nan x Valid (165ms) (cvc5)
4688
let
is_zero
(x: t) : bool ensures { result <-> Impl.is_zero x } = Impl.is_zero x

Raised by to_dyadic on a float that is not finite

4691
exception
NotFinite
4692
Valid (206ms) (colibri2)
4693
let
to_dyadic
(x: t) : dyadic
4694
ensures { is_finite x /\ dval result = to_real x }
4695
raises { NotFinite -> not (is_finite x) }
4696
= match x.value with Finite _ _ -> Round.to_dyadic x | _ -> raise NotFinite end

Raised by round_int_q on a denominator that is not positive

4699
exception
NonPositiveDen
4700
Valid (204ms) (cvc5)
4701
let
round_int_q
(m: mode) (n d: int) : int
4702
ensures { 0 < d /\ result = round_int m (from_int n /. from_int d) }
4703
raises { NonPositiveDen -> d <= 0 }
4704
= if d <= 0 then raise NonPositiveDen else Integral.round_int_q m n d
4705
Valid (183ms) (colibri2)
4706
let
add
(m: mode) (x y: t) : t
4707
ensures { x.fmt = y.fmt -> result = fp_add m x y }
4708
= Impl.add m x y
4709
Valid (186ms) (colibri2)
4710
let
convert
(fm: format) (m: mode) (x: t) : t
4711
ensures { result = fp_convert fm m x }
4712
= Impl.convert fm m x
4713
Valid (197ms) (cvc5)
4714
let
div
(m: mode) (x y: t) : t
4715
ensures { x.fmt = y.fmt -> result = fp_div m x y }
4716
= Impl.div m x y
4717
Valid (189ms) (colibri2)
4718
let
dyadic_of
(x: t) : (int, int)
4719
ensures { let (n, e) = result in is_finite x -> to_real x = from_int n *. pow2r e }
4720
= Exact.dyadic_of x
4721
Valid (199ms) (colibri2)
4722
let
fma
(m: mode) (x y z: t) : t
4723
ensures { x.fmt = y.fmt /\ x.fmt = z.fmt -> result = fp_fma m x y z }
4724
= Impl.fma m x y z
4725
Valid (186ms) (colibri2)
4726
let
ieee_eq_p
(x y: t) : bool
4727
ensures { result <-> ieee_eq x y }
4728
= Impl.ieee_eq_p x y
4729
Valid (192ms) (colibri2)
4730
let
ieee_le_p
(x y: t) : bool
4731
ensures { result <-> ieee_le x y }
4732
= Impl.ieee_le_p x y
4733
Valid (178ms) (colibri2)
4734
let
ieee_lt_p
(x y: t) : bool
4735
ensures { result <-> ieee_lt x y }
4736
= Impl.ieee_lt_p x y
4737
Valid (192ms) (cvc5)
4738
let
index_t
(x: t) : int
4739
ensures { not is_nan x -> result = index x }
4740
= Impl.index_t x
4741
Valid (187ms) (colibri2)
4742
let
is_normal_p
(x: t) : bool
4743
ensures { result <-> is_normal x }
4744
= Special.is_normal_p x
4745
Valid (185ms) (colibri2)
4746
let
is_subnormal_p
(x: t) : bool
4747
ensures { result <-> is_subnormal x }
4748
= Special.is_subnormal_p x
4749
Valid (204ms) (colibri2)
4750
let
le_diff_zero
(x y: t) : bool
4751
ensures { result <-> le_dz x y }
4752
= Impl.le_diff_zero x y
4753
Valid (182ms) (colibri2)
4754
let
lt_diff_zero
(x y: t) : bool
4755
ensures { result <-> lt_dz x y }
4756
= Impl.lt_diff_zero x y
4757
Valid (199ms) (colibri2)
4758
let
mid_index
(x y: t) : t
4759
ensures { not is_nan x /\ not is_nan y /\ x.fmt = y.fmt ->
4760
result = of_index x.fmt (div (index x + index y) 2) }
4761
= Impl.mid_index x y
4762
Valid (179ms) (cvc5)
4763
let
mul
(m: mode) (x y: t) : t
4764
ensures { x.fmt = y.fmt -> result = fp_mul m x y }
4765
= Impl.mul m x y
4766
Valid (187ms) (colibri2)
4767
let
nan_bits
(fm: format) : ((int, int), (int, int))
4768
ensures { let ((a1, b1), (a2, b2)) = result in forall b. 0 <= b < 2 * half_bits fm ->
4769
(is_nan (of_bits fm b) <-> a1 <= b <= b1 \/ a2 <= b <= b2) }
4770
= BitsIv.nan_bits fm
4771
Valid (182ms) (colibri2)
4772
let
next_down
(x: t) : t
4773
ensures { not is_nan x /\ not is_minus_infinity x ->
4774
result = of_index x.fmt (next_down_index (index x)) }
4775
ensures { is_nan x \/ is_minus_infinity x -> result = x }
4776
= Impl.next_down x
4777
Valid (184ms) (colibri2)
4778
let
next_up
(x: t) : t
4779
ensures { not is_nan x /\ not is_plus_infinity x ->
4780
result = of_index x.fmt (next_up_index (index x)) }
4781
ensures { is_nan x \/ is_plus_infinity x -> result = x }
4782
= Impl.next_up x
4783
Valid (192ms) (colibri2)
4784
let
of_bits_p
(fm: format) (b: int) : t
4785
ensures { result = of_bits fm b }
4786
= Impl.of_bits_p fm b
4787
Valid (191ms) (colibri2)
4788
let
of_index_p
(fm: format) (i: int) : t
4789
ensures { result = of_index fm i }
4790
= Impl.of_index_p fm i
4791
Valid (32ms) (colibri2)
4792
let
nb_sign_p
(fm: format) : int
4793
ensures { result = nb_sign fm }
4794
= Round.nb_sign_p fm
4795
Valid (45ms) (colibri2)
4796
let
binade_p
(fm: format) : int
4797
ensures { result = binade fm }
4798
= Round.binade_p fm
4799
Valid (183ms) (colibri2)
4800
let
pred_diff_zero
(x: t) : t
4801
ensures { not is_nan x /\ not is_minus_infinity x -> result = pred_dz x }
4802
= Impl.pred_diff_zero x
4803
Valid (171ms) (colibri2)
4804
let
remainder
(x y: t) : t
4805
ensures { result = fp_rem x y }
4806
= Integral.remainder x y
4807
Valid (169ms) (colibri2)
4808
let
round_bracket
(fm: format) (m: mode) (n1 d1 n2 d2: int) : option t
4809
ensures { match result with
4810
| None -> d1 <= 0 \/ d2 <= 0 \/
4811
round_f fm m (n1 < 0) (Fin (from_int n1 /. from_int d1))
4812
<> round_f fm m (n2 < 0) (Fin (from_int n2 /. from_int d2))
4813
| Some x -> forall r. from_int n1 /. from_int d1 <=. r <=. from_int n2 /. from_int d2 ->
4814
x = round_f fm m (r <. 0.0) (Fin r)
4815
end }
4816
= Impl.round_bracket fm m n1 d1 n2 d2
4817
Valid (178ms) (colibri2)
4818
let
round_q
(fm: format) (m: mode) (zneg: bool) (n d: int) : t
4819
ensures { 0 < d -> result = round_f fm m zneg (Fin (from_int n /. from_int d)) }
4820
= Impl.round_q fm m zneg n d
4821
Valid (169ms) (colibri2)
4822
let
round_to_integral
(m: mode) (x: t) : t
4823
ensures { result = fp_rti m x }
4824
= Impl.round_to_integral m x
4825
Valid (180ms) (colibri2)
4826
let
signed_values
(fm: format) (neg: bool) : signed
4827
ensures { at_i fm result.sv_zero (ix neg 0) }
4828
ensures { at_i fm result.sv_first_subnormal (ix neg 1) }
4829
ensures { at_i fm result.sv_last_subnormal (ix neg (binade fm - 1)) }
4830
ensures { at_i fm result.sv_first_normal (ix neg (binade fm)) }
4831
ensures { at_i fm result.sv_last_normal (ix neg (nb_sign fm - 2)) }
4832
ensures { at_i fm result.sv_infinite (ix neg (nb_sign fm - 1)) }
4833
ensures { if neg then is_minus_infinity result.sv_infinite
4834
else is_plus_infinity result.sv_infinite }
4835
= Special.signed_values fm neg
4836
Valid (188ms) (colibri2)
4837
let
sqrt
(m: mode) (x: t) : t
4838
ensures { result = fp_sqrt m x }
4839
= Impl.sqrt m x
4840
Valid (202ms) (colibri2)
4841
let
succ_diff_zero
(x: t) : t
4842
ensures { not is_nan x /\ not is_plus_infinity x -> result = succ_dz x }
4843
= Impl.succ_diff_zero x
4844
Valid (175ms) (colibri2)
4845
let
to_bits_p
(x: t) : int
4846
ensures { result = to_bits x }
4847
= Impl.to_bits_p x
4848
Valid (183ms) (colibri2)
4849
let
float_lo
(f: t) (strict exact: bool) : t
4850
ensures { forall r. f = round_f f.fmt RTP (r <. 0.0) (Fin r) ->
4851
(exact <-> is_finite f /\ to_real f = r) ->
4852
forall v. v.fmt = f.fmt -> is_finite v ->
4853
(if strict then r <. to_real v else r <=. to_real v) -> le_dz result v }
4854
= Impl.float_lo f strict exact
4855
Valid (182ms) (colibri2)
4856
let
float_hi
(f: t) (strict exact: bool) : t
4857
ensures { forall r. f = round_f f.fmt RTN (r <. 0.0) (Fin r) ->
4858
(exact <-> is_finite f /\ to_real f = r) ->
4859
forall v. v.fmt = f.fmt -> is_finite v ->
4860
(if strict then to_real v <. r else to_real v <=. r) -> le_dz v result }
4861
= Impl.float_hi f strict exact
4862
Valid (181ms) (colibri2)
4863
let
nearest_bounds
(lo hi: t) : option bounds
4864
ensures { match result with
4865
| None -> not (is_finite lo /\ is_finite hi /\ lo.fmt = hi.fmt)
4866
| Some b -> bounds_ok b lo hi
4867
end }
4868
= Nearest.nearest_bounds lo hi
4869
Valid (204ms) (cvc5)
4870
let
add_bounds
(lo hi: t) : option (t, t)
4871
ensures { match result with
4872
| None -> not one_sign lo hi
4873
| Some (a, b) -> one_sign lo hi /\ a.fmt = lo.fmt /\ b.fmt = lo.fmt /\
4874
forall m x y. x.fmt = lo.fmt -> y.fmt = lo.fmt ->
4875
le_dz lo (fp_add m x y) -> le_dz (fp_add m x y) hi -> le_dz a x /\ le_dz x b
4876
end }
4877
= AddBound.add_bounds lo hi
4878
4879
end