18346e333Sjtc /* e_lgammaf_r.c -- float version of e_lgamma_r.c.
28346e333Sjtc * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
38346e333Sjtc */
48346e333Sjtc
58346e333Sjtc /*
68346e333Sjtc * ====================================================
78346e333Sjtc * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
88346e333Sjtc *
98346e333Sjtc * Developed at SunPro, a Sun Microsystems, Inc. business.
108346e333Sjtc * Permission to use, copy, modify, and distribute this
118346e333Sjtc * software is freely granted, provided that this notice
128346e333Sjtc * is preserved.
138346e333Sjtc * ====================================================
148346e333Sjtc */
158346e333Sjtc
16dd7adfbfSlukem #include <sys/cdefs.h>
17d1f06e0bSjtc #if defined(LIBM_SCCS) && !defined(lint)
18*aa30599eSwiz __RCSID("$NetBSD: e_lgammaf_r.c,v 1.6 2002/05/26 22:01:51 wiz Exp $");
198346e333Sjtc #endif
208346e333Sjtc
218346e333Sjtc #include "math.h"
228346e333Sjtc #include "math_private.h"
238346e333Sjtc
248346e333Sjtc static const float
258346e333Sjtc two23= 8.3886080000e+06, /* 0x4b000000 */
268346e333Sjtc half= 5.0000000000e-01, /* 0x3f000000 */
278346e333Sjtc one = 1.0000000000e+00, /* 0x3f800000 */
288346e333Sjtc pi = 3.1415927410e+00, /* 0x40490fdb */
298346e333Sjtc a0 = 7.7215664089e-02, /* 0x3d9e233f */
308346e333Sjtc a1 = 3.2246702909e-01, /* 0x3ea51a66 */
318346e333Sjtc a2 = 6.7352302372e-02, /* 0x3d89f001 */
328346e333Sjtc a3 = 2.0580807701e-02, /* 0x3ca89915 */
338346e333Sjtc a4 = 7.3855509982e-03, /* 0x3bf2027e */
348346e333Sjtc a5 = 2.8905137442e-03, /* 0x3b3d6ec6 */
358346e333Sjtc a6 = 1.1927076848e-03, /* 0x3a9c54a1 */
368346e333Sjtc a7 = 5.1006977446e-04, /* 0x3a05b634 */
378346e333Sjtc a8 = 2.2086278477e-04, /* 0x39679767 */
388346e333Sjtc a9 = 1.0801156895e-04, /* 0x38e28445 */
398346e333Sjtc a10 = 2.5214456400e-05, /* 0x37d383a2 */
408346e333Sjtc a11 = 4.4864096708e-05, /* 0x383c2c75 */
418346e333Sjtc tc = 1.4616321325e+00, /* 0x3fbb16c3 */
428346e333Sjtc tf = -1.2148628384e-01, /* 0xbdf8cdcd */
438346e333Sjtc /* tt = -(tail of tf) */
448346e333Sjtc tt = 6.6971006518e-09, /* 0x31e61c52 */
458346e333Sjtc t0 = 4.8383611441e-01, /* 0x3ef7b95e */
468346e333Sjtc t1 = -1.4758771658e-01, /* 0xbe17213c */
478346e333Sjtc t2 = 6.4624942839e-02, /* 0x3d845a15 */
488346e333Sjtc t3 = -3.2788541168e-02, /* 0xbd064d47 */
498346e333Sjtc t4 = 1.7970675603e-02, /* 0x3c93373d */
508346e333Sjtc t5 = -1.0314224288e-02, /* 0xbc28fcfe */
518346e333Sjtc t6 = 6.1005386524e-03, /* 0x3bc7e707 */
528346e333Sjtc t7 = -3.6845202558e-03, /* 0xbb7177fe */
538346e333Sjtc t8 = 2.2596477065e-03, /* 0x3b141699 */
548346e333Sjtc t9 = -1.4034647029e-03, /* 0xbab7f476 */
558346e333Sjtc t10 = 8.8108185446e-04, /* 0x3a66f867 */
568346e333Sjtc t11 = -5.3859531181e-04, /* 0xba0d3085 */
578346e333Sjtc t12 = 3.1563205994e-04, /* 0x39a57b6b */
588346e333Sjtc t13 = -3.1275415677e-04, /* 0xb9a3f927 */
598346e333Sjtc t14 = 3.3552918467e-04, /* 0x39afe9f7 */
608346e333Sjtc u0 = -7.7215664089e-02, /* 0xbd9e233f */
618346e333Sjtc u1 = 6.3282704353e-01, /* 0x3f2200f4 */
628346e333Sjtc u2 = 1.4549225569e+00, /* 0x3fba3ae7 */
638346e333Sjtc u3 = 9.7771751881e-01, /* 0x3f7a4bb2 */
648346e333Sjtc u4 = 2.2896373272e-01, /* 0x3e6a7578 */
658346e333Sjtc u5 = 1.3381091878e-02, /* 0x3c5b3c5e */
668346e333Sjtc v1 = 2.4559779167e+00, /* 0x401d2ebe */
678346e333Sjtc v2 = 2.1284897327e+00, /* 0x4008392d */
688346e333Sjtc v3 = 7.6928514242e-01, /* 0x3f44efdf */
698346e333Sjtc v4 = 1.0422264785e-01, /* 0x3dd572af */
708346e333Sjtc v5 = 3.2170924824e-03, /* 0x3b52d5db */
718346e333Sjtc s0 = -7.7215664089e-02, /* 0xbd9e233f */
728346e333Sjtc s1 = 2.1498242021e-01, /* 0x3e5c245a */
738346e333Sjtc s2 = 3.2577878237e-01, /* 0x3ea6cc7a */
748346e333Sjtc s3 = 1.4635047317e-01, /* 0x3e15dce6 */
758346e333Sjtc s4 = 2.6642270386e-02, /* 0x3cda40e4 */
768346e333Sjtc s5 = 1.8402845599e-03, /* 0x3af135b4 */
778346e333Sjtc s6 = 3.1947532989e-05, /* 0x3805ff67 */
788346e333Sjtc r1 = 1.3920053244e+00, /* 0x3fb22d3b */
798346e333Sjtc r2 = 7.2193557024e-01, /* 0x3f38d0c5 */
808346e333Sjtc r3 = 1.7193385959e-01, /* 0x3e300f6e */
818346e333Sjtc r4 = 1.8645919859e-02, /* 0x3c98bf54 */
828346e333Sjtc r5 = 7.7794247773e-04, /* 0x3a4beed6 */
838346e333Sjtc r6 = 7.3266842264e-06, /* 0x36f5d7bd */
848346e333Sjtc w0 = 4.1893854737e-01, /* 0x3ed67f1d */
858346e333Sjtc w1 = 8.3333335817e-02, /* 0x3daaaaab */
868346e333Sjtc w2 = -2.7777778450e-03, /* 0xbb360b61 */
878346e333Sjtc w3 = 7.9365057172e-04, /* 0x3a500cfd */
888346e333Sjtc w4 = -5.9518753551e-04, /* 0xba1c065c */
898346e333Sjtc w5 = 8.3633989561e-04, /* 0x3a5b3dd2 */
908346e333Sjtc w6 = -1.6309292987e-03; /* 0xbad5c4e8 */
918346e333Sjtc
928346e333Sjtc static const float zero= 0.0000000000e+00;
938346e333Sjtc
94*aa30599eSwiz static float
sin_pif(float x)95*aa30599eSwiz sin_pif(float x)
968346e333Sjtc {
978346e333Sjtc float y,z;
988346e333Sjtc int n,ix;
998346e333Sjtc
1008346e333Sjtc GET_FLOAT_WORD(ix,x);
1018346e333Sjtc ix &= 0x7fffffff;
1028346e333Sjtc
1038346e333Sjtc if(ix<0x3e800000) return __kernel_sinf(pi*x,zero,0);
1048346e333Sjtc y = -x; /* x is assume negative */
1058346e333Sjtc
1068346e333Sjtc /*
1078346e333Sjtc * argument reduction, make sure inexact flag not raised if input
1088346e333Sjtc * is an integer
1098346e333Sjtc */
1108346e333Sjtc z = floorf(y);
1118346e333Sjtc if(z!=y) { /* inexact anyway */
1128346e333Sjtc y *= (float)0.5;
1138346e333Sjtc y = (float)2.0*(y - floorf(y)); /* y = |x| mod 2.0 */
1148346e333Sjtc n = (int) (y*(float)4.0);
1158346e333Sjtc } else {
1168346e333Sjtc if(ix>=0x4b800000) {
1178346e333Sjtc y = zero; n = 0; /* y must be even */
1188346e333Sjtc } else {
1198346e333Sjtc if(ix<0x4b000000) z = y+two23; /* exact */
1208346e333Sjtc GET_FLOAT_WORD(n,z);
1218346e333Sjtc n &= 1;
1228346e333Sjtc y = n;
1238346e333Sjtc n<<= 2;
1248346e333Sjtc }
1258346e333Sjtc }
1268346e333Sjtc switch (n) {
1278346e333Sjtc case 0: y = __kernel_sinf(pi*y,zero,0); break;
1288346e333Sjtc case 1:
1298346e333Sjtc case 2: y = __kernel_cosf(pi*((float)0.5-y),zero); break;
1308346e333Sjtc case 3:
1318346e333Sjtc case 4: y = __kernel_sinf(pi*(one-y),zero,0); break;
1328346e333Sjtc case 5:
1338346e333Sjtc case 6: y = -__kernel_cosf(pi*(y-(float)1.5),zero); break;
1348346e333Sjtc default: y = __kernel_sinf(pi*(y-(float)2.0),zero,0); break;
1358346e333Sjtc }
1368346e333Sjtc return -y;
1378346e333Sjtc }
1388346e333Sjtc
1398346e333Sjtc
140*aa30599eSwiz float
__ieee754_lgammaf_r(float x,int * signgamp)141*aa30599eSwiz __ieee754_lgammaf_r(float x, int *signgamp)
1428346e333Sjtc {
1438346e333Sjtc float t,y,z,nadj,p,p1,p2,p3,q,r,w;
1448346e333Sjtc int i,hx,ix;
1458346e333Sjtc
146dd7adfbfSlukem nadj = 0;
1478346e333Sjtc GET_FLOAT_WORD(hx,x);
1488346e333Sjtc
1498346e333Sjtc /* purge off +-inf, NaN, +-0, and negative arguments */
1508346e333Sjtc *signgamp = 1;
1518346e333Sjtc ix = hx&0x7fffffff;
1528346e333Sjtc if(ix>=0x7f800000) return x*x;
1538346e333Sjtc if(ix==0) return one/zero;
1548346e333Sjtc if(ix<0x1c800000) { /* |x|<2**-70, return -log(|x|) */
1558346e333Sjtc if(hx<0) {
1568346e333Sjtc *signgamp = -1;
1578346e333Sjtc return -__ieee754_logf(-x);
1588346e333Sjtc } else return -__ieee754_logf(x);
1598346e333Sjtc }
1608346e333Sjtc if(hx<0) {
1618346e333Sjtc if(ix>=0x4b000000) /* |x|>=2**23, must be -integer */
1628346e333Sjtc return one/zero;
1638346e333Sjtc t = sin_pif(x);
1648346e333Sjtc if(t==zero) return one/zero; /* -integer */
1658346e333Sjtc nadj = __ieee754_logf(pi/fabsf(t*x));
1668346e333Sjtc if(t<zero) *signgamp = -1;
1678346e333Sjtc x = -x;
1688346e333Sjtc }
1698346e333Sjtc
1708346e333Sjtc /* purge off 1 and 2 */
1718346e333Sjtc if (ix==0x3f800000||ix==0x40000000) r = 0;
1728346e333Sjtc /* for x < 2.0 */
1738346e333Sjtc else if(ix<0x40000000) {
1748346e333Sjtc if(ix<=0x3f666666) { /* lgamma(x) = lgamma(x+1)-log(x) */
1758346e333Sjtc r = -__ieee754_logf(x);
1768346e333Sjtc if(ix>=0x3f3b4a20) {y = one-x; i= 0;}
1778346e333Sjtc else if(ix>=0x3e6d3308) {y= x-(tc-one); i=1;}
1788346e333Sjtc else {y = x; i=2;}
1798346e333Sjtc } else {
1808346e333Sjtc r = zero;
1818346e333Sjtc if(ix>=0x3fdda618) {y=(float)2.0-x;i=0;} /* [1.7316,2] */
1828346e333Sjtc else if(ix>=0x3F9da620) {y=x-tc;i=1;} /* [1.23,1.73] */
1838346e333Sjtc else {y=x-one;i=2;}
1848346e333Sjtc }
1858346e333Sjtc switch(i) {
1868346e333Sjtc case 0:
1878346e333Sjtc z = y*y;
1888346e333Sjtc p1 = a0+z*(a2+z*(a4+z*(a6+z*(a8+z*a10))));
1898346e333Sjtc p2 = z*(a1+z*(a3+z*(a5+z*(a7+z*(a9+z*a11)))));
1908346e333Sjtc p = y*p1+p2;
1918346e333Sjtc r += (p-(float)0.5*y); break;
1928346e333Sjtc case 1:
1938346e333Sjtc z = y*y;
1948346e333Sjtc w = z*y;
1958346e333Sjtc p1 = t0+w*(t3+w*(t6+w*(t9 +w*t12))); /* parallel comp */
1968346e333Sjtc p2 = t1+w*(t4+w*(t7+w*(t10+w*t13)));
1978346e333Sjtc p3 = t2+w*(t5+w*(t8+w*(t11+w*t14)));
1988346e333Sjtc p = z*p1-(tt-w*(p2+y*p3));
1998346e333Sjtc r += (tf + p); break;
2008346e333Sjtc case 2:
2018346e333Sjtc p1 = y*(u0+y*(u1+y*(u2+y*(u3+y*(u4+y*u5)))));
2028346e333Sjtc p2 = one+y*(v1+y*(v2+y*(v3+y*(v4+y*v5))));
2038346e333Sjtc r += (-(float)0.5*y + p1/p2);
2048346e333Sjtc }
2058346e333Sjtc }
2068346e333Sjtc else if(ix<0x41000000) { /* x < 8.0 */
2078346e333Sjtc i = (int)x;
2088346e333Sjtc t = zero;
2098346e333Sjtc y = x-(float)i;
2108346e333Sjtc p = y*(s0+y*(s1+y*(s2+y*(s3+y*(s4+y*(s5+y*s6))))));
2118346e333Sjtc q = one+y*(r1+y*(r2+y*(r3+y*(r4+y*(r5+y*r6)))));
2128346e333Sjtc r = half*y+p/q;
2138346e333Sjtc z = one; /* lgamma(1+s) = log(s) + lgamma(s) */
2148346e333Sjtc switch(i) {
2158346e333Sjtc case 7: z *= (y+(float)6.0); /* FALLTHRU */
2168346e333Sjtc case 6: z *= (y+(float)5.0); /* FALLTHRU */
2178346e333Sjtc case 5: z *= (y+(float)4.0); /* FALLTHRU */
2188346e333Sjtc case 4: z *= (y+(float)3.0); /* FALLTHRU */
2198346e333Sjtc case 3: z *= (y+(float)2.0); /* FALLTHRU */
2208346e333Sjtc r += __ieee754_logf(z); break;
2218346e333Sjtc }
2228346e333Sjtc /* 8.0 <= x < 2**58 */
2238346e333Sjtc } else if (ix < 0x5c800000) {
2248346e333Sjtc t = __ieee754_logf(x);
2258346e333Sjtc z = one/x;
2268346e333Sjtc y = z*z;
2278346e333Sjtc w = w0+z*(w1+y*(w2+y*(w3+y*(w4+y*(w5+y*w6)))));
2288346e333Sjtc r = (x-half)*(t-one)+w;
2298346e333Sjtc } else
2308346e333Sjtc /* 2**58 <= x <= inf */
2318346e333Sjtc r = x*(__ieee754_logf(x)-one);
2328346e333Sjtc if(hx<0) r = nadj - r;
2338346e333Sjtc return r;
2348346e333Sjtc }
235