X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Fmath%2Flgamma_r.c;h=f9984cd0c66031dd0c37c894daa0ee9be870b610;hb=8910efd0e474fab85bf00657a6eeae4d69a87f63;hp=a8ef195651d68d52d8a34f1532c15029bbdd8968;hpb=de7db6e927e568c52eaffeb51d7726a094ed0180;p=musl diff --git a/src/math/lgamma_r.c b/src/math/lgamma_r.c index a8ef1956..f9984cd0 100644 --- a/src/math/lgamma_r.c +++ b/src/math/lgamma_r.c @@ -81,9 +81,6 @@ #include "libm.h" static const double -two52= 4.50359962737049600000e+15, /* 0x43300000, 0x00000000 */ -half= 5.00000000000000000000e-01, /* 0x3FE00000, 0x00000000 */ -one = 1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */ pi = 3.14159265358979311600e+00, /* 0x400921FB, 0x54442D18 */ a0 = 7.72156649015328655494e-02, /* 0x3FB3C467, 0xE37DB0C8 */ a1 = 3.22467033424113591611e-01, /* 0x3FD4A34C, 0xC4A60FAD */ @@ -148,110 +145,79 @@ w4 = -5.95187557450339963135e-04, /* 0xBF4380CB, 0x8C0FE741 */ w5 = 8.36339918996282139126e-04, /* 0x3F4B67BA, 0x4CDAD5D1 */ w6 = -1.63092934096575273989e-03; /* 0xBF5AB89D, 0x0B9E43E4 */ -static const double zero = 0.00000000000000000000e+00; - +/* sin(pi*x) assuming x > 2^-100, if sin(pi*x)==0 the sign is arbitrary */ static double sin_pi(double x) { - double y,z; - int n,ix; - - GET_HIGH_WORD(ix, x); - ix &= 0x7fffffff; + int n; - if (ix < 0x3fd00000) - return __sin(pi*x, zero, 0); + /* spurious inexact if odd int */ + x = 2.0*(x*0.5 - floor(x*0.5)); /* x mod 2.0 */ - y = -x; /* negative x is assumed */ + n = (int)(x*4.0); + n = (n+1)/2; + x -= n*0.5f; + x *= pi; - /* - * argument reduction, make sure inexact flag not raised if input - * is an integer - */ - z = floor(y); - if (z != y) { /* inexact anyway */ - y *= 0.5; - y = 2.0*(y - floor(y)); /* y = |x| mod 2.0 */ - n = (int)(y*4.0); - } else { - if (ix >= 0x43400000) { - y = zero; /* y must be even */ - n = 0; - } else { - if (ix < 0x43300000) - z = y + two52; /* exact */ - GET_LOW_WORD(n, z); - n &= 1; - y = n; - n <<= 2; - } - } switch (n) { - case 0: y = __sin(pi*y, zero, 0); break; - case 1: - case 2: y = __cos(pi*(0.5-y), zero); break; - case 3: - case 4: y = __sin(pi*(one-y), zero, 0); break; - case 5: - case 6: y = -__cos(pi*(y-1.5), zero); break; - default: y = __sin(pi*(y-2.0), zero, 0); break; + default: /* case 4: */ + case 0: return __sin(x, 0.0, 0); + case 1: return __cos(x, 0.0); + case 2: return __sin(-x, 0.0, 0); + case 3: return -__cos(x, 0.0); } - return -y; } - double __lgamma_r(double x, int *signgamp) { - double t,y,z,nadj,p,p1,p2,p3,q,r,w; - int32_t hx; - int i,lx,ix; - - EXTRACT_WORDS(hx, lx, x); + union {double f; uint64_t i;} u = {x}; + double_t t,y,z,nadj,p,p1,p2,p3,q,r,w; + uint32_t ix; + int sign,i; /* purge off +-inf, NaN, +-0, tiny and negative arguments */ *signgamp = 1; - ix = hx & 0x7fffffff; + sign = u.i>>63; + ix = u.i>>32 & 0x7fffffff; if (ix >= 0x7ff00000) return x*x; - if ((ix|lx) == 0) - return one/zero; - if (ix < 0x3b900000) { /* |x|<2**-70, return -log(|x|) */ - if(hx < 0) { + if (ix < (0x3ff-70)<<20) { /* |x|<2**-70, return -log(|x|) */ + if(sign) { + x = -x; *signgamp = -1; - return -log(-x); } return -log(x); } - if (hx < 0) { - if (ix >= 0x43300000) /* |x|>=2**52, must be -integer */ - return one/zero; + if (sign) { + x = -x; t = sin_pi(x); - if (t == zero) /* -integer */ - return one/zero; - nadj = log(pi/fabs(t*x)); - if (t < zero) + if (t == 0.0) /* -integer */ + return 1.0/(x-x); + if (t > 0.0) *signgamp = -1; - x = -x; + else + t = -t; + nadj = log(pi/(t*x)); } /* purge off 1 and 2 */ - if (((ix - 0x3ff00000)|lx) == 0 || ((ix - 0x40000000)|lx) == 0) + if ((ix == 0x3ff00000 || ix == 0x40000000) && (uint32_t)u.i == 0) r = 0; /* for x < 2.0 */ else if (ix < 0x40000000) { if (ix <= 0x3feccccc) { /* lgamma(x) = lgamma(x+1)-log(x) */ r = -log(x); if (ix >= 0x3FE76944) { - y = one - x; + y = 1.0 - x; i = 0; } else if (ix >= 0x3FCDA661) { - y = x - (tc-one); + y = x - (tc-1.0); i = 1; } else { y = x; i = 2; } } else { - r = zero; + r = 0.0; if (ix >= 0x3FFBB4C3) { /* [1.7316,2] */ y = 2.0 - x; i = 0; @@ -259,7 +225,7 @@ double __lgamma_r(double x, int *signgamp) y = x - tc; i = 1; } else { - y = x - one; + y = x - 1.0; i = 2; } } @@ -282,16 +248,16 @@ double __lgamma_r(double x, int *signgamp) break; case 2: p1 = y*(u0+y*(u1+y*(u2+y*(u3+y*(u4+y*u5))))); - p2 = one+y*(v1+y*(v2+y*(v3+y*(v4+y*v5)))); + p2 = 1.0+y*(v1+y*(v2+y*(v3+y*(v4+y*v5)))); r += -0.5*y + p1/p2; } } else if (ix < 0x40200000) { /* x < 8.0 */ i = (int)x; y = x - (double)i; p = y*(s0+y*(s1+y*(s2+y*(s3+y*(s4+y*(s5+y*s6)))))); - q = one+y*(r1+y*(r2+y*(r3+y*(r4+y*(r5+y*r6))))); - r = half*y+p/q; - z = one; /* lgamma(1+s) = log(s) + lgamma(s) */ + q = 1.0+y*(r1+y*(r2+y*(r3+y*(r4+y*(r5+y*r6))))); + r = 0.5*y+p/q; + z = 1.0; /* lgamma(1+s) = log(s) + lgamma(s) */ switch (i) { case 7: z *= y + 6.0; /* FALLTHRU */ case 6: z *= y + 5.0; /* FALLTHRU */ @@ -303,13 +269,13 @@ double __lgamma_r(double x, int *signgamp) } } else if (ix < 0x43900000) { /* 8.0 <= x < 2**58 */ t = log(x); - z = one/x; + z = 1.0/x; y = z*z; w = w0+z*(w1+y*(w2+y*(w3+y*(w4+y*(w5+y*w6))))); - r = (x-half)*(t-one)+w; + r = (x-0.5)*(t-1.0)+w; } else /* 2**58 <= x <= inf */ - r = x*(log(x)-one); - if (hx < 0) + r = x*(log(x)-1.0); + if (sign) r = nadj - r; return r; }