#include <stdint.h>
#include <fenv.h>
#ifdef CORE_MATH_SUPPORT_ERRNO
#include <errno.h>
#endif
#if (defined(__clang__) && __clang_major__ >= 14) || (defined(__GNUC__) && __GNUC__ >= 14 && __BITINT_MAXWIDTH__ && __BITINT_MAXWIDTH__ >= 128)
typedef unsigned _BitInt(128) u128;
#else
typedef unsigned __int128 u128;
#endif
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#endif
#pragma STDC FENV_ACCESS ON
typedef union {long double f; struct {uint64_t m; uint16_t e;};} b80u80_t;
inline static int
is_nan (b80u80_t s)
{
int e = s.e;
uint64_t m = s.m;
return (e & 0x7fff) == 0x7fff && (m << 1) != 0;
}
inline static int
is_snan (b80u80_t s)
{
int e = s.e;
uint64_t m = s.m;
m = m << 1; return (e & 0x7fff) == 0x7fff && m != 0 && (m >> 63) == 0;
}
long double
cr_hypotl (long double x, long double y)
{
fexcept_t flag;
fegetexceptflag (&flag, FE_INEXACT);
b80u80_t sx = {.f = x}, sy = {.f = y};
int x_exp = (sx.e & 0x7fff) - 0x3fff;
int y_exp = (sy.e & 0x7fff) - 0x3fff;
if (x_exp - y_exp - (sx.m < sy.m) < 0) {
uint64_t m = sx.m;
sx.m = sy.m;
sy.m = m;
int e = sx.e;
sx.e = sy.e;
sy.e = e;
int t = x_exp;
x_exp = y_exp;
y_exp = t;
}
if (__builtin_expect (x_exp == 0x4000, 0)) {
if (is_snan (sx) || is_snan (sy))
return sx.f + sy.f;
if (is_nan (sx) || is_nan (sy)) {
if (is_nan (sx) && is_nan (sy)) return sx.f + sy.f;
if (x_exp != 0x4000 || y_exp != 0x4000) return sx.f + sy.f;
}
return (sx.m == 0x8000000000000000ull) ? sx.f * sx.f : sy.f * sy.f;
}
uint64_t mx = sx.m, my = sy.m;
if (__builtin_expect (y_exp == -0x3fff, 0)) { if (__builtin_expect (my == 0, 0)) {
if (x_exp == -0x3fff && mx == 0){
return +0.0L;
}
else {
sx.e &= 0x7fff; return sx.f; }
}
int k = __builtin_clzll (my);
my <<= k;
y_exp -= k - 1;
if (x_exp == -0x3fff) {
k = __builtin_clzll (sx.m); mx <<= k;
x_exp -= k - 1;
}
}
int d = x_exp - y_exp;
if (d >= 32) { double z = 1.0;
if (d == 32) {
u128 yy = (u128) my * (u128) my;
uint64_t h = yy >> 64, l = yy, m = mx;
#define ONE_FOURTH 0x4000000000000000ull
if (x_exp >= -16382 && (h > m || (h == m && l > ONE_FOURTH)))
z = 0x1.0000000000001p+0; }
sx.e &= 0x7fff; if (z + 0x1p-53 > z) {
sx.m ++;
if (__builtin_expect (sx.m == 0, 0)) {
sx.e ++;
sx.m = 0x8000000000000000ull;
feraiseexcept (FE_OVERFLOW);
#ifdef CORE_MATH_SUPPORT_ERRNO
errno = ERANGE; #endif
}
}
if (sx.e == 0) {
feraiseexcept (FE_UNDERFLOW);
#ifdef CORE_MATH_SUPPORT_ERRNO
errno = ERANGE; #endif
}
return sx.f;
}
int dd = d + d; u128 xx = (u128) mx * (u128) mx;
u128 yy = (u128) my * (u128) my;
u128 hh = xx + (yy >> dd);
u128 ll = (dd > 0) ? yy << (128 - dd) : 0;
if (hh < xx) { ll = (hh << 126) | (ll >> 2);
hh = (((u128) 1) << 126) | (hh >> 2);
x_exp ++;
}
if (__builtin_expect (x_exp >= 0x3fff, 0)) { #define HUGE 0x1.fffffffffffffffep+16383L
if (x_exp >= 0x4000) { // sure overflow
#ifdef CORE_MATH_SUPPORT_ERRNO
errno = ERANGE; // overflow
#endif
return HUGE + HUGE;
}
// now x_exp = 0x3fff
u128 HT, LT;
switch (fegetround ()) {
case FE_TONEAREST: // threshold is (2^64-1/2)^2
HT = ((u128) 0xffffffffffffffffull) << 64;
LT = (u128) 1 << 126;
break;
case FE_UPWARD: // threshold is (2^64-1)^2
HT = (((u128) 0xfffffffffffffffeull) << 64) + 1;
LT = 0;
break;
default: // FE_DOWNWARD and FE_TOWARDZERO: threshold is (2^64)^2
HT = LT = ~ (u128) 0;
// since the low 64 bits of ll are zero, we can't have hh=HT and ll=LT
}
// we have overflow iff hh*2^128+ll >= HT*2^128+LT
if (hh > HT || (hh == HT && ll >= LT)) {
long double res = HUGE + 0x1p+16319L; // add 1/2 ulp(HUGE)
#ifdef CORE_MATH_SUPPORT_ERRNO
/* We have overflow:
* for RNDZ or RNDD: never
* for RNDN: when res = +Inf
* for RNDU: always */
if (res > 0x1.fffffffffffffffep+16383L)
errno = ERANGE; // overflow
#endif
return res;
}
}
// now sqrt(x^2 + y^2) < 2^16384*(1-2^-65)
// use sqrtl to compute a 64-bit approximation of sqrt(hh)
b80u80_t z;
int high = hh >> 127;
z.m = hh >> (63 + high); // upper 64 bits from hh
z.e = 16509 + high; // ensure 2^126 <= z < 2^128
z.f = __builtin_sqrtl (z.f);
u128 th = z.m;
/* Warning: for RNDU, z.f might be rounded to 2^64, in which case
z.e = 16447 instead of 16446 as expected. */
if (__builtin_expect (z.e > 16446, 0))
// we should have z.e = 16447 and th = 2^63
th = th << 1;
/* sqrt(x^2+y^2) ~ th * 2^(x_exp - 63) with 2^63 <= th < 2^64
thus 2^x_exp <= sqrt(x^2+y^2) < 2^(x_exp + 1)
and since the smallest normal is 2^-16382,
we are in the subnormal case when x_exp < -16382 = -0x3ffe */
if (__builtin_expect (x_exp < -0x3ffe, 0)) // subnormal case
{
int k = -0x3ffe - x_exp; // we should have k < 64
// shift right hh and ll by 2k bits, and th by k bits
th >>= k;
ll = (hh << (128 - 2 * k)) | (ll >> (2 * k));
hh = hh >> (2 * k);
x_exp += k - 1; // -1 due to exponent shift for subnormals
}
// compute r = hh - th^2
u128 r = hh - th * th;
if ((r >> 127) != 0) // th too large
{
// th -> th-1 thus r -> r + 2*th - 1
r += 2 * th - 1;
th --;
}
// if th was too large, then r < 2th+1 now, thus using "else" is ok
else if (r >= 2 * th + 1) // th too small
{
// th -> th+1, thus r -> r - 2*th - 1
r -= 2 * th + 1;
th ++;
}
// invariant: r = hh - th^2
int exact = 0;
if (__builtin_expect (r == 0 && ll == 0, 0)) // exact case
{
// restore the inexact flag
fesetexceptflag (&flag, FE_INEXACT);
exact = 1;
}
double eps = 0x1p-53;
const u128 thres = (u128) 1 << 126;
if (r > th || (r == th && (ll > thres || (ll == thres && (th & 1)))))
eps = 0x1.8p-53;
b80u80_t res = {.m = th, .e = x_exp + 0x3fff};
int underflow = res.e == 0;
if (!exact && 1.0 + eps > 1.0) {
res.m ++;
if (__builtin_expect (res.e == 0 && th == 0x7fffffffffffffffull, 0)) {
if (1.0 + 0x1p-53 == 1.0) { const u128 thres_r = 0xbfffffffffffffffull;
const u128 thres_ll = (u128) 1 << 112;
if (r > thres_r || (r == thres_r && ll >= thres_ll))
underflow = 0;
}
if (1.0 + 0x1p-53 > 1.0)
if (eps == 0x1.8p-53)
underflow = 0;
}
if (res.m == 0) {
res.e ++;
res.m = (uint64_t) 1 << 63;
}
}
if (underflow && !exact) {
feraiseexcept (FE_UNDERFLOW);
#ifdef CORE_MATH_SUPPORT_ERRNO
errno = ERANGE; #endif
}
return res.f;
}