#include <stdint.h>
#ifdef CORE_MATH_SUPPORT_ERRNO
#include <errno.h>
#endif
#include <math.h>
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#endif
#pragma STDC FENV_ACCESS ON
typedef union {double f; uint64_t u;} b64u64_u;
typedef union {__bf16 f; uint16_t u;} b16u16_u;
static inline uint16_t
asuint (__bf16 f)
{
b16u16_u u = {.f = f};
return u.u;
}
static inline int
is_inf (uint16_t u)
{
return u == 0x7f80 || u == 0xff80;
}
static inline int
is_qnan (uint16_t u)
{
return 0x7fc0 <= (u & 0x7fff);
}
__bf16 cr_hypot_bf16(__bf16 x, __bf16 y){
b16u16_u sx = {.f = x}, sy = {.f = y};
b64u64_u tx = {.f = x}, ty = {.f = y};
double z = __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;
__bf16 ret = z;
#ifdef CORE_MATH_SUPPORT_ERRNO
if (z != 0 && (double) ret != z && z < 0x1p-126) {
int t = 1.0f + 0x1p-24f > 1.0f; // true for rndu only
int u = 1.0f - 0x1p-25f < 1.0f; // true for rndz/rndd only
if (u || (z < 0x1.ffp-127 && !t) || (z <= 0x1.fep-127 && t))
errno = ERANGE; }
if (z > 0x1.fep+127 && z < 0x1p129) {
int t = 1.0f + 0x1p-24f > 1.0f; // true for rndu only
int u = 1.0f - 0x1p-25f < 1.0f; // true for rndz/rndd only
if (t || (z >= 0x1.ffp+127 && !u) || (z >= 0x1p+128 && u))
errno = ERANGE; }
#endif
return ret;
}
__bf16 hypot_bf16 (__bf16 x, __bf16 y) {
return (__bf16) hypotf ((float) x, (float) y);
}