#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[] = {0x1.921fb4p0f, -0x1.fffb44p-1f, -0x1.25e6b8p-10f,
-0x1.3cc114p-3f, -0x1.a85b22p-5f};
/* degree-4 minimax polynomial for acos(x) over [0.25,0.5], with relative error
bounded by 2^-20.789, manually optimized to reduce the number of exceptions
*/
static const float p1[] = {0x1.91b67p0f, -0x1.f515cap-1f, -0x1.bd043ap-4f,
0x1.7e2d5ap-4f, -0x1.190806p-2f};
/* degree-4 minimax polynomial for acos(x)/sqrt(1-x) over [0.5,1],
with relative error bounded by 2^-23.583 */
static const float p2[] = {0x1.91fa1cp0f, -0x1.ae5c5ep-3f, 0x1.31640cp-4f,
-0x1.98038p-6f, 0x1.251b5p-8f};
_Float16 cr_acospif16 (_Float16 x)
{
b32u32_u v = {.f = x};
uint32_t u = v.u;
uint32_t au = u & 0x7fffffffu;
if (au >= 0x3f800000u) { // NaN, Inf, or |x| >= 1
if (au == 0x3f800000u)
return (u == 0x3f800000u) ? 0.0f16 : 1.0f;
#ifdef CORE_MATH_SUPPORT_ERRNO
errno = EDOM;
#endif
if ((au >> 23) == 0x3ff && ((au & 0x7fffff) != 0)) // qNaN or sNaN
return x;
return 0.0f / 0.0f; // will signal invalid and return sNaN
}
float t = v.f, tt = t * t, c1, c3, y;
if (u >> 31) // x < 0
t = -t;
#define INV_PI 0x1.45f306p-2f
if (au < 0x3e800000u) { // |x| < 0.25
if (au == 0) return 0.5f; // would be inexact with the code below
if (au == 0x3c55a000u) // |x| = 0x1.ab4p-7
return (au == u) ? 0x1.fbbffcp-2f : 0x1.022002p-1f;
if (au == 0x3d8a2000u) // |x| = 0x1.144p-4
return (au == u) ? 0x1.ea0002p-2f : 0x1.0afffep-1f;
if (u == 0x3d21c000u) // x = 0x1.438p-5
return 0x1.f31ffep-2f;
if (u == 0x3db3a000u) // x = 0x1.674p-4
return 0x1.e36002p-2f;
c1 = __builtin_fmaf (p0[2], t, p0[1]);
c3 = __builtin_fmaf (p0[4], t, p0[3]);
y = __builtin_fmaf (c3, tt, c1);
y = __builtin_fmaf (y, t, p0[0]);
y = y * INV_PI;
if (u >> 31) y = 1.0f - y;
}
else if (au < 0x3f000000u) { c1 = __builtin_fmaf (p1[2], t, p1[1]);
c3 = __builtin_fmaf (p1[4], t, p1[3]);
y = __builtin_fmaf (c3, tt, c1);
y = __builtin_fmaf (y, t, p1[0]);
y = y * INV_PI;
if (u >> 31) y = 1.0f - y;
}
else { if (au == 0x3f3ca000u) return (au == u) ? 0x1.e4000ap-3f : 0x1.86fffep-1f;
c1 = __builtin_fmaf (p2[2], t, p2[1]);
c3 = __builtin_fmaf (p2[4], t, p2[3]);
y = __builtin_fmaf (c3, tt, c1);
y = __builtin_fmaf (y, t, p2[0]);
y = sqrtf (1.0f - t) * y;
y = y * INV_PI;
if (u >> 31) y = 1.0f - y;
}
return y;
}
_Float16 acospif16 (_Float16 x) {
return (_Float16) acosf ((float) x) / (float) M_PI;
}