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