#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 {float f; uint32_t u;} b32u32_u;
static const float p0[] = {1.0f, 0x1.552e8ep-3f, 0x1.43696ep-4f};
/* degree-7 minimax polynomial for asin(x) over [0.25,0.5], with relative error
bounded by 2^-19.202, with coefficients of odd degree only, and degree-1
coefficient forced to 1
*/
static const float p1[] = {1.0f, 0x1.55a7ep-3f, 0x1.258e54p-4f, 0x1.08e44p-4f};
_Float16 cr_asinf16 (_Float16 x)
{
b32u32_u v = {.f = x};
uint32_t u = v.u;
uint32_t au = u & 0x7fffffffu;
#define HALF_PI 0x1.921fb6p+0f
if (au >= 0x3f800000u) { // NaN, Inf, or |x| >= 1
if (au == 0x3f800000u) // |x| = 1
return (u == 0x3f800000u) ? HALF_PI : -HALF_PI;
#ifdef CORE_MATH_SUPPORT_ERRNO
errno = EDOM;
#endif
if ((au >> 23) == 0x3ff && ((au & 0x7fffff) != 0)) // qNaN or sNaN
return x + x;
return 0.0f / 0.0f; // will signal invalid and return sNaN
}
float t = v.f, tt, c1, c5, y;
/* for |x| < 0.5 we don't reduce t into -t, since the polynomials
p0 and p1 are valid also on the negative side
int reduce = au >= 0x3f000000u; if (reduce) {
if (u >> 31) t = -t;
t = sqrtf ((1.0f - t) * 0.5f);
b32u32_u w = {.f = t};
au = w.u & 0x7fffffffu;
}
tt = t * t;
if (au < 0x3e800000u) {
if (au <= 0x3d388000u && !reduce) {
if (au == 0) return x;
#ifdef CORE_MATH_SUPPORT_ERRNO
if (au <= 0x387fc000u) errno = ERANGE; #endif
return (au == u) ? v.f + 0x1p-26f : v.f - 0x1p-26f;
}
if (au == 0x3dd30000u) // |x| = 0x1.a6p-4
return (au == u) ? 0x1.a6c00ap-4f : -0x1.a6c00ap-4f;
if (au == 0x3d688000u) // |x| = 0x1.d1p-5
return (au == u) ? 0x1.d14004p-5f : -0x1.d14004p-5f;
if (au == 0x3dfa0000u) // |x| = 0x1.f4p-4
return (au == u) ? 0x1.f5400ap-4f : -0x1.f5400ap-4f;
/* Warning for rounding toward -Inf: let p0(t) = t*q(t). If we first
compute q(t) and then multiply by t, for tiny t and rounding we will
get q(t)=1, and then t, whereas the correct result is nextbelow(t).
c1 = __builtin_fmaf (p0[2], tt, p0[1]);
y = __builtin_fmaf (c1, tt * t, t);
}
else { if (au == 0x3eb24000u) return (au == u) ? 0x1.6c2012p-2f : -0x1.6c2012p-2f;
if (au == 0x3ed96000u) // |x| = 0x1.b2cp-2
return (au == u) ? 0x1.c0fffap-2f : -0x1.c0fffap-2f;
if (au == 0x3ef0a000u) // |x| = 0x1.e14p-2
return (au == u) ? 0x1.f4fffp-2f : -0x1.f4fffp-2f;
c5 = __builtin_fmaf (p1[3], tt, p1[2]);
c1 = __builtin_fmaf (p1[1], tt, p1[0]);
y = __builtin_fmaf (c5, tt * tt, c1);
y = t * y;
}
if (reduce) {
y = HALF_PI - 2.0f * y;
if (u >> 31) y = -y;
}
return y;
}
_Float16 asinf16 (_Float16 x) {
return (_Float16) asinf ((float) x);
}