xref: /haiku/src/system/libroot/posix/musl/math/tgammal.c (revision f504f61099b010fbfa94b1cc63d2e9072c7f7185)
1*f504f610SAugustin Cavalier /* origin: OpenBSD /usr/src/lib/libm/src/ld80/e_tgammal.c */
2*f504f610SAugustin Cavalier /*
3*f504f610SAugustin Cavalier  * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
4*f504f610SAugustin Cavalier  *
5*f504f610SAugustin Cavalier  * Permission to use, copy, modify, and distribute this software for any
6*f504f610SAugustin Cavalier  * purpose with or without fee is hereby granted, provided that the above
7*f504f610SAugustin Cavalier  * copyright notice and this permission notice appear in all copies.
8*f504f610SAugustin Cavalier  *
9*f504f610SAugustin Cavalier  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10*f504f610SAugustin Cavalier  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*f504f610SAugustin Cavalier  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12*f504f610SAugustin Cavalier  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*f504f610SAugustin Cavalier  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*f504f610SAugustin Cavalier  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15*f504f610SAugustin Cavalier  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16*f504f610SAugustin Cavalier  */
17*f504f610SAugustin Cavalier /*
18*f504f610SAugustin Cavalier  *      Gamma function
19*f504f610SAugustin Cavalier  *
20*f504f610SAugustin Cavalier  *
21*f504f610SAugustin Cavalier  * SYNOPSIS:
22*f504f610SAugustin Cavalier  *
23*f504f610SAugustin Cavalier  * long double x, y, tgammal();
24*f504f610SAugustin Cavalier  *
25*f504f610SAugustin Cavalier  * y = tgammal( x );
26*f504f610SAugustin Cavalier  *
27*f504f610SAugustin Cavalier  *
28*f504f610SAugustin Cavalier  * DESCRIPTION:
29*f504f610SAugustin Cavalier  *
30*f504f610SAugustin Cavalier  * Returns gamma function of the argument.  The result is
31*f504f610SAugustin Cavalier  * correctly signed.
32*f504f610SAugustin Cavalier  *
33*f504f610SAugustin Cavalier  * Arguments |x| <= 13 are reduced by recurrence and the function
34*f504f610SAugustin Cavalier  * approximated by a rational function of degree 7/8 in the
35*f504f610SAugustin Cavalier  * interval (2,3).  Large arguments are handled by Stirling's
36*f504f610SAugustin Cavalier  * formula. Large negative arguments are made positive using
37*f504f610SAugustin Cavalier  * a reflection formula.
38*f504f610SAugustin Cavalier  *
39*f504f610SAugustin Cavalier  *
40*f504f610SAugustin Cavalier  * ACCURACY:
41*f504f610SAugustin Cavalier  *
42*f504f610SAugustin Cavalier  *                      Relative error:
43*f504f610SAugustin Cavalier  * arithmetic   domain     # trials      peak         rms
44*f504f610SAugustin Cavalier  *    IEEE     -40,+40      10000       3.6e-19     7.9e-20
45*f504f610SAugustin Cavalier  *    IEEE    -1755,+1755   10000       4.8e-18     6.5e-19
46*f504f610SAugustin Cavalier  *
47*f504f610SAugustin Cavalier  * Accuracy for large arguments is dominated by error in powl().
48*f504f610SAugustin Cavalier  *
49*f504f610SAugustin Cavalier  */
50*f504f610SAugustin Cavalier 
51*f504f610SAugustin Cavalier #include "libm.h"
52*f504f610SAugustin Cavalier 
53*f504f610SAugustin Cavalier #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
tgammal(long double x)54*f504f610SAugustin Cavalier long double tgammal(long double x)
55*f504f610SAugustin Cavalier {
56*f504f610SAugustin Cavalier 	return tgamma(x);
57*f504f610SAugustin Cavalier }
58*f504f610SAugustin Cavalier #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384
59*f504f610SAugustin Cavalier /*
60*f504f610SAugustin Cavalier tgamma(x+2) = tgamma(x+2) P(x)/Q(x)
61*f504f610SAugustin Cavalier 0 <= x <= 1
62*f504f610SAugustin Cavalier Relative error
63*f504f610SAugustin Cavalier n=7, d=8
64*f504f610SAugustin Cavalier Peak error =  1.83e-20
65*f504f610SAugustin Cavalier Relative error spread =  8.4e-23
66*f504f610SAugustin Cavalier */
67*f504f610SAugustin Cavalier static const long double P[8] = {
68*f504f610SAugustin Cavalier  4.212760487471622013093E-5L,
69*f504f610SAugustin Cavalier  4.542931960608009155600E-4L,
70*f504f610SAugustin Cavalier  4.092666828394035500949E-3L,
71*f504f610SAugustin Cavalier  2.385363243461108252554E-2L,
72*f504f610SAugustin Cavalier  1.113062816019361559013E-1L,
73*f504f610SAugustin Cavalier  3.629515436640239168939E-1L,
74*f504f610SAugustin Cavalier  8.378004301573126728826E-1L,
75*f504f610SAugustin Cavalier  1.000000000000000000009E0L,
76*f504f610SAugustin Cavalier };
77*f504f610SAugustin Cavalier static const long double Q[9] = {
78*f504f610SAugustin Cavalier -1.397148517476170440917E-5L,
79*f504f610SAugustin Cavalier  2.346584059160635244282E-4L,
80*f504f610SAugustin Cavalier -1.237799246653152231188E-3L,
81*f504f610SAugustin Cavalier -7.955933682494738320586E-4L,
82*f504f610SAugustin Cavalier  2.773706565840072979165E-2L,
83*f504f610SAugustin Cavalier -4.633887671244534213831E-2L,
84*f504f610SAugustin Cavalier -2.243510905670329164562E-1L,
85*f504f610SAugustin Cavalier  4.150160950588455434583E-1L,
86*f504f610SAugustin Cavalier  9.999999999999999999908E-1L,
87*f504f610SAugustin Cavalier };
88*f504f610SAugustin Cavalier 
89*f504f610SAugustin Cavalier /*
90*f504f610SAugustin Cavalier static const long double P[] = {
91*f504f610SAugustin Cavalier -3.01525602666895735709e0L,
92*f504f610SAugustin Cavalier -3.25157411956062339893e1L,
93*f504f610SAugustin Cavalier -2.92929976820724030353e2L,
94*f504f610SAugustin Cavalier -1.70730828800510297666e3L,
95*f504f610SAugustin Cavalier -7.96667499622741999770e3L,
96*f504f610SAugustin Cavalier -2.59780216007146401957e4L,
97*f504f610SAugustin Cavalier -5.99650230220855581642e4L,
98*f504f610SAugustin Cavalier -7.15743521530849602425e4L
99*f504f610SAugustin Cavalier };
100*f504f610SAugustin Cavalier static const long double Q[] = {
101*f504f610SAugustin Cavalier  1.00000000000000000000e0L,
102*f504f610SAugustin Cavalier -1.67955233807178858919e1L,
103*f504f610SAugustin Cavalier  8.85946791747759881659e1L,
104*f504f610SAugustin Cavalier  5.69440799097468430177e1L,
105*f504f610SAugustin Cavalier -1.98526250512761318471e3L,
106*f504f610SAugustin Cavalier  3.31667508019495079814e3L,
107*f504f610SAugustin Cavalier  1.60577839621734713377e4L,
108*f504f610SAugustin Cavalier -2.97045081369399940529e4L,
109*f504f610SAugustin Cavalier -7.15743521530849602412e4L
110*f504f610SAugustin Cavalier };
111*f504f610SAugustin Cavalier */
112*f504f610SAugustin Cavalier #define MAXGAML 1755.455L
113*f504f610SAugustin Cavalier /*static const long double LOGPI = 1.14472988584940017414L;*/
114*f504f610SAugustin Cavalier 
115*f504f610SAugustin Cavalier /* Stirling's formula for the gamma function
116*f504f610SAugustin Cavalier tgamma(x) = sqrt(2 pi) x^(x-.5) exp(-x) (1 + 1/x P(1/x))
117*f504f610SAugustin Cavalier z(x) = x
118*f504f610SAugustin Cavalier 13 <= x <= 1024
119*f504f610SAugustin Cavalier Relative error
120*f504f610SAugustin Cavalier n=8, d=0
121*f504f610SAugustin Cavalier Peak error =  9.44e-21
122*f504f610SAugustin Cavalier Relative error spread =  8.8e-4
123*f504f610SAugustin Cavalier */
124*f504f610SAugustin Cavalier static const long double STIR[9] = {
125*f504f610SAugustin Cavalier  7.147391378143610789273E-4L,
126*f504f610SAugustin Cavalier -2.363848809501759061727E-5L,
127*f504f610SAugustin Cavalier -5.950237554056330156018E-4L,
128*f504f610SAugustin Cavalier  6.989332260623193171870E-5L,
129*f504f610SAugustin Cavalier  7.840334842744753003862E-4L,
130*f504f610SAugustin Cavalier -2.294719747873185405699E-4L,
131*f504f610SAugustin Cavalier -2.681327161876304418288E-3L,
132*f504f610SAugustin Cavalier  3.472222222230075327854E-3L,
133*f504f610SAugustin Cavalier  8.333333333333331800504E-2L,
134*f504f610SAugustin Cavalier };
135*f504f610SAugustin Cavalier 
136*f504f610SAugustin Cavalier #define MAXSTIR 1024.0L
137*f504f610SAugustin Cavalier static const long double SQTPI = 2.50662827463100050242E0L;
138*f504f610SAugustin Cavalier 
139*f504f610SAugustin Cavalier /* 1/tgamma(x) = z P(z)
140*f504f610SAugustin Cavalier  * z(x) = 1/x
141*f504f610SAugustin Cavalier  * 0 < x < 0.03125
142*f504f610SAugustin Cavalier  * Peak relative error 4.2e-23
143*f504f610SAugustin Cavalier  */
144*f504f610SAugustin Cavalier static const long double S[9] = {
145*f504f610SAugustin Cavalier -1.193945051381510095614E-3L,
146*f504f610SAugustin Cavalier  7.220599478036909672331E-3L,
147*f504f610SAugustin Cavalier -9.622023360406271645744E-3L,
148*f504f610SAugustin Cavalier -4.219773360705915470089E-2L,
149*f504f610SAugustin Cavalier  1.665386113720805206758E-1L,
150*f504f610SAugustin Cavalier -4.200263503403344054473E-2L,
151*f504f610SAugustin Cavalier -6.558780715202540684668E-1L,
152*f504f610SAugustin Cavalier  5.772156649015328608253E-1L,
153*f504f610SAugustin Cavalier  1.000000000000000000000E0L,
154*f504f610SAugustin Cavalier };
155*f504f610SAugustin Cavalier 
156*f504f610SAugustin Cavalier /* 1/tgamma(-x) = z P(z)
157*f504f610SAugustin Cavalier  * z(x) = 1/x
158*f504f610SAugustin Cavalier  * 0 < x < 0.03125
159*f504f610SAugustin Cavalier  * Peak relative error 5.16e-23
160*f504f610SAugustin Cavalier  * Relative error spread =  2.5e-24
161*f504f610SAugustin Cavalier  */
162*f504f610SAugustin Cavalier static const long double SN[9] = {
163*f504f610SAugustin Cavalier  1.133374167243894382010E-3L,
164*f504f610SAugustin Cavalier  7.220837261893170325704E-3L,
165*f504f610SAugustin Cavalier  9.621911155035976733706E-3L,
166*f504f610SAugustin Cavalier -4.219773343731191721664E-2L,
167*f504f610SAugustin Cavalier -1.665386113944413519335E-1L,
168*f504f610SAugustin Cavalier -4.200263503402112910504E-2L,
169*f504f610SAugustin Cavalier  6.558780715202536547116E-1L,
170*f504f610SAugustin Cavalier  5.772156649015328608727E-1L,
171*f504f610SAugustin Cavalier -1.000000000000000000000E0L,
172*f504f610SAugustin Cavalier };
173*f504f610SAugustin Cavalier 
174*f504f610SAugustin Cavalier static const long double PIL = 3.1415926535897932384626L;
175*f504f610SAugustin Cavalier 
176*f504f610SAugustin Cavalier /* Gamma function computed by Stirling's formula.
177*f504f610SAugustin Cavalier  */
stirf(long double x)178*f504f610SAugustin Cavalier static long double stirf(long double x)
179*f504f610SAugustin Cavalier {
180*f504f610SAugustin Cavalier 	long double y, w, v;
181*f504f610SAugustin Cavalier 
182*f504f610SAugustin Cavalier 	w = 1.0/x;
183*f504f610SAugustin Cavalier 	/* For large x, use rational coefficients from the analytical expansion.  */
184*f504f610SAugustin Cavalier 	if (x > 1024.0)
185*f504f610SAugustin Cavalier 		w = (((((6.97281375836585777429E-5L * w
186*f504f610SAugustin Cavalier 		 + 7.84039221720066627474E-4L) * w
187*f504f610SAugustin Cavalier 		 - 2.29472093621399176955E-4L) * w
188*f504f610SAugustin Cavalier 		 - 2.68132716049382716049E-3L) * w
189*f504f610SAugustin Cavalier 		 + 3.47222222222222222222E-3L) * w
190*f504f610SAugustin Cavalier 		 + 8.33333333333333333333E-2L) * w
191*f504f610SAugustin Cavalier 		 + 1.0;
192*f504f610SAugustin Cavalier 	else
193*f504f610SAugustin Cavalier 		w = 1.0 + w * __polevll(w, STIR, 8);
194*f504f610SAugustin Cavalier 	y = expl(x);
195*f504f610SAugustin Cavalier 	if (x > MAXSTIR) { /* Avoid overflow in pow() */
196*f504f610SAugustin Cavalier 		v = powl(x, 0.5L * x - 0.25L);
197*f504f610SAugustin Cavalier 		y = v * (v / y);
198*f504f610SAugustin Cavalier 	} else {
199*f504f610SAugustin Cavalier 		y = powl(x, x - 0.5L) / y;
200*f504f610SAugustin Cavalier 	}
201*f504f610SAugustin Cavalier 	y = SQTPI * y * w;
202*f504f610SAugustin Cavalier 	return y;
203*f504f610SAugustin Cavalier }
204*f504f610SAugustin Cavalier 
tgammal(long double x)205*f504f610SAugustin Cavalier long double tgammal(long double x)
206*f504f610SAugustin Cavalier {
207*f504f610SAugustin Cavalier 	long double p, q, z;
208*f504f610SAugustin Cavalier 
209*f504f610SAugustin Cavalier 	if (!isfinite(x))
210*f504f610SAugustin Cavalier 		return x + INFINITY;
211*f504f610SAugustin Cavalier 
212*f504f610SAugustin Cavalier 	q = fabsl(x);
213*f504f610SAugustin Cavalier 	if (q > 13.0) {
214*f504f610SAugustin Cavalier 		if (x < 0.0) {
215*f504f610SAugustin Cavalier 			p = floorl(q);
216*f504f610SAugustin Cavalier 			z = q - p;
217*f504f610SAugustin Cavalier 			if (z == 0)
218*f504f610SAugustin Cavalier 				return 0 / z;
219*f504f610SAugustin Cavalier 			if (q > MAXGAML) {
220*f504f610SAugustin Cavalier 				z = 0;
221*f504f610SAugustin Cavalier 			} else {
222*f504f610SAugustin Cavalier 				if (z > 0.5) {
223*f504f610SAugustin Cavalier 					p += 1.0;
224*f504f610SAugustin Cavalier 					z = q - p;
225*f504f610SAugustin Cavalier 				}
226*f504f610SAugustin Cavalier 				z = q * sinl(PIL * z);
227*f504f610SAugustin Cavalier 				z = fabsl(z) * stirf(q);
228*f504f610SAugustin Cavalier 				z = PIL/z;
229*f504f610SAugustin Cavalier 			}
230*f504f610SAugustin Cavalier 			if (0.5 * p == floorl(q * 0.5))
231*f504f610SAugustin Cavalier 				z = -z;
232*f504f610SAugustin Cavalier 		} else if (x > MAXGAML) {
233*f504f610SAugustin Cavalier 			z = x * 0x1p16383L;
234*f504f610SAugustin Cavalier 		} else {
235*f504f610SAugustin Cavalier 			z = stirf(x);
236*f504f610SAugustin Cavalier 		}
237*f504f610SAugustin Cavalier 		return z;
238*f504f610SAugustin Cavalier 	}
239*f504f610SAugustin Cavalier 
240*f504f610SAugustin Cavalier 	z = 1.0;
241*f504f610SAugustin Cavalier 	while (x >= 3.0) {
242*f504f610SAugustin Cavalier 		x -= 1.0;
243*f504f610SAugustin Cavalier 		z *= x;
244*f504f610SAugustin Cavalier 	}
245*f504f610SAugustin Cavalier 	while (x < -0.03125L) {
246*f504f610SAugustin Cavalier 		z /= x;
247*f504f610SAugustin Cavalier 		x += 1.0;
248*f504f610SAugustin Cavalier 	}
249*f504f610SAugustin Cavalier 	if (x <= 0.03125L)
250*f504f610SAugustin Cavalier 		goto small;
251*f504f610SAugustin Cavalier 	while (x < 2.0) {
252*f504f610SAugustin Cavalier 		z /= x;
253*f504f610SAugustin Cavalier 		x += 1.0;
254*f504f610SAugustin Cavalier 	}
255*f504f610SAugustin Cavalier 	if (x == 2.0)
256*f504f610SAugustin Cavalier 		return z;
257*f504f610SAugustin Cavalier 
258*f504f610SAugustin Cavalier 	x -= 2.0;
259*f504f610SAugustin Cavalier 	p = __polevll(x, P, 7);
260*f504f610SAugustin Cavalier 	q = __polevll(x, Q, 8);
261*f504f610SAugustin Cavalier 	z = z * p / q;
262*f504f610SAugustin Cavalier 	return z;
263*f504f610SAugustin Cavalier 
264*f504f610SAugustin Cavalier small:
265*f504f610SAugustin Cavalier 	/* z==1 if x was originally +-0 */
266*f504f610SAugustin Cavalier 	if (x == 0 && z != 1)
267*f504f610SAugustin Cavalier 		return x / x;
268*f504f610SAugustin Cavalier 	if (x < 0.0) {
269*f504f610SAugustin Cavalier 		x = -x;
270*f504f610SAugustin Cavalier 		q = z / (x * __polevll(x, SN, 8));
271*f504f610SAugustin Cavalier 	} else
272*f504f610SAugustin Cavalier 		q = z / (x * __polevll(x, S, 8));
273*f504f610SAugustin Cavalier 	return q;
274*f504f610SAugustin Cavalier }
275*f504f610SAugustin Cavalier #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384
276*f504f610SAugustin Cavalier // TODO: broken implementation to make things compile
tgammal(long double x)277*f504f610SAugustin Cavalier long double tgammal(long double x)
278*f504f610SAugustin Cavalier {
279*f504f610SAugustin Cavalier 	return tgamma(x);
280*f504f610SAugustin Cavalier }
281*f504f610SAugustin Cavalier #endif
282