library colibrilib.fbin — module ExportThe 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.
module Export4636use int.Int4637use int.Power4638use int.EuclideanDivision4639use real.RealInfix4640use real.FromInt4641use option.Option4642use colibrilib.fp.Widen4643use colibrilib.fp.Ereal4644use Pow2r4645use Format4646use Float4647use Round4648use Impl4649use Nearest4650use BitsIv4651use Special4652use Integral4653use AddBound4654use Exact4655
Raised by mk_format on a format without two bits of exponent and two of significand
exception InvalidFormat4659
The format of eb bits of exponent and sb of significand
Valid (227ms) (colibri2)let mk_format (eb sb: int) : format4662ensures { result.eb = eb /\ result.sb = sb }4663raises { InvalidFormat -> not (1 < eb /\ 1 < sb) }4664= if 1 < eb && 1 < sb then { eb = eb; sb = sb } else raise InvalidFormat4665
The NaN, the infinities and the zeros of a format
Valid (198ms) (colibri2)let nan (fm: format) : t4668ensures { result.fmt = fm /\ result.value = NaN }4669= Float.mk fm NaN4670Valid (211ms) (colibri2)4671let inf (fm: format) (neg: bool) : t4672ensures { result.fmt = fm /\ result.value = if neg then MinusInf else PlusInf }4673= Float.mk fm (if neg then MinusInf else PlusInf)4674Valid (269ms) (colibri2)4675let zero (fm: format) (neg: bool) : t4676ensures { result.fmt = fm /\ result.value = Finite neg 0 }4677= {proof…}4678assert { power 2 fm.eb >= 2 && power 2 (fm.sb - 1) >= 14679by power 2 fm.eb = 2 * power 2 (fm.eb - 1) };4680assert { 1 < nb_sign fm };4681{qed}4682Float.mk fm (Finite neg 0)4683Valid (187ms) (cvc5)4684let abs (x: t) : t ensures { result = Impl.abs x } = Impl.abs x Valid (173ms) (colibri2)4685let neg (x: t) : t ensures { result = Impl.neg x } = Impl.neg x Valid (195ms) (colibri2)4686let is_nan (x: t) : bool ensures { result <-> Impl.is_nan x } = Impl.is_nan x Valid (165ms) (cvc5)4687let is_zero (x: t) : bool ensures { result <-> Impl.is_zero x } = Impl.is_zero x4688
Raised by to_dyadic on a float that is not finite
exception NotFinite4691Valid (206ms) (colibri2)4692let to_dyadic (x: t) : dyadic4693ensures { is_finite x /\ dval result = to_real x }4694raises { NotFinite -> not (is_finite x) }4695= match x.value with Finite _ _ -> Round.to_dyadic x | _ -> raise NotFinite end4696
Raised by round_int_q on a denominator that is not positive
exception NonPositiveDen4699Valid (204ms) (cvc5)4700let round_int_q (m: mode) (n d: int) : int4701ensures { 0 < d /\ result = round_int m (from_int n /. from_int d) }4702raises { NonPositiveDen -> d <= 0 }4703= if d <= 0 then raise NonPositiveDen else Integral.round_int_q m n d4704Valid (183ms) (colibri2)4705let add (m: mode) (x y: t) : t4706ensures { x.fmt = y.fmt -> result = fp_add m x y }4707= Impl.add m x y4708Valid (186ms) (colibri2)4709let convert (fm: format) (m: mode) (x: t) : t4710ensures { result = fp_convert fm m x }4711= Impl.convert fm m x4712Valid (197ms) (cvc5)4713let div (m: mode) (x y: t) : t4714ensures { x.fmt = y.fmt -> result = fp_div m x y }4715= Impl.div m x y4716Valid (189ms) (colibri2)4717let dyadic_of (x: t) : (int, int)4718ensures { let (n, e) = result in is_finite x -> to_real x = from_int n *. pow2r e }4719= Exact.dyadic_of x4720Valid (199ms) (colibri2)4721let fma (m: mode) (x y z: t) : t4722ensures { x.fmt = y.fmt /\ x.fmt = z.fmt -> result = fp_fma m x y z }4723= Impl.fma m x y z4724Valid (186ms) (colibri2)4725let ieee_eq_p (x y: t) : bool4726ensures { result <-> ieee_eq x y }4727= Impl.ieee_eq_p x y4728Valid (192ms) (colibri2)4729let ieee_le_p (x y: t) : bool4730ensures { result <-> ieee_le x y }4731= Impl.ieee_le_p x y4732Valid (178ms) (colibri2)4733let ieee_lt_p (x y: t) : bool4734ensures { result <-> ieee_lt x y }4735= Impl.ieee_lt_p x y4736Valid (192ms) (cvc5)4737let index_t (x: t) : int4738ensures { not is_nan x -> result = index x }4739= Impl.index_t x4740Valid (187ms) (colibri2)4741let is_normal_p (x: t) : bool4742ensures { result <-> is_normal x }4743= Special.is_normal_p x4744Valid (185ms) (colibri2)4745let is_subnormal_p (x: t) : bool4746ensures { result <-> is_subnormal x }4747= Special.is_subnormal_p x4748Valid (204ms) (colibri2)4749let le_diff_zero (x y: t) : bool4750ensures { result <-> le_dz x y }4751= Impl.le_diff_zero x y4752Valid (182ms) (colibri2)4753let lt_diff_zero (x y: t) : bool4754ensures { result <-> lt_dz x y }4755= Impl.lt_diff_zero x y4756Valid (199ms) (colibri2)4757let mid_index (x y: t) : t4758ensures { not is_nan x /\ not is_nan y /\ x.fmt = y.fmt ->4759result = of_index x.fmt (div (index x + index y) 2) }4760= Impl.mid_index x y4761Valid (179ms) (cvc5)4762let mul (m: mode) (x y: t) : t4763ensures { x.fmt = y.fmt -> result = fp_mul m x y }4764= Impl.mul m x y4765Valid (187ms) (colibri2)4766let nan_bits (fm: format) : ((int, int), (int, int))4767ensures { let ((a1, b1), (a2, b2)) = result in forall b. 0 <= b < 2 * half_bits fm ->4768(is_nan (of_bits fm b) <-> a1 <= b <= b1 \/ a2 <= b <= b2) }4769= BitsIv.nan_bits fm4770Valid (182ms) (colibri2)4771let next_down (x: t) : t4772ensures { not is_nan x /\ not is_minus_infinity x ->4773result = of_index x.fmt (next_down_index (index x)) }4774ensures { is_nan x \/ is_minus_infinity x -> result = x }4775= Impl.next_down x4776Valid (184ms) (colibri2)4777let next_up (x: t) : t4778ensures { not is_nan x /\ not is_plus_infinity x ->4779result = of_index x.fmt (next_up_index (index x)) }4780ensures { is_nan x \/ is_plus_infinity x -> result = x }4781= Impl.next_up x4782Valid (192ms) (colibri2)4783let of_bits_p (fm: format) (b: int) : t4784ensures { result = of_bits fm b }4785= Impl.of_bits_p fm b4786Valid (191ms) (colibri2)4787let of_index_p (fm: format) (i: int) : t4788ensures { result = of_index fm i }4789= Impl.of_index_p fm i4790Valid (32ms) (colibri2)4791let nb_sign_p (fm: format) : int4792ensures { result = nb_sign fm }4793= Round.nb_sign_p fm4794Valid (45ms) (colibri2)4795let binade_p (fm: format) : int4796ensures { result = binade fm }4797= Round.binade_p fm4798Valid (183ms) (colibri2)4799let pred_diff_zero (x: t) : t4800ensures { not is_nan x /\ not is_minus_infinity x -> result = pred_dz x }4801= Impl.pred_diff_zero x4802Valid (171ms) (colibri2)4803let remainder (x y: t) : t4804ensures { result = fp_rem x y }4805= Integral.remainder x y4806Valid (169ms) (colibri2)4807let round_bracket (fm: format) (m: mode) (n1 d1 n2 d2: int) : option t4808ensures { match result with4809| None -> d1 <= 0 \/ d2 <= 0 \/4810round_f fm m (n1 < 0) (Fin (from_int n1 /. from_int d1))4811<> round_f fm m (n2 < 0) (Fin (from_int n2 /. from_int d2))4812| Some x -> forall r. from_int n1 /. from_int d1 <=. r <=. from_int n2 /. from_int d2 ->4813x = round_f fm m (r <. 0.0) (Fin r)4814end }4815= Impl.round_bracket fm m n1 d1 n2 d24816Valid (178ms) (colibri2)4817let round_q (fm: format) (m: mode) (zneg: bool) (n d: int) : t4818ensures { 0 < d -> result = round_f fm m zneg (Fin (from_int n /. from_int d)) }4819= Impl.round_q fm m zneg n d4820Valid (169ms) (colibri2)4821let round_to_integral (m: mode) (x: t) : t4822ensures { result = fp_rti m x }4823= Impl.round_to_integral m x4824Valid (180ms) (colibri2)4825let signed_values (fm: format) (neg: bool) : signed4826ensures { at_i fm result.sv_zero (ix neg 0) }4827ensures { at_i fm result.sv_first_subnormal (ix neg 1) }4828ensures { at_i fm result.sv_last_subnormal (ix neg (binade fm - 1)) }4829ensures { at_i fm result.sv_first_normal (ix neg (binade fm)) }4830ensures { at_i fm result.sv_last_normal (ix neg (nb_sign fm - 2)) }4831ensures { at_i fm result.sv_infinite (ix neg (nb_sign fm - 1)) }4832ensures { if neg then is_minus_infinity result.sv_infinite4833else is_plus_infinity result.sv_infinite }4834= Special.signed_values fm neg4835Valid (188ms) (colibri2)4836let sqrt (m: mode) (x: t) : t4837ensures { result = fp_sqrt m x }4838= Impl.sqrt m x4839Valid (202ms) (colibri2)4840let succ_diff_zero (x: t) : t4841ensures { not is_nan x /\ not is_plus_infinity x -> result = succ_dz x }4842= Impl.succ_diff_zero x4843Valid (175ms) (colibri2)4844let to_bits_p (x: t) : int4845ensures { result = to_bits x }4846= Impl.to_bits_p x4847Valid (183ms) (colibri2)4848let float_lo (f: t) (strict exact: bool) : t4849ensures { forall r. f = round_f f.fmt RTP (r <. 0.0) (Fin r) ->4850(exact <-> is_finite f /\ to_real f = r) ->4851forall v. v.fmt = f.fmt -> is_finite v ->4852(if strict then r <. to_real v else r <=. to_real v) -> le_dz result v }4853= Impl.float_lo f strict exact4854Valid (182ms) (colibri2)4855let float_hi (f: t) (strict exact: bool) : t4856ensures { forall r. f = round_f f.fmt RTN (r <. 0.0) (Fin r) ->4857(exact <-> is_finite f /\ to_real f = r) ->4858forall v. v.fmt = f.fmt -> is_finite v ->4859(if strict then to_real v <. r else to_real v <=. r) -> le_dz v result }4860= Impl.float_hi f strict exact4861Valid (181ms) (colibri2)4862let nearest_bounds (lo hi: t) : option bounds4863ensures { match result with4864| None -> not (is_finite lo /\ is_finite hi /\ lo.fmt = hi.fmt)4865| Some b -> bounds_ok b lo hi4866end }4867= Nearest.nearest_bounds lo hi4868Valid (204ms) (cvc5)4869let add_bounds (lo hi: t) : option (t, t)4870ensures { match result with4871| None -> not one_sign lo hi4872| Some (a, b) -> one_sign lo hi /\ a.fmt = lo.fmt /\ b.fmt = lo.fmt /\4873forall m x y. x.fmt = lo.fmt -> y.fmt = lo.fmt ->4874le_dz lo (fp_add m x y) -> le_dz (fp_add m x y) hi -> le_dz a x /\ le_dz x b4875end }4876= AddBound.add_bounds lo hi48774878end4879