#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
#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
typedef union {float f; uint32_t u;} b32u32_u;
typedef union {double f; uint64_t u;} b64u64_u;
_Float16 cr_tgammaf16(_Float16 xf16){
float x = xf16;
b32u32_u t = {.f = x};
uint32_t ax = t.u<<1;
if(__builtin_expect(ax>=(0xffu<<24), 0)){
if(ax==(0xffu<<24)){
if(t.u>>31){
#ifdef CORE_MATH_SUPPORT_ERRNO
errno = EDOM;
#endif
return x / x;
}
return x;
}
return x + x;
}
double z = x;
if(__builtin_expect(ax<=0x6f000000u, 0)){
volatile double d = (0x1.fa658c23b1578p-1 - 0x1.d0a118f324b63p-1*z)*z - 0x1.2788cfc6fb619p-1;
double f = 1.0/z + d;
_Float16 r = f;
#ifdef CORE_MATH_SUPPORT_ERRNO
if (x != 0x1p-16f || r > 0x1.fffffep+127f)
errno = ERANGE; // overflow
#endif
return r;
}
float fx = __builtin_floorf(x);
if(__builtin_expect(x >= 0x1.274p+3f, 0)){
#ifdef CORE_MATH_SUPPORT_ERRNO
/* The C standard says that if the function overflows,
errno is set to ERANGE. */
errno = ERANGE;
#endif
return 0x1p127f * 0x1p127f;
}
int32_t k = fx; // specific tgammaf16: no overflow possible
if(__builtin_expect(fx==x, 0)){ /* x is integer */
if(x == 0.0f){
#ifdef CORE_MATH_SUPPORT_ERRNO
errno = ERANGE;
#endif
return 1.0f/x;
}
if(x < 0.0f) {
#ifdef CORE_MATH_SUPPORT_ERRNO
errno = EDOM;
#endif
return 0.0f / 0.0f; /* should raise the "Invalid operation" exception */
}
double t0 = 1, x0 = 1;
for(int32_t i=1; i<k; i++, x0 += 1.0) t0 *= x0;
return t0;
}
// specific tgammaf16: changed threshold of -42 to -16
if(__builtin_expect(x<-16.0f, 0)){ /* negative non-integer */
/* For x < -16, x non-integer, |gamma(x)| < 2^-25. */
static const float sgn[2] = {0x1p-127f, -0x1p-127f};
#ifdef CORE_MATH_SUPPORT_ERRNO
/* The C standard says that if the function underflows,
errno is set to ERANGE. */
errno = ERANGE; // underflow
#endif
return 0x1p-127f * sgn[k&1];
}
static const double c[] =
{0x1.c9a76be577123p+0, 0x1.8f2754ddcf90dp+0, 0x1.0d1191949419bp+0, 0x1.e1f42cf0ae4a1p-2,
0x1.82b358a3ab638p-3, 0x1.e1f2b30cd907bp-5, 0x1.240f6d4071bd8p-6, 0x1.1522c9f3cd012p-8,
0x1.1fd0051a0525bp-10, 0x1.9808a8b96c37ep-13, 0x1.b3f78e01152b5p-15, 0x1.49c85a7e1fd04p-18,
0x1.471ca49184475p-19, -0x1.368f0b7ed9e36p-23, 0x1.882222f9049efp-23, -0x1.a69ed2042842cp-25};
double m = z - 0x1.7p+1, i = roundeven_finite(m), step = __builtin_copysign(1.0,i);
double d = m - i, d2 = d*d, d4 = d2*d2, d8 = d4*d4;
double f = (c[0] + d*c[1]) + d2*(c[2] + d*c[3]) + d4*((c[4] + d*c[5]) + d2*(c[6] + d*c[7]))
+ d8*((c[8] + d*c[9]) + d2*(c[10] + d*c[11]) + d4*((c[12] + d*c[13]) + d2*(c[14] + d*c[15])));
int jm = __builtin_fabs(i);
double w = 1;
if(jm){
z -= 0.5 + step*0.5;
w = z;
for(int j=jm-1; j; j--) {z -= step; w *= z;}
}
if(i<=-0.5) w = 1/w;
f *= w;
_Float16 r = f;
#ifdef CORE_MATH_SUPPORT_ERRNO
if (__builtin_fabs (f) < 0x1p-14f)
errno = ERANGE; // underflow
#endif
return r;
}
// dummy function since GNU libc does not provide it
_Float16 tgammaf16 (_Float16 x) {
return (_Float16) tgammaf ((float) x);
}