Skip to content

Commit 2d80f17

Browse files
committed
Add support for __fp16 where appropriate
GCC supports this since version 4.5 (but only on ARM platforms), Clang supports it since version 6 on all targets.
1 parent 8c229d7 commit 2d80f17

File tree

10 files changed

+26
-3
lines changed

10 files changed

+26
-3
lines changed

src/ansi-c/ansi_c_language.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ bool ansi_c_languaget::parse(
7878
ansi_c_parser.ts_18661_3_Floatn_types=config.ansi_c.ts_18661_3_Floatn_types;
7979
ansi_c_parser.float16_type = config.ansi_c.float16_type;
8080
ansi_c_parser.bf16_type = config.ansi_c.bf16_type;
81+
ansi_c_parser.fp16_type = config.ansi_c.fp16_type;
8182
ansi_c_parser.cpp98=false; // it's not C++
8283
ansi_c_parser.cpp11=false; // it's not C++
8384
ansi_c_parser.mode=config.ansi_c.mode;
@@ -202,6 +203,7 @@ bool ansi_c_languaget::to_expr(
202203
ansi_c_parser.ts_18661_3_Floatn_types=config.ansi_c.ts_18661_3_Floatn_types;
203204
ansi_c_parser.float16_type = config.ansi_c.float16_type;
204205
ansi_c_parser.bf16_type = config.ansi_c.bf16_type;
206+
ansi_c_parser.fp16_type = config.ansi_c.fp16_type;
205207
ansi_c_parser.cpp98 = false; // it's not C++
206208
ansi_c_parser.cpp11 = false; // it's not C++
207209
ansi_c_parser.mode = config.ansi_c.mode;

src/ansi-c/ansi_c_parser.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ class ansi_c_parsert:public parsert
3737
for_has_scope(false),
3838
ts_18661_3_Floatn_types(false),
3939
float16_type(false),
40-
bf16_type(false)
40+
bf16_type(false),
41+
fp16_type(false)
4142
{
4243
// set up global scope
4344
scopes.clear();
@@ -69,6 +70,7 @@ class ansi_c_parsert:public parsert
6970
bool ts_18661_3_Floatn_types;
7071
bool float16_type;
7172
bool bf16_type;
73+
bool fp16_type;
7274

7375
typedef ansi_c_identifiert identifiert;
7476
typedef ansi_c_scopet scopet;

src/ansi-c/builtin_factory.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ static bool convert(
5454
ansi_c_parser.ts_18661_3_Floatn_types = config.ansi_c.ts_18661_3_Floatn_types;
5555
ansi_c_parser.float16_type = config.ansi_c.float16_type;
5656
ansi_c_parser.bf16_type = config.ansi_c.bf16_type;
57+
ansi_c_parser.fp16_type = config.ansi_c.fp16_type;
5758
ansi_c_parser.cpp98=false; // it's not C++
5859
ansi_c_parser.cpp11=false; // it's not C++
5960
ansi_c_parser.mode=config.ansi_c.mode;

src/ansi-c/gcc_version.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,11 @@ void configure_gcc(const gcc_versiont &gcc_version)
174174
gcc_version.is_at_least(13u)) ||
175175
(gcc_version.flavor == gcc_versiont::flavort::CLANG &&
176176
gcc_version.is_at_least(15u));
177+
178+
config.ansi_c.fp16_type =
179+
(gcc_version.flavor == gcc_versiont::flavort::GCC &&
180+
gcc_version.is_at_least(4u, 5u) &&
181+
(config.ansi_c.arch == "arm" || config.ansi_c.arch == "arm64")) ||
182+
(gcc_version.flavor == gcc_versiont::flavort::CLANG &&
183+
gcc_version.is_at_least(6u));
177184
}

src/ansi-c/scanner.l

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,12 @@ enable_or_disable ("enable"|"disable")
546546
return make_identifier();
547547
}
548548

549+
"__fp16" { if(PARSER.fp16_type)
550+
{ loc(); return TOK_GCC_FLOAT16; }
551+
else
552+
return make_identifier();
553+
}
554+
549555
"_Float32" { if(PARSER.ts_18661_3_Floatn_types)
550556
{ loc(); return TOK_GCC_FLOAT32; }
551557
else

src/cpp/cpp_parser.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ bool cpp_parsert::parse()
4242
false; // these are still typedefs
4343
token_buffer.ansi_c_parser.float16_type = *support_float16;
4444
token_buffer.ansi_c_parser.bf16_type = *support_float16;
45+
token_buffer.ansi_c_parser.fp16_type = *support_float16;
4546
token_buffer.ansi_c_parser.in = in;
4647
token_buffer.ansi_c_parser.mode = mode;
4748
token_buffer.ansi_c_parser.set_file(get_file());

src/goto-instrument/contracts/contracts_wrangler.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ void contracts_wranglert::mangle(
147147
ansi_c_parser.ts_18661_3_Floatn_types = config.ansi_c.ts_18661_3_Floatn_types;
148148
ansi_c_parser.float16_type = config.ansi_c.float16_type;
149149
ansi_c_parser.bf16_type = config.ansi_c.bf16_type;
150+
ansi_c_parser.fp16_type = config.ansi_c.fp16_type;
150151
ansi_c_parser.cpp98 = false; // it's not C++
151152
ansi_c_parser.cpp11 = false; // it's not C++
152153
ansi_c_parser.mode = config.ansi_c.mode;

src/util/config.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,7 @@ bool configt::set(const cmdlinet &cmdline)
837837
ansi_c.ts_18661_3_Floatn_types=false;
838838
ansi_c.float16_type = false;
839839
ansi_c.bf16_type = false;
840+
ansi_c.fp16_type = false;
840841
ansi_c.c_standard=ansi_ct::default_c_standard();
841842
ansi_c.endianness=ansi_ct::endiannesst::NO_ENDIANNESS;
842843
ansi_c.os=ansi_ct::ost::NO_OS;
@@ -1328,8 +1329,8 @@ void configt::set_from_symbol_table(const symbol_table_baset &symbol_table)
13281329
ansi_c.char_is_unsigned=unsigned_from_ns(ns, "char_is_unsigned")!=0;
13291330
ansi_c.wchar_t_is_unsigned=unsigned_from_ns(ns, "wchar_t_is_unsigned")!=0;
13301331
// for_has_scope, single_precision_constant, rounding_mode,
1331-
// ts_18661_3_Floatn_types, float16_type, bf16_type are not architectural
1332-
// features, and thus not stored in namespace
1332+
// ts_18661_3_Floatn_types, float16_type, bf16_type, fp16_type are not
1333+
// architectural features, and thus not stored in namespace
13331334

13341335
ansi_c.alignment=unsigned_from_ns(ns, "alignment");
13351336

src/util/config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ class configt
153153
bool gcc__float128_type; // __float128, a gcc extension since 4.3/4.5
154154
bool float16_type; // _Float16 (Clang >= 15, GCC >= 12)
155155
bool bf16_type; // __bf16 (Clang >= 15, GCC >= 13)
156+
bool fp16_type; // __fp16 (GCC >= 4.5 on ARM, Clang >= 6)
156157
bool single_precision_constant;
157158
enum class c_standardt
158159
{

unit/cpp_scanner.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ int main(int argc, const char *argv[])
4343
ansi_c_parser.ts_18661_3_Floatn_types = false;
4444
ansi_c_parser.float16_type = false;
4545
ansi_c_parser.bf16_type = false;
46+
ansi_c_parser.fp16_type = false;
4647
ansi_c_parser.in=∈
4748
cpp_parser.in=∈
4849

0 commit comments

Comments
 (0)