#include <stdint.h>
#ifdef CORE_MATH_SUPPORT_ERRNO
#include <errno.h>
#endif
#include <fenv.h>
#include <math.h>
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#endif
#pragma STDC FENV_ACCESS ON
typedef union {_Float16 f; uint16_t u;} b16u16_u;
typedef union {double f; uint64_t u;} b64u64_u;
static inline int
is_qnan (uint16_t u)
{
return 0x7e00 <= (u & 0x7fff);
}
static inline int
is_inf (uint16_t u)
{
return u == 0x7c00 || u == 0xfc00;
}
_Float16 cr_hypotf16(_Float16 x, _Float16 y){
b16u16_u sx = {.f = x}, sy = {.f = y};
b64u64_u tx = {.f = x}, ty = {.f = y};
double ret = __builtin_sqrt (tx.f * tx.f + ty.f * ty.f);
if (is_inf (sx.u) && is_qnan (sy.u)) return tx.f * tx.f;
if (is_inf (sy.u) && is_qnan (sx.u)) return ty.f * ty.f;
#ifdef CORE_MATH_SUPPORT_ERRNO
int rnd = fegetround();
if (rnd == FE_TONEAREST) { if (ret < 0x1.ffep-15 && (double) (_Float16) ret != ret) errno = ERANGE;
if (ret >= 0x1.ffep+15)
errno = ERANGE;
}
if (rnd == FE_TOWARDZERO || rnd == FE_DOWNWARD) { if (ret < 0x1p-14 && (double) (_Float16) ret != ret) errno = ERANGE;
if (ret > 0x1p+16)
errno = ERANGE;
}
if (rnd == FE_UPWARD) { if (ret < 0x1.ffcp-15 && (double) (_Float16) ret != ret) errno = ERANGE;
if (ret > 0x1.ffcp+15)
errno = ERANGE;
}
#endif
return ret;
}
_Float16 hypotf16 (_Float16 x, _Float16 y) {
return (_Float16) hypotf ((float) x, (float) y);
}