Use _Float16 for half conversions if available

Implement support for half-precision floating-point conversions using
`_Float16` introduced in C11 extension ISO/IEC TS 18661-3.
This commit is contained in:
Kal Conley
2023-02-23 17:13:37 -08:00
committed by Thiago Macieira
parent 2455693393
commit 3cba6b11aa
11 changed files with 38 additions and 1 deletions
+1
View File
@@ -31,6 +31,7 @@
#ifndef __STDC_LIMIT_MACROS
# define __STDC_LIMIT_MACROS 1
#endif
#define __STDC_WANT_IEC_60559_TYPES_EXT__
#include "cbor.h"
#include "cborinternal_p.h"
@@ -27,6 +27,7 @@
#ifndef __STDC_LIMIT_MACROS
# define __STDC_LIMIT_MACROS 1
#endif
#define __STDC_WANT_IEC_60559_TYPES_EXT__
#include "cbor.h"
+1
View File
@@ -27,6 +27,7 @@
#ifndef __STDC_LIMIT_MACROS
# define __STDC_LIMIT_MACROS 1
#endif
#define __STDC_WANT_IEC_60559_TYPES_EXT__
#include "cbor.h"
+28 -1
View File
@@ -25,11 +25,19 @@
#ifndef CBORINTERNAL_P_H
#define CBORINTERNAL_P_H
/* Dependent source files (*.c) must define __STDC_WANT_IEC_60559_TYPES_EXT__
* before <float.h> is (transitively) first included.
*/
#if !defined(__STDC_WANT_IEC_60559_TYPES_EXT__)
# error __STDC_WANT_IEC_60559_TYPES_EXT__ not defined
#endif
#include "compilersupport_p.h"
#ifndef CBOR_NO_FLOATING_POINT
# include <float.h>
# include <math.h>
# include <string.h>
#else
# ifndef CBOR_NO_HALF_FLOAT_TYPE
# define CBOR_NO_HALF_FLOAT_TYPE 1
@@ -37,7 +45,26 @@
#endif
#ifndef CBOR_NO_HALF_FLOAT_TYPE
# if defined(__F16C__) || defined(__AVX2__)
/* Check for FLT16_MANT_DIG using integer comparison. Clang headers incorrectly
* define this macro unconditionally when __STDC_WANT_IEC_60559_TYPES_EXT__
* is defined (regardless of actual support for _Float16).
*/
# if FLT16_MANT_DIG > 0 || __FLT16_MANT_DIG__ > 0
static inline unsigned short encode_half(float x)
{
unsigned short h;
_Float16 f = (_Float16)x;
memcpy(&h, &f, 2);
return h;
}
static inline float decode_half(unsigned short x)
{
_Float16 f;
memcpy(&f, &x, 2);
return (float)f;
}
# elif defined(__F16C__) || defined(__AVX2__)
# include <immintrin.h>
static inline unsigned short encode_half(float val)
{
+1
View File
@@ -31,6 +31,7 @@
#ifndef __STDC_LIMIT_MACROS
# define __STDC_LIMIT_MACROS 1
#endif
#define __STDC_WANT_IEC_60559_TYPES_EXT__
#include "cbor.h"
#include "cborinternal_p.h"
+1
View File
@@ -31,6 +31,7 @@
#ifndef __STDC_LIMIT_MACROS
# define __STDC_LIMIT_MACROS 1
#endif
#define __STDC_WANT_IEC_60559_TYPES_EXT__
#include "cbor.h"
#include "compilersupport_p.h"
+1
View File
@@ -27,6 +27,7 @@
#ifndef __STDC_LIMIT_MACROS
# define __STDC_LIMIT_MACROS 1
#endif
#define __STDC_WANT_IEC_60559_TYPES_EXT__
#include "cbor.h"
+1
View File
@@ -27,6 +27,7 @@
#ifndef __STDC_LIMIT_MACROS
# define __STDC_LIMIT_MACROS 1
#endif
#define __STDC_WANT_IEC_60559_TYPES_EXT__
#include "cbor.h"
#include "cborinternal_p.h"
+1
View File
@@ -28,6 +28,7 @@
#ifndef __STDC_LIMIT_MACROS
# define __STDC_LIMIT_MACROS 1
#endif
#define __STDC_WANT_IEC_60559_TYPES_EXT__
#include "cbor.h"
#include "cborjson.h"
+1
View File
@@ -27,6 +27,7 @@
#ifndef __STDC_LIMIT_MACROS
# define __STDC_LIMIT_MACROS 1
#endif
#define __STDC_WANT_IEC_60559_TYPES_EXT__
#include "cbor.h"
#include "cborinternal_p.h"
+1
View File
@@ -23,6 +23,7 @@
****************************************************************************/
#define _GNU_SOURCE
#define __STDC_WANT_IEC_60559_TYPES_EXT__
#include "cbor.h"
#include "cborinternal_p.h"
#include "compilersupport_p.h"