Skip to content

Commit f1b097b

Browse files
Merge pull request #985 from goblint/float128
Support for machines where `float128` is not the same size as `long double`
2 parents 219750f + 9497b2b commit f1b097b

File tree

7 files changed

+37
-15
lines changed

7 files changed

+37
-15
lines changed

goblint.opam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ dev-repo: "git+https://github.com/goblint/analyzer.git"
7474
# also remember to generate/adjust goblint.opam.locked!
7575
available: os-distribution != "alpine" & arch != "arm64"
7676
pin-depends: [
77-
[ "goblint-cil.2.0.1" "git+https://github.com/goblint/cil.git#c03ade107208546ef59cd14dcefa7b55f1506494" ]
77+
[ "goblint-cil.2.0.1" "git+https://github.com/goblint/cil.git#4ef5a0865ce81c740c93da73e20a4f26daab3f1b" ]
7878
# TODO: add back after release, only pinned for optimization (https://github.com/ocaml-ppx/ppx_deriving/pull/252)
7979
[ "ppx_deriving.5.2.1" "git+https://github.com/ocaml-ppx/ppx_deriving.git#0a89b619f94cbbfc3b0fb3255ab4fe5bc77d32d6" ]
8080
# TODO: add back after release, only pinned for CI stability

goblint.opam.locked

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ conflicts: [
128128
pin-depends: [
129129
[
130130
"goblint-cil.2.0.1"
131-
"git+https://github.com/goblint/cil.git#c03ade107208546ef59cd14dcefa7b55f1506494"
131+
"git+https://github.com/goblint/cil.git#4ef5a0865ce81c740c93da73e20a4f26daab3f1b"
132132
]
133133
[
134134
"apron.v0.9.13"

goblint.opam.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# also remember to generate/adjust goblint.opam.locked!
33
available: os-distribution != "alpine" & arch != "arm64"
44
pin-depends: [
5-
[ "goblint-cil.2.0.1" "git+https://github.com/goblint/cil.git#c03ade107208546ef59cd14dcefa7b55f1506494" ]
5+
[ "goblint-cil.2.0.1" "git+https://github.com/goblint/cil.git#4ef5a0865ce81c740c93da73e20a4f26daab3f1b" ]
66
# TODO: add back after release, only pinned for optimization (https://github.com/ocaml-ppx/ppx_deriving/pull/252)
77
[ "ppx_deriving.5.2.1" "git+https://github.com/ocaml-ppx/ppx_deriving.git#0a89b619f94cbbfc3b0fb3255ab4fe5bc77d32d6" ]
88
# TODO: add back after release, only pinned for CI stability

src/analyses/base.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -782,8 +782,8 @@ struct
782782
| Const (CInt (num,ikind,str)) ->
783783
(match str with Some x -> M.tracel "casto" "CInt (%s, %a, %s)\n" (Cilint.string_of_cilint num) d_ikind ikind x | None -> ());
784784
`Int (ID.cast_to ikind (IntDomain.of_const (num,ikind,str)))
785-
| Const (CReal (_, (FFloat | FDouble | FLongDouble as fkind), Some str)) -> `Float (FD.of_string fkind str) (* prefer parsing from string due to higher precision *)
786-
| Const (CReal (num, (FFloat | FDouble | FLongDouble as fkind), None)) -> `Float (FD.of_const fkind num)
785+
| Const (CReal (_,fkind, Some str)) when not (Cilfacade.isComplexFKind fkind) -> `Float (FD.of_string fkind str) (* prefer parsing from string due to higher precision *)
786+
| Const (CReal (num, fkind, None)) when not (Cilfacade.isComplexFKind fkind) -> `Float (FD.of_const fkind num)
787787
(* String literals *)
788788
| Const (CStr (x,_)) -> `Address (AD.from_string x) (* normal 8-bit strings, type: char* *)
789789
| Const (CWStr (xs,_) as c) -> (* wide character strings, type: wchar_t* *)

src/cdomains/floatDomain.ml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -784,38 +784,44 @@ module FloatIntervalImplLifted = struct
784784
type t =
785785
| F32 of F1.t
786786
| F64 of F2.t
787-
| FLong of F2.t [@@deriving to_yojson, eq, ord, hash]
787+
| FLong of F2.t
788+
| FFloat128 of F2.t [@@deriving to_yojson, eq, ord, hash]
788789

789790
let show = function
790791
| F32 a -> "float: " ^ F1.show a
791792
| F64 a -> "double: " ^ F2.show a
792793
| FLong a -> "long double: " ^ F2.show a
794+
| FFloat128 a -> "float128: " ^ F2.show a
793795

794796
let lift2 (op32, op64) x y = match x, y with
795797
| F32 a, F32 b -> F32 (op32 a b)
796798
| F64 a, F64 b -> F64 (op64 a b)
797799
| FLong a, FLong b -> FLong (op64 a b)
800+
| FFloat128 a, FFloat128 b -> FFloat128 (op64 a b)
798801
| _ -> failwith ("fkinds do not match. Values: " ^ show x ^ " and " ^ show y)
799802

800803
let lift2_cmp (op32, op64) x y = match x, y with
801804
| F32 a, F32 b -> op32 a b
802805
| F64 a, F64 b -> op64 a b
803806
| FLong a, FLong b -> op64 a b
807+
| FFloat128 a, FFloat128 b -> op64 a b
804808
| _ -> failwith ("fkinds do not match. Values: " ^ show x ^ " and " ^ show y)
805809

806810
let lift (op32, op64) = function
807811
| F32 a -> F32 (op32 a)
808812
| F64 a -> F64 (op64 a)
809813
| FLong a -> FLong (op64 a)
814+
| FFloat128 a -> FFloat128 (op64 a)
810815

811816
let dispatch (op32, op64) = function
812817
| F32 a -> op32 a
813-
| F64 a | FLong a -> op64 a
818+
| F64 a | FLong a | FFloat128 a-> op64 a
814819

815820
let dispatch_fkind fkind (op32, op64) = match fkind with
816821
| FFloat -> F32 (op32 ())
817822
| FDouble -> F64 (op64 ())
818823
| FLongDouble -> FLong (op64 ())
824+
| FFloat128 -> FFloat128 (op64 ())
819825
| _ ->
820826
(* this should never be reached, as we have to check for invalid fkind elsewhere,
821827
however we could instead of crashing also return top_of some fkind to avoid this and nonetheless have no actual information about anything*)
@@ -869,6 +875,7 @@ module FloatIntervalImplLifted = struct
869875
| F32 _ -> FFloat
870876
| F64 _ -> FDouble
871877
| FLong _ -> FLongDouble
878+
| FFloat128 _ -> FFloat128
872879

873880
let leq = lift2_cmp (F1.leq, F2.leq)
874881
let join = lift2 (F1.join, F2.join)

src/cdomains/valueDomain.ml

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ struct
144144
match t with
145145
| t when is_mutex_type t -> `Mutex
146146
| TInt (ik,_) -> `Int (ID.top_of ik)
147-
| TFloat ((FFloat | FDouble | FLongDouble as fkind), _) -> `Float (FD.top_of fkind)
147+
| TFloat (fkind, _) when not (Cilfacade.isComplexFKind fkind) -> `Float (FD.top_of fkind)
148148
| TPtr _ -> `Address AD.top_ptr
149149
| TComp ({cstruct=true; _} as ci,_) -> `Struct (Structs.create (fun fd -> init_value ~varAttr:fd.fattr fd.ftype) ci)
150150
| TComp ({cstruct=false; _},_) -> `Union (Unions.top ())
@@ -161,7 +161,7 @@ struct
161161
match t with
162162
| _ when is_mutex_type t -> `Mutex
163163
| TInt (ik,_) -> `Int (ID.(cast_to ik (top_of ik)))
164-
| TFloat ((FFloat | FDouble | FLongDouble as fkind), _) -> `Float (FD.top_of fkind)
164+
| TFloat (fkind, _) when not (Cilfacade.isComplexFKind fkind) -> `Float (FD.top_of fkind)
165165
| TPtr _ -> `Address AD.top_ptr
166166
| TComp ({cstruct=true; _} as ci,_) -> `Struct (Structs.create (fun fd -> top_value ~varAttr:fd.fattr fd.ftype) ci)
167167
| TComp ({cstruct=false; _},_) -> `Union (Unions.top ())
@@ -191,7 +191,7 @@ struct
191191
match t with
192192
| _ when is_mutex_type t -> `Mutex
193193
| TInt (ikind, _) -> `Int (ID.of_int ikind BI.zero)
194-
| TFloat ((FFloat | FDouble | FLongDouble as fkind), _) -> `Float (FD.of_const fkind 0.0)
194+
| TFloat (fkind, _) when not (Cilfacade.isComplexFKind fkind) -> `Float (FD.of_const fkind 0.0)
195195
| TPtr _ -> `Address AD.null_ptr
196196
| TComp ({cstruct=true; _} as ci,_) -> `Struct (Structs.create (fun fd -> zero_init_value ~varAttr:fd.fattr fd.ftype) ci)
197197
| TComp ({cstruct=false; _} as ci,_) ->
@@ -280,10 +280,13 @@ struct
280280
| TFloat (FDouble,_), TFloat (FFloat,_) -> true
281281
| TFloat (FLongDouble,_), TFloat (FFloat,_) -> true
282282
| TFloat (FLongDouble,_), TFloat (FDouble,_) -> true
283+
| TFloat (FFloat128, _), TFloat (FFloat,_) -> true
284+
| TFloat (FFloat128, _), TFloat (FDouble,_) -> true
285+
| TFloat (FFloat128, _), TFloat (FLongDouble,_) -> true
283286
| _, TFloat _ -> false (* casting float to an integral type always looses the decimals *)
284-
| TFloat ((FFloat | FDouble | FLongDouble), _), TInt((IBool | IChar | IUChar | ISChar | IShort | IUShort), _) -> true (* resonably small integers can be stored in all fkinds *)
285-
| TFloat ((FDouble | FLongDouble), _), TInt((IInt | IUInt | ILong | IULong), _) -> true (* values stored in between 16 and 32 bits can only be stored in at least doubles *)
286-
| TFloat _, _ -> false (* all wider integers can not be completly put into a float, partially because our internal representation of long double is the same as for doubles *)
287+
| TFloat (fk, _), TInt((IBool | IChar | IUChar | ISChar | IShort | IUShort), _) when not (Cilfacade.isComplexFKind fk) -> true (* reasonably small integers can be stored in all fkinds *)
288+
| TFloat ((FDouble | FLongDouble | FFloat128), _), TInt((IInt | IUInt | ILong | IULong), _) -> true (* values stored in between 16 and 32 bits can only be stored in at least doubles *)
289+
| TFloat _, _ -> false (* all wider integers can not be completely put into a float, partially because our internal representation of long double is the same as for doubles *)
287290
| (TInt _ | TEnum _ | TPtr _) , (TInt _ | TEnum _ | TPtr _) ->
288291
IntDomain.Size.is_cast_injective ~from_type:t1 ~to_type:t2 && bitsSizeOf t2 >= bitsSizeOf t1
289292
| _ -> false
@@ -388,7 +391,7 @@ struct
388391
(match Structs.get x first with `Int x -> x | _ -> raise CastError)*)
389392
| _ -> log_top __POS__; ID.top_of ik
390393
))
391-
| TFloat ((FFloat | FDouble | FLongDouble as fkind),_) ->
394+
| TFloat (fkind,_) when not (Cilfacade.isComplexFKind fkind) ->
392395
(match v with
393396
|`Int ix -> `Float (FD.of_int fkind ix)
394397
|`Float fx -> `Float (FD.cast_to fkind fx)
@@ -809,7 +812,7 @@ struct
809812
(* only return an actual value if we have a type and return actually the exact same type *)
810813
| `Float f_value, TFloat(fkind, _) when FD.get_fkind f_value = fkind -> `Float f_value
811814
| `Float _, t -> top_value t
812-
| _, TFloat((FFloat | FDouble | FLongDouble as fkind), _) -> `Float (FD.top_of fkind)
815+
| _, TFloat(fkind, _) when not (Cilfacade.isComplexFKind fkind)-> `Float (FD.top_of fkind)
813816
| _ ->
814817
let x = cast ~torg:l_fld.ftype fld.ftype value in
815818
let l', o' = shift_one_over l o in

src/util/cilfacade.ml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,25 @@ let typeOfRealAndImagComponents t =
201201
| FFloat -> FFloat (* [float] *)
202202
| FDouble -> FDouble (* [double] *)
203203
| FLongDouble -> FLongDouble (* [long double] *)
204+
| FFloat128 -> FFloat128 (* [float128] *)
204205
| FComplexFloat -> FFloat
205206
| FComplexDouble -> FDouble
206207
| FComplexLongDouble -> FLongDouble
208+
| FComplexFloat128 -> FComplexFloat128
207209
in
208210
TFloat (newfkind fkind, attrs)
209211
| _ -> raise (TypeOfError RealImag_NonNumerical)
210212

213+
let isComplexFKind = function
214+
| FFloat
215+
| FDouble
216+
| FLongDouble
217+
| FFloat128 -> false
218+
| FComplexFloat
219+
| FComplexDouble
220+
| FComplexLongDouble
221+
| FComplexFloat128 -> true
222+
211223
let rec typeOf (e: exp) : typ =
212224
match e with
213225
| Const(CInt (_, ik, _)) -> TInt(ik, [])

0 commit comments

Comments
 (0)