Search code examples
cgccfloating-pointclanghalf-precision-float

How to correctly determine at compile time that _Float16 is supported?


I am trying to determine at compile time that _Float16 is supported:

#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:

  • define FLT16_MAX
  • do not support _Float16

The main question: How to correctly determine at compile time that _Float16 is supported?

The extra question: Does the C11 (or newer) standard require to not define _MIN / _MAX macros if the corresponding floating type is not supported? 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). Is it the same for floating types?

UPD20211117:

  1. Invoking gcc w/o -pedantic causes the warning disappear. And _Float16 is supported. Great!
  2. Invoking clang w/o -pedantic does not cause the error disappear. Proabbly it is a bug.

Thanks to user n. 1.8e9-where's-my-share m. for the idea.

UPD20211118: gcc: with -pedantic the FLT16_MAX is defined, which is unexpected (or not?).


Solution

  • 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__).

    Extra: the same principle is used to determine the support of types from stdint.h: check if the associated MIN / MAX macros are defined.