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