[英]How to correctly determine at compile time that _Float16 is supported?
I am trying to determine at compile time that _Float16
is supported:我试图在编译时确定支持
_Float16
:
#define __STDC_WANT_IEC_60559_TYPES_EXT__
#include <float.h>
#ifdef FLT16_MAX
_Float16 f16;
#endif
Invocations:调用:
# gcc trunk on linux on x86_64
$ gcc -std=c11 -pedantic -Wall -Wextra
t0.c:4:1: warning: ISO C does not support the '_Float16' type [-Wpedantic]
# clang trunk on linux on x86_64
$ clang -std=c11 -pedantic -Wall -Wextra
t0.c:4:1: error: _Float16 is not supported on this target
Here we see that both gcc and clang:在这里我们看到 gcc 和 clang:
FLT16_MAX
FLT16_MAX
_Float16
_Float16
The main question: How to correctly determine at compile time that _Float16
is supported?主要问题:如何在编译时正确确定支持
_Float16
?
The extra question: Does the C11 (or newer) standard require to not define _MIN
/ _MAX
macros if the corresponding floating type is not supported?额外的问题:如果不支持相应的浮点类型,C11(或更新的)标准是否要求不定义
_MIN
/ _MAX
宏? For example, for integer types ( <stdint.h>
) it is true: "nor shall it define the associated macros" (C11, 7.20 Integer types <stdint.h>, 4).例如,对于整数类型 (
<stdint.h>
) 为真:“也不应定义关联的宏”(C11,7.20 整数类型 <stdint.h>,4)。 Is it the same for floating types?浮动类型是否相同?
UPD20211117: UPD20211117:
-pedantic
causes the warning disappear.-pedantic
会导致警告消失。 And _Float16
is supported._Float16
。 Great!-pedantic
does not cause the error disappear.-pedantic
不会导致错误消失。 Proabbly it is a bug. Thanks to user n.感谢用户 n。 1.8e9-where's-my-share m.
1.8e9-where's-my-share m. for the idea.
为了这个想法。
UPD20211118: gcc: with -pedantic
the FLT16_MAX
is defined, which is unexpected (or not?). UPD20211118:gcc:使用
-pedantic
定义了FLT16_MAX
,这是意外的(或不是?)。
How to correctly determine at compile time that _Float16 is supported?
如何在编译时正确确定支持 _Float16?
Compile a program that uses _Float16
.编译一个使用
_Float16
的程序。 If it compiles, it means it is supported.如果它编译,则意味着它被支持。
The support of _FloatN
can be determined via checking if the associated MIN / MAX
(and other) macros are defined (after including float.h
preceded with __STDC_WANT_IEC_60559_TYPES_EXT__
). _FloatN
的支持可以通过检查是否定义了相关的MIN / MAX
(和其他)宏来确定(在包括float.h
前面有__STDC_WANT_IEC_60559_TYPES_EXT__
)。
Extra: the same principle is used to determine the support of types from stdint.h
: check if the associated MIN / MAX
macros are defined.额外:使用相同的原则来确定对
stdint.h
类型的支持:检查是否定义了关联的MIN / MAX
宏。
Use clang's __is_identifier
: https://clang.llvm.org/docs/LanguageExtensions.html#is-identifier使用 clang 的
__is_identifier
: https ://clang.llvm.org/docs/LanguageExtensions.html#is-identifier
__is_identifier
evaluates to 1 if the argument is just a regular identifier and not a reserved word, in the sense that it can then be used as the name of a user-defined function or variable.如果参数只是一个常规标识符而不是保留字,则
__is_identifier
的计算结果为 1,因为它可以用作用户定义的函数或变量的名称。 Otherwise it evaluates to 0.否则它的计算结果为 0。
So hopefully this code works:所以希望这段代码有效:
#ifdef __is_identifier // Compatibility with non-clang compilers.
#if !__is_identifier(_Float16)
/// This typedef should always match the one in riscv-vector.h
typedef _Float16 f16;
#define FLOAT16_BUILTIN
#endif
#endif
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.