1*f504f610SAugustin Cavalier /*
2*f504f610SAugustin Cavalier "A Precision Approximation of the Gamma Function" - Cornelius Lanczos (1964)
3*f504f610SAugustin Cavalier "Lanczos Implementation of the Gamma Function" - Paul Godfrey (2001)
4*f504f610SAugustin Cavalier "An Analysis of the Lanczos Gamma Approximation" - Glendon Ralph Pugh (2004)
5*f504f610SAugustin Cavalier
6*f504f610SAugustin Cavalier approximation method:
7*f504f610SAugustin Cavalier
8*f504f610SAugustin Cavalier (x - 0.5) S(x)
9*f504f610SAugustin Cavalier Gamma(x) = (x + g - 0.5) * ----------------
10*f504f610SAugustin Cavalier exp(x + g - 0.5)
11*f504f610SAugustin Cavalier
12*f504f610SAugustin Cavalier with
13*f504f610SAugustin Cavalier a1 a2 a3 aN
14*f504f610SAugustin Cavalier S(x) ~= [ a0 + ----- + ----- + ----- + ... + ----- ]
15*f504f610SAugustin Cavalier x + 1 x + 2 x + 3 x + N
16*f504f610SAugustin Cavalier
17*f504f610SAugustin Cavalier with a0, a1, a2, a3,.. aN constants which depend on g.
18*f504f610SAugustin Cavalier
19*f504f610SAugustin Cavalier for x < 0 the following reflection formula is used:
20*f504f610SAugustin Cavalier
21*f504f610SAugustin Cavalier Gamma(x)*Gamma(-x) = -pi/(x sin(pi x))
22*f504f610SAugustin Cavalier
23*f504f610SAugustin Cavalier most ideas and constants are from boost and python
24*f504f610SAugustin Cavalier */
25*f504f610SAugustin Cavalier #include "libm.h"
26*f504f610SAugustin Cavalier
27*f504f610SAugustin Cavalier static const double pi = 3.141592653589793238462643383279502884;
28*f504f610SAugustin Cavalier
29*f504f610SAugustin Cavalier /* sin(pi x) with x > 0x1p-100, if sin(pi*x)==0 the sign is arbitrary */
sinpi(double x)30*f504f610SAugustin Cavalier static double sinpi(double x)
31*f504f610SAugustin Cavalier {
32*f504f610SAugustin Cavalier int n;
33*f504f610SAugustin Cavalier
34*f504f610SAugustin Cavalier /* argument reduction: x = |x| mod 2 */
35*f504f610SAugustin Cavalier /* spurious inexact when x is odd int */
36*f504f610SAugustin Cavalier x = x * 0.5;
37*f504f610SAugustin Cavalier x = 2 * (x - floor(x));
38*f504f610SAugustin Cavalier
39*f504f610SAugustin Cavalier /* reduce x into [-.25,.25] */
40*f504f610SAugustin Cavalier n = 4 * x;
41*f504f610SAugustin Cavalier n = (n+1)/2;
42*f504f610SAugustin Cavalier x -= n * 0.5;
43*f504f610SAugustin Cavalier
44*f504f610SAugustin Cavalier x *= pi;
45*f504f610SAugustin Cavalier switch (n) {
46*f504f610SAugustin Cavalier default: /* case 4 */
47*f504f610SAugustin Cavalier case 0:
48*f504f610SAugustin Cavalier return __sin(x, 0, 0);
49*f504f610SAugustin Cavalier case 1:
50*f504f610SAugustin Cavalier return __cos(x, 0);
51*f504f610SAugustin Cavalier case 2:
52*f504f610SAugustin Cavalier return __sin(-x, 0, 0);
53*f504f610SAugustin Cavalier case 3:
54*f504f610SAugustin Cavalier return -__cos(x, 0);
55*f504f610SAugustin Cavalier }
56*f504f610SAugustin Cavalier }
57*f504f610SAugustin Cavalier
58*f504f610SAugustin Cavalier #define N 12
59*f504f610SAugustin Cavalier //static const double g = 6.024680040776729583740234375;
60*f504f610SAugustin Cavalier static const double gmhalf = 5.524680040776729583740234375;
61*f504f610SAugustin Cavalier static const double Snum[N+1] = {
62*f504f610SAugustin Cavalier 23531376880.410759688572007674451636754734846804940,
63*f504f610SAugustin Cavalier 42919803642.649098768957899047001988850926355848959,
64*f504f610SAugustin Cavalier 35711959237.355668049440185451547166705960488635843,
65*f504f610SAugustin Cavalier 17921034426.037209699919755754458931112671403265390,
66*f504f610SAugustin Cavalier 6039542586.3520280050642916443072979210699388420708,
67*f504f610SAugustin Cavalier 1439720407.3117216736632230727949123939715485786772,
68*f504f610SAugustin Cavalier 248874557.86205415651146038641322942321632125127801,
69*f504f610SAugustin Cavalier 31426415.585400194380614231628318205362874684987640,
70*f504f610SAugustin Cavalier 2876370.6289353724412254090516208496135991145378768,
71*f504f610SAugustin Cavalier 186056.26539522349504029498971604569928220784236328,
72*f504f610SAugustin Cavalier 8071.6720023658162106380029022722506138218516325024,
73*f504f610SAugustin Cavalier 210.82427775157934587250973392071336271166969580291,
74*f504f610SAugustin Cavalier 2.5066282746310002701649081771338373386264310793408,
75*f504f610SAugustin Cavalier };
76*f504f610SAugustin Cavalier static const double Sden[N+1] = {
77*f504f610SAugustin Cavalier 0, 39916800, 120543840, 150917976, 105258076, 45995730, 13339535,
78*f504f610SAugustin Cavalier 2637558, 357423, 32670, 1925, 66, 1,
79*f504f610SAugustin Cavalier };
80*f504f610SAugustin Cavalier /* n! for small integer n */
81*f504f610SAugustin Cavalier static const double fact[] = {
82*f504f610SAugustin Cavalier 1, 1, 2, 6, 24, 120, 720, 5040.0, 40320.0, 362880.0, 3628800.0, 39916800.0,
83*f504f610SAugustin Cavalier 479001600.0, 6227020800.0, 87178291200.0, 1307674368000.0, 20922789888000.0,
84*f504f610SAugustin Cavalier 355687428096000.0, 6402373705728000.0, 121645100408832000.0,
85*f504f610SAugustin Cavalier 2432902008176640000.0, 51090942171709440000.0, 1124000727777607680000.0,
86*f504f610SAugustin Cavalier };
87*f504f610SAugustin Cavalier
88*f504f610SAugustin Cavalier /* S(x) rational function for positive x */
S(double x)89*f504f610SAugustin Cavalier static double S(double x)
90*f504f610SAugustin Cavalier {
91*f504f610SAugustin Cavalier double_t num = 0, den = 0;
92*f504f610SAugustin Cavalier int i;
93*f504f610SAugustin Cavalier
94*f504f610SAugustin Cavalier /* to avoid overflow handle large x differently */
95*f504f610SAugustin Cavalier if (x < 8)
96*f504f610SAugustin Cavalier for (i = N; i >= 0; i--) {
97*f504f610SAugustin Cavalier num = num * x + Snum[i];
98*f504f610SAugustin Cavalier den = den * x + Sden[i];
99*f504f610SAugustin Cavalier }
100*f504f610SAugustin Cavalier else
101*f504f610SAugustin Cavalier for (i = 0; i <= N; i++) {
102*f504f610SAugustin Cavalier num = num / x + Snum[i];
103*f504f610SAugustin Cavalier den = den / x + Sden[i];
104*f504f610SAugustin Cavalier }
105*f504f610SAugustin Cavalier return num/den;
106*f504f610SAugustin Cavalier }
107*f504f610SAugustin Cavalier
tgamma(double x)108*f504f610SAugustin Cavalier double tgamma(double x)
109*f504f610SAugustin Cavalier {
110*f504f610SAugustin Cavalier union {double f; uint64_t i;} u = {x};
111*f504f610SAugustin Cavalier double absx, y;
112*f504f610SAugustin Cavalier double_t dy, z, r;
113*f504f610SAugustin Cavalier uint32_t ix = u.i>>32 & 0x7fffffff;
114*f504f610SAugustin Cavalier int sign = u.i>>63;
115*f504f610SAugustin Cavalier
116*f504f610SAugustin Cavalier /* special cases */
117*f504f610SAugustin Cavalier if (ix >= 0x7ff00000)
118*f504f610SAugustin Cavalier /* tgamma(nan)=nan, tgamma(inf)=inf, tgamma(-inf)=nan with invalid */
119*f504f610SAugustin Cavalier return x + INFINITY;
120*f504f610SAugustin Cavalier if (ix < (0x3ff-54)<<20)
121*f504f610SAugustin Cavalier /* |x| < 2^-54: tgamma(x) ~ 1/x, +-0 raises div-by-zero */
122*f504f610SAugustin Cavalier return 1/x;
123*f504f610SAugustin Cavalier
124*f504f610SAugustin Cavalier /* integer arguments */
125*f504f610SAugustin Cavalier /* raise inexact when non-integer */
126*f504f610SAugustin Cavalier if (x == floor(x)) {
127*f504f610SAugustin Cavalier if (sign)
128*f504f610SAugustin Cavalier return 0/0.0;
129*f504f610SAugustin Cavalier if (x <= sizeof fact/sizeof *fact)
130*f504f610SAugustin Cavalier return fact[(int)x - 1];
131*f504f610SAugustin Cavalier }
132*f504f610SAugustin Cavalier
133*f504f610SAugustin Cavalier /* x >= 172: tgamma(x)=inf with overflow */
134*f504f610SAugustin Cavalier /* x =< -184: tgamma(x)=+-0 with underflow */
135*f504f610SAugustin Cavalier if (ix >= 0x40670000) { /* |x| >= 184 */
136*f504f610SAugustin Cavalier if (sign) {
137*f504f610SAugustin Cavalier FORCE_EVAL((float)(0x1p-126/x));
138*f504f610SAugustin Cavalier if (floor(x) * 0.5 == floor(x * 0.5))
139*f504f610SAugustin Cavalier return 0;
140*f504f610SAugustin Cavalier return -0.0;
141*f504f610SAugustin Cavalier }
142*f504f610SAugustin Cavalier x *= 0x1p1023;
143*f504f610SAugustin Cavalier return x;
144*f504f610SAugustin Cavalier }
145*f504f610SAugustin Cavalier
146*f504f610SAugustin Cavalier absx = sign ? -x : x;
147*f504f610SAugustin Cavalier
148*f504f610SAugustin Cavalier /* handle the error of x + g - 0.5 */
149*f504f610SAugustin Cavalier y = absx + gmhalf;
150*f504f610SAugustin Cavalier if (absx > gmhalf) {
151*f504f610SAugustin Cavalier dy = y - absx;
152*f504f610SAugustin Cavalier dy -= gmhalf;
153*f504f610SAugustin Cavalier } else {
154*f504f610SAugustin Cavalier dy = y - gmhalf;
155*f504f610SAugustin Cavalier dy -= absx;
156*f504f610SAugustin Cavalier }
157*f504f610SAugustin Cavalier
158*f504f610SAugustin Cavalier z = absx - 0.5;
159*f504f610SAugustin Cavalier r = S(absx) * exp(-y);
160*f504f610SAugustin Cavalier if (x < 0) {
161*f504f610SAugustin Cavalier /* reflection formula for negative x */
162*f504f610SAugustin Cavalier /* sinpi(absx) is not 0, integers are already handled */
163*f504f610SAugustin Cavalier r = -pi / (sinpi(absx) * absx * r);
164*f504f610SAugustin Cavalier dy = -dy;
165*f504f610SAugustin Cavalier z = -z;
166*f504f610SAugustin Cavalier }
167*f504f610SAugustin Cavalier r += dy * (gmhalf+0.5) * r / y;
168*f504f610SAugustin Cavalier z = pow(y, 0.5*z);
169*f504f610SAugustin Cavalier y = r * z * z;
170*f504f610SAugustin Cavalier return y;
171*f504f610SAugustin Cavalier }
172*f504f610SAugustin Cavalier
173*f504f610SAugustin Cavalier #if 0
174*f504f610SAugustin Cavalier double __lgamma_r(double x, int *sign)
175*f504f610SAugustin Cavalier {
176*f504f610SAugustin Cavalier double r, absx;
177*f504f610SAugustin Cavalier
178*f504f610SAugustin Cavalier *sign = 1;
179*f504f610SAugustin Cavalier
180*f504f610SAugustin Cavalier /* special cases */
181*f504f610SAugustin Cavalier if (!isfinite(x))
182*f504f610SAugustin Cavalier /* lgamma(nan)=nan, lgamma(+-inf)=inf */
183*f504f610SAugustin Cavalier return x*x;
184*f504f610SAugustin Cavalier
185*f504f610SAugustin Cavalier /* integer arguments */
186*f504f610SAugustin Cavalier if (x == floor(x) && x <= 2) {
187*f504f610SAugustin Cavalier /* n <= 0: lgamma(n)=inf with divbyzero */
188*f504f610SAugustin Cavalier /* n == 1,2: lgamma(n)=0 */
189*f504f610SAugustin Cavalier if (x <= 0)
190*f504f610SAugustin Cavalier return 1/0.0;
191*f504f610SAugustin Cavalier return 0;
192*f504f610SAugustin Cavalier }
193*f504f610SAugustin Cavalier
194*f504f610SAugustin Cavalier absx = fabs(x);
195*f504f610SAugustin Cavalier
196*f504f610SAugustin Cavalier /* lgamma(x) ~ -log(|x|) for tiny |x| */
197*f504f610SAugustin Cavalier if (absx < 0x1p-54) {
198*f504f610SAugustin Cavalier *sign = 1 - 2*!!signbit(x);
199*f504f610SAugustin Cavalier return -log(absx);
200*f504f610SAugustin Cavalier }
201*f504f610SAugustin Cavalier
202*f504f610SAugustin Cavalier /* use tgamma for smaller |x| */
203*f504f610SAugustin Cavalier if (absx < 128) {
204*f504f610SAugustin Cavalier x = tgamma(x);
205*f504f610SAugustin Cavalier *sign = 1 - 2*!!signbit(x);
206*f504f610SAugustin Cavalier return log(fabs(x));
207*f504f610SAugustin Cavalier }
208*f504f610SAugustin Cavalier
209*f504f610SAugustin Cavalier /* second term (log(S)-g) could be more precise here.. */
210*f504f610SAugustin Cavalier /* or with stirling: (|x|-0.5)*(log(|x|)-1) + poly(1/|x|) */
211*f504f610SAugustin Cavalier r = (absx-0.5)*(log(absx+gmhalf)-1) + (log(S(absx)) - (gmhalf+0.5));
212*f504f610SAugustin Cavalier if (x < 0) {
213*f504f610SAugustin Cavalier /* reflection formula for negative x */
214*f504f610SAugustin Cavalier x = sinpi(absx);
215*f504f610SAugustin Cavalier *sign = 2*!!signbit(x) - 1;
216*f504f610SAugustin Cavalier r = log(pi/(fabs(x)*absx)) - r;
217*f504f610SAugustin Cavalier }
218*f504f610SAugustin Cavalier return r;
219*f504f610SAugustin Cavalier }
220*f504f610SAugustin Cavalier
221*f504f610SAugustin Cavalier weak_alias(__lgamma_r, lgamma_r);
222*f504f610SAugustin Cavalier #endif
223