diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 462d9aa7502a4319f12654bdf87b741acfc13991..375e9df15abd4a310753c006803b97acf1e2a801 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -50,6 +50,7 @@ list( APPEND cdi_src_files exse.h extra.h extralib.c + float16.h file.c file.h gaussian_latitudes.c diff --git a/src/Makefile.am b/src/Makefile.am index c124470cd86a9bd9a895c16198d55e1548954f1f..db8e2314f81c0158ed33025327a777575257b685 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -127,6 +127,7 @@ libcdi_la_SOURCES = \ exse.h \ extra.h \ extralib.c \ + float16.h \ file.c \ file.h \ gaussian_latitudes.c \ diff --git a/src/binary.c b/src/binary.c index 9b09fc44be782636ef2c08b5bc066df6a721e1da..dc459ff7be6cf2281661d29efb367f5e33997f35 100644 --- a/src/binary.c +++ b/src/binary.c @@ -170,9 +170,19 @@ binWriteFlt64(int fileID, int byteswap, size_t size, double *ptr) { if (byteswap) swap8byte(ptr, size); fileWrite(fileID, (void *) ptr, 8 * size); + return 0; +} +#ifdef HAVE_FLOAT16 +int +binWriteFlt16(int fileID, int byteswap, size_t size, _Float16 *ptr) +{ + if (byteswap) swap4byte(ptr, size); + fileWrite(fileID, (void *) ptr, 2 * size); return 0; } +#endif + /* * Local Variables: * c-file-style: "Java" diff --git a/src/binary.h b/src/binary.h index 0ed95069b4dcbc78fe7cf6384203b0d2a7e94484..cc4992cdb28c7d83d6b106e4b3e49ac5ed4e104f 100644 --- a/src/binary.h +++ b/src/binary.h @@ -3,6 +3,7 @@ #include <stdio.h> #include <inttypes.h> +#include "float16.h" #ifndef HOST_ENDIANNESS #ifdef __cplusplus @@ -30,6 +31,10 @@ int binWriteInt64(int fileID, int byteswap, size_t size, int64_t *ptr); int binWriteFlt32(int fileID, int byteswap, size_t size, float *ptr); int binWriteFlt64(int fileID, int byteswap, size_t size, double *ptr); +#ifdef HAVE_FLOAT16 +int binWriteFlt16(int fileID, int byteswap, size_t size, _Float16 *ptr); +#endif + #endif /* BINARY_H */ /* * Local Variables: diff --git a/src/float16.h b/src/float16.h new file mode 100644 index 0000000000000000000000000000000000000000..2e2287d292a6a47ab4eeb1f96ee93496253dfbcd --- /dev/null +++ b/src/float16.h @@ -0,0 +1,21 @@ +#ifndef FLOAT16_H +#define FLOAT16_H + +// CAUTION: __is_identifier behaves opposite how you would expect! +// '__is_identifier' returns '0' if '__x' is a reserved identifier provided by +// the compiler and '1' otherwise. +// borrowed from LLVM __config header under Apache license 2. +// (https://www.mend.io/blog/top-10-apache-license-questions-answered/) +#ifndef __is_identifier // Optional of course. +#define __is_identifier(x) 1 // Compatibility with non-clang compilers. +#endif + +// More sensible macro for keyword detection +#define __has_keyword(__x) !(__is_identifier(__x)) + +// map a half float type, if available, to _OptionalHalfFloatType +#if __has_keyword(_Float16) +#define HAVE_FLOAT16 +#endif + +#endif