| 1 |
/* SPDX-License-Identifier: MIT |
| 2 |
* |
| 3 |
* Permission is hereby granted, free of charge, to any person |
| 4 |
* obtaining a copy of this software and associated documentation |
| 5 |
* files (the "Software"), to deal in the Software without |
| 6 |
* restriction, including without limitation the rights to use, copy, |
| 7 |
* modify, merge, publish, distribute, sublicense, and/or sell copies |
| 8 |
* of the Software, and to permit persons to whom the Software is |
| 9 |
* furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice shall be |
| 12 |
* included in all copies or substantial portions of the Software. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 15 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 17 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 18 |
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 19 |
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 20 |
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 |
* SOFTWARE. |
| 22 |
* |
| 23 |
* Copyright: |
| 24 |
* 2021 Evan Nemerson <[email protected]> |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "hedley.h" |
| 28 |
#include "simde-common.h" |
| 29 |
#include "simde-detect-clang.h" |
| 30 |
|
| 31 |
#if !defined(SIMDE_FLOAT16_H) |
| 32 |
#define SIMDE_FLOAT16_H |
| 33 |
|
| 34 |
HEDLEY_DIAGNOSTIC_PUSH |
| 35 |
SIMDE_DISABLE_UNWANTED_DIAGNOSTICS |
| 36 |
SIMDE_BEGIN_DECLS_ |
| 37 |
|
| 38 |
/* Portable version which should work on pretty much any compiler. |
| 39 |
* Obviously you can't rely on compiler support for things like |
| 40 |
* conversion to/from 32-bit floats, so make sure you always use the |
| 41 |
* functions and macros in this file! |
| 42 |
* |
| 43 |
* The portable implementations are (heavily) based on CC0 code by |
| 44 |
* Fabian Giesen: <https://gist.github.com/rygorous/2156668> (see also |
| 45 |
* <https://fgiesen.wordpress.com/2012/03/28/half-to-float-done-quic/>). |
| 46 |
* I have basically just modified it to get rid of some UB (lots of |
| 47 |
* aliasing, right shifting a negative value), use fixed-width types, |
| 48 |
* and work in C. */ |
| 49 |
#define SIMDE_FLOAT16_API_PORTABLE 1 |
| 50 |
/* _Float16, per C standard (TS 18661-3; |
| 51 |
* <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1945.pdf>). */ |
| 52 |
#define SIMDE_FLOAT16_API_FLOAT16 2 |
| 53 |
/* clang >= 6.0 supports __fp16 as an interchange format on all |
| 54 |
* targets, but only allows you to use them for arguments and return |
| 55 |
* values on targets which have defined an ABI. We get around the |
| 56 |
* restriction by wrapping the __fp16 in a struct, but we can't do |
| 57 |
* that on Arm since it would break compatibility with the NEON F16 |
| 58 |
* functions. */ |
| 59 |
#define SIMDE_FLOAT16_API_FP16_NO_ABI 3 |
| 60 |
/* This is basically __fp16 as specified by Arm, where arugments and |
| 61 |
* return values are raw __fp16 values not structs. */ |
| 62 |
#define SIMDE_FLOAT16_API_FP16 4 |
| 63 |
|
| 64 |
/* Choosing an implementation. This is a bit rough, but I don't have |
| 65 |
* any ideas on how to improve it. If you do, patches are definitely |
| 66 |
* welcome. */ |
| 67 |
#if !defined(SIMDE_FLOAT16_API) |
| 68 |
#if 0 && !defined(__cplusplus) |
| 69 |
/* I haven't found a way to detect this. It seems like defining |
| 70 |
* __STDC_WANT_IEC_60559_TYPES_EXT__, then including float.h, then |
| 71 |
* checking for defined(FLT16_MAX) should work, but both gcc and |
| 72 |
* clang will define the constants even if _Float16 is not |
| 73 |
* supported. Ideas welcome. */ |
| 74 |
#define SIMDE_FLOAT16_API SIMDE_FLOAT16_API_FLOAT16 |
| 75 |
#elif defined(__ARM_FP16_FORMAT_IEEE) && defined(SIMDE_ARM_NEON_FP16) |
| 76 |
#define SIMDE_FLOAT16_API SIMDE_FLOAT16_API_FP16 |
| 77 |
#elif defined(__FLT16_MIN__) && (defined(__clang__) && (!defined(SIMDE_ARCH_AARCH64) || SIMDE_DETECT_CLANG_VERSION_CHECK(7,0,0))) |
| 78 |
#define SIMDE_FLOAT16_API SIMDE_FLOAT16_API_FP16_NO_ABI |
| 79 |
#else |
| 80 |
#define SIMDE_FLOAT16_API SIMDE_FLOAT16_API_PORTABLE |
| 81 |
#endif |
| 82 |
#endif |
| 83 |
|
| 84 |
#if SIMDE_FLOAT16_API == SIMDE_FLOAT16_API_FLOAT16 |
| 85 |
typedef _Float16 simde_float16; |
| 86 |
#define SIMDE_FLOAT16_C(value) value##f16 |
| 87 |
#elif SIMDE_FLOAT16_API == SIMDE_FLOAT16_API_FP16_NO_ABI |
| 88 |
typedef struct { __fp16 value; } simde_float16; |
| 89 |
#if defined(SIMDE_STATEMENT_EXPR_) |
| 90 |
#define SIMDE_FLOAT16_C(value) (__extension__({ ((simde_float16) { HEDLEY_DIAGNOSTIC_PUSH SIMDE_DIAGNOSTIC_DISABLE_C99_EXTENSIONS_ HEDLEY_STATIC_CAST(__fp16, (value)) }); HEDLEY_DIAGNOSTIC_POP })) |
| 91 |
#else |
| 92 |
#define SIMDE_FLOAT16_C(value) ((simde_float16) { HEDLEY_STATIC_CAST(__fp16, (value)) }) |
| 93 |
#endif |
| 94 |
#elif SIMDE_FLOAT16_API == SIMDE_FLOAT16_API_FP16 |
| 95 |
typedef __fp16 simde_float16; |
| 96 |
#define SIMDE_FLOAT16_C(value) HEDLEY_STATIC_CAST(__fp16, (value)) |
| 97 |
#elif SIMDE_FLOAT16_API == SIMDE_FLOAT16_API_PORTABLE |
| 98 |
typedef struct { uint16_t value; } simde_float16; |
| 99 |
#else |
| 100 |
#error No 16-bit floating point API. |
| 101 |
#endif |
| 102 |
|
| 103 |
#if \ |
| 104 |
defined(SIMDE_VECTOR_OPS) && \ |
| 105 |
(SIMDE_FLOAT16_API != SIMDE_FLOAT16_API_PORTABLE) && \ |
| 106 |
(SIMDE_FLOAT16_API != SIMDE_FLOAT16_API_FP16_NO_ABI) |
| 107 |
#define SIMDE_FLOAT16_VECTOR |
| 108 |
#endif |
| 109 |
|
| 110 |
/* Reinterpret -- you *generally* shouldn't need these, they're really |
| 111 |
* intended for internal use. However, on x86 half-precision floats |
| 112 |
* get stuffed into a __m128i/__m256i, so it may be useful. */ |
| 113 |
|
| 114 |
SIMDE_DEFINE_CONVERSION_FUNCTION_(simde_float16_as_uint16, uint16_t, simde_float16) |
| 115 |
SIMDE_DEFINE_CONVERSION_FUNCTION_(simde_uint16_as_float16, simde_float16, uint16_t) |
| 116 |
|
| 117 |
#define SIMDE_NANHF simde_uint16_as_float16(0x7E00) |
| 118 |
#define SIMDE_INFINITYHF simde_uint16_as_float16(0x7C00) |
| 119 |
|
| 120 |
/* Conversion -- convert between single-precision and half-precision |
| 121 |
* floats. */ |
| 122 |
|
| 123 |
static HEDLEY_ALWAYS_INLINE HEDLEY_CONST |
| 124 |
simde_float16 |
| 125 |
simde_float16_from_float32 (simde_float32 value) { |
| 126 |
simde_float16 res; |
| 127 |
|
| 128 |
#if \ |
| 129 |
(SIMDE_FLOAT16_API == SIMDE_FLOAT16_API_FLOAT16) || \ |
| 130 |
(SIMDE_FLOAT16_API == SIMDE_FLOAT16_API_FP16) |
| 131 |
res = HEDLEY_STATIC_CAST(simde_float16, value); |
| 132 |
#elif (SIMDE_FLOAT16_API == SIMDE_FLOAT16_API_FP16_NO_ABI) |
| 133 |
res.value = HEDLEY_STATIC_CAST(__fp16, value); |
| 134 |
#else |
| 135 |
/* This code is CC0, based heavily on code by Fabian Giesen. */ |
| 136 |
uint32_t f32u = simde_float32_as_uint32(value); |
| 137 |
static const uint32_t f32u_infty = UINT32_C(255) << 23; |
| 138 |
static const uint32_t f16u_max = (UINT32_C(127) + UINT32_C(16)) << 23; |
| 139 |
static const uint32_t denorm_magic = |
| 140 |
((UINT32_C(127) - UINT32_C(15)) + (UINT32_C(23) - UINT32_C(10)) + UINT32_C(1)) << 23; |
| 141 |
uint16_t f16u; |
| 142 |
|
| 143 |
uint32_t sign = f32u & (UINT32_C(1) << 31); |
| 144 |
f32u ^= sign; |
| 145 |
|
| 146 |
/* NOTE all the integer compares in this function cast the operands |
| 147 |
* to signed values to help compilers vectorize to SSE2, which lacks |
| 148 |
* unsigned comparison instructions. This is fine since all |
| 149 |
* operands are below 0x80000000 (we clear the sign bit). */ |
| 150 |
|
| 151 |
if (f32u > f16u_max) { /* result is Inf or NaN (all exponent bits set) */ |
| 152 |
f16u = (f32u > f32u_infty) ? UINT32_C(0x7e00) : UINT32_C(0x7c00); /* NaN->qNaN and Inf->Inf */ |
| 153 |
} else { /* (De)normalized number or zero */ |
| 154 |
if (f32u < (UINT32_C(113) << 23)) { /* resulting FP16 is subnormal or zero */ |
| 155 |
/* use a magic value to align our 10 mantissa bits at the bottom of |
| 156 |
* the float. as long as FP addition is round-to-nearest-even this |
| 157 |
* just works. */ |
| 158 |
f32u = simde_float32_as_uint32(simde_uint32_as_float32(f32u) + simde_uint32_as_float32(denorm_magic)); |
| 159 |
|
| 160 |
/* and one integer subtract of the bias later, we have our final float! */ |
| 161 |
f16u = HEDLEY_STATIC_CAST(uint16_t, f32u - denorm_magic); |
| 162 |
} else { |
| 163 |
uint32_t mant_odd = (f32u >> 13) & 1; |
| 164 |
|
| 165 |
/* update exponent, rounding bias part 1 */ |
| 166 |
f32u += (HEDLEY_STATIC_CAST(uint32_t, 15 - 127) << 23) + UINT32_C(0xfff); |
| 167 |
/* rounding bias part 2 */ |
| 168 |
f32u += mant_odd; |
| 169 |
/* take the bits! */ |
| 170 |
f16u = HEDLEY_STATIC_CAST(uint16_t, f32u >> 13); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
f16u |= sign >> 16; |
| 175 |
res = simde_uint16_as_float16(f16u); |
| 176 |
#endif |
| 177 |
|
| 178 |
return res; |
| 179 |
} |
| 180 |
|
| 181 |
static HEDLEY_ALWAYS_INLINE HEDLEY_CONST |
| 182 |
simde_float32 |
| 183 |
simde_float16_to_float32 (simde_float16 value) { |
| 184 |
simde_float32 res; |
| 185 |
|
| 186 |
#if defined(SIMDE_FLOAT16_FLOAT16) || defined(SIMDE_FLOAT16_FP16) |
| 187 |
res = HEDLEY_STATIC_CAST(simde_float32, value); |
| 188 |
#else |
| 189 |
/* This code is CC0, based heavily on code by Fabian Giesen. */ |
| 190 |
uint16_t half = simde_float16_as_uint16(value); |
| 191 |
const simde_float32 denorm_magic = simde_uint32_as_float32((UINT32_C(113) << 23)); |
| 192 |
const uint32_t shifted_exp = UINT32_C(0x7c00) << 13; /* exponent mask after shift */ |
| 193 |
uint32_t f32u; |
| 194 |
|
| 195 |
f32u = (half & UINT32_C(0x7fff)) << 13; /* exponent/mantissa bits */ |
| 196 |
uint32_t exp = shifted_exp & f32u; /* just the exponent */ |
| 197 |
f32u += (UINT32_C(127) - UINT32_C(15)) << 23; /* exponent adjust */ |
| 198 |
|
| 199 |
/* handle exponent special cases */ |
| 200 |
if (exp == shifted_exp) /* Inf/NaN? */ |
| 201 |
f32u += (UINT32_C(128) - UINT32_C(16)) << 23; /* extra exp adjust */ |
| 202 |
else if (exp == 0) { /* Zero/Denormal? */ |
| 203 |
f32u += (1) << 23; /* extra exp adjust */ |
| 204 |
f32u = simde_float32_as_uint32(simde_uint32_as_float32(f32u) - denorm_magic); /* renormalize */ |
| 205 |
} |
| 206 |
|
| 207 |
f32u |= (half & UINT32_C(0x8000)) << 16; /* sign bit */ |
| 208 |
res = simde_uint32_as_float32(f32u); |
| 209 |
#endif |
| 210 |
|
| 211 |
return res; |
| 212 |
} |
| 213 |
|
| 214 |
#ifdef SIMDE_FLOAT16_C |
| 215 |
#define SIMDE_FLOAT16_VALUE(value) SIMDE_FLOAT16_C(value) |
| 216 |
#else |
| 217 |
#define SIMDE_FLOAT16_VALUE(value) simde_float16_from_float32(SIMDE_FLOAT32_C(value)) |
| 218 |
#endif |
| 219 |
|
| 220 |
SIMDE_END_DECLS_ |
| 221 |
HEDLEY_DIAGNOSTIC_POP |
| 222 |
|
| 223 |
#endif /* !defined(SIMDE_FLOAT16_H) */ |