libm: Sync with FreeBSD (gains 6 long double functions)
[dragonfly.git] / lib / libm / src / e_lgammaf_r.c
CommitLineData
b34b60bc
JS
1/* e_lgammaf_r.c -- float version of e_lgamma_r.c.
2 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
2fedfd5c 3 * Conversion to float fixed By Steven G. Kargl.
b34b60bc
JS
4 */
5
6/*
7 * ====================================================
8 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
9 *
10 * Developed at SunPro, a Sun Microsystems, Inc. business.
11 * Permission to use, copy, modify, and distribute this
12 * software is freely granted, provided that this notice
13 * is preserved.
14 * ====================================================
b34b60bc
JS
15 */
16
2fedfd5c 17
6ff43c94 18#include "math.h"
b34b60bc
JS
19#include "math_private.h"
20
2fedfd5c
JM
21static const volatile float vzero = 0;
22
b34b60bc 23static const float
2fedfd5c
JM
24zero= 0,
25half= 0.5,
26one = 1,
b34b60bc 27pi = 3.1415927410e+00, /* 0x40490fdb */
2fedfd5c
JM
28/*
29 * Domain y in [0x1p-27, 0.27], range ~[-3.4599e-10, 3.4590e-10]:
30 * |(lgamma(2 - y) + 0.5 * y) / y - a(y)| < 2**-31.4
31 */
32a0 = 7.72156641e-02, /* 0x3d9e233f */
33a1 = 3.22467119e-01, /* 0x3ea51a69 */
34a2 = 6.73484802e-02, /* 0x3d89ee00 */
35a3 = 2.06395667e-02, /* 0x3ca9144f */
36a4 = 6.98275631e-03, /* 0x3be4cf9b */
37a5 = 4.11768444e-03, /* 0x3b86eda4 */
38/*
39 * Domain x in [tc-0.24, tc+0.28], range ~[-5.6577e-10, 5.5677e-10]:
40 * |(lgamma(x) - tf) - t(x - tc)| < 2**-30.8.
41 */
42tc = 1.46163213e+00, /* 0x3fbb16c3 */
43tf = -1.21486291e-01, /* 0xbdf8cdce */
44t0 = -2.94064460e-11, /* 0xae0154b7 */
45t1 = -2.35939837e-08, /* 0xb2caabb8 */
46t2 = 4.83836412e-01, /* 0x3ef7b968 */
47t3 = -1.47586212e-01, /* 0xbe1720d7 */
48t4 = 6.46013096e-02, /* 0x3d844db1 */
49t5 = -3.28450352e-02, /* 0xbd068884 */
50t6 = 1.86483748e-02, /* 0x3c98c47a */
51t7 = -9.89206228e-03, /* 0xbc221251 */
52/*
53 * Domain y in [-0.1, 0.232], range ~[-8.4931e-10, 8.7794e-10]:
54 * |(lgamma(1 + y) + 0.5 * y) / y - u(y) / v(y)| < 2**-31.2
55 */
56u0 = -7.72156641e-02, /* 0xbd9e233f */
57u1 = 7.36789703e-01, /* 0x3f3c9e40 */
58u2 = 4.95649040e-01, /* 0x3efdc5b6 */
59v1 = 1.10958421e+00, /* 0x3f8e06db */
60v2 = 2.10598111e-01, /* 0x3e57a708 */
61v3 = -1.02995494e-02, /* 0xbc28bf71 */
62/*
63 * Domain x in (2, 3], range ~[-5.5189e-11, 5.2317e-11]:
64 * |(lgamma(y+2) - 0.5 * y) / y - s(y)/r(y)| < 2**-35.0
65 * with y = x - 2.
66 */
67s0 = -7.72156641e-02, /* 0xbd9e233f */
68s1 = 2.69987404e-01, /* 0x3e8a3bca */
69s2 = 1.42851010e-01, /* 0x3e124789 */
70s3 = 1.19389519e-02, /* 0x3c439b98 */
71r1 = 6.79650068e-01, /* 0x3f2dfd8c */
72r2 = 1.16058730e-01, /* 0x3dedb033 */
73r3 = 3.75673687e-03, /* 0x3b763396 */
74/*
75 * Domain z in [8, 0x1p24], range ~[-1.2640e-09, 1.2640e-09]:
76 * |lgamma(x) - (x - 0.5) * (log(x) - 1) - w(1/x)| < 2**-29.6.
77 */
78w0 = 4.18938547e-01, /* 0x3ed67f1d */
79w1 = 8.33332464e-02, /* 0x3daaaa9f */
80w2 = -2.76129087e-03; /* 0xbb34f6c6 */
81
82static float
83sin_pif(float x)
b34b60bc 84{
2fedfd5c 85 volatile float vz;
b34b60bc 86 float y,z;
2fedfd5c
JM
87 int n;
88
89 y = -x;
90
91 vz = y+0x1p23F; /* depend on 0 <= y < 0x1p23 */
92 z = vz-0x1p23F; /* rintf(y) for the above range */
93 if (z == y)
94 return zero;
95
96 vz = y+0x1p21F;
97 GET_FLOAT_WORD(n,vz); /* bits for rounded y (units 0.25) */
98 z = vz-0x1p21F; /* y rounded to a multiple of 0.25 */
99 if (z > y) {
100 z -= 0.25F; /* adjust to round down */
101 n--;
102 }
103 n &= 7; /* octant of y mod 2 */
104 y = y - z + n * 0.25F; /* y mod 2 */
105
b34b60bc 106 switch (n) {
1a3b704c 107 case 0: y = __kernel_sindf(pi*y); break;
b34b60bc 108 case 1:
1a3b704c 109 case 2: y = __kernel_cosdf(pi*((float)0.5-y)); break;
b34b60bc 110 case 3:
1a3b704c 111 case 4: y = __kernel_sindf(pi*(one-y)); break;
b34b60bc 112 case 5:
1a3b704c
JM
113 case 6: y = -__kernel_cosdf(pi*(y-(float)1.5)); break;
114 default: y = __kernel_sindf(pi*(y-(float)2.0)); break;
b34b60bc
JS
115 }
116 return -y;
117}
118
119
120float
6ff43c94 121__ieee754_lgammaf_r(float x, int *signgamp)
b34b60bc 122{
2fedfd5c 123 float nadj,p,p1,p2,p3,q,r,t,w,y,z;
1a3b704c
JM
124 int32_t hx;
125 int i,ix;
b34b60bc 126
b34b60bc
JS
127 GET_FLOAT_WORD(hx,x);
128
2fedfd5c 129 /* purge +-Inf and NaNs */
b34b60bc
JS
130 *signgamp = 1;
131 ix = hx&0x7fffffff;
132 if(ix>=0x7f800000) return x*x;
2fedfd5c
JM
133
134 /* purge +-0 and tiny arguments */
135 *signgamp = 1-2*((uint32_t)hx>>31);
136 if(ix<0x32000000) { /* |x|<2**-27, return -log(|x|) */
137 if(ix==0)
138 return one/vzero;
139 return -__ieee754_logf(fabsf(x));
b34b60bc 140 }
2fedfd5c
JM
141
142 /* purge negative integers and start evaluation for other x < 0 */
b34b60bc 143 if(hx<0) {
2fedfd5c
JM
144 *signgamp = 1;
145 if(ix>=0x4b000000) /* |x|>=2**23, must be -integer */
146 return one/vzero;
b34b60bc 147 t = sin_pif(x);
2fedfd5c 148 if(t==zero) return one/vzero; /* -integer */
6ff43c94 149 nadj = __ieee754_logf(pi/fabsf(t*x));
b34b60bc
JS
150 if(t<zero) *signgamp = -1;
151 x = -x;
152 }
153
2fedfd5c 154 /* purge 1 and 2 */
b34b60bc
JS
155 if (ix==0x3f800000||ix==0x40000000) r = 0;
156 /* for x < 2.0 */
157 else if(ix<0x40000000) {
158 if(ix<=0x3f666666) { /* lgamma(x) = lgamma(x+1)-log(x) */
6ff43c94 159 r = -__ieee754_logf(x);
b34b60bc
JS
160 if(ix>=0x3f3b4a20) {y = one-x; i= 0;}
161 else if(ix>=0x3e6d3308) {y= x-(tc-one); i=1;}
162 else {y = x; i=2;}
163 } else {
164 r = zero;
2fedfd5c 165 if(ix>=0x3fdda618) {y=2-x;i=0;} /* [1.7316,2] */
b34b60bc
JS
166 else if(ix>=0x3F9da620) {y=x-tc;i=1;} /* [1.23,1.73] */
167 else {y=x-one;i=2;}
168 }
169 switch(i) {
170 case 0:
171 z = y*y;
2fedfd5c
JM
172 p1 = a0+z*(a2+z*a4);
173 p2 = z*(a1+z*(a3+z*a5));
b34b60bc 174 p = y*p1+p2;
2fedfd5c 175 r += p-y/2; break;
b34b60bc 176 case 1:
2fedfd5c
JM
177 p = t0+y*t1+y*y*(t2+y*(t3+y*(t4+y*(t5+y*(t6+y*t7)))));
178 r += tf + p; break;
b34b60bc 179 case 2:
2fedfd5c
JM
180 p1 = y*(u0+y*(u1+y*u2));
181 p2 = one+y*(v1+y*(v2+y*v3));
182 r += p1/p2-y/2;
b34b60bc
JS
183 }
184 }
2fedfd5c
JM
185 /* x < 8.0 */
186 else if(ix<0x41000000) {
187 i = x;
188 y = x-i;
189 p = y*(s0+y*(s1+y*(s2+y*s3)));
190 q = one+y*(r1+y*(r2+y*r3));
191 r = y/2+p/q;
b34b60bc
JS
192 z = one; /* lgamma(1+s) = log(s) + lgamma(s) */
193 switch(i) {
2fedfd5c
JM
194 case 7: z *= (y+6); /* FALLTHRU */
195 case 6: z *= (y+5); /* FALLTHRU */
196 case 5: z *= (y+4); /* FALLTHRU */
197 case 4: z *= (y+3); /* FALLTHRU */
198 case 3: z *= (y+2); /* FALLTHRU */
6ff43c94 199 r += __ieee754_logf(z); break;
b34b60bc 200 }
2fedfd5c
JM
201 /* 8.0 <= x < 2**27 */
202 } else if (ix < 0x4d000000) {
6ff43c94 203 t = __ieee754_logf(x);
b34b60bc
JS
204 z = one/x;
205 y = z*z;
2fedfd5c 206 w = w0+z*(w1+y*w2);
b34b60bc
JS
207 r = (x-half)*(t-one)+w;
208 } else
2fedfd5c 209 /* 2**27 <= x <= inf */
6ff43c94 210 r = x*(__ieee754_logf(x)-one);
b34b60bc
JS
211 if(hx<0) r = nadj - r;
212 return r;
213}