Skip to content

Commit 95aa21c

Browse files
authored
Merge pull request #193 from goblint/float16
Add `_Float16` support
2 parents e2c534c + ecdc4c9 commit 95aa21c

11 files changed

Lines changed: 124 additions & 11 deletions

File tree

src/cil.ml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,10 +309,12 @@ and fkind =
309309
| FDouble (** [double] *)
310310
| FLongDouble (** [long double] *)
311311
| FFloat128 (** [float128] *)
312+
| FFloat16 (** [_Float16] *)
312313
| FComplexFloat (** [float _Complex] *)
313314
| FComplexDouble (** [double _Complex] *)
314315
| FComplexLongDouble (** [long double _Complex]*)
315316
| FComplexFloat128 (** [_float128 _Complex]*)
317+
| FComplexFloat16 (** [_Float16 _Complex]*)
316318

317319
(** An attribute has a name and some optional parameters *)
318320
and attribute = Attr of string * attrparam list
@@ -1665,10 +1667,12 @@ let typeOfRealAndImagComponents t =
16651667
| FDouble -> FDouble (* [double] *)
16661668
| FLongDouble -> FLongDouble (* [long double] *)
16671669
| FFloat128 -> FFloat128
1670+
| FFloat16 -> FFloat16
16681671
| FComplexFloat -> FFloat
16691672
| FComplexDouble -> FDouble
16701673
| FComplexLongDouble -> FLongDouble
16711674
| FComplexFloat128 -> FFloat128
1675+
| FComplexFloat16 -> FFloat16
16721676
in
16731677
TFloat (newfkind fkind, attrs)
16741678
| _ -> E.s (E.bug "unexpected non-numerical type for argument to __real__/__imag__ ")
@@ -1679,10 +1683,12 @@ let getComplexFkind = function
16791683
| FDouble -> FComplexDouble
16801684
| FLongDouble -> FComplexLongDouble
16811685
| FFloat128 -> FComplexFloat128
1686+
| FFloat16 -> FComplexFloat16
16821687
| FComplexFloat -> FComplexFloat
16831688
| FComplexDouble -> FComplexDouble
16841689
| FComplexLongDouble -> FComplexLongDouble
16851690
| FComplexFloat128 -> FComplexFloat128
1691+
| FComplexFloat16 -> FComplexFloat16
16861692

16871693
let var vi : lval = (Var vi, NoOffset)
16881694
(* let assign vi e = Instrs(Set (var vi, e), lu) *)
@@ -1755,10 +1761,12 @@ let d_fkind () = function
17551761
| FDouble -> text "double"
17561762
| FLongDouble -> text "long double"
17571763
| FFloat128 -> text "_Float128"
1764+
| FFloat16 -> text "_Float16"
17581765
| FComplexFloat -> text "_Complex float"
17591766
| FComplexDouble -> text "_Complex double"
17601767
| FComplexLongDouble -> text "_Complex long double"
17611768
| FComplexFloat128 -> text "_Complex _Float128"
1769+
| FComplexFloat16 -> text "_Complex _Float16"
17621770

17631771
let d_storage () = function
17641772
NoStorage -> nil
@@ -1855,10 +1863,12 @@ let d_const () c =
18551863
| FDouble -> nil
18561864
| FLongDouble -> chr 'L'
18571865
| FFloat128 -> text "F128"
1866+
| FFloat16 -> text "F16"
18581867
| FComplexFloat -> text "iF"
18591868
| FComplexDouble -> chr 'i'
18601869
| FComplexLongDouble -> text "iL"
1861-
| FComplexFloat128 -> text "iF128")
1870+
| FComplexFloat128 -> text "iF128"
1871+
| FComplexFloat16 -> text "iF16")
18621872
| CEnum(_, s, ei) -> text s
18631873

18641874

@@ -2108,6 +2118,7 @@ let floatKindForSize (s:int) =
21082118
else if s = !M.theMachine.M.sizeof_float then FFloat
21092119
else if s = !M.theMachine.M.sizeof_longdouble then FLongDouble
21102120
else if s = !M.theMachine.M.sizeof_float128 then FFloat128
2121+
else if s = !M.theMachine.M.sizeof_float16 then FFloat16
21112122
else raise Not_found
21122123

21132124
(* Represents an integer as for a given kind. Returns a flag saying
@@ -2267,10 +2278,12 @@ let rec alignOf_int t =
22672278
| TFloat(FDouble, _) -> !M.theMachine.M.alignof_double
22682279
| TFloat(FLongDouble, _) -> !M.theMachine.M.alignof_longdouble
22692280
| TFloat(FFloat128, _) -> !M.theMachine.M.alignof_float128
2281+
| TFloat(FFloat16, _) -> !M.theMachine.M.alignof_float16
22702282
| TFloat(FComplexFloat, _) -> !M.theMachine.M.alignof_floatcomplex
22712283
| TFloat(FComplexDouble, _) -> !M.theMachine.M.alignof_doublecomplex
22722284
| TFloat(FComplexLongDouble, _) -> !M.theMachine.M.alignof_longdoublecomplex
22732285
| TFloat(FComplexFloat128, _) -> !M.theMachine.M.alignof_float128complex
2286+
| TFloat(FComplexFloat16, _) -> !M.theMachine.M.alignof_float16complex
22742287
| TNamed (t, _) -> alignOf_int t.ttype
22752288
| TArray (t, _, _) -> alignOf_int t
22762289
| TPtr _ | TBuiltin_va_list _ -> !M.theMachine.M.alignof_ptr
@@ -2425,10 +2438,12 @@ and bitsSizeOf t =
24252438
| TFloat(FDouble, _) -> 8 * !M.theMachine.M.sizeof_double
24262439
| TFloat(FLongDouble, _) -> 8 * !M.theMachine.M.sizeof_longdouble
24272440
| TFloat(FFloat128, _) -> 8 * !M.theMachine.M.sizeof_float128
2441+
| TFloat(FFloat16, _) -> 8 * !M.theMachine.M.sizeof_float16
24282442
| TFloat(FFloat, _) -> 8 * !M.theMachine.M.sizeof_float
24292443
| TFloat(FComplexDouble, _) -> 8 * !M.theMachine.M.sizeof_doublecomplex
24302444
| TFloat(FComplexLongDouble, _) -> 8 * !M.theMachine.M.sizeof_longdoublecomplex
24312445
| TFloat(FComplexFloat128, _) -> 8 * !M.theMachine.M.sizeof_float128complex
2446+
| TFloat(FComplexFloat16, _) -> 8 * !M.theMachine.M.sizeof_float16complex
24322447
| TFloat(FComplexFloat, _) -> 8 * !M.theMachine.M.sizeof_floatcomplex
24332448
| TEnum (ei, _) -> bitsSizeOf (TInt(ei.ekind, []))
24342449
| TPtr _ -> 8 * !M.theMachine.M.sizeof_ptr

src/cil.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,12 @@ and fkind =
296296
| FDouble (** [double] *)
297297
| FLongDouble (** [long double] *)
298298
| FFloat128 (** [float128] *)
299+
| FFloat16 (** [_Float16] *)
299300
| FComplexFloat (** [float _Complex] *)
300301
| FComplexDouble (** [double _Complex] *)
301302
| FComplexLongDouble (** [long double _Complex]*)
302303
| FComplexFloat128 (** [_float128 _Complex]*)
304+
| FComplexFloat16 (** [_Float16 _Complex]*)
303305

304306
(** {b Attributes.} *)
305307

src/frontc/cabs.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ type typeSpecifier = (* Merge all specifiers into one type *)
7272
| Tfloat128 (* TODO needed? *)
7373
| Tfloat32x
7474
| Tfloat64x
75+
| Tfloat16
7576
| Tdouble
7677
| Tsigned
7778
| Tsizet (* used temporarily to translate offsetof() *)

src/frontc/cabs2cil.ml

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,7 +1373,7 @@ let arithmeticConversion (* c.f. ISO 6.3.1.8 *)
13731373
(t2: typ) : typ =
13741374
let resultingFType fkind1 t1 fkind2 t2 =
13751375
(* t1 and t2 are the original types before unrollType, so TNamed is preserved if possible *)
1376-
let isComplex f = f = FComplexFloat || f = FComplexDouble || f = FComplexLongDouble || f = FComplexFloat128 in
1376+
let isComplex f = f = FComplexFloat || f = FComplexDouble || f = FComplexLongDouble || f = FComplexFloat128 || f = FComplexFloat16 in
13771377
match fkind1, fkind2 with
13781378
| FComplexFloat128, _ -> t1
13791379
| _, FComplexFloat128 -> t2
@@ -1389,7 +1389,11 @@ let arithmeticConversion (* c.f. ISO 6.3.1.8 *)
13891389
| other, FDouble -> if isComplex other then TFloat(FComplexDouble, []) else t2
13901390
| FComplexFloat, other -> t1
13911391
| other, FComplexFloat -> t2
1392-
| FFloat, FFloat -> t1
1392+
| FFloat, other -> if isComplex other then TFloat(FComplexFloat, []) else t1
1393+
| other, FFloat -> if isComplex other then TFloat(FComplexFloat, []) else t2
1394+
| FComplexFloat16, other -> t1
1395+
| other, FComplexFloat16 -> t2
1396+
| FFloat16, FFloat16 -> t1
13931397
in
13941398
match unrollType t1, unrollType t2 with
13951399
| TFloat(fkind1, _), TFloat(fkind2, _) -> resultingFType fkind1 t1 fkind2 t2
@@ -2620,6 +2624,7 @@ let rec doSpecList (suggestedAnonName: string) (* This string will be part of
26202624

26212625
| [A.Tlong; A.Tdouble] -> TFloat(FLongDouble, [])
26222626
| [A.Tfloat128] -> TFloat(FFloat128, [])
2627+
| [A.Tfloat16] -> TFloat(FFloat16, [])
26232628
(* Now the other type specifiers *)
26242629
| [A.Tdefault] -> E.s (error "Default outside generic associations")
26252630
| [A.Tnamed n] -> begin
@@ -3739,6 +3744,8 @@ and doExp (asconst: bool) (* This expression is used as a constant *)
37393744
let baseint, kind =
37403745
if hasSuffix str "F128" then
37413746
String.sub str 0 (l - 4), FFloat128
3747+
else if hasSuffix str "F16" then
3748+
String.sub str 0 (l - 3), FFloat16
37423749
else if hasSuffix str "Q" then
37433750
String.sub str 0 (l - 1), FFloat128
37443751
else if hasSuffix str "L" then
@@ -3773,6 +3780,8 @@ and doExp (asconst: bool) (* This expression is used as a constant *)
37733780
let baseint, kind =
37743781
if hasSuffix str "iF128" || hasSuffix str "F128i" then
37753782
String.sub str 0 (l - 5), FComplexFloat128
3783+
else if hasSuffix str "iF16" || hasSuffix str "F16i" then
3784+
String.sub str 0 (l - 4), FComplexFloat16
37763785
else if hasSuffix str "Qi" || hasSuffix str "iQ" then
37773786
String.sub str 0 (l - 2), FComplexFloat128
37783787
else if hasSuffix str "iL" || hasSuffix str "Li" then
@@ -3860,11 +3869,13 @@ and doExp (asconst: bool) (* This expression is used as a constant *)
38603869
| FFloat
38613870
| FDouble
38623871
| FLongDouble
3863-
| FFloat128 -> 8
3872+
| FFloat128
3873+
| FFloat16 -> 8
38643874
| FComplexFloat
38653875
| FComplexDouble
38663876
| FComplexLongDouble
3867-
| FComplexFloat128 -> 9
3877+
| FComplexFloat128
3878+
| FComplexFloat16 -> 9
38683879
end
38693880
| TEnum _ -> 3
38703881
| TPtr _ -> 5
@@ -4530,7 +4541,7 @@ and doExp (asconst: bool) (* This expression is used as a constant *)
45304541
(* if the t we determined here is complex, but the return types of all the fptrs are not, the return *)
45314542
(* type should not be complex *)
45324543
let isComplex t = match t with
4533-
| TFloat(f, _) -> f = FComplexFloat || f = FComplexDouble || f = FComplexLongDouble || f = FComplexFloat128
4544+
| TFloat(f, _) -> f = FComplexFloat || f = FComplexDouble || f = FComplexLongDouble || f = FComplexFloat128 || f = FComplexFloat16
45344545
| _ -> false
45354546
in
45364547
if List.for_all (fun x -> not (isComplex x)) retTypes then

src/frontc/clexer.mll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ let init_lexicon _ =
143143
("_Float64", fun loc -> FLOAT64 loc);
144144
("_Float32x", fun loc -> FLOAT32X loc);
145145
("_Float64x", fun loc -> FLOAT64X loc);
146+
("_Float16", fun loc -> FLOAT16 loc);
146147
("double", fun loc -> DOUBLE loc);
147148
("void", fun loc -> VOID loc);
148149
("enum", fun loc -> ENUM loc);

src/frontc/cparser.mly

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ let transformOffsetOf (speclist, dtype) member =
265265
%token<Cabs.cabsloc> INT128 FLOAT128 COMPLEX /* C99 */
266266
%token<Cabs.cabsloc> FLOAT32 FLOAT64 /* FloatN */
267267
%token<Cabs.cabsloc> FLOAT32X FLOAT64X /* FloatNx */
268+
%token<Cabs.cabsloc> FLOAT16
268269
%token<Cabs.cabsloc> GENERIC NORETURN /* C11 */
269270
%token<Cabs.cabsloc> AUTOTYPE /* GCC */
270271
%token<Cabs.cabsloc> ENUM STRUCT TYPEDEF UNION
@@ -1097,6 +1098,7 @@ type_spec: /* ISO 6.7.2 */
10971098
| FLOAT128 { Tfloat128, $1 }
10981099
| FLOAT32X { Tfloat32x, $1 }
10991100
| FLOAT64X { Tfloat64x, $1 }
1101+
| FLOAT16 { Tfloat16, $1 }
11001102
| DOUBLE { Tdouble, $1 }
11011103
| AUTOTYPE { Tauto, $1 }
11021104
/* | COMPLEX FLOAT { Tfloat, $2 } */

src/frontc/cprint.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ and print_type_spec = function
175175
| Tfloat128 -> print "__float128"
176176
| Tfloat32x -> print "_Float32x"
177177
| Tfloat64x -> print "_Float64x"
178+
| Tfloat16 -> print "_Float16"
178179
| Tdouble -> print "double "
179180
| Tsigned -> printu "signed"
180181
| Tunsigned -> print "unsigned "

src/machdep-ml.c

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ typedef int bool;
8686
#define UNDERSCORE_NAME "false"
8787
#endif
8888

89+
#ifdef HAVE_FLOAT16_DEF
90+
#define HAVE_FLOAT16 "true"
91+
#else
92+
#define HAVE_FLOAT16 "false"
93+
#endif
94+
8995
#endif
9096

9197

@@ -106,8 +112,8 @@ int main(int argc, char **argv)
106112
{
107113
int env = argc == 2 && !strcmp(argv[1], "--env");
108114
int alignof_short, alignof_int, alignof_long, alignof_ptr, alignof_enum,
109-
alignof_float, alignof_float32x, alignof_float64x, alignof_double, alignof_longdouble, alignof_float128,
110-
alignof_floatcomplex, alignof_doublecomplex, alignof_longdoublecomplex, alignof_float128complex,
115+
alignof_float, alignof_float32x, alignof_float64x, alignof_double, alignof_longdouble, alignof_float128, alignof_float16,
116+
alignof_floatcomplex, alignof_doublecomplex, alignof_longdoublecomplex, alignof_float128complex, alignof_float16complex,
111117
sizeof_fun,
112118
alignof_fun, alignof_str, alignof_aligned, alignof_longlong,
113119
little_endian, char_is_unsigned, alignof_bool;
@@ -231,6 +237,19 @@ int main(int argc, char **argv)
231237
alignof_float128 = (intptr_t)(&((struct s1*)0)->ld);
232238
}
233239

240+
#ifdef HAVE_FLOAT16_DEF
241+
// The alignment of float16
242+
{
243+
struct s1 {
244+
char c;
245+
_Float16 ld;
246+
};
247+
alignof_float16 = (intptr_t)(&((struct s1*)0)->ld);
248+
}
249+
#else
250+
alignof_float16 = 0;
251+
#endif
252+
234253
// The alignment of a float complex
235254
{
236255
struct floatstruct {
@@ -267,6 +286,19 @@ int main(int argc, char **argv)
267286
alignof_float128complex = (intptr_t)(&((struct s1*)0)->ld);
268287
}
269288

289+
#ifdef HAVE_FLOAT16_DEF
290+
// The alignment of float16 complex
291+
{
292+
struct s1 {
293+
char c;
294+
_Float16 _Complex ld;
295+
};
296+
alignof_float16complex = (intptr_t)(&((struct s1*)0)->ld);
297+
}
298+
#else
299+
alignof_float16complex = 0;
300+
#endif
301+
270302

271303
alignof_str = __alignof("a string");
272304
alignof_fun = __alignof(main);
@@ -304,11 +336,11 @@ int main(int argc, char **argv)
304336
{
305337
fprintf(stderr, "Generating CIL_MACHINE machine dependency information string (for CIL)\n");
306338
printf("short=%d,%d int=%d,%d long=%d,%d long_long=%d,%d pointer=%d,%d "
307-
"alignof_enum=%d float=%d,%d float32x=%d,%d float64x=%d,%d double=%d,%d long_double=%d,%d float128=%d,%d float_complex=%d,%d double_complex=%d,%d long_double_complex=%d,%d float128_complex=%d,%d void=%d "
339+
"alignof_enum=%d float=%d,%d float32x=%d,%d float64x=%d,%d double=%d,%d long_double=%d,%d float128=%d,%d float16=%d,%d float_complex=%d,%d double_complex=%d,%d long_double_complex=%d,%d float128_complex=%d,%d float16_complex=%d,%d void=%d "
308340
"bool=%d,%d fun=%d,%d alignof_string=%d max_alignment=%d size_t=%s "
309341
"wchar_t=%s char16_t=%s char32_t=%s char_signed=%s "
310342
"big_endian=%s __thread_is_keyword=%s __builtin_va_list=%s "
311-
"underscore_name=%s\n",
343+
"underscore_name=%s have_float16=%s\n",
312344
(int)sizeof(short), alignof_short, (int)sizeof(int), alignof_int,
313345
(int)sizeof(long), alignof_long, (int)sizeof(long long), alignof_longlong,
314346
(int)sizeof(int *), alignof_ptr,
@@ -322,16 +354,26 @@ int main(int argc, char **argv)
322354
(int)sizeof(float), alignof_float, (int)sizeof(double), alignof_double,
323355
(int)sizeof(long double), alignof_longdouble,
324356
(int)sizeof(_Float128), alignof_float128,
357+
#ifdef HAVE_FLOAT16_DEF
358+
(int)sizeof(_Float16), alignof_float16,
359+
#else
360+
0, 0,
361+
#endif
325362
(int)sizeof(float _Complex), alignof_floatcomplex, (int)sizeof(double _Complex), alignof_doublecomplex,
326363
(int)sizeof(long double _Complex), alignof_longdoublecomplex,
327364
(int)sizeof(_Float128 _Complex), alignof_float128complex,
365+
#ifdef HAVE_FLOAT16_DEF
366+
(int)sizeof(_Float16 _Complex), alignof_float16complex,
367+
#else
368+
0, 0,
369+
#endif
328370
(int)sizeof(void),
329371
(int)sizeof(bool), alignof_bool,
330372
sizeof_fun, alignof_fun, alignof_str, alignof_aligned,
331373
underscore(TYPE_SIZE_T), underscore(TYPE_WCHAR_T), underscore(TYPE_CHAR16_T), underscore(TYPE_CHAR32_T),
332374
char_is_unsigned ? "false" : "true",
333375
little_endian ? "false" : "true",
334-
THREAD_IS_KEYWORD, HAVE_BUILTIN_VA_LIST, UNDERSCORE_NAME);
376+
THREAD_IS_KEYWORD, HAVE_BUILTIN_VA_LIST, UNDERSCORE_NAME, HAVE_FLOAT16);
335377
}
336378
else
337379
{
@@ -359,10 +401,20 @@ int main(int argc, char **argv)
359401
printf("\t sizeof_double = %d;\n", (int)sizeof(double));
360402
printf("\t sizeof_longdouble = %d;\n", (int)sizeof(long double));
361403
printf("\t sizeof_float128 = %d;\n", (int)sizeof(_Float128));
404+
#ifdef HAVE_FLOAT16_DEF
405+
printf("\t sizeof_float16 = %d;\n", (int)sizeof(_Float16));
406+
#else
407+
printf("\t sizeof_float16 = %d;\n", 0);
408+
#endif
362409
printf("\t sizeof_floatcomplex = %d;\n", (int)sizeof(float _Complex));
363410
printf("\t sizeof_doublecomplex = %d;\n", (int)sizeof(double _Complex));
364411
printf("\t sizeof_longdoublecomplex = %d;\n", (int)sizeof(long double _Complex));
365412
printf("\t sizeof_float128complex = %d;\n", (int)sizeof(_Float128 _Complex));
413+
#ifdef HAVE_FLOAT16_DEF
414+
printf("\t sizeof_float16complex = %d;\n", (int)sizeof(_Float16 _Complex));
415+
#else
416+
printf("\t sizeof_float16complex = %d;\n", 0);
417+
#endif
366418
printf("\t sizeof_void = %d;\n", (int)sizeof(void));
367419
printf("\t sizeof_fun = %d;\n", (int)sizeof_fun);
368420
printf("\t size_t = \"%s\";\n", TYPE_SIZE_T);
@@ -386,10 +438,20 @@ int main(int argc, char **argv)
386438
printf("\t alignof_double = %d;\n", alignof_double);
387439
printf("\t alignof_longdouble = %d;\n", alignof_longdouble);
388440
printf("\t alignof_float128 = %d;\n", alignof_float128);
441+
#ifdef HAVE_FLOAT16_DEF
442+
printf("\t alignof_float16 = %d;\n", alignof_float16);
443+
#else
444+
printf("\t alignof_float16 = %d;\n", 0);
445+
#endif
389446
printf("\t alignof_floatcomplex = %d;\n", alignof_floatcomplex);
390447
printf("\t alignof_doublecomplex = %d;\n", alignof_doublecomplex);
391448
printf("\t alignof_longdoublecomplex = %d;\n", alignof_longdoublecomplex);
392449
printf("\t alignof_float128complex = %d;\n", alignof_float128complex);
450+
#ifdef HAVE_FLOAT16_DEF
451+
printf("\t alignof_float16complex = %d;\n", alignof_float16complex);
452+
#else
453+
printf("\t alignof_float16complex = %d;\n", alignof_float16complex);
454+
#endif
393455
printf("\t alignof_str = %d;\n", alignof_str);
394456
printf("\t alignof_fun = %d;\n", alignof_fun);
395457
printf("\t alignof_aligned = %d;\n", alignof_aligned);
@@ -398,6 +460,7 @@ int main(int argc, char **argv)
398460
printf("\t __builtin_va_list = %s;\n", HAVE_BUILTIN_VA_LIST);
399461
printf("\t __thread_is_keyword = %s;\n", THREAD_IS_KEYWORD);
400462
printf("\t little_endian = %s;\n", little_endian ? "true" : "false");
463+
printf("\t have_float16 = %s;\n", HAVE_FLOAT16);
401464
}
402465
return 0;
403466
}

0 commit comments

Comments
 (0)