Commit a4fad462 authored by Evan Nemerson's avatar Evan Nemerson

f16c: initial implementation

Arm implementations are currently disables since it is causing problems
on Drone.io, though I can't reproduce with qemu.  I'll need to try to
get it running on hardware.
parent 22d85b1c
...@@ -308,6 +308,9 @@ ...@@ -308,6 +308,9 @@
# if defined(__VPCLMULQDQ__) # if defined(__VPCLMULQDQ__)
# define SIMDE_ARCH_X86_VPCLMULQDQ 1 # define SIMDE_ARCH_X86_VPCLMULQDQ 1
# endif # endif
# if defined(__F16C__)
# define SIMDE_ARCH_X86_F16C 1
# endif
#endif #endif
/* Itanium /* Itanium
......
...@@ -678,6 +678,15 @@ typedef SIMDE_FLOAT64_TYPE simde_float64; ...@@ -678,6 +678,15 @@ typedef SIMDE_FLOAT64_TYPE simde_float64;
# include <fenv.h> # include <fenv.h>
#endif #endif
#define SIMDE_DEFINE_CONVERSION_FUNCTION_(Name, T_To, T_From) \
static HEDLEY_ALWAYS_INLINE HEDLEY_CONST \
T_To \
Name (T_From value) { \
T_To r; \
simde_memcpy(&r, &value, sizeof(r)); \
return r; \
}
#include "check.h" #include "check.h"
/* GCC/clang have a bunch of functionality in builtins which we would /* GCC/clang have a bunch of functionality in builtins which we would
......
/* SPDX-License-Identifier: MIT
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Copyright:
* 2021 Evan Nemerson <evan@nemerson.com>
*/
#include "hedley.h"
#include "simde-common.h"
#include "simde-detect-clang.h"
#if !defined(SIMDE_FLOAT16_H)
#define SIMDE_FLOAT16_H
/* Portable version which should work on pretty much any compiler.
* Obviously you can't rely on compiler support for things like
* conversion to/from 32-bit floats, so make sure you always use the
* functions and macros in this file!
*
* The portable implementations are (heavily) based on CC0 code by
* Fabian Giesen: <https://gist.github.com/rygorous/2156668> (see also
* <https://fgiesen.wordpress.com/2012/03/28/half-to-float-done-quic/>).
* I have basically just modified it to get rid of some UB (lots of
* aliasing, right shifting a negative value), use fixed-width types,
* and work in C. */
#define SIMDE_FLOAT16_API_PORTABLE 1
/* _Float16, per C standard (TS 18661-3;
* <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1945.pdf>). */
#define SIMDE_FLOAT16_API_FLOAT16 2
/* clang >= 6.0 supports __fp16 as an interchange format on all
* targets, but only allows you to use them for arguments and return
* values on targets which have defined an ABI. We get around the
* restriction by wrapping the __fp16 in a struct, but we can't do
* that on Arm since it would break compatibility with the NEON F16
* functions. */
#define SIMDE_FLOAT16_API_FP16_NO_ABI 3
/* This is basically __fp16 as specified by Arm, where arugments and
* return values are raw __fp16 values not structs. */
#define SIMDE_FLOAT16_API_FP16 4
/* Choosing an implementation. This is a bit rough, but I don't have
* any ideas on how to improve it. If you do, patches are definitely
* welcome. */
#if !defined(SIMDE_FLOAT16_API)
#if 0 && !defined(__cplusplus)
/* I haven't found a way to detect this. It seems like defining
* __STDC_WANT_IEC_60559_TYPES_EXT__, then including float.h, then
* checking for defined(FLT16_MAX) should work, but both gcc and
* clang will define the constants even if _Float16 is not
* supported. Ideas welcome. */
#define SIMDE_FLOAT16_API SIMDE_FLOAT16_API_FLOAT16
#elif defined(__ARM_FP16_FORMAT_IEEE)
#define SIMDE_FLOAT16_API SIMDE_FLOAT16_API_FP16
#elif defined(__clang__) && defined(__FLT16_MIN__)
#define SIMDE_FLOAT16_API SIMDE_FLOAT16_API_FP16_NO_ABI
#else
#define SIMDE_FLOAT16_API SIMDE_FLOAT16_API_PORTABLE
#endif
#endif
#if SIMDE_FLOAT16_API == SIMDE_FLOAT16_API_FLOAT16
typedef _Float16 simde_float16;
#define SIMDE_FLOAT16_C(value) value##f16
#elif SIMDE_FLOAT16_API == SIMDE_FLOAT16_API_FP16_NO_ABI
typedef struct { __fp16 value; } simde_float16;
#define SIMDE_FLOAT16_C(value) ((simde_float16) { HEDLEY_STATIC_CAST(__fp16, (value)) })
#elif SIMDE_FLOAT16_API == SIMDE_FLOAT16_API_FP16
typedef __fp16 simde_float16;
#define SIMDE_FLOAT16_C(value) HEDLEY_STATIC_CAST(__fp16, (value))
#elif SIMDE_FLOAT16_API == SIMDE_FLOAT16_API_PORTABLE
typedef struct { uint16_t value; } simde_float16;
#else
#error No 16-bit floating point API.
#endif
/* Reinterpret -- you *generally* shouldn't need these, they're really
* intended for internal use. However, on x86 half-precision floats
* get stuffed into a __m128i/__m256i, so it may be useful. */
SIMDE_DEFINE_CONVERSION_FUNCTION_(simde_float16_as_u16, uint16_t, simde_float16)
SIMDE_DEFINE_CONVERSION_FUNCTION_(simde_float16_reinterpret_u16, simde_float16, uint16_t)
SIMDE_DEFINE_CONVERSION_FUNCTION_(simde_float32_as_u32, uint32_t, simde_float32)
SIMDE_DEFINE_CONVERSION_FUNCTION_(simde_float32_reinterpret_u32, simde_float32, uint32_t)
/* Conversion -- convert between single-precision and half-precision
* floats. */
static HEDLEY_ALWAYS_INLINE HEDLEY_CONST
simde_float16
simde_float16_from_float32 (simde_float32 value) {
simde_float16 res;
#if \
(SIMDE_FLOAT16_API == SIMDE_FLOAT16_API_FLOAT16) || \
(SIMDE_FLOAT16_API == SIMDE_FLOAT16_API_FP16)
res = HEDLEY_STATIC_CAST(simde_float16, value);
#elif (SIMDE_FLOAT16_API == SIMDE_FLOAT16_API_FP16_NO_ABI)
res.value = HEDLEY_STATIC_CAST(__fp16, value);
#else
/* This code is CC0, based heavily on code by Fabian Giesen. */
uint32_t f32u = simde_float32_as_u32(value);
static const uint32_t f32u_infty = UINT32_C(255) << 23;
static const uint32_t f16u_max = (UINT32_C(127) + UINT32_C(16)) << 23;
static const uint32_t denorm_magic =
((UINT32_C(127) - UINT32_C(15)) + (UINT32_C(23) - UINT32_C(10)) + UINT32_C(1)) << 23;
uint16_t f16u;
uint32_t sign = f32u & (UINT32_C(1) << 31);
f32u ^= sign;
/* NOTE all the integer compares in this function cast the operands
* to signed values to help compilers vectorize to SSE2, which lacks
* unsigned comparison instructions. This is fine since all
* operands are below 0x80000000 (we clear the sign bit). */
if (f32u > f16u_max) { /* result is Inf or NaN (all exponent bits set) */
f16u = (f32u > f32u_infty) ? UINT32_C(0x7e00) : UINT32_C(0x7c00); /* NaN->qNaN and Inf->Inf */
} else { /* (De)normalized number or zero */
if (f32u < (UINT32_C(113) << 23)) { /* resulting FP16 is subnormal or zero */
/* use a magic value to align our 10 mantissa bits at the bottom of
* the float. as long as FP addition is round-to-nearest-even this
* just works. */
f32u = simde_float32_as_u32(simde_float32_reinterpret_u32(f32u) + simde_float32_reinterpret_u32(denorm_magic));
/* and one integer subtract of the bias later, we have our final float! */
f16u = HEDLEY_STATIC_CAST(uint16_t, f32u - denorm_magic);
} else {
uint32_t mant_odd = (f32u >> 13) & 1;
/* update exponent, rounding bias part 1 */
f32u += (HEDLEY_STATIC_CAST(uint32_t, 15 - 127) << 23) + UINT32_C(0xfff);
/* rounding bias part 2 */
f32u += mant_odd;
/* take the bits! */
f16u = HEDLEY_STATIC_CAST(uint16_t, f32u >> 13);
}
}
f16u |= sign >> 16;
res = simde_float16_reinterpret_u16(f16u);
#endif
return res;
}
static HEDLEY_ALWAYS_INLINE HEDLEY_CONST
simde_float32
simde_float16_to_float32 (simde_float16 value) {
simde_float32 res;
#if defined(SIMDE_FLOAT16_FLOAT16) || defined(SIMDE_FLOAT16_FP16)
res = HEDLEY_STATIC_CAST(simde_float32, value);
#else
/* This code is CC0, based heavily on code by Fabian Giesen. */
uint16_t half = simde_float16_as_u16(value);
const simde_float32 denorm_magic = simde_float32_reinterpret_u32((UINT32_C(113) << 23));
const uint32_t shifted_exp = UINT32_C(0x7c00) << 13; /* exponent mask after shift */
uint32_t f32u;
f32u = (half & UINT32_C(0x7fff)) << 13; /* exponent/mantissa bits */
uint32_t exp = shifted_exp & f32u; /* just the exponent */
f32u += (UINT32_C(127) - UINT32_C(15)) << 23; /* exponent adjust */
/* handle exponent special cases */
if (exp == shifted_exp) /* Inf/NaN? */
f32u += (UINT32_C(128) - UINT32_C(16)) << 23; /* extra exp adjust */
else if (exp == 0) { /* Zero/Denormal? */
f32u += (1) << 23; /* extra exp adjust */
f32u = simde_float32_as_u32(simde_float32_reinterpret_u32(f32u) - denorm_magic); /* renormalize */
}
f32u |= (half & UINT32_C(0x8000)) << 16; /* sign bit */
res = simde_float32_reinterpret_u32(f32u);
#endif
return res;
}
#if !defined(SIMDE_FLOAT16_C)
#define SIMDE_FLOAT16_C(value) simde_float16_from_float32(SIMDE_FLOAT32_C(value))
#endif
#endif /* !defined(SIMDE_FLOAT16_H) */
...@@ -217,6 +217,12 @@ ...@@ -217,6 +217,12 @@
#endif #endif
#endif #endif
#if !defined(SIMDE_X86_F16C_NATIVE) && !defined(SIMDE_X86_F16C_NO_NATIVE) && !defined(SIMDE_NO_NATIVE)
#if defined(SIMDE_ARCH_X86_F16C)
#define SIMDE_X86_F16C_NATIVE
#endif
#endif
#if !defined(SIMDE_X86_SVML_NATIVE) && !defined(SIMDE_X86_SVML_NO_NATIVE) && !defined(SIMDE_NO_NATIVE) #if !defined(SIMDE_X86_SVML_NATIVE) && !defined(SIMDE_X86_SVML_NO_NATIVE) && !defined(SIMDE_NO_NATIVE)
#if defined(__INTEL_COMPILER) #if defined(__INTEL_COMPILER)
#define SIMDE_X86_SVML_NATIVE #define SIMDE_X86_SVML_NATIVE
...@@ -484,6 +490,9 @@ ...@@ -484,6 +490,9 @@
#if !defined(SIMDE_X86_VPCLMULQDQ_NATIVE) #if !defined(SIMDE_X86_VPCLMULQDQ_NATIVE)
#define SIMDE_X86_VPCLMULQDQ_ENABLE_NATIVE_ALIASES #define SIMDE_X86_VPCLMULQDQ_ENABLE_NATIVE_ALIASES
#endif #endif
#if !defined(SIMDE_X86_F16C_NATIVE)
#define SIMDE_X86_F16C_ENABLE_NATIVE_ALIASES
#endif
#if !defined(SIMDE_ARM_NEON_A32V7_NATIVE) #if !defined(SIMDE_ARM_NEON_A32V7_NATIVE)
#define SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES #define SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES
......
...@@ -1192,6 +1192,29 @@ SIMDE_DISABLE_UNWANTED_DIAGNOSTICS ...@@ -1192,6 +1192,29 @@ SIMDE_DISABLE_UNWANTED_DIAGNOSTICS
#endif #endif
#endif #endif
/*** Comparison macros (which don't raise invalid errors) ***/
#if defined(isunordered)
#define simde_math_isunordered(x, y) isunordered(x, y)
#elif HEDLEY_HAS_BUILTIN(__builtin_isunordered)
#define simde_math_isunordered(x, y) __builtin_isunordered(x, y)
#else
static HEDLEY_INLINE
int simde_math_isunordered(double x, double y) {
return (x != y) && (x != x || y != y);
}
#define simde_math_isunordered simde_math_isunordered
static HEDLEY_INLINE
int simde_math_isunorderedf(float x, float y) {
return (x != y) && (x != x || y != y);
}
#define simde_math_isunorderedf simde_math_isunorderedf
#endif
#if !defined(simde_math_isunorderedf)
#define simde_math_isunorderedf simde_math_isunordered
#endif
/*** Additional functions not in libm ***/ /*** Additional functions not in libm ***/
#if defined(simde_math_fabs) && defined(simde_math_sqrt) && defined(simde_math_exp) #if defined(simde_math_fabs) && defined(simde_math_sqrt) && defined(simde_math_exp)
......
/* SPDX-License-Identifier: MIT
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Copyright:
* 2021 Evan Nemerson <evan@nemerson.com>
*/
#include "../simde-common.h"
#include "../simde-math.h"
#include "../simde-f16.h"
#if !defined(SIMDE_X86_F16C_H)
#define SIMDE_X86_F16C_H
#include "avx.h"
#if !defined(SIMDE_X86_PF16C_NATIVE) && defined(SIMDE_ENABLE_NATIVE_ALIASES)
# define SIMDE_X86_PF16C_ENABLE_NATIVE_ALIASES
#endif
HEDLEY_DIAGNOSTIC_PUSH
SIMDE_DISABLE_UNWANTED_DIAGNOSTICS
SIMDE_BEGIN_DECLS_
SIMDE_FUNCTION_ATTRIBUTES
simde__m128i
simde_mm_cvtps_ph(simde__m128 a, const int sae) {
#if defined(SIMDE_X86_F16C_NATIVE)
switch (sae & SIMDE_MM_FROUND_NO_EXC) {
case SIMDE_MM_FROUND_NO_EXC:
return _mm_cvtps_ph(a, SIMDE_MM_FROUND_NO_EXC);
default:
return _mm_cvtps_ph(a, 0);
}
#else
simde__m128_private a_ = simde__m128_to_private(a);
simde__m128i_private r_ = simde__m128i_to_private(simde_mm_setzero_si128());
HEDLEY_STATIC_CAST(void, sae);
#if defined(SIMDE_ARM_NEON_A32V7_NATIVE) && (__ARM_FP & 2) && 0
r_.neon_f16 = vcombine_f16(vcvt_f16_f32(a_.neon_f32), vdup_n_f16(SIMDE_FLOAT16_C(0.0)));
#else
SIMDE_VECTORIZE
for (size_t i = 0 ; i < (sizeof(a_.f32) / sizeof(a_.f32[0])) ; i++) {
r_.u16[i] = simde_float16_as_u16(simde_float16_from_float32(a_.f32[i]));
}
#endif
return simde__m128i_from_private(r_);
#endif
}
#if defined(SIMDE_X86_F16C_ENABLE_NATIVE_ALIASES)
#define _mm_cvtps_ph(a, sae) simde_mm_cvtps_ph(a, sae)
#endif
SIMDE_FUNCTION_ATTRIBUTES
simde__m128
simde_mm_cvtph_ps(simde__m128i a) {
#if defined(SIMDE_X86_F16C_NATIVE)
return _mm_cvtph_ps(a);
#else
simde__m128i_private a_ = simde__m128i_to_private(a);
simde__m128_private r_;
#if defined(SIMDE_ARM_NEON_A32V7_NATIVE) && (__ARM_FP & 2) && 0
r_.neon_f32 = vcvt_f32_f16(vget_low_f16(a_.neon_f16));
#else
SIMDE_VECTORIZE
for (size_t i = 0 ; i < (sizeof(a_.f32) / sizeof(a_.f32[0])) ; i++) {
r_.f32[i] = simde_float16_to_float32(simde_float16_reinterpret_u16(a_.u16[i]));
}
#endif
return simde__m128_from_private(r_);
#endif
}
#if defined(SIMDE_X86_F16C_ENABLE_NATIVE_ALIASES)
#define _mm_cvtph_ps(a) simde_mm_cvtph_ps(a)
#endif
SIMDE_FUNCTION_ATTRIBUTES
simde__m128i
simde_mm256_cvtps_ph(simde__m256 a, const int sae) {
#if defined(SIMDE_X86_F16C_NATIVE) && defined(SIMDE_X86_AVX_NATIVE)
switch (sae & SIMDE_MM_FROUND_NO_EXC) {
case SIMDE_MM_FROUND_NO_EXC:
return _mm256_cvtps_ph(a, SIMDE_MM_FROUND_NO_EXC);
default:
return _mm256_cvtps_ph(a, 0);
}
#else
simde__m256_private a_ = simde__m256_to_private(a);
simde__m128i_private r_;
HEDLEY_STATIC_CAST(void, sae);
#if defined(SIMDE_X86_F16C_NATIVE)
return _mm_castps_si128(_mm_movelh_ps(
_mm_castsi128_ps(_mm_cvtps_ph(a_.m128[0], SIMDE_MM_FROUND_NO_EXC)),
_mm_castsi128_ps(_mm_cvtps_ph(a_.m128[1], SIMDE_MM_FROUND_NO_EXC))
));
#else
SIMDE_VECTORIZE
for (size_t i = 0 ; i < (sizeof(a_.f32) / sizeof(a_.f32[0])) ; i++) {
r_.u16[i] = simde_float16_as_u16(simde_float16_from_float32(a_.f32[i]));
}
#endif
return simde__m128i_from_private(r_);
#endif
}
#if defined(SIMDE_X86_F16C_ENABLE_NATIVE_ALIASES)
#define _mm256_cvtps_ph(a, sae) simde_mm256_cvtps_ph(a, sae)
#endif
SIMDE_FUNCTION_ATTRIBUTES
simde__m256
simde_mm256_cvtph_ps(simde__m128i a) {
#if defined(SIMDE_X86_F16C_NATIVE) && defined(SIMDE_X86_AVX_NATIVE)
return _mm256_cvtph_ps(a);
#elif defined(SIMDE_X86_F16C_NATIVE)
return _mm256_setr_m128(
_mm_cvtph_ps(a),
_mm_cvtph_ps(_mm_castps_si128(_mm_permute_ps(_mm_castsi128_ps(a), 0xee)))
);
#else
simde__m128i_private a_ = simde__m128i_to_private(a);
simde__m256_private r_;
SIMDE_VECTORIZE
for (size_t i = 0 ; i < (sizeof(r_.f32) / sizeof(r_.f32[0])) ; i++) {
r_.f32[i] = simde_float16_to_float32(simde_float16_reinterpret_u16(a_.u16[i]));
}
return simde__m256_from_private(r_);
#endif
}
#if defined(SIMDE_X86_F16C_ENABLE_NATIVE_ALIASES)
#define _mm256_cvtph_ps(a) simde_mm256_cvtph_ps(a)
#endif
SIMDE_END_DECLS_
HEDLEY_DIAGNOSTIC_POP
#endif /* !defined(SIMDE_X86_F16C_H) */
...@@ -91,6 +91,9 @@ typedef union { ...@@ -91,6 +91,9 @@ typedef union {
SIMDE_ALIGN_TO_16 uint16x8_t neon_u16; SIMDE_ALIGN_TO_16 uint16x8_t neon_u16;
SIMDE_ALIGN_TO_16 uint32x4_t neon_u32; SIMDE_ALIGN_TO_16 uint32x4_t neon_u32;
SIMDE_ALIGN_TO_16 uint64x2_t neon_u64; SIMDE_ALIGN_TO_16 uint64x2_t neon_u64;
#if defined(__ARM_FP16_FORMAT_IEEE)
SIMDE_ALIGN_TO_16 float16x8_t neon_f16;
#endif
SIMDE_ALIGN_TO_16 float32x4_t neon_f32; SIMDE_ALIGN_TO_16 float32x4_t neon_f32;
#if defined(SIMDE_ARCH_AARCH64) #if defined(SIMDE_ARCH_AARCH64)
SIMDE_ALIGN_TO_16 float64x2_t neon_f64; SIMDE_ALIGN_TO_16 float64x2_t neon_f64;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment