xref: /onnv-gate/usr/src/lib/libast/common/uwin/gamma.c (revision 4887:feebf9260c2e)
1*4887Schin #include "FEATURE/uwin"
2*4887Schin 
3*4887Schin #if !_UWIN || _lib_gamma
4*4887Schin 
_STUB_gamma()5*4887Schin void _STUB_gamma(){}
6*4887Schin 
7*4887Schin #else
8*4887Schin 
9*4887Schin /*-
10*4887Schin  * Copyright (c) 1992, 1993
11*4887Schin  *	The Regents of the University of California.  All rights reserved.
12*4887Schin  *
13*4887Schin  * Redistribution and use in source and binary forms, with or without
14*4887Schin  * modification, are permitted provided that the following conditions
15*4887Schin  * are met:
16*4887Schin  * 1. Redistributions of source code must retain the above copyright
17*4887Schin  *    notice, this list of conditions and the following disclaimer.
18*4887Schin  * 2. Redistributions in binary form must reproduce the above copyright
19*4887Schin  *    notice, this list of conditions and the following disclaimer in the
20*4887Schin  *    documentation and/or other materials provided with the distribution.
21*4887Schin  * 3. Neither the name of the University nor the names of its contributors
22*4887Schin  *    may be used to endorse or promote products derived from this software
23*4887Schin  *    without specific prior written permission.
24*4887Schin  *
25*4887Schin  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26*4887Schin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27*4887Schin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28*4887Schin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29*4887Schin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30*4887Schin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31*4887Schin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32*4887Schin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33*4887Schin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34*4887Schin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35*4887Schin  * SUCH DAMAGE.
36*4887Schin  */
37*4887Schin 
38*4887Schin #ifndef lint
39*4887Schin static char sccsid[] = "@(#)gamma.c	8.1 (Berkeley) 6/4/93";
40*4887Schin #endif /* not lint */
41*4887Schin 
42*4887Schin /*
43*4887Schin  * This code by P. McIlroy, Oct 1992;
44*4887Schin  *
45*4887Schin  * The financial support of UUNET Communications Services is greatfully
46*4887Schin  * acknowledged.
47*4887Schin  */
48*4887Schin 
49*4887Schin #define gamma	______gamma
50*4887Schin 
51*4887Schin #include <math.h>
52*4887Schin #include <errno.h>
53*4887Schin #include "mathimpl.h"
54*4887Schin 
55*4887Schin #undef	gamma
56*4887Schin 
57*4887Schin /* METHOD:
58*4887Schin  * x < 0: Use reflection formula, G(x) = pi/(sin(pi*x)*x*G(x))
59*4887Schin  * 	At negative integers, return +Inf, and set errno.
60*4887Schin  *
61*4887Schin  * x < 6.5:
62*4887Schin  *	Use argument reduction G(x+1) = xG(x) to reach the
63*4887Schin  *	range [1.066124,2.066124].  Use a rational
64*4887Schin  *	approximation centered at the minimum (x0+1) to
65*4887Schin  *	ensure monotonicity.
66*4887Schin  *
67*4887Schin  * x >= 6.5: Use the asymptotic approximation (Stirling's formula)
68*4887Schin  *	adjusted for equal-ripples:
69*4887Schin  *
70*4887Schin  *	log(G(x)) ~= (x-.5)*(log(x)-1) + .5(log(2*pi)-1) + 1/x*P(1/(x*x))
71*4887Schin  *
72*4887Schin  *	Keep extra precision in multiplying (x-.5)(log(x)-1), to
73*4887Schin  *	avoid premature round-off.
74*4887Schin  *
75*4887Schin  * Special values:
76*4887Schin  *	non-positive integer:	Set overflow trap; return +Inf;
77*4887Schin  *	x > 171.63:		Set overflow trap; return +Inf;
78*4887Schin  *	NaN: 			Set invalid trap;  return NaN
79*4887Schin  *
80*4887Schin  * Accuracy: Gamma(x) is accurate to within
81*4887Schin  *	x > 0:  error provably < 0.9ulp.
82*4887Schin  *	Maximum observed in 1,000,000 trials was .87ulp.
83*4887Schin  *	x < 0:
84*4887Schin  *	Maximum observed error < 4ulp in 1,000,000 trials.
85*4887Schin  */
86*4887Schin 
87*4887Schin static double neg_gam __P((double));
88*4887Schin static double small_gam __P((double));
89*4887Schin static double smaller_gam __P((double));
90*4887Schin static struct Double large_gam __P((double));
91*4887Schin static struct Double ratfun_gam __P((double, double));
92*4887Schin 
93*4887Schin /*
94*4887Schin  * Rational approximation, A0 + x*x*P(x)/Q(x), on the interval
95*4887Schin  * [1.066.., 2.066..] accurate to 4.25e-19.
96*4887Schin  */
97*4887Schin #define LEFT -.3955078125	/* left boundary for rat. approx */
98*4887Schin #define x0 .461632144968362356785	/* xmin - 1 */
99*4887Schin 
100*4887Schin #define a0_hi 0.88560319441088874992
101*4887Schin #define a0_lo -.00000000000000004996427036469019695
102*4887Schin #define P0	 6.21389571821820863029017800727e-01
103*4887Schin #define P1	 2.65757198651533466104979197553e-01
104*4887Schin #define P2	 5.53859446429917461063308081748e-03
105*4887Schin #define P3	 1.38456698304096573887145282811e-03
106*4887Schin #define P4	 2.40659950032711365819348969808e-03
107*4887Schin #define Q0	 1.45019531250000000000000000000e+00
108*4887Schin #define Q1	 1.06258521948016171343454061571e+00
109*4887Schin #define Q2	-2.07474561943859936441469926649e-01
110*4887Schin #define Q3	-1.46734131782005422506287573015e-01
111*4887Schin #define Q4	 3.07878176156175520361557573779e-02
112*4887Schin #define Q5	 5.12449347980666221336054633184e-03
113*4887Schin #define Q6	-1.76012741431666995019222898833e-03
114*4887Schin #define Q7	 9.35021023573788935372153030556e-05
115*4887Schin #define Q8	 6.13275507472443958924745652239e-06
116*4887Schin /*
117*4887Schin  * Constants for large x approximation (x in [6, Inf])
118*4887Schin  * (Accurate to 2.8*10^-19 absolute)
119*4887Schin  */
120*4887Schin #define lns2pi_hi 0.418945312500000
121*4887Schin #define lns2pi_lo -.000006779295327258219670263595
122*4887Schin #define Pa0	 8.33333333333333148296162562474e-02
123*4887Schin #define Pa1	-2.77777777774548123579378966497e-03
124*4887Schin #define Pa2	 7.93650778754435631476282786423e-04
125*4887Schin #define Pa3	-5.95235082566672847950717262222e-04
126*4887Schin #define Pa4	 8.41428560346653702135821806252e-04
127*4887Schin #define Pa5	-1.89773526463879200348872089421e-03
128*4887Schin #define Pa6	 5.69394463439411649408050664078e-03
129*4887Schin #define Pa7	-1.44705562421428915453880392761e-02
130*4887Schin 
131*4887Schin static const double zero = 0., one = 1.0, tiny = 1e-300;
132*4887Schin static int endian;
133*4887Schin /*
134*4887Schin  * TRUNC sets trailing bits in a floating-point number to zero.
135*4887Schin  * is a temporary variable.
136*4887Schin  */
137*4887Schin #if defined(vax) || defined(tahoe)
138*4887Schin #define _IEEE		0
139*4887Schin #define TRUNC(x)	x = (double) (float) (x)
140*4887Schin #else
141*4887Schin #define _IEEE		1
142*4887Schin #define TRUNC(x)	*(((int *) &x) + endian) &= 0xf8000000
143*4887Schin #define infnan(x)	0.0
144*4887Schin #endif
145*4887Schin 
146*4887Schin extern double gamma(x)
147*4887Schin 	double x;
148*4887Schin {
149*4887Schin 	struct Double u;
150*4887Schin 	endian = (*(int *) &one) ? 1 : 0;
151*4887Schin 
152*4887Schin 	if (x >= 6) {
153*4887Schin 		if(x > 171.63)
154*4887Schin 			return(one/zero);
155*4887Schin 		u = large_gam(x);
156*4887Schin 		return(__exp__D(u.a, u.b));
157*4887Schin 	} else if (x >= 1.0 + LEFT + x0)
158*4887Schin 		return (small_gam(x));
159*4887Schin 	else if (x > 1.e-17)
160*4887Schin 		return (smaller_gam(x));
161*4887Schin 	else if (x > -1.e-17) {
162*4887Schin 		if (x == 0.0)
163*4887Schin 			if (!_IEEE) return (infnan(ERANGE));
164*4887Schin 			else return (one/x);
165*4887Schin 		one+1e-20;		/* Raise inexact flag. */
166*4887Schin 		return (one/x);
167*4887Schin 	} else if (!finite(x)) {
168*4887Schin 		if (_IEEE)		/* x = NaN, -Inf */
169*4887Schin 			return (x*x);
170*4887Schin 		else
171*4887Schin 			return (infnan(EDOM));
172*4887Schin 	 } else
173*4887Schin 		return (neg_gam(x));
174*4887Schin }
175*4887Schin /*
176*4887Schin  * Accurate to max(ulp(1/128) absolute, 2^-66 relative) error.
177*4887Schin  */
178*4887Schin static struct Double
large_gam(x)179*4887Schin large_gam(x)
180*4887Schin 	double x;
181*4887Schin {
182*4887Schin 	double z, p;
183*4887Schin 	struct Double t, u, v;
184*4887Schin 
185*4887Schin 	z = one/(x*x);
186*4887Schin 	p = Pa0+z*(Pa1+z*(Pa2+z*(Pa3+z*(Pa4+z*(Pa5+z*(Pa6+z*Pa7))))));
187*4887Schin 	p = p/x;
188*4887Schin 
189*4887Schin 	u = __log__D(x);
190*4887Schin 	u.a -= one;
191*4887Schin 	v.a = (x -= .5);
192*4887Schin 	TRUNC(v.a);
193*4887Schin 	v.b = x - v.a;
194*4887Schin 	t.a = v.a*u.a;			/* t = (x-.5)*(log(x)-1) */
195*4887Schin 	t.b = v.b*u.a + x*u.b;
196*4887Schin 	/* return t.a + t.b + lns2pi_hi + lns2pi_lo + p */
197*4887Schin 	t.b += lns2pi_lo; t.b += p;
198*4887Schin 	u.a = lns2pi_hi + t.b; u.a += t.a;
199*4887Schin 	u.b = t.a - u.a;
200*4887Schin 	u.b += lns2pi_hi; u.b += t.b;
201*4887Schin 	return (u);
202*4887Schin }
203*4887Schin /*
204*4887Schin  * Good to < 1 ulp.  (provably .90 ulp; .87 ulp on 1,000,000 runs.)
205*4887Schin  * It also has correct monotonicity.
206*4887Schin  */
207*4887Schin static double
small_gam(x)208*4887Schin small_gam(x)
209*4887Schin 	double x;
210*4887Schin {
211*4887Schin 	double y, ym1, t;
212*4887Schin 	struct Double yy, r;
213*4887Schin 	y = x - one;
214*4887Schin 	ym1 = y - one;
215*4887Schin 	if (y <= 1.0 + (LEFT + x0)) {
216*4887Schin 		yy = ratfun_gam(y - x0, 0);
217*4887Schin 		return (yy.a + yy.b);
218*4887Schin 	}
219*4887Schin 	r.a = y;
220*4887Schin 	TRUNC(r.a);
221*4887Schin 	yy.a = r.a - one;
222*4887Schin 	y = ym1;
223*4887Schin 	yy.b = r.b = y - yy.a;
224*4887Schin 	/* Argument reduction: G(x+1) = x*G(x) */
225*4887Schin 	for (ym1 = y-one; ym1 > LEFT + x0; y = ym1--, yy.a--) {
226*4887Schin 		t = r.a*yy.a;
227*4887Schin 		r.b = r.a*yy.b + y*r.b;
228*4887Schin 		r.a = t;
229*4887Schin 		TRUNC(r.a);
230*4887Schin 		r.b += (t - r.a);
231*4887Schin 	}
232*4887Schin 	/* Return r*gamma(y). */
233*4887Schin 	yy = ratfun_gam(y - x0, 0);
234*4887Schin 	y = r.b*(yy.a + yy.b) + r.a*yy.b;
235*4887Schin 	y += yy.a*r.a;
236*4887Schin 	return (y);
237*4887Schin }
238*4887Schin /*
239*4887Schin  * Good on (0, 1+x0+LEFT].  Accurate to 1ulp.
240*4887Schin  */
241*4887Schin static double
smaller_gam(x)242*4887Schin smaller_gam(x)
243*4887Schin 	double x;
244*4887Schin {
245*4887Schin 	double t, d;
246*4887Schin 	struct Double r, xx;
247*4887Schin 	if (x < x0 + LEFT) {
248*4887Schin 		t = x, TRUNC(t);
249*4887Schin 		d = (t+x)*(x-t);
250*4887Schin 		t *= t;
251*4887Schin 		xx.a = (t + x), TRUNC(xx.a);
252*4887Schin 		xx.b = x - xx.a; xx.b += t; xx.b += d;
253*4887Schin 		t = (one-x0); t += x;
254*4887Schin 		d = (one-x0); d -= t; d += x;
255*4887Schin 		x = xx.a + xx.b;
256*4887Schin 	} else {
257*4887Schin 		xx.a =  x, TRUNC(xx.a);
258*4887Schin 		xx.b = x - xx.a;
259*4887Schin 		t = x - x0;
260*4887Schin 		d = (-x0 -t); d += x;
261*4887Schin 	}
262*4887Schin 	r = ratfun_gam(t, d);
263*4887Schin 	d = r.a/x, TRUNC(d);
264*4887Schin 	r.a -= d*xx.a; r.a -= d*xx.b; r.a += r.b;
265*4887Schin 	return (d + r.a/x);
266*4887Schin }
267*4887Schin /*
268*4887Schin  * returns (z+c)^2 * P(z)/Q(z) + a0
269*4887Schin  */
270*4887Schin static struct Double
ratfun_gam(z,c)271*4887Schin ratfun_gam(z, c)
272*4887Schin 	double z, c;
273*4887Schin {
274*4887Schin 	double p, q;
275*4887Schin 	struct Double r, t;
276*4887Schin 
277*4887Schin 	q = Q0 +z*(Q1+z*(Q2+z*(Q3+z*(Q4+z*(Q5+z*(Q6+z*(Q7+z*Q8)))))));
278*4887Schin 	p = P0 + z*(P1 + z*(P2 + z*(P3 + z*P4)));
279*4887Schin 
280*4887Schin 	/* return r.a + r.b = a0 + (z+c)^2*p/q, with r.a truncated to 26 bits. */
281*4887Schin 	p = p/q;
282*4887Schin 	t.a = z, TRUNC(t.a);		/* t ~= z + c */
283*4887Schin 	t.b = (z - t.a) + c;
284*4887Schin 	t.b *= (t.a + z);
285*4887Schin 	q = (t.a *= t.a);		/* t = (z+c)^2 */
286*4887Schin 	TRUNC(t.a);
287*4887Schin 	t.b += (q - t.a);
288*4887Schin 	r.a = p, TRUNC(r.a);		/* r = P/Q */
289*4887Schin 	r.b = p - r.a;
290*4887Schin 	t.b = t.b*p + t.a*r.b + a0_lo;
291*4887Schin 	t.a *= r.a;			/* t = (z+c)^2*(P/Q) */
292*4887Schin 	r.a = t.a + a0_hi, TRUNC(r.a);
293*4887Schin 	r.b = ((a0_hi-r.a) + t.a) + t.b;
294*4887Schin 	return (r);			/* r = a0 + t */
295*4887Schin }
296*4887Schin 
297*4887Schin static double
neg_gam(x)298*4887Schin neg_gam(x)
299*4887Schin 	double x;
300*4887Schin {
301*4887Schin 	int sgn = 1;
302*4887Schin 	struct Double lg, lsine;
303*4887Schin 	double y, z;
304*4887Schin 
305*4887Schin 	y = floor(x + .5);
306*4887Schin 	if (y == x)		/* Negative integer. */
307*4887Schin 		if(!_IEEE)
308*4887Schin 			return (infnan(ERANGE));
309*4887Schin 		else
310*4887Schin 			return (one/zero);
311*4887Schin 	z = fabs(x - y);
312*4887Schin 	y = .5*ceil(x);
313*4887Schin 	if (y == ceil(y))
314*4887Schin 		sgn = -1;
315*4887Schin 	if (z < .25)
316*4887Schin 		z = sin(M_PI*z);
317*4887Schin 	else
318*4887Schin 		z = cos(M_PI*(0.5-z));
319*4887Schin 	/* Special case: G(1-x) = Inf; G(x) may be nonzero. */
320*4887Schin 	if (x < -170) {
321*4887Schin 		if (x < -190)
322*4887Schin 			return ((double)sgn*tiny*tiny);
323*4887Schin 		y = one - x;		/* exact: 128 < |x| < 255 */
324*4887Schin 		lg = large_gam(y);
325*4887Schin 		lsine = __log__D(M_PI/z);	/* = TRUNC(log(u)) + small */
326*4887Schin 		lg.a -= lsine.a;		/* exact (opposite signs) */
327*4887Schin 		lg.b -= lsine.b;
328*4887Schin 		y = -(lg.a + lg.b);
329*4887Schin 		z = (y + lg.a) + lg.b;
330*4887Schin 		y = __exp__D(y, z);
331*4887Schin 		if (sgn < 0) y = -y;
332*4887Schin 		return (y);
333*4887Schin 	}
334*4887Schin 	y = one-x;
335*4887Schin 	if (one-y == x)
336*4887Schin 		y = gamma(y);
337*4887Schin 	else		/* 1-x is inexact */
338*4887Schin 		y = -x*gamma(-x);
339*4887Schin 	if (sgn < 0) y = -y;
340*4887Schin 	return (M_PI / (y*z));
341*4887Schin }
342*4887Schin 
343*4887Schin #endif
344