Skip to content

Commit 36f9d99

Browse files
committed
C/C++ front-end: accept all floating-point extensions that GCC and Clang support
Testing with Compiler Explorer showed that Clang supports a subset of GCC's extensions, and GCC also supports some more extensions that what we previously covered.
1 parent 5bce1c3 commit 36f9d99

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

regression/ansi-c/float_constant1/main.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,21 @@ STATIC_ASSERT(__builtin_types_compatible_p(_Float64, __typeof(1.0f64)));
2121
STATIC_ASSERT(__builtin_types_compatible_p(_Float128, __typeof(1.0f128)));
2222
STATIC_ASSERT(__builtin_types_compatible_p(_Float32x, __typeof(1.0f32x)));
2323
STATIC_ASSERT(__builtin_types_compatible_p(_Float64x, __typeof(1.0f64x)));
24+
// f128x should be supported by GCC >= 7 (and for C++ in GCC >=13), but there
25+
// are not current GCC ports that actually support such types
2426
STATIC_ASSERT(__builtin_types_compatible_p(_Float128x, __typeof(1.0f128x)));
2527
#endif
2628

29+
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 13
30+
STATIC_ASSERT(__builtin_types_compatible_p(_Float16, __typeof(1.0f16)));
31+
STATIC_ASSERT(__builtin_types_compatible_p(__bf16, __typeof(1.0bf16)));
32+
STATIC_ASSERT(__builtin_types_compatible_p(__bf16, __typeof(1.BF16)));
33+
#endif
34+
35+
#if defined(__clang__) && __clang_major__ >= 6
36+
STATIC_ASSERT(__builtin_types_compatible_p(_Float16, __typeof(1.0f16)));
37+
#endif
38+
2739
#ifdef __GNUC__
2840
_Complex c;
2941
#endif

src/ansi-c/literals/parse_float.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ parse_floatt::parse_floatt(const std::string &src)
7777
p++;
7878

7979
// get exponent
80-
while(*p!=0 && *p!='f' && *p!='l' &&
81-
*p!='w' && *p!='q' && *p!='d')
80+
while(*p != 0 && *p != 'f' && *p != 'l' && *p != 'w' && *p != 'q' &&
81+
*p != 'd' && *p != 'b')
8282
{
8383
str_exponent+=*p;
8484
p++;
@@ -121,10 +121,8 @@ parse_floatt::parse_floatt(const std::string &src)
121121
p++;
122122

123123
// get fraction part
124-
while(*p!=0 && *p!='e' &&
125-
*p!='f' && *p!='l' &&
126-
*p!='w' && *p!='q' && *p!='d' &&
127-
*p!='i' && *p!='j')
124+
while(*p != 0 && *p != 'e' && *p != 'f' && *p != 'l' && *p != 'w' &&
125+
*p != 'q' && *p != 'd' && *p != 'i' && *p != 'j' && *p != 'b')
128126
{
129127
str_fraction_part+=*p;
130128
p++;
@@ -139,9 +137,8 @@ parse_floatt::parse_floatt(const std::string &src)
139137
p++;
140138

141139
// get exponent
142-
while(*p!=0 && *p!='f' && *p!='l' &&
143-
*p!='w' && *p!='q' && *p!='d' &&
144-
*p!='i' && *p!='j')
140+
while(*p != 0 && *p != 'f' && *p != 'l' && *p != 'w' && *p != 'q' &&
141+
*p != 'd' && *p != 'i' && *p != 'j' && *p != 'b')
145142
{
146143
str_exponent+=*p;
147144
p++;
@@ -173,7 +170,7 @@ parse_floatt::parse_floatt(const std::string &src)
173170
is_float80=false;
174171
is_float128=is_float128x=false;
175172

176-
if(strcmp(p, "f16")==0)
173+
if(strcmp(p, "f16") == 0 || strcmp(p, "bf16") == 0)
177174
is_float16=true;
178175
else if(strcmp(p, "f32")==0)
179176
is_float32=true;

src/ansi-c/scanner.l

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,13 @@ hexfloat1 "0"[xX]{hexdigit}*"."{hexdigit}+[pP][+-]?{integer}
217217
hexfloat2 "0"[xX]{hexdigit}*"."[pP][+-]?{integer}
218218
hexfloat3 "0"[xX]{hexdigit}*[pP][+-]?{integer}
219219
float_suffix [fFlLiIjJ]*
220-
gcc_ext_float_suffix [wWqQ]|[dD][fFdDlL]?|"f16"|"f32"|"f64"|"f128"|"f32x"|"f64x"|"f128x"
220+
clang_ext_float_suffix [qQ]|"f16"|"F16"
221+
gcc_ext_float_width (("bf"|"BF")"16")|([fF]("32"|"64"|"128"|"32x"|"64x"|"128x"))
222+
gcc_ext_float_suffix {clang_ext_float_suffix}|[wW]|[dD][fFdDlL]?|{gcc_ext_float_width}
221223
float {float1}|{float2}|{float3}|{hexfloat1}|{hexfloat2}|{hexfloat3}
222224
float_s {float}{float_suffix}|{integer}[fF]
223225
gcc_ext_float_s {float}{gcc_ext_float_suffix}
226+
clang_ext_float_s {float}{clang_ext_float_suffix}
224227
cppstart {ws}"#"{ws}
225228
cpplineno {cppstart}"line"*{ws}{integer}{ws}.*{newline}
226229
cppdirective {cppstart}({newline}|[^p].*|"p"[^r].*|"pr"[^a].*|"pra"[^g].*|"prag"[^m].*|"pragm"[^a].*)
@@ -1561,6 +1564,18 @@ __decltype { if(PARSER.cpp98 &&
15611564
return TOK_INTEGER;
15621565
}
15631566

1567+
{clang_ext_float_s} { if(PARSER.mode!=configt::ansi_ct::flavourt::GCC &&
1568+
PARSER.mode != configt::ansi_ct::flavourt::CLANG)
1569+
{
1570+
yyansi_cerror("Preprocessor directive found");
1571+
return TOK_SCANNER_ERROR;
1572+
}
1573+
newstack(yyansi_clval);
1574+
parser_stack(yyansi_clval)=convert_float_literal(yytext);
1575+
PARSER.set_source_location(parser_stack(yyansi_clval));
1576+
return TOK_FLOATING;
1577+
}
1578+
15641579
{gcc_ext_float_s} { if(PARSER.mode!=configt::ansi_ct::flavourt::GCC)
15651580
{
15661581
yyansi_cerror("Preprocessor directive found");

0 commit comments

Comments
 (0)