Re: [PATCH] iio: tools: Use proper macro to detect _Float16 support

From: David Laight

Date: Mon Sep 28 2026 - 05:08:51 EST


On Fri, 18 Sep 2026 20:13:02 +0400
"Ivan A. Melnikov" <iv@xxxxxxxxxxxx> wrote:

> __FLT16_MAX__ is an internal compiler macro. GCC defines it if
> _Float16 is supported by the target in general, but the support
> may depend on the exact target options. For example, on i586
> without -msse2 I get the following error from GCC15:
>
> iio_generic_buffer.c: In function 'print2byte':
> iio_generic_buffer.c:141:17: error: invalid conversion from type '_Float16' without option '-msse2'
> 141 | printf("%05f ", ((float)converter.f + info->offset) * info->scale);
> | ^~~~~~
>
> To fix that, use the public macro without double underscores.

Assuming float and _Float16 are both IEEE 754 you can directly convert them.
If I've typed it correctly:
union {
uint32_t u;
float f;
} converter;
converter.u = (input & 0x8000) << 16 | (input & 0x7fff) << 13;
printf(... converter.f * 0x1p112 ...)

That handles denormalised values (near zero) but not infinity and NaN.

David

>
> Fixes: cdd445d4b2a9 ("iio: tools: Add support for floating-point types in buffer scan elements")
> Signed-off-by: Ivan A. Melnikov <iv@xxxxxxxxxxxx>
> ---
> tools/iio/iio_generic_buffer.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/iio/iio_generic_buffer.c b/tools/iio/iio_generic_buffer.c
> index 000193612aad..9ea93984e0e9 100644
> --- a/tools/iio/iio_generic_buffer.c
> +++ b/tools/iio/iio_generic_buffer.c
> @@ -131,7 +131,7 @@ static void print2byte(uint16_t input, struct iio_channel_info *info)
> printf("%05f ", ((float)input + info->offset) * info->scale);
> break;
> case 'f': {
> -#if defined(__FLT16_MAX__)
> +#if defined(FLT16_MAX)
> union {
> uint16_t u;
> _Float16 f;