float __ieee754_y1f(float x) { float z, s,c,ss,cc,u,v; int32_t hx,ix; GET_FLOAT_WORD(hx,x); ix = 0x7fffffff&hx; /* if Y1(NaN) is NaN, Y1(-inf) is NaN, Y1(inf) is 0 */ if(__builtin_expect(ix>=0x7f800000, 0)) return one/(x+x*x); if(__builtin_expect(ix==0, 0)) return -HUGE_VALF+x; /* -inf and overflow exception. */ if(__builtin_expect(hx<0, 0)) return zero/(zero*x); if(ix >= 0x40000000) { /* |x| >= 2.0 */ SET_RESTORE_ROUNDF (FE_TONEAREST); __sincosf (x, &s, &c); ss = -s-c; cc = s-c; if(ix<0x7f000000) { /* make sure x+x not overflow */ z = __cosf(x+x); if ((s*c)>zero) cc = z/ss; else ss = z/cc; } /* y1(x) = sqrt(2/(pi*x))*(p1(x)*sin(x0)+q1(x)*cos(x0)) * where x0 = x-3pi/4 * Better formula: * cos(x0) = cos(x)cos(3pi/4)+sin(x)sin(3pi/4) * = 1/sqrt(2) * (sin(x) - cos(x)) * sin(x0) = sin(x)cos(3pi/4)-cos(x)sin(3pi/4) * = -1/sqrt(2) * (cos(x) + sin(x)) * To avoid cancellation, use * sin(x) +- cos(x) = -cos(2x)/(sin(x) -+ cos(x)) * to compute the worse one. */ if(ix>0x48000000) z = (invsqrtpi*ss)/__ieee754_sqrtf(x); else { u = ponef(x); v = qonef(x); z = invsqrtpi*(u*ss+v*cc)/__ieee754_sqrtf(x); } return z; } if(__builtin_expect(ix<=0x33000000, 0)) { /* x < 2**-25 */ z = -tpi / x; if (__isinff (z)) __set_errno (ERANGE); return z; } z = x*x; u = U0[0]+z*(U0[1]+z*(U0[2]+z*(U0[3]+z*U0[4]))); v = one+z*(V0[0]+z*(V0[1]+z*(V0[2]+z*(V0[3]+z*V0[4])))); return(x*(u/v) + tpi*(__ieee754_j1f(x)*__ieee754_logf(x)-one/x)); }
float __ieee754_gammaf_r (float x, int *signgamp) { int32_t hx; float ret; GET_FLOAT_WORD (hx, x); if (__glibc_unlikely ((hx & 0x7fffffff) == 0)) { /* Return value for x == 0 is Inf with divide by zero exception. */ *signgamp = 0; return 1.0 / x; } if (__builtin_expect (hx < 0, 0) && (u_int32_t) hx < 0xff800000 && __rintf (x) == x) { /* Return value for integer x < 0 is NaN with invalid exception. */ *signgamp = 0; return (x - x) / (x - x); } if (__glibc_unlikely (hx == 0xff800000)) { /* x == -Inf. According to ISO this is NaN. */ *signgamp = 0; return x - x; } if (__glibc_unlikely ((hx & 0x7f800000) == 0x7f800000)) { /* Positive infinity (return positive infinity) or NaN (return NaN). */ *signgamp = 0; return x + x; } if (x >= 36.0f) { /* Overflow. */ *signgamp = 0; ret = math_narrow_eval (FLT_MAX * FLT_MAX); return ret; } else { SET_RESTORE_ROUNDF (FE_TONEAREST); if (x > 0.0f) { *signgamp = 0; int exp2_adj; float tret = gammaf_positive (x, &exp2_adj); ret = __scalbnf (tret, exp2_adj); } else if (x >= -FLT_EPSILON / 4.0f) { *signgamp = 0; ret = 1.0f / x; } else { float tx = __truncf (x); *signgamp = (tx == 2.0f * __truncf (tx / 2.0f)) ? -1 : 1; if (x <= -42.0f) /* Underflow. */ ret = FLT_MIN * FLT_MIN; else { float frac = tx - x; if (frac > 0.5f) frac = 1.0f - frac; float sinpix = (frac <= 0.25f ? __sinf ((float) M_PI * frac) : __cosf ((float) M_PI * (0.5f - frac))); int exp2_adj; float tret = (float) M_PI / (-x * sinpix * gammaf_positive (-x, &exp2_adj)); ret = __scalbnf (tret, -exp2_adj); math_check_force_underflow_nonneg (ret); } } ret = math_narrow_eval (ret); } if (isinf (ret) && x != 0) { if (*signgamp < 0) { ret = math_narrow_eval (-__copysignf (FLT_MAX, ret) * FLT_MAX); ret = -ret; } else ret = math_narrow_eval (__copysignf (FLT_MAX, ret) * FLT_MAX); return ret; } else if (ret == 0) { if (*signgamp < 0) { ret = math_narrow_eval (-__copysignf (FLT_MIN, ret) * FLT_MIN); ret = -ret; } else ret = math_narrow_eval (__copysignf (FLT_MIN, ret) * FLT_MIN); return ret; } else return ret; }
float __lgamma_negf (float x, int *signgamp) { /* Determine the half-integer region X lies in, handle exact integers and determine the sign of the result. */ int i = __floorf (-2 * x); if ((i & 1) == 0 && i == -2 * x) return 1.0f / 0.0f; float xn = ((i & 1) == 0 ? -i / 2 : (-i - 1) / 2); i -= 4; *signgamp = ((i & 2) == 0 ? -1 : 1); SET_RESTORE_ROUNDF (FE_TONEAREST); /* Expand around the zero X0 = X0_HI + X0_LO. */ float x0_hi = lgamma_zeros[i][0], x0_lo = lgamma_zeros[i][1]; float xdiff = x - x0_hi - x0_lo; /* For arguments in the range -3 to -2, use polynomial approximations to an adjusted version of the gamma function. */ if (i < 2) { int j = __floorf (-8 * x) - 16; float xm = (-33 - 2 * j) * 0.0625f; float x_adj = x - xm; size_t deg = poly_deg[j]; size_t end = poly_end[j]; float g = poly_coeff[end]; for (size_t j = 1; j <= deg; j++) g = g * x_adj + poly_coeff[end - j]; return __log1pf (g * xdiff / (x - xn)); } /* The result we want is log (sinpi (X0) / sinpi (X)) + log (gamma (1 - X0) / gamma (1 - X)). */ float x_idiff = fabsf (xn - x), x0_idiff = fabsf (xn - x0_hi - x0_lo); float log_sinpi_ratio; if (x0_idiff < x_idiff * 0.5f) /* Use log not log1p to avoid inaccuracy from log1p of arguments close to -1. */ log_sinpi_ratio = __ieee754_logf (lg_sinpi (x0_idiff) / lg_sinpi (x_idiff)); else { /* Use log1p not log to avoid inaccuracy from log of arguments close to 1. X0DIFF2 has positive sign if X0 is further from XN than X is from XN, negative sign otherwise. */ float x0diff2 = ((i & 1) == 0 ? xdiff : -xdiff) * 0.5f; float sx0d2 = lg_sinpi (x0diff2); float cx0d2 = lg_cospi (x0diff2); log_sinpi_ratio = __log1pf (2 * sx0d2 * (-sx0d2 + cx0d2 * lg_cotpi (x_idiff))); } float log_gamma_ratio; float y0 = math_narrow_eval (1 - x0_hi); float y0_eps = -x0_hi + (1 - y0) - x0_lo; float y = math_narrow_eval (1 - x); float y_eps = -x + (1 - y); /* We now wish to compute LOG_GAMMA_RATIO = log (gamma (Y0 + Y0_EPS) / gamma (Y + Y_EPS)). XDIFF accurately approximates the difference Y0 + Y0_EPS - Y - Y_EPS. Use Stirling's approximation. */ float log_gamma_high = (xdiff * __log1pf ((y0 - e_hi - e_lo + y0_eps) / e_hi) + (y - 0.5f + y_eps) * __log1pf (xdiff / y)); /* Compute the sum of (B_2k / 2k(2k-1))(Y0^-(2k-1) - Y^-(2k-1)). */ float y0r = 1 / y0, yr = 1 / y; float y0r2 = y0r * y0r, yr2 = yr * yr; float rdiff = -xdiff / (y * y0); float bterm[NCOEFF]; float dlast = rdiff, elast = rdiff * yr * (yr + y0r); bterm[0] = dlast * lgamma_coeff[0]; for (size_t j = 1; j < NCOEFF; j++) { float dnext = dlast * y0r2 + elast; float enext = elast * yr2; bterm[j] = dnext * lgamma_coeff[j]; dlast = dnext; elast = enext; } float log_gamma_low = 0; for (size_t j = 0; j < NCOEFF; j++) log_gamma_low += bterm[NCOEFF - 1 - j]; log_gamma_ratio = log_gamma_high + log_gamma_low; return log_sinpi_ratio + log_gamma_ratio; }