123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298(**************************************************************************)(* This file is part of FArith. *)(* *)(* Copyright (C) 2015-2015 *)(* CEA (Commissariat a l'energie atomique et aux energies *)(* alternatives) *)(* *)(* you can redistribute it and/or modify it under the terms of the GNU *)(* Lesser General Public License as published by the Free Software *)(* Foundation, version 2.1. *)(* *)(* It is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)(* GNU Lesser General Public License for more details. *)(* *)(* See the GNU Lesser General Public License version 2.1 *)(* (enclosed file LGPLv2.1). *)(* *)(**************************************************************************)openBasemoduleFormat=Stdlib.FormatmoduleZ=structincludeZlethash_fold_tst=Base.Hash.fold_ints(hasht)endmoduleMode=structtypet=Farith_Big.mode=NE|ZR|DN|UP|NA[@@derivingeq,ord,hash,show]endmoduleClassify=structtypet=FloatClass.float_class=|PNormal|NNormal|PSubn|NSubn|PZero|NZero|PInf|NInf|NaN[@@derivingeq,ord,hash,show]endtype'vgeneric='vGenericFloat.coq_Generic={mw:Z.t;ew:Z.t;value:'v;}[@@derivingeq,ord,hash]moduleF=structopenGenericFloatincludeGenericFloatletmwt=Z.to_intt.mwletewt=Z.to_intt.ewletpp_signfmtb=Format.pp_print_stringfmt(ifbthen"-"else"+")typebinary_float=BinarySingleNaN.binary_float=|B754_zeroofbool|B754_infinityofbool|B754_nan|B754_finiteofbool*Z.t*Z.t[@@derivingeq,ord,hash]typet=binary_floatgeneric[@@derivingeq,ord,hash]letpp_binary_floatfmt(t:binary_float)=matchtwith|B754_zerob->Format.fprintffmt"%a0"pp_signb|B754_infinityb->Format.fprintffmt"%a∞"pp_signb|B754_nan->Format.fprintffmt"NaN"|B754_finite(b,m,e)->letrecoddifyap=ifZ.equal(Z.logandaZ.one)Z.zerothenoddify(Z.shift_right_trunca1)(Z.succp)elseifZ.equalaZ.zerothen(Z.zero,Z.zero)else(a,p)inletm,e=oddifymeinFormat.fprintffmt"%a%ap%a"pp_signbZ.pp_printmZ.pp_printeletppfmtt=pp_binary_floatfmtt.valueletshow=Format.asprintf"%a"pp(** lexer for finite float *)letlex_floats=matchString.indexs'p'with|Somek->letm=String.subs~pos:0~len:kinlete=String.subs~pos:(Int.succk)~len:(String.lengths-k-1)in(Z.of_stringm,Z.of_stringe)|None->(Z.of_strings,Z.zero)letof_q~mw~ewmodeq=of_q(Z.of_intmw)(Z.of_intew)modeqletof_bits~mw~ewz=of_bits(Z.of_intmw)(Z.of_intew)zletto_bitst=to_bitstletnan~mw~ew=nan(Z.of_intmw)(Z.of_intew)letzero~mw~ewb=zero(Z.of_intmw)(Z.of_intew)bletinf~mw~ewb=inf(Z.of_intmw)(Z.of_intew)bletround~mw~ewmode(f:t):t=matchf.valuewith|B754_zero_|B754_infinity_|B754_nan->{mw=Z.of_intmw;ew=Z.of_intew;value=f.value}|B754_finite(_,_,_)->of_q~mw~ewmode(to_qf)letof_floatf=of_bits~mw:52~ew:11@@Z.extract(Z.of_int64(Int64.bits_of_floatf))064letto_floatmodef=round~mw:52~ew:11modef|>to_bits|>funz->Z.signed_extractz064|>Z.to_int64|>Int64.float_of_bitsletis_zerot=matcht.valuewithB754_zero_->true|_->falseletis_infinitet=matcht.valuewithB754_infinity_->true|_->falseletis_nant=matcht.valuewithB754_nan->true|_->falseletis_negativet=matcht.valuewith|B754_infinityb|B754_zerob|B754_finite(b,_,_)->b|B754_nan->falseletis_positivet=matcht.valuewith|B754_infinityb|B754_zerob|B754_finite(b,_,_)->notb|B754_nan->falseletis_normalt=matchclassifytwith|FloatClass.PNormal|FloatClass.NNormal->true|_->falseletis_subnormalt=matchclassifytwith|FloatClass.PSubn|FloatClass.NSubn->true|_->falseendmoduleI=structopenGenericFloatincludeGenericIntervaltypeinterval=Interval.coq_Interval'=|Inan|IntvofF.binary_float*F.binary_float*bool[@@derivingeq,ord,hash]typet=intervalgeneric[@@derivingeq,ord,hash]letmwt=Z.to_intt.mwletewt=Z.to_intt.ewletppfmt(t:t)=matcht.valuewith|Inan->Format.fprintffmt"{ NaN }"|Intv(a,b,nan)->ifnanthenFormat.fprintffmt"[%a, %a] + NaN"F.pp_binary_floataF.pp_binary_floatbelseFormat.fprintffmt"[%a, %a]"F.pp_binary_floataF.pp_binary_floatblettop~mw~ew=top(Z.of_intmw)(Z.of_intew)end(*
module D = struct
type 't conf = 't Farith_F_aux.fconf
include Farith_F_aux.D
include Common
let mw conf = Z.to_int (Farith_F_aux.mw conf)
let ew conf = Z.to_int (Farith_F_aux.ew conf)
let roundq ~mw ~ew mode q = roundq (Z.of_int mw) (Z.of_int ew) mode q
let pp conf fmt x = pp fmt (cast_to_t conf x)
(* let of_string conf mode s =
* let m,e = lex_float s in
* finite conf mode m e *)
end
module type S = sig
type t
val conf : t D.conf
val compare: t -> t -> int
val equal: t -> t -> bool
val hash : t -> int
val opp : t -> t
val add : mode -> t -> t -> t
val sub : mode -> t -> t -> t
val mul : mode -> t -> t -> t
val div : mode -> t -> t -> t
val sqrt : mode -> t -> t
val abs : t -> t
val of_bits : Z.t -> t
val to_bits : t -> Z.t
val of_z : mode -> Z.t -> t (** Round. *)
val of_q : mode -> Q.t -> t (** Round. *)
val to_q : t -> Q.t (** Exact. *)
val conv : 't D.conf -> mode -> t -> 't
val infinity: bool -> t
val infp : t
val infm : t
val zero : bool -> t
val zerop: t
val nan: bool -> Z.t -> t
val default_nan: t
val finite: mode -> Z.t -> Z.t -> t
val classify: t -> classify
val le : t -> t -> bool
val lt : t -> t -> bool
val ge : t -> t -> bool
val gt : t -> t -> bool
val eq : t -> t -> bool
val neq : t -> t -> bool
val fcompare : t -> t -> int option
val pp : Format.formatter -> t -> unit
val of_string: mode -> string -> t
end
module B32 = struct include Farith_F_aux.B32 include Common
let of_string mode s =
let m,e = lex_float s in
finite mode m e
end
module B64 = struct include Farith_F_aux.B64 include Common
(** unfortunately of_bits and to_bits wants positive argument (unsigned int64)
and Z.of_int64/Z.to_int64 wants signed argument (signed int64)
*)
let mask_one = Z.pred (Z.shift_left Z.one 64)
let of_float f =
let fb = (Big_int_Z.big_int_of_int64 (Int64.bits_of_float f)) in
let fb = Z.logand mask_one fb in
of_bits fb
let mask_63 = Z.shift_left Z.one 63
let intmask_63 = Int64.shift_left Int64.one 63
let to_float f =
let fb = to_bits f in
let i = if Z.logand mask_63 fb = Z.zero then Z.to_int64 fb
else Int64.logor intmask_63 (Z.to_int64 (Z.logxor mask_63 fb)) in
Int64.float_of_bits i
let of_string mode s =
let m,e = lex_float s in
finite mode m e
end
module type Size =
sig
val mw : int (** mantissa size (in bits) *)
val ew : int (** exponent size (in bits) *)
end
module Make(Size : Size) =
struct
include Farith_F_aux.Make
(struct
let mw = Z.of_int Size.mw
let ew = Z.of_int Size.ew
end)
include Common
let of_string mode s =
let m,e = lex_float s in
finite mode m e
end
*)letflocq_version=Version.coq_Flocq_version