#include <stdint.h>
#ifdef CORE_MATH_SUPPORT_ERRNO
#include <errno.h>
#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;
typedef uint64_t u64;
float cr_atanf(float x){
const double pi2 = 0x1.921fb54442d18p+0;
b32u32_u t = {.f = x};
int e = (t.u>>23)&0xff, gt = e>=127;
uint32_t ta = t.u & 0x7fffffff;
if(__builtin_expect(ta >= 0x4c700518u, 0)) { if (ta > 0x7f800000u) return x + x; return __builtin_copysign(pi2,(double)x); }
if (__builtin_expect(e<127-13, 0)){ if (__builtin_expect(e<127-25, 0)){ if(!(t.u<<1)) return x;
float res = __builtin_fmaf(-x, __builtin_fabsf(x), x);
#ifdef CORE_MATH_SUPPORT_ERRNO
if (__builtin_fabsf (x) < 0x1p-126f || __builtin_fabsf (res) < 0x1p-126f)
errno = ERANGE; // underflow
#endif
return res;
}
return __builtin_fmaf(-0x1.5555555555555p-2f*x, x*x, x);
}
/* now |x| >= 0x1p-13
double z = x;
if (gt) z = 1/z;
double z2 = z*z, z4 = z2*z2, z8 = z4*z4;
static const double cn[] =
{0x1.51eccde075d67p-2, 0x1.a76bb5637f2f2p-1, 0x1.81e0eed20de88p-1,
0x1.376c8ca67d11dp-2, 0x1.aec7b69202ac6p-5, 0x1.9561899acc73ep-9,
0x1.bf9fa5b67e6p-16};
static const double cd[] =
{0x1.51eccde075d66p-2, 0x1.dfbdd7b392d28p-1, 0x1p+0,
0x1.fd22bf0e89b54p-2, 0x1.d91ff8b576282p-4, 0x1.653ea99fc9bbp-7,
0x1.1e7fcc202340ap-12};
double cn0 = cn[0] + z2*cn[1];
double cn2 = cn[2] + z2*cn[3];
double cn4 = cn[4] + z2*cn[5];
double cn6 = cn[6];
cn0 += z4*cn2;
cn4 += z4*cn6;
cn0 += z8*cn4;
cn0 *= z;
double cd0 = cd[0] + z2*cd[1];
double cd2 = cd[2] + z2*cd[3];
double cd4 = cd[4] + z2*cd[5];
double cd6 = cd[6];
cd0 += z4*cd2;
cd4 += z4*cd6;
cd0 += z8*cd4;
double r = cn0/cd0;
if (!gt) return r;
#define PI_OVER2_H 0x1.9p0
#define PI_OVER2_L 0x1.0fdaa22168c23p-7
r = (__builtin_copysign(PI_OVER2_L, z) - r) + __builtin_copysign(PI_OVER2_H, z);
return r;
}