#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#ifdef CORE_MATH_SUPPORT_ERRNO
#include <errno.h>
#endif
#include <math.h>
#include <fenv.h>
#ifdef __x86_64__
#include <x86intrin.h>
#define FLAG_T uint32_t
#else
#define FLAG_T fexcept_t
#endif
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#endif
#pragma STDC FENV_ACCESS ON
typedef union {float f; uint32_t u;} b32u32_u;
typedef union {double f; uint64_t u;} b64u64_u;
static FLAG_T
get_flag (void)
{
#if defined(__x86_64__)
return _mm_getcsr ();
#else
fexcept_t flag;
fegetexceptflag (&flag, FE_INEXACT);
return flag;
#endif
}
static void
set_flag (FLAG_T flag)
{
#ifdef __x86_64__
_mm_setcsr (flag);
#else
fesetexceptflag (&flag, FE_INEXACT);
#endif
}
static inline int is_signalingf(float x) {
b32u32_u u = {.f = x};
u.u ^= 0x00400000;
return (u.u & 0x7fffffff) > 0x7fc00000;
}
static inline int isint(float y){
b32u32_u wy = {.f = y};
int ey = ((wy.u>>23) & 0xff) - 127, s = ey + 9;
if(ey>=0){
if(s>=32) return 1;
return !(wy.u<<s);
}
if(!(wy.u<<1)) return 1;
return 0;
}
#if ((defined(__GNUC__) && __GNUC__ >= 10) || (defined(__clang__) && __clang_major__ >= 17)) && !defined(_MSC_VER) && (defined(__aarch64__) || defined(__x86_64__) || defined(__i386__))
# define roundeven_finite(x) __builtin_roundeven (x)
#else
static double
roundeven_finite (double x)
{
double ix;
# if (defined(__GNUC__) || defined(__clang__)) && (defined(__AVX__) || defined(__SSE4_1__) || (__ARM_ARCH >= 8))
# if defined __AVX__
__asm__("vroundsd $0x8,%1,%1,%0":"=x"(ix):"x"(x));
# elif __ARM_ARCH >= 8
__asm__ ("frintn %d0, %d1":"=w"(ix):"w"(x));
# else
__asm__("roundsd $0x8,%1,%0":"=x"(ix):"x"(x));
# endif
# else
ix = __builtin_round (x);
if (__builtin_fabs (ix - x) == 0.5)
{
union { double f; uint64_t n; } u, v;
u.f = ix;
v.f = ix - __builtin_copysign (1.0, x);
if (__builtin_ctz (v.n) > __builtin_ctz (u.n))
ix = v.f;
}
# endif
return ix;
}
#endif
static void
fast_two_sum (double *s, double *t, double a, double b)
{
*s = a + b;
double e = *s - a;
*t = b - e;
}
static inline void a_mul(double *hi, double *lo, double a, double b) {
*hi = a * b;
*lo = __builtin_fma (a, b, -*hi);
}
static inline void s_mul (double *hi, double *lo, double a, double bh,
double bl) {
a_mul (hi, lo, a, bh);
*lo = __builtin_fma (a, bl, *lo);
}
static inline void d_mul(double *hi, double *lo, double ah, double al,
double bh, double bl) {
double s, t;
a_mul(hi, &s, ah, bh);
t = __builtin_fma(al, bh, s);
*lo = __builtin_fma(ah, bl, t);
}
#define INVLOG2 0x1.71547652b82fep+0
#define LOG2 0x1.62e42fefa39efp-1
static double p1 (double z)
{
static const double P[] = {0, 0x1.71547652b82fep0, -0x1.71547652b8d11p-1,
0x1.ec709dc3a5014p-2, -0x1.715475b144983p-2, 0x1.2776c3fda300ep-2,
-0x1.ec990162358cep-3, 0x1.a645337c29e27p-3};
double z2 = z * z;
double c5 = __builtin_fma (P[6], z, P[5]);
double c3 = __builtin_fma (P[4], z, P[3]);
double c1 = __builtin_fma (P[2], z, P[1]);
double z4 = z2 * z2;
c5 = __builtin_fma (P[7], z2, c5);
c1 = __builtin_fma (c3, z2, c1);
c1 = __builtin_fma (c5, z4, c1);
return z * c1;
}
static void p2 (double *h, double *l, double zh, double zl)
{
static const double P2[] = {
0x1.71547652b82fep+0, 0x1.777d0ffda0d8p-56, -0x1.71547652b82fep-1, -0x1.777d0fd20b49cp-57, 0x1.ec709dc3a03fdp-2, 0x1.d27f05171b74ap-56, -0x1.71547652b82fep-2, -0x1.7814e70b828bp-58, 0x1.2776c50ef9bfep-2, 0x1.e4f63e12bff83p-56, -0x1.ec709dc3a03f4p-3, 0x1.a61762a7adeccp-3, -0x1.71547652d8849p-3, 0x1.484b13d7e7029p-3, -0x1.2776c1b2a40fdp-3, 0x1.0c9a80f9b7c1cp-3, -0x1.ecc68011212p-4, 0x1.c6e4b91fd10e5p-4, };
double t;
*h = P2[4+13]; for (int i = 12; i >= 7; i--)
*h = __builtin_fma (*h, zh, P2[4+i]);
s_mul (h, l, *h, zh, zl);
fast_two_sum (h, &t, P2[10], *h);
*l += t; d_mul (h, l, *h, *l, zh, zl);
fast_two_sum (h, &t, P2[8], *h);
*l += t + P2[9]; d_mul (h, l, *h, *l, zh, zl);
fast_two_sum (h, &t, P2[6], *h);
*l += t + P2[7]; d_mul (h, l, *h, *l, zh, zl);
fast_two_sum (h, &t, P2[4], *h);
*l += t + P2[5]; d_mul (h, l, *h, *l, zh, zl);
fast_two_sum (h, &t, P2[2], *h);
*l += t + P2[3]; d_mul (h, l, *h, *l, zh, zl);
fast_two_sum (h, &t, P2[0], *h);
*l += t + P2[1]; d_mul (h, l, *h, *l, zh, zl);
}
static double q1 (double z)
{
static const double Q[] = {1.0, 0x1.62e42fef6d01ap-1,
0x1.ebfbdff7feebap-3, 0x1.c6b167e579beep-5, 0x1.3b2b3428d06dep-7};
double z2 = z * z;
double c3 = __builtin_fma (Q[4], z, Q[3]);
double c0 = __builtin_fma (Q[1], z, Q[0]);
double c2 = __builtin_fma (c3 , z, Q[2]);
return __builtin_fma (c2, z2, c0);
}
static void q2 (double *qh, double *ql, double h, double l)
{
static const double Q2[] = {1.0, 0x1.62e42fefa39efp-1, 0x1.abc9d45534d06p-56, 0x1.ebfbdff82c58fp-3, -0x1.5e4383cf9ddf7p-57, 0x1.c6b08d704a0cp-5, -0x1.6cbc55586c8f1p-59, 0x1.3b2ab6fba4e77p-7, 0x1.5d87fe789aec5p-10, 0x1.430912f879daap-13, 0x1.ffcc774b2367ap-17, 0x1.62c017b9bdfe6p-20, };
double h2 = h * h;
double c7 = __builtin_fma (Q2[11], h, Q2[10]);
double c5 = __builtin_fma (Q2[9], h, Q2[8]);
c5 = __builtin_fma (c7, h2, c5);
*qh = c5 * h;
fast_two_sum (qh, ql, Q2[7], *qh);
double t;
d_mul (qh, ql, *qh, *ql, h, l);
fast_two_sum (qh, &t, Q2[5], *qh);
*ql += t + Q2[6];
d_mul (qh, ql, *qh, *ql, h, l);
fast_two_sum (qh, &t, Q2[3], *qh);
*ql += t + Q2[4];
d_mul (qh, ql, *qh, *ql, h, l);
fast_two_sum (qh, &t, Q2[1], *qh);
*ql += t + Q2[2];
d_mul (qh, ql, *qh, *ql, h, l);
fast_two_sum (qh, &t, Q2[0], *qh);
*ql += t;
}
static const double inv[] = {
0x1.68p+0, 0x1.6p+0, 0x1.58p+0, 0x1.5p+0, 0x1.4cp+0, 0x1.44p+0, 0x1.4p+0,
0x1.38p+0, 0x1.34p+0, 0x1.2cp+0, 0x1.28p+0, 0x1.2p+0, 0x1.1cp+0, 0x1.18p+0,
0x1.14p+0, 0x1.1p+0, 0x1.0cp+0, 0x1.08p+0, 0x1p+0, 0x1p+0, 0x1.f4p-1,
0x1.ecp-1, 0x1.e4p-1, 0x1.ep-1, 0x1.d8p-1, 0x1.dp-1, 0x1.cap-1, 0x1.c4p-1,
0x1.bep-1, 0x1.b8p-1, 0x1.b2p-1, 0x1.acp-1, 0x1.a8p-1, 0x1.a2p-1,
0x1.9cp-1, 0x1.98p-1, 0x1.92p-1, 0x1.8cp-1, 0x1.88p-1, 0x1.84p-1,
0x1.8p-1, 0x1.7cp-1, 0x1.76p-1, 0x1.72p-1, 0x1.6ep-1, 0x1.6ap-1,
};
static const double log2inv[][2] = {
{-0x1.f7a8568cb06cfp-2,0x1.8f3673ffdd785p-57},
{-0x1.d6753e032ea0fp-2,0x1.c141e66faaaadp-62},
{-0x1.b47ebf73882a1p-2,0x1.6fae441c09d76p-56},
{-0x1.91bba891f1709p-2,0x1.2d352bea51e59p-56},
{-0x1.800a563161c54p-2,-0x1.9575b04fa6fbdp-57},
{-0x1.5c01a39fbd688p-2,0x1.817fd3b7d7e5dp-56},
{-0x1.49a784bcd1b8bp-2,0x1.b6d40900b2502p-62},
{-0x1.24407ab0e073ap-2,0x1.f6e91ad16ecffp-56},
{-0x1.11307dad30b76p-2,0x1.a7b47d2c352d9p-57},
{-0x1.d49ee4c32597p-3,0x1.b85a54d7ee2fdp-58},
{-0x1.acf5e2db4ec94p-3,0x1.01ee1343fe7cap-59},
{-0x1.5c01a39fbd688p-3,0x1.817fd3b7d7e5dp-57},
{-0x1.32ae9e278ae1ap-3,-0x1.f51f2c075a74cp-59},
{-0x1.08c588cda79e4p-3,0x1.a7610e40bd6abp-57},
{-0x1.bc84240adabbap-4,-0x1.8ecb169b9465fp-58},
{-0x1.663f6fac91316p-4,-0x1.f3314e0985116p-58},
{-0x1.0eb389fa29f9bp-4,0x1.30c22d15199b8p-58},
{-0x1.6bad3758efd87p-5,-0x1.89b03784b5be1p-60},
{0x0p+0,0x0p+0},
{0x0p+0,0x0p+0},
{0x1.184b8e4c56af8p-5,0x1.491f06c085bc2p-60},
{0x1.d6ebd1f1febfep-5,0x1.155660710eb2ap-63},
{0x1.4c560fe68af88p-4,0x1.c141e66faaaadp-61},
{0x1.7d60496cfbb4cp-4,0x1.9ced1447e30adp-58},
{0x1.e0b1ae8f2fd56p-4,0x1.92ce9636c90ap-58},
{0x1.22dadc2ab3497p-3,-0x1.696e2866c718ep-58},
{0x1.494f863b8df35p-3,-0x1.1562d61af73f8p-57},
{0x1.7046031c79f85p-3,-0x1.0798d1aa21694p-57},
{0x1.97c1cb13c7ec1p-3,-0x1.e95734abd2fccp-57},
{0x1.bfc67a7fff4ccp-3,0x1.bc0af7b82e7d7p-61},
{0x1.e857d3d361368p-3,-0x1.086fce864a1f6p-57},
{0x1.08bce0d95fa38p-2,-0x1.3d56efe4338fep-58},
{0x1.169c05363f158p-2,0x1.c8d43e017579bp-56},
{0x1.2baa0c34be1ecp-2,-0x1.0132ae5e417cdp-58},
{0x1.4106017c3eca3p-2,-0x1.c658d602e66bp-56},
{0x1.4f6fbb2cec598p-2,0x1.e393a16b94b52p-56},
{0x1.6552b49986277p-2,0x1.ac9080333c605p-56},
{0x1.7b89f02cf2aadp-2,0x1.8f89e2eb553b2p-57},
{0x1.8a8980abfbd32p-2,0x1.99aa6df8b7d83p-56},
{0x1.99b072a96c6b2p-2,0x1.bca36fd02defp-56},
{0x1.a8ff971810a5ep-2,0x1.817fd3b7d7e5dp-58},
{0x1.b877c57b1b07p-2,-0x1.01d98c3531027p-58},
{0x1.cffae611ad12bp-2,0x1.8a38b4175d665p-56},
{0x1.dfdd89d586e2bp-2,0x1.38c8946414c6ap-59},
{0x1.efec61b011f85p-2,0x1.6d261f1753e0bp-56},
{0x1.0014332be0033p-1,-0x1.7398fe685f171p-55},
};
static double _log2p1 (double x)
{
double u = 1.0 + x;
b64u64_u v = {.f = u};
uint64_t m = v.u & 0xfffffffffffffull;
int64_t e = (v.u >> 52) - 0x3ff + (m >= 0x6a09e667f3bcdull);
v.u -= e * 0x10000000000000ll;
double t = v.f;
v.f += 2.0; int i = (v.u >> 45) - 0x2002d; double r = inv[i];
double z = __builtin_fma (r, t, -1.0); double p = p1 (z);
return (double) e + (log2inv[i][0] + p);
}
static const double exp2_T[] = {
-0x1p-1, -0x1.ep-2, -0x1.cp-2, -0x1.ap-2, -0x1.8p-2, -0x1.6p-2, -0x1.4p-2,
-0x1.2p-2, -0x1p-2, -0x1.cp-3, -0x1.8p-3, -0x1.4p-3, -0x1p-3, -0x1.8p-4,
-0x1p-4, -0x1p-5, 0x0p+0, 0x1p-5, 0x1p-4, 0x1.8p-4, 0x1p-3, 0x1.4p-3,
0x1.8p-3, 0x1.cp-3, 0x1p-2, 0x1.2p-2, 0x1.4p-2, 0x1.6p-2, 0x1.8p-2,
0x1.ap-2, 0x1.cp-2, 0x1.ep-2, 0x1p-1,
};
static const double exp2_U[][2] = {
{0x1.6a09e667f3bcdp-1,-0x1.bdd3413b26456p-55},
{0x1.71f75e8ec5f74p-1,-0x1.16e4786887a99p-56},
{0x1.7a11473eb0187p-1,-0x1.41577ee04992fp-56},
{0x1.82589994cce13p-1,-0x1.d4c1dd41532d8p-55},
{0x1.8ace5422aa0dbp-1,0x1.6e9f156864b27p-55},
{0x1.93737b0cdc5e5p-1,-0x1.75fc781b57ebcp-58},
{0x1.9c49182a3f09p-1,0x1.c7c46b071f2bep-57},
{0x1.a5503b23e255dp-1,-0x1.d2f6edb8d41e1p-55},
{0x1.ae89f995ad3adp-1,0x1.7a1cd345dcc81p-55},
{0x1.b7f76f2fb5e47p-1,-0x1.5584f7e54ac3bp-57},
{0x1.c199bdd85529cp-1,0x1.11065895048ddp-56},
{0x1.cb720dcef9069p-1,0x1.503cbd1e949dbp-57},
{0x1.d5818dcfba487p-1,0x1.2ed02d75b3707p-56},
{0x1.dfc97337b9b5fp-1,-0x1.1a5cd4f184b5cp-55},
{0x1.ea4afa2a490dap-1,-0x1.e9c23179c2893p-55},
{0x1.f50765b6e454p-1,0x1.9d3e12dd8a18bp-55},
{0x1p+0,0x0p+0},
{0x1.059b0d3158574p+0,0x1.d73e2a475b465p-55},
{0x1.0b5586cf9890fp+0,0x1.8a62e4adc610bp-54},
{0x1.11301d0125b51p+0,-0x1.6c51039449b3ap-54},
{0x1.172b83c7d517bp+0,-0x1.19041b9d78a76p-55},
{0x1.1d4873168b9aap+0,0x1.e016e00a2643cp-54},
{0x1.2387a6e756238p+0,0x1.9b07eb6c70573p-54},
{0x1.29e9df51fdee1p+0,0x1.612e8afad1255p-55},
{0x1.306fe0a31b715p+0,0x1.6f46ad23182e4p-55},
{0x1.371a7373aa9cbp+0,-0x1.63aeabf42eae2p-54},
{0x1.3dea64c123422p+0,0x1.ada0911f09ebcp-55},
{0x1.44e086061892dp+0,0x1.89b7a04ef80dp-59},
{0x1.4bfdad5362a27p+0,0x1.d4397afec42e2p-56},
{0x1.5342b569d4f82p+0,-0x1.07abe1db13cadp-55},
{0x1.5ab07dd485429p+0,0x1.6324c054647adp-54},
{0x1.6247eb03a5585p+0,-0x1.383c17e40b497p-54},
{0x1.6a09e667f3bcdp+0,-0x1.bdd3413b26456p-54},
};
static float exp2_1 (double t)
{
double k = roundeven_finite (t); double r = t - k; b64u64_u v = {.f = 3.015625 + r}; int i = (v.u >> 46) - 0x10010; r -= exp2_T[i]; v.f = exp2_U[i][0] * q1 (r);
b64u64_u err = {.f = 0x1.1dp-41}; v.u += (int64_t) k * 0x10000000000000ll;
if (__builtin_expect (v.f < 0x1.00000000008e2p-126, 0))
return -1.0f;
err.u += (int64_t) k * 0x10000000000000ll; float lb = v.f - err.f, rb = v.f + err.f;
if (lb != rb) return -1.0f;
#ifdef CORE_MATH_SUPPORT_ERRNO
if (lb > 0x1.fffffep+127f)
errno = ERANGE; // overflow
#endif
return lb;
}
// return non-zero if (1+x)^y is exact or midpoint in binary32
// adapted from powf.c, commit 8ec0730
// set midpoint to non-zero if (1+x)^y is a midpoint
// (assume midpoint=0 initially)
static int
is_exact_or_midpoint (float x, float y, int *midpoint)
{
/* All cases such that (1+x)^y might be exact are:
(a) x = 0
(b) y integer, 0 <= y <= 15 (because 3^16 has 26 bits)
(c) y<0: x=0 or (1+x=2^e and |y|=n*2^-k with 2^k dividing e)
(d) y>0: y=n*2^f with -4 <= f <= -1 and 1 <= n <= 15
In cases (b)-(d), the low 16 bits of the encoding of y are zero,
thus we use that for an early exit test.
(For case (c), x=1 and y=-149, only 16 low bits of the
encoding of y are zero.) */
b32u32_u v = {.f = x}, w = {.f = y};
if (__builtin_expect ((v.u << 1) != 0 && // |0| <> 1
(w.u << (32 - 16)) != 0, 1))
return 0;
if (__builtin_expect ((v.u << 1) == 0, 0)) // x = 0
return 1; // never midpoint
int32_t e = ((v.u << 1) >> 24) - 0x96; // ulp(x) = 2^e
/* We assume (1+x)^y cannot be exact in binary32 if 1+x is not exact in
binary64:
* 1+x cannot be exact in double when |x| < 2^-53, thus since
ufp(x)=2^(e+23), when e+23 < -53, i.e., when e < -76.
* similarly, 1+x cannot be exact when |x| > 2^53, which occurs when
e+23 > 53, i.e., e > 30. */
if (e < -76 || 30 < e)
return 0;
b64u64_u vd = {.f = 1.0 + (double) x};
// recompute e for 1+x
e = ((vd.u << 1) >> 53) - 0x433; // ulp53(1+x) = 2^e
static const uint64_t xmax[] = { 0, 0xffffff, 5791, 321, 75, 31, 17, 11,
7, 5, 5, 3, 3, 3, 3, 3};
if (y >= 0 && isint (y)) {
uint64_t m = vd.u & 0xfffffffffffffull; if (e >= -1074)
m |= 0x10000000000000ull;
else e++;
int t = __builtin_ctzll (m);
m = m >> t;
e += t;
if (y == 0 || y == 1)
{
*midpoint = m > 0x1000000u;
return 1;
}
if (m == 1)
return -149 <= y * e && y * e < 128; if (y < 0 || 15 < y)
return 0;
int y_int = (int) y;
if (m > xmax[y_int])
return 0;
uint64_t my = m * m;
for (int i = 2; i < y_int; i++)
my = my * m;
t = 64 - __builtin_clzll (my);
int32_t ez = e * y_int + t;
if (ez <= -149 || 128 < ez)
return 0;
*midpoint = my > 0x1000000u;
return e * y_int >= -149;
}
uint32_t n = w.u & 0x7fffff;
int32_t f = ((w.u << 1) >> 24) - 0x96;
if (f >= -149)
n |= 0x800000;
else f++;
int t = __builtin_ctz (n);
n = n >> t;
f += t;
uint64_t m = vd.u & 0xfffffffffffffull;
if (e >= -1074)
m |= 0x10000000000000ull;
else e++;
t = __builtin_ctzll (m);
m = m >> t;
e += t;
if (y < 0)
{
if (m != 1) return 0;
t = __builtin_ctz (e);
if (-f > t) return 0; int32_t ez;
if (e >= 0)
ez = ((f >= 0) ? -(e << f) : -(e >> (-f))) * n;
else
ez = ((f >= 0) ? (-e << f) : (-e >> (-f))) * n;
return -149 <= ez && ez < 128;
}
while (f++) {
if (e&1) return 0;
e = e / 2;
double dm = (double) m;
double s = __builtin_round (__builtin_sqrt (dm));
if (s * s != dm)
return 0;
m = (uint64_t) s;
}
if (m > 1)
{
if (15 < n)
return 0;
if (m > xmax[n])
return 0;
}
uint32_t my = m, n0 = n;
while (n0-- > 1)
my = my * m;
t = 32 - __builtin_clz (my);
return -149 <= e * (int) n && e * (int) n + t <= 128;
}
static float exp2_2 (double h, double l, float x, float y, int exact,
FLAG_T flag)
{
if (y == 1.0f) {
set_flag (flag); float res = 1.0f + x;
#ifdef CORE_MATH_SUPPORT_ERRNO
if (res > 0x1.fffffep+127f)
errno = ERANGE; // overflow
#endif
return res;
}
double k = roundeven_finite (h); // |k| <= 150
// check easy cases h+l is tiny thus 2^(h+l) rounds to 1, 1- or 1+
if (k == 0 && __builtin_fabs (h) <= 0x1.715476af0d4c8p-25) {
return 1.0 + h * 0.5;
}
double r = h - k; fast_two_sum (&h, &l, r, l);
b64u64_u v = {.f = 3.015625 + h}; int i = (v.u >> 46) - 0x10010; h -= exp2_T[i]; fast_two_sum (&h, &l, h, l); double qh, ql;
q2 (&qh, &ql, h, l);
d_mul (&qh, &ql, exp2_U[i][0], exp2_U[i][1], qh, ql);
fast_two_sum (&qh, &ql, qh, ql);
b64u64_u w = {.f = qh};
if (((w.u + 1) & 0xfffffffull) <= 2) {
static const double Err[2] = {0x1.b1p-84, 0x1.edp-95, };
int small = k == 0 && i == 16 && __builtin_fabs (h) <= 0x1p-18;
double err = Err[small];
v.f = qh + (ql - err);
v.u += (int64_t) k * 0x10000000000000ll;
w.f = qh + (ql + err);
w.u += (int64_t) k * 0x10000000000000ll;
float left, right;
if (exact) {
int vtz = __builtin_ctzll (v.u);
int wtz = __builtin_ctzll (w.u);
set_flag (flag); return (vtz >= wtz) ? v.f : w.f;
}
left = v.f;
right = w.f;
if (left != right) {
printf ("Rounding test of accurate path failed for compound(%a,%a)\n",
(double) x, (double) y);
printf ("Please report the above to core-math@inria.fr\n");
exit (1);
}
}
v.f = qh + ql;
if ((w.u << 36) == 0 && v.f == qh && ql != 0)
v.u += (ql > 0) ? 1 : -1; v.u += (int64_t) k * 0x10000000000000ll; float res = v.f;
#ifdef CORE_MATH_SUPPORT_ERRNO
double tiny = 0x1p-55;
if (res < 0x1p-126f // RNDZ, RNDD
|| (res == 0x1p-126f && v.f <= 0x1.fffffep-127) || (res == 0x1p-126f && v.f < 0x1.ffffffp-127 && 1.0 - tiny == 1.0 + tiny))
errno = ERANGE;
if (res > 0x1.fffffep+127f || v.f >= 0x1p128)
errno = ERANGE; #endif
return res; }
static void
log2p1_accurate (double *h, double *l, double x)
{
if (1.0 >= x) { if (__builtin_expect (__builtin_fabs (x) >= 0x1p-53, 1))
fast_two_sum (h, l, 1.0, x); else
*h = 1.0, *l = x; }
else
fast_two_sum (h, l, x, 1.0);
b64u64_u v = {.f = *h};
uint64_t m = v.u & 0xfffffffffffffull;
int64_t e = (v.u >> 52) - 0x3ff + (m >= 0x6a09e667f3bcdull);
static const double scale[] = {
0x1p+29, 0x1p+28, 0x1p+27, 0x1p+26, 0x1p+25, 0x1p+24, 0x1p+23, 0x1p+22,
0x1p+21, 0x1p+20, 0x1p+19, 0x1p+18, 0x1p+17, 0x1p+16, 0x1p+15, 0x1p+14,
0x1p+13, 0x1p+12, 0x1p+11, 0x1p+10, 0x1p+9, 0x1p+8, 0x1p+7, 0x1p+6,
0x1p+5, 0x1p+4, 0x1p+3, 0x1p+2, 0x1p+1, 0x1p+0, 0x1p-1, 0x1p-2, 0x1p-3,
0x1p-4, 0x1p-5, 0x1p-6, 0x1p-7, 0x1p-8, 0x1p-9, 0x1p-10, 0x1p-11, 0x1p-12,
0x1p-13, 0x1p-14, 0x1p-15, 0x1p-16, 0x1p-17, 0x1p-18, 0x1p-19, 0x1p-20,
0x1p-21, 0x1p-22, 0x1p-23, 0x1p-24, 0x1p-25, 0x1p-26, 0x1p-27, 0x1p-28,
0x1p-29, 0x1p-30, 0x1p-31, 0x1p-32, 0x1p-33, 0x1p-34, 0x1p-35, 0x1p-36,
0x1p-37, 0x1p-38, 0x1p-39, 0x1p-40, 0x1p-41, 0x1p-42, 0x1p-43, 0x1p-44,
0x1p-45, 0x1p-46, 0x1p-47, 0x1p-48, 0x1p-49, 0x1p-50, 0x1p-51, 0x1p-52,
0x1p-53, 0x1p-54, 0x1p-55, 0x1p-56, 0x1p-57, 0x1p-58, 0x1p-59, 0x1p-60,
0x1p-61, 0x1p-62, 0x1p-63, 0x1p-64, 0x1p-65, 0x1p-66, 0x1p-67, 0x1p-68,
0x1p-69, 0x1p-70, 0x1p-71, 0x1p-72, 0x1p-73, 0x1p-74, 0x1p-75, 0x1p-76,
0x1p-77, 0x1p-78, 0x1p-79, 0x1p-80, 0x1p-81, 0x1p-82, 0x1p-83, 0x1p-84,
0x1p-85, 0x1p-86, 0x1p-87, 0x1p-88, 0x1p-89, 0x1p-90, 0x1p-91, 0x1p-92,
0x1p-93, 0x1p-94, 0x1p-95, 0x1p-96, 0x1p-97, 0x1p-98, 0x1p-99, 0x1p-100,
0x1p-101, 0x1p-102, 0x1p-103, 0x1p-104, 0x1p-105, 0x1p-106, 0x1p-107,
0x1p-108, 0x1p-109, 0x1p-110, 0x1p-111, 0x1p-112, 0x1p-113, 0x1p-114,
0x1p-115, 0x1p-116, 0x1p-117, 0x1p-118, 0x1p-119, 0x1p-120, 0x1p-121,
0x1p-122, 0x1p-123, 0x1p-124, 0x1p-125, 0x1p-126, 0x1p-127, 0x1p-128,
};
*h *= scale[e + 29];
*l *= scale[e + 29];
v.f = 2.0 + *h; int i = (v.u >> 45) - 0x2002d; double r = inv[i];
double zh = __builtin_fma (r, *h, -1.0); double zl = r * *l;
fast_two_sum (&zh, &zl, zh, zl);
double ph, pl;
p2 (&ph, &pl, zh, zl);
fast_two_sum (h, l, (double) e, log2inv[i][0]);
*l += log2inv[i][1];
double t;
fast_two_sum (h, &t, *h, ph);
*l += t + pl;
}
static double
accurate_path (float x, float y, int exact, FLAG_T flag)
{
double h, l;
log2p1_accurate (&h, &l, x);
s_mul (&h, &l, y, h, l);
return exp2_2 (h, l, x, y, exact, flag);
}
__attribute__((noinline)) float as_compoundf_special(float x, float y){
b32u32_u nx = {.f = x}, ny = {.f = y};
uint32_t ax = nx.u<<1, ay = ny.u<<1;
if (__builtin_expect(ax == 0 || ay == 0, 0)) { if (ax == 0) return is_signalingf (y) ? x + y : 1.0f;
if (ay == 0) { if (is_signalingf (x)) return x + y; if (x < -1.0f) {
#ifdef CORE_MATH_SUPPORT_ERRNO
errno = EDOM;
#endif
return 0.0f / 0.0f; }
else
return 1.0f; }
}
const b32u32_u mone = {.f = -1.0f};
if(__builtin_expect (ay >= 0xffu<<24, 0)){ if(ax > 0xffu<<24) return x + y; if(ay == 0xffu<<24) { if (nx.u > mone.u) return 0.0f / 0.0f; int sy = ny.u>>31; if (nx.u == mone.u){
if(sy == 0)
return 0.0f; else
return -y; }
if (x < 0.0f){
if(sy == 0)
return 0.0f; else
return -y; }
if (x > 0.0f){
if(sy != 0)
return 0.0f;
else
return y; }
return 1.0f;
}
return x + y; }
if (__builtin_expect (nx.u >= 0xffu<<23, 0)){
if (ax == 0xffu<<24){ if (nx.u>>31) return 0.0f / 0.0f; return (ny.u>>31) ? 1.0f/x : x;
}
if (ax > 0xffu<<24) return x + y; if (nx.u > mone.u) {
#ifdef CORE_MATH_SUPPORT_ERRNO
errno = EDOM;
#endif
return 0.0f / 0.0f; }
if (ny.u>>31) { #ifdef CORE_MATH_SUPPORT_ERRNO
errno = ERANGE;
#endif
return 1.0f/0.0f;
} else return 0.0f;
}
return 0.0f;
}
float cr_compoundf (float x, float y)
{
const b32u32_u mone = {.f = -1.0f};
b32u32_u nx = {.f = x}, ny = {.f = y};
if (__builtin_expect(nx.u >= mone.u, 0) ) return as_compoundf_special(x,y);
uint32_t ax = nx.u<<1, ay = ny.u<<1;
if (__builtin_expect(ax == 0 || ax >= 0xffu<<24 || ay == 0 || ay >= 0xffu<<24, 0) ) return as_compoundf_special(x,y);
double xd = x, yd = y;
b64u64_u tx = {.f = xd}, ty = {.f = yd};
volatile FLAG_T flag = get_flag ();
double l;
if (__builtin_expect (ax < 0x62000000u, 0)) {
double t = xd - (xd * xd) * 0.5;
l = INVLOG2 * t;
} else {
l = _log2p1 (tx.f);
}
b64u64_u t = {.f = l * ty.f};
if (__builtin_expect ((t.u << 1) >= (0x406ull<<53), 0)) { if (t.u >= 0x3018bull<<46) { #ifdef CORE_MATH_SUPPORT_ERRNO
errno = ERANGE; #endif
return 0x1p-126f * 0x1p-126f;
} else if (!(t.u >> 63)) { // t >= 128: overflow
#ifdef CORE_MATH_SUPPORT_ERRNO
errno = ERANGE; // overflow
#endif
return 0x1p126f * 0x1p126f;
}
}
/* since |t| < 150, the absolute error on t is bounded by
150*2^-47.909 < 2^-40.680 */
// 2^t rounds to 1 to nearest when |t| <= 0x1.715476af0d4d9p-25
if (__builtin_expect ((t.u << 1) <= 0x7cce2a8ed5e1a9b2ull, 0))
return (t.u >> 63) ? 1.0f - 0x1p-25f : 1.0f + 0x1p-25f;
int midpoint = 0;
int exact = is_exact_or_midpoint (x, y, &midpoint);
float res = exp2_1 (t.f);
if (__builtin_expect (res != -1.0f, 1))
{
if (exact && !midpoint)
set_flag (flag); // restore flags
return res;
}
/* we restore the flags, since exp2_1() might yield a spurious overflow,
for example for x,y=0x1.59ff68p+71,0x1.cab692p+0
set_flag (flag);
return accurate_path (x, y, exact, flag);
}
#ifndef SKIP_C_FUNC_REDEF
float compoundf(float x, float y){
return powf(1.0f+x,y);
}
#endif