xref: /titanic_41/usr/src/lib/libast/common/uwin/lgamma.c (revision da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968)
1*da2e3ebdSchin #include "FEATURE/uwin"
2*da2e3ebdSchin 
3*da2e3ebdSchin #if !_UWIN || _lib_lgamma
4*da2e3ebdSchin 
_STUB_lgamma()5*da2e3ebdSchin void _STUB_lgamma(){}
6*da2e3ebdSchin 
7*da2e3ebdSchin #else
8*da2e3ebdSchin 
9*da2e3ebdSchin /*-
10*da2e3ebdSchin  * Copyright (c) 1992, 1993
11*da2e3ebdSchin  *	The Regents of the University of California.  All rights reserved.
12*da2e3ebdSchin  *
13*da2e3ebdSchin  * Redistribution and use in source and binary forms, with or without
14*da2e3ebdSchin  * modification, are permitted provided that the following conditions
15*da2e3ebdSchin  * are met:
16*da2e3ebdSchin  * 1. Redistributions of source code must retain the above copyright
17*da2e3ebdSchin  *    notice, this list of conditions and the following disclaimer.
18*da2e3ebdSchin  * 2. Redistributions in binary form must reproduce the above copyright
19*da2e3ebdSchin  *    notice, this list of conditions and the following disclaimer in the
20*da2e3ebdSchin  *    documentation and/or other materials provided with the distribution.
21*da2e3ebdSchin  * 3. Neither the name of the University nor the names of its contributors
22*da2e3ebdSchin  *    may be used to endorse or promote products derived from this software
23*da2e3ebdSchin  *    without specific prior written permission.
24*da2e3ebdSchin  *
25*da2e3ebdSchin  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26*da2e3ebdSchin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27*da2e3ebdSchin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28*da2e3ebdSchin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29*da2e3ebdSchin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30*da2e3ebdSchin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31*da2e3ebdSchin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32*da2e3ebdSchin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33*da2e3ebdSchin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34*da2e3ebdSchin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35*da2e3ebdSchin  * SUCH DAMAGE.
36*da2e3ebdSchin  */
37*da2e3ebdSchin 
38*da2e3ebdSchin #ifndef lint
39*da2e3ebdSchin static char sccsid[] = "@(#)lgamma.c	8.2 (Berkeley) 11/30/93";
40*da2e3ebdSchin #endif /* not lint */
41*da2e3ebdSchin 
42*da2e3ebdSchin /*
43*da2e3ebdSchin  * Coded by Peter McIlroy, Nov 1992;
44*da2e3ebdSchin  *
45*da2e3ebdSchin  * The financial support of UUNET Communications Services is greatfully
46*da2e3ebdSchin  * acknowledged.
47*da2e3ebdSchin  */
48*da2e3ebdSchin 
49*da2e3ebdSchin #define gamma	______gamma
50*da2e3ebdSchin #define lgamma	______lgamma
51*da2e3ebdSchin 
52*da2e3ebdSchin #include <math.h>
53*da2e3ebdSchin #include <errno.h>
54*da2e3ebdSchin #include "mathimpl.h"
55*da2e3ebdSchin 
56*da2e3ebdSchin #undef	gamma
57*da2e3ebdSchin #undef	lgamma
58*da2e3ebdSchin 
59*da2e3ebdSchin /* Log gamma function.
60*da2e3ebdSchin  * Error:  x > 0 error < 1.3ulp.
61*da2e3ebdSchin  *	   x > 4, error < 1ulp.
62*da2e3ebdSchin  *	   x > 9, error < .6ulp.
63*da2e3ebdSchin  * 	   x < 0, all bets are off. (When G(x) ~ 1, log(G(x)) ~ 0)
64*da2e3ebdSchin  * Method:
65*da2e3ebdSchin  *	x > 6:
66*da2e3ebdSchin  *		Use the asymptotic expansion (Stirling's Formula)
67*da2e3ebdSchin  *	0 < x < 6:
68*da2e3ebdSchin  *		Use gamma(x+1) = x*gamma(x) for argument reduction.
69*da2e3ebdSchin  *		Use rational approximation in
70*da2e3ebdSchin  *		the range 1.2, 2.5
71*da2e3ebdSchin  *		Two approximations are used, one centered at the
72*da2e3ebdSchin  *		minimum to ensure monotonicity; one centered at 2
73*da2e3ebdSchin  *		to maintain small relative error.
74*da2e3ebdSchin  *	x < 0:
75*da2e3ebdSchin  *		Use the reflection formula,
76*da2e3ebdSchin  *		G(1-x)G(x) = PI/sin(PI*x)
77*da2e3ebdSchin  * Special values:
78*da2e3ebdSchin  *	non-positive integer	returns +Inf.
79*da2e3ebdSchin  *	NaN			returns NaN
80*da2e3ebdSchin */
81*da2e3ebdSchin static int endian;
82*da2e3ebdSchin #if defined(vax) || defined(tahoe)
83*da2e3ebdSchin #define _IEEE		0
84*da2e3ebdSchin /* double and float have same size exponent field */
85*da2e3ebdSchin #define TRUNC(x)	x = (double) (float) (x)
86*da2e3ebdSchin #else
87*da2e3ebdSchin #define _IEEE		1
88*da2e3ebdSchin #define TRUNC(x)	*(((int *) &x) + endian) &= 0xf8000000
89*da2e3ebdSchin #define infnan(x)	0.0
90*da2e3ebdSchin #endif
91*da2e3ebdSchin 
92*da2e3ebdSchin static double small_lgam(double);
93*da2e3ebdSchin static double large_lgam(double);
94*da2e3ebdSchin static double neg_lgam(double);
95*da2e3ebdSchin static double zero = 0.0, one = 1.0;
96*da2e3ebdSchin int signgam;
97*da2e3ebdSchin 
98*da2e3ebdSchin #define UNDERFL (1e-1020 * 1e-1020)
99*da2e3ebdSchin 
100*da2e3ebdSchin #define LEFT	(1.0 - (x0 + .25))
101*da2e3ebdSchin #define RIGHT	(x0 - .218)
102*da2e3ebdSchin /*
103*da2e3ebdSchin  * Constants for approximation in [1.244,1.712]
104*da2e3ebdSchin */
105*da2e3ebdSchin #define x0	0.461632144968362356785
106*da2e3ebdSchin #define x0_lo	-.000000000000000015522348162858676890521
107*da2e3ebdSchin #define a0_hi	-0.12148629128932952880859
108*da2e3ebdSchin #define a0_lo	.0000000007534799204229502
109*da2e3ebdSchin #define r0	-2.771227512955130520e-002
110*da2e3ebdSchin #define r1	-2.980729795228150847e-001
111*da2e3ebdSchin #define r2	-3.257411333183093394e-001
112*da2e3ebdSchin #define r3	-1.126814387531706041e-001
113*da2e3ebdSchin #define r4	-1.129130057170225562e-002
114*da2e3ebdSchin #define r5	-2.259650588213369095e-005
115*da2e3ebdSchin #define s0	 1.714457160001714442e+000
116*da2e3ebdSchin #define s1	 2.786469504618194648e+000
117*da2e3ebdSchin #define s2	 1.564546365519179805e+000
118*da2e3ebdSchin #define s3	 3.485846389981109850e-001
119*da2e3ebdSchin #define s4	 2.467759345363656348e-002
120*da2e3ebdSchin /*
121*da2e3ebdSchin  * Constants for approximation in [1.71, 2.5]
122*da2e3ebdSchin */
123*da2e3ebdSchin #define a1_hi	4.227843350984671344505727574870e-01
124*da2e3ebdSchin #define a1_lo	4.670126436531227189e-18
125*da2e3ebdSchin #define p0	3.224670334241133695662995251041e-01
126*da2e3ebdSchin #define p1	3.569659696950364669021382724168e-01
127*da2e3ebdSchin #define p2	1.342918716072560025853732668111e-01
128*da2e3ebdSchin #define p3	1.950702176409779831089963408886e-02
129*da2e3ebdSchin #define p4	8.546740251667538090796227834289e-04
130*da2e3ebdSchin #define q0	1.000000000000000444089209850062e+00
131*da2e3ebdSchin #define q1	1.315850076960161985084596381057e+00
132*da2e3ebdSchin #define q2	6.274644311862156431658377186977e-01
133*da2e3ebdSchin #define q3	1.304706631926259297049597307705e-01
134*da2e3ebdSchin #define q4	1.102815279606722369265536798366e-02
135*da2e3ebdSchin #define q5	2.512690594856678929537585620579e-04
136*da2e3ebdSchin #define q6	-1.003597548112371003358107325598e-06
137*da2e3ebdSchin /*
138*da2e3ebdSchin  * Stirling's Formula, adjusted for equal-ripple. x in [6,Inf].
139*da2e3ebdSchin */
140*da2e3ebdSchin #define lns2pi	.418938533204672741780329736405
141*da2e3ebdSchin #define pb0	 8.33333333333333148296162562474e-02
142*da2e3ebdSchin #define pb1	-2.77777777774548123579378966497e-03
143*da2e3ebdSchin #define pb2	 7.93650778754435631476282786423e-04
144*da2e3ebdSchin #define pb3	-5.95235082566672847950717262222e-04
145*da2e3ebdSchin #define pb4	 8.41428560346653702135821806252e-04
146*da2e3ebdSchin #define pb5	-1.89773526463879200348872089421e-03
147*da2e3ebdSchin #define pb6	 5.69394463439411649408050664078e-03
148*da2e3ebdSchin #define pb7	-1.44705562421428915453880392761e-02
149*da2e3ebdSchin 
lgamma(double x)150*da2e3ebdSchin extern __pure double lgamma(double x)
151*da2e3ebdSchin {
152*da2e3ebdSchin 	double r;
153*da2e3ebdSchin 
154*da2e3ebdSchin 	signgam = 1;
155*da2e3ebdSchin 	endian = ((*(int *) &one)) ? 1 : 0;
156*da2e3ebdSchin 
157*da2e3ebdSchin 	if (!finite(x))
158*da2e3ebdSchin 		if (_IEEE)
159*da2e3ebdSchin 			return (x+x);
160*da2e3ebdSchin 		else return (infnan(EDOM));
161*da2e3ebdSchin 
162*da2e3ebdSchin 	if (x > 6 + RIGHT) {
163*da2e3ebdSchin 		r = large_lgam(x);
164*da2e3ebdSchin 		return (r);
165*da2e3ebdSchin 	} else if (x > 1e-16)
166*da2e3ebdSchin 		return (small_lgam(x));
167*da2e3ebdSchin 	else if (x > -1e-16) {
168*da2e3ebdSchin 		if (x < 0)
169*da2e3ebdSchin 			signgam = -1, x = -x;
170*da2e3ebdSchin 		return (-log(x));
171*da2e3ebdSchin 	} else
172*da2e3ebdSchin 		return (neg_lgam(x));
173*da2e3ebdSchin }
174*da2e3ebdSchin 
175*da2e3ebdSchin static double
large_lgam(double x)176*da2e3ebdSchin large_lgam(double x)
177*da2e3ebdSchin {
178*da2e3ebdSchin 	double z, p, x1;
179*da2e3ebdSchin 	struct Double t, u, v;
180*da2e3ebdSchin 	u = __log__D(x);
181*da2e3ebdSchin 	u.a -= 1.0;
182*da2e3ebdSchin 	if (x > 1e15) {
183*da2e3ebdSchin 		v.a = x - 0.5;
184*da2e3ebdSchin 		TRUNC(v.a);
185*da2e3ebdSchin 		v.b = (x - v.a) - 0.5;
186*da2e3ebdSchin 		t.a = u.a*v.a;
187*da2e3ebdSchin 		t.b = x*u.b + v.b*u.a;
188*da2e3ebdSchin 		if (_IEEE == 0 && !finite(t.a))
189*da2e3ebdSchin 			return(infnan(ERANGE));
190*da2e3ebdSchin 		return(t.a + t.b);
191*da2e3ebdSchin 	}
192*da2e3ebdSchin 	x1 = 1./x;
193*da2e3ebdSchin 	z = x1*x1;
194*da2e3ebdSchin 	p = pb0+z*(pb1+z*(pb2+z*(pb3+z*(pb4+z*(pb5+z*(pb6+z*pb7))))));
195*da2e3ebdSchin 					/* error in approximation = 2.8e-19 */
196*da2e3ebdSchin 
197*da2e3ebdSchin 	p = p*x1;			/* error < 2.3e-18 absolute */
198*da2e3ebdSchin 					/* 0 < p < 1/64 (at x = 5.5) */
199*da2e3ebdSchin 	v.a = x = x - 0.5;
200*da2e3ebdSchin 	TRUNC(v.a);			/* truncate v.a to 26 bits. */
201*da2e3ebdSchin 	v.b = x - v.a;
202*da2e3ebdSchin 	t.a = v.a*u.a;			/* t = (x-.5)*(log(x)-1) */
203*da2e3ebdSchin 	t.b = v.b*u.a + x*u.b;
204*da2e3ebdSchin 	t.b += p; t.b += lns2pi;	/* return t + lns2pi + p */
205*da2e3ebdSchin 	return (t.a + t.b);
206*da2e3ebdSchin }
207*da2e3ebdSchin 
208*da2e3ebdSchin static double
small_lgam(double x)209*da2e3ebdSchin small_lgam(double x)
210*da2e3ebdSchin {
211*da2e3ebdSchin 	int x_int;
212*da2e3ebdSchin 	double y, z, t, r = 0, p, q, hi, lo;
213*da2e3ebdSchin 	struct Double rr;
214*da2e3ebdSchin 	x_int = (int)(x + .5);
215*da2e3ebdSchin 	y = x - x_int;
216*da2e3ebdSchin 	if (x_int <= 2 && y > RIGHT) {
217*da2e3ebdSchin 		t = y - x0;
218*da2e3ebdSchin 		y--; x_int++;
219*da2e3ebdSchin 		goto CONTINUE;
220*da2e3ebdSchin 	} else if (y < -LEFT) {
221*da2e3ebdSchin 		t = y +(1.0-x0);
222*da2e3ebdSchin CONTINUE:
223*da2e3ebdSchin 		z = t - x0_lo;
224*da2e3ebdSchin 		p = r0+z*(r1+z*(r2+z*(r3+z*(r4+z*r5))));
225*da2e3ebdSchin 		q = s0+z*(s1+z*(s2+z*(s3+z*s4)));
226*da2e3ebdSchin 		r = t*(z*(p/q) - x0_lo);
227*da2e3ebdSchin 		t = .5*t*t;
228*da2e3ebdSchin 		z = 1.0;
229*da2e3ebdSchin 		switch (x_int) {
230*da2e3ebdSchin 		case 6:	z  = (y + 5);
231*da2e3ebdSchin 		case 5:	z *= (y + 4);
232*da2e3ebdSchin 		case 4:	z *= (y + 3);
233*da2e3ebdSchin 		case 3:	z *= (y + 2);
234*da2e3ebdSchin 			rr = __log__D(z);
235*da2e3ebdSchin 			rr.b += a0_lo; rr.a += a0_hi;
236*da2e3ebdSchin 			return(((r+rr.b)+t+rr.a));
237*da2e3ebdSchin 		case 2: return(((r+a0_lo)+t)+a0_hi);
238*da2e3ebdSchin 		case 0: r -= log1p(x);
239*da2e3ebdSchin 		default: rr = __log__D(x);
240*da2e3ebdSchin 			rr.a -= a0_hi; rr.b -= a0_lo;
241*da2e3ebdSchin 			return(((r - rr.b) + t) - rr.a);
242*da2e3ebdSchin 		}
243*da2e3ebdSchin 	} else {
244*da2e3ebdSchin 		p = p0+y*(p1+y*(p2+y*(p3+y*p4)));
245*da2e3ebdSchin 		q = q0+y*(q1+y*(q2+y*(q3+y*(q4+y*(q5+y*q6)))));
246*da2e3ebdSchin 		p = p*(y/q);
247*da2e3ebdSchin 		t = (double)(float) y;
248*da2e3ebdSchin 		z = y-t;
249*da2e3ebdSchin 		hi = (double)(float) (p+a1_hi);
250*da2e3ebdSchin 		lo = a1_hi - hi; lo += p; lo += a1_lo;
251*da2e3ebdSchin 		r = lo*y + z*hi;	/* q + r = y*(a0+p/q) */
252*da2e3ebdSchin 		q = hi*t;
253*da2e3ebdSchin 		z = 1.0;
254*da2e3ebdSchin 		switch (x_int) {
255*da2e3ebdSchin 		case 6:	z  = (y + 5);
256*da2e3ebdSchin 		case 5:	z *= (y + 4);
257*da2e3ebdSchin 		case 4:	z *= (y + 3);
258*da2e3ebdSchin 		case 3:	z *= (y + 2);
259*da2e3ebdSchin 			rr = __log__D(z);
260*da2e3ebdSchin 			r += rr.b; r += q;
261*da2e3ebdSchin 			return(rr.a + r);
262*da2e3ebdSchin 		case 2:	return (q+ r);
263*da2e3ebdSchin 		case 0: rr = __log__D(x);
264*da2e3ebdSchin 			r -= rr.b; r -= log1p(x);
265*da2e3ebdSchin 			r += q; r-= rr.a;
266*da2e3ebdSchin 			return(r);
267*da2e3ebdSchin 		default: rr = __log__D(x);
268*da2e3ebdSchin 			r -= rr.b;
269*da2e3ebdSchin 			q -= rr.a;
270*da2e3ebdSchin 			return (r+q);
271*da2e3ebdSchin 		}
272*da2e3ebdSchin 	}
273*da2e3ebdSchin }
274*da2e3ebdSchin 
275*da2e3ebdSchin static double
neg_lgam(double x)276*da2e3ebdSchin neg_lgam(double x)
277*da2e3ebdSchin {
278*da2e3ebdSchin 	int xi;
279*da2e3ebdSchin 	double y, z, one = 1.0, zero = 0.0;
280*da2e3ebdSchin 	extern double gamma();
281*da2e3ebdSchin 
282*da2e3ebdSchin 	/* avoid destructive cancellation as much as possible */
283*da2e3ebdSchin 	if (x > -170) {
284*da2e3ebdSchin 		xi = (int)x;
285*da2e3ebdSchin 		if (xi == x)
286*da2e3ebdSchin 			if (_IEEE)
287*da2e3ebdSchin 				return(one/zero);
288*da2e3ebdSchin 			else
289*da2e3ebdSchin 				return(infnan(ERANGE));
290*da2e3ebdSchin 		y = gamma(x);
291*da2e3ebdSchin 		if (y < 0)
292*da2e3ebdSchin 			y = -y, signgam = -1;
293*da2e3ebdSchin 		return (log(y));
294*da2e3ebdSchin 	}
295*da2e3ebdSchin 	z = floor(x + .5);
296*da2e3ebdSchin 	if (z == x) {		/* convention: G(-(integer)) -> +Inf */
297*da2e3ebdSchin 		if (_IEEE)
298*da2e3ebdSchin 			return (one/zero);
299*da2e3ebdSchin 		else
300*da2e3ebdSchin 			return (infnan(ERANGE));
301*da2e3ebdSchin 	}
302*da2e3ebdSchin 	y = .5*ceil(x);
303*da2e3ebdSchin 	if (y == ceil(y))
304*da2e3ebdSchin 		signgam = -1;
305*da2e3ebdSchin 	x = -x;
306*da2e3ebdSchin 	z = fabs(x + z);	/* 0 < z <= .5 */
307*da2e3ebdSchin 	if (z < .25)
308*da2e3ebdSchin 		z = sin(M_PI*z);
309*da2e3ebdSchin 	else
310*da2e3ebdSchin 		z = cos(M_PI*(0.5-z));
311*da2e3ebdSchin 	z = log(M_PI/(z*x));
312*da2e3ebdSchin 	y = large_lgam(x);
313*da2e3ebdSchin 	return (z - y);
314*da2e3ebdSchin }
315*da2e3ebdSchin 
316*da2e3ebdSchin #endif
317