xref: /freebsd/lib/msun/bsdsrc/b_tgamma.c (revision 43a5ec4eb41567cc92586503212743d89686d78f)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * The original code, FreeBSD's old svn r93211, contained the following
34  * attribution:
35  *
36  *    This code by P. McIlroy, Oct 1992;
37  *
38  *    The financial support of UUNET Communications Services is greatfully
39  *    acknowledged.
40  *
41  *  The algorithm remains, but the code has been re-arranged to facilitate
42  *  porting to other precisions.
43  */
44 
45 /* @(#)gamma.c	8.1 (Berkeley) 6/4/93 */
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
48 
49 #include <float.h>
50 
51 #include "math.h"
52 #include "math_private.h"
53 
54 /* Used in b_log.c and below. */
55 struct Double {
56 	double a;
57 	double b;
58 };
59 
60 #include "b_log.c"
61 #include "b_exp.c"
62 
63 /*
64  * The range is broken into several subranges.  Each is handled by its
65  * helper functions.
66  *
67  *         x >=   6.0: large_gam(x)
68  *   6.0 > x >= xleft: small_gam(x) where xleft = 1 + left + x0.
69  * xleft > x >   iota: smaller_gam(x) where iota = 1e-17.
70  *  iota > x >  -itoa: Handle x near 0.
71  * -iota > x         : neg_gam
72  *
73  * Special values:
74  *	-Inf:			return NaN and raise invalid;
75  *	negative integer:	return NaN and raise invalid;
76  *	other x ~< 177.79:	return +-0 and raise underflow;
77  *	+-0:			return +-Inf and raise divide-by-zero;
78  *	finite x ~> 171.63:	return +Inf and raise overflow;
79  *	+Inf:			return +Inf;
80  *	NaN: 			return NaN.
81  *
82  * Accuracy: tgamma(x) is accurate to within
83  *	x > 0:  error provably < 0.9ulp.
84  *	Maximum observed in 1,000,000 trials was .87ulp.
85  *	x < 0:
86  *	Maximum observed error < 4ulp in 1,000,000 trials.
87  */
88 
89 /*
90  * Constants for large x approximation (x in [6, Inf])
91  * (Accurate to 2.8*10^-19 absolute)
92  */
93 
94 static const double zero = 0.;
95 static const volatile double tiny = 1e-300;
96 /*
97  * x >= 6
98  *
99  * Use the asymptotic approximation (Stirling's formula) adjusted fof
100  * equal-ripples:
101  *
102  * log(G(x)) ~= (x-0.5)*(log(x)-1) + 0.5(log(2*pi)-1) + 1/x*P(1/(x*x))
103  *
104  * Keep extra precision in multiplying (x-.5)(log(x)-1), to avoid
105  * premature round-off.
106  *
107  * Accurate to max(ulp(1/128) absolute, 2^-66 relative) error.
108  */
109 static const double
110     ln2pi_hi =  0.41894531250000000,
111     ln2pi_lo = -6.7792953272582197e-6,
112     Pa0 =  8.3333333333333329e-02, /* 0x3fb55555, 0x55555555 */
113     Pa1 = -2.7777777777735404e-03, /* 0xbf66c16c, 0x16c145ec */
114     Pa2 =  7.9365079044114095e-04, /* 0x3f4a01a0, 0x183de82d */
115     Pa3 = -5.9523715464225254e-04, /* 0xbf438136, 0x0e681f62 */
116     Pa4 =  8.4161391899445698e-04, /* 0x3f4b93f8, 0x21042a13 */
117     Pa5 = -1.9065246069191080e-03, /* 0xbf5f3c8b, 0x357cb64e */
118     Pa6 =  5.9047708485785158e-03, /* 0x3f782f99, 0xdaf5d65f */
119     Pa7 = -1.6484018705183290e-02; /* 0xbf90e12f, 0xc4fb4df0 */
120 
121 static struct Double
122 large_gam(double x)
123 {
124 	double p, z, thi, tlo, xhi, xlo;
125 	struct Double u;
126 
127 	z = 1 / (x * x);
128 	p = Pa0 + z * (Pa1 + z * (Pa2 + z * (Pa3 + z * (Pa4 + z * (Pa5 +
129 	    z * (Pa6 + z * Pa7))))));
130 	p = p / x;
131 
132 	u = __log__D(x);
133 	u.a -= 1;
134 
135 	/* Split (x - 0.5) in high and low parts. */
136 	x -= 0.5;
137 	xhi = (float)x;
138 	xlo = x - xhi;
139 
140 	/* Compute  t = (x-.5)*(log(x)-1) in extra precision. */
141 	thi = xhi * u.a;
142 	tlo = xlo * u.a + x * u.b;
143 
144 	/* Compute thi + tlo + ln2pi_hi + ln2pi_lo + p. */
145 	tlo += ln2pi_lo;
146 	tlo += p;
147 	u.a = ln2pi_hi + tlo;
148 	u.a += thi;
149 	u.b = thi - u.a;
150 	u.b += ln2pi_hi;
151 	u.b += tlo;
152 	return (u);
153 }
154 /*
155  * Rational approximation, A0 + x * x * P(x) / Q(x), on the interval
156  * [1.066.., 2.066..] accurate to 4.25e-19.
157  *
158  * Returns r.a + r.b = a0 + (z + c)^2 * p / q, with r.a truncated.
159  */
160 static const double
161 #if 0
162     a0_hi =  8.8560319441088875e-1,
163     a0_lo = -4.9964270364690197e-17,
164 #else
165     a0_hi =  8.8560319441088875e-01, /* 0x3fec56dc, 0x82a74aef */
166     a0_lo = -4.9642368725563397e-17, /* 0xbc8c9deb, 0xaa64afc3 */
167 #endif
168     P0 =  6.2138957182182086e-1,
169     P1 =  2.6575719865153347e-1,
170     P2 =  5.5385944642991746e-3,
171     P3 =  1.3845669830409657e-3,
172     P4 =  2.4065995003271137e-3,
173     Q0 =  1.4501953125000000e+0,
174     Q1 =  1.0625852194801617e+0,
175     Q2 = -2.0747456194385994e-1,
176     Q3 = -1.4673413178200542e-1,
177     Q4 =  3.0787817615617552e-2,
178     Q5 =  5.1244934798066622e-3,
179     Q6 = -1.7601274143166700e-3,
180     Q7 =  9.3502102357378894e-5,
181     Q8 =  6.1327550747244396e-6;
182 
183 static struct Double
184 ratfun_gam(double z, double c)
185 {
186 	double p, q, thi, tlo;
187 	struct Double r;
188 
189 	q = Q0 + z * (Q1 + z * (Q2 + z * (Q3 + z * (Q4 + z * (Q5 +
190 	    z * (Q6 + z * (Q7 + z * Q8)))))));
191 	p = P0 + z * (P1 + z * (P2 + z * (P3 + z * P4)));
192 	p = p / q;
193 
194 	/* Split z into high and low parts. */
195 	thi = (float)z;
196 	tlo = (z - thi) + c;
197 	tlo *= (thi + z);
198 
199 	/* Split (z+c)^2 into high and low parts. */
200 	thi *= thi;
201 	q = thi;
202 	thi = (float)thi;
203 	tlo += (q - thi);
204 
205 	/* Split p/q into high and low parts. */
206 	r.a = (float)p;
207 	r.b = p - r.a;
208 
209 	tlo = tlo * p + thi * r.b + a0_lo;
210 	thi *= r.a;				/* t = (z+c)^2*(P/Q) */
211 	r.a = (float)(thi + a0_hi);
212 	r.b = ((a0_hi - r.a) + thi) + tlo;
213 	return (r);				/* r = a0 + t */
214 }
215 /*
216  * x < 6
217  *
218  * Use argument reduction G(x+1) = xG(x) to reach the range [1.066124,
219  * 2.066124].  Use a rational approximation centered at the minimum
220  * (x0+1) to ensure monotonicity.
221  *
222  * Good to < 1 ulp.  (provably .90 ulp; .87 ulp on 1,000,000 runs.)
223  * It also has correct monotonicity.
224  */
225 static const double
226     left = -0.3955078125,	/* left boundary for rat. approx */
227     x0 = 4.6163214496836236e-1;	/* xmin - 1 */
228 
229 static double
230 small_gam(double x)
231 {
232 	double t, y, ym1;
233 	struct Double yy, r;
234 
235 	y = x - 1;
236 	if (y <= 1 + (left + x0)) {
237 		yy = ratfun_gam(y - x0, 0);
238 		return (yy.a + yy.b);
239 	}
240 
241 	r.a = (float)y;
242 	yy.a = r.a - 1;
243 	y = y - 1 ;
244 	r.b = yy.b = y - yy.a;
245 
246 	/* Argument reduction: G(x+1) = x*G(x) */
247 	for (ym1 = y - 1; ym1 > left + x0; y = ym1--, yy.a--) {
248 		t = r.a * yy.a;
249 		r.b = r.a * yy.b + y * r.b;
250 		r.a = (float)t;
251 		r.b += (t - r.a);
252 	}
253 
254 	/* Return r*tgamma(y). */
255 	yy = ratfun_gam(y - x0, 0);
256 	y = r.b * (yy.a + yy.b) + r.a * yy.b;
257 	y += yy.a * r.a;
258 	return (y);
259 }
260 /*
261  * Good on (0, 1+x0+left].  Accurate to 1 ulp.
262  */
263 static double
264 smaller_gam(double x)
265 {
266 	double d, rhi, rlo, t, xhi, xlo;
267 	struct Double r;
268 
269 	if (x < x0 + left) {
270 		t = (float)x;
271 		d = (t + x) * (x - t);
272 		t *= t;
273 		xhi = (float)(t + x);
274 		xlo = x - xhi;
275 		xlo += t;
276 		xlo += d;
277 		t = 1 - x0;
278 		t += x;
279 		d = 1 - x0;
280 		d -= t;
281 		d += x;
282 		x = xhi + xlo;
283 	} else {
284 		xhi = (float)x;
285 		xlo = x - xhi;
286 		t = x - x0;
287 		d = - x0 - t;
288 		d += x;
289 	}
290 
291 	r = ratfun_gam(t, d);
292 	d = (float)(r.a / x);
293 	r.a -= d * xhi;
294 	r.a -= d * xlo;
295 	r.a += r.b;
296 
297 	return (d + r.a / x);
298 }
299 /*
300  * x < 0
301  *
302  * Use reflection formula, G(x) = pi/(sin(pi*x)*x*G(x)).
303  * At negative integers, return NaN and raise invalid.
304  */
305 static double
306 neg_gam(double x)
307 {
308 	int sgn = 1;
309 	struct Double lg, lsine;
310 	double y, z;
311 
312 	y = ceil(x);
313 	if (y == x)		/* Negative integer. */
314 		return ((x - x) / zero);
315 
316 	z = y - x;
317 	if (z > 0.5)
318 		z = 1 - z;
319 
320 	y = y / 2;
321 	if (y == ceil(y))
322 		sgn = -1;
323 
324 	if (z < 0.25)
325 		z = sinpi(z);
326 	else
327 		z = cospi(0.5 - z);
328 
329 	/* Special case: G(1-x) = Inf; G(x) may be nonzero. */
330 	if (x < -170) {
331 
332 		if (x < -190)
333 			return (sgn * tiny * tiny);
334 
335 		y = 1 - x;			/* exact: 128 < |x| < 255 */
336 		lg = large_gam(y);
337 		lsine = __log__D(M_PI / z);	/* = TRUNC(log(u)) + small */
338 		lg.a -= lsine.a;		/* exact (opposite signs) */
339 		lg.b -= lsine.b;
340 		y = -(lg.a + lg.b);
341 		z = (y + lg.a) + lg.b;
342 		y = __exp__D(y, z);
343 		if (sgn < 0) y = -y;
344 		return (y);
345 	}
346 
347 	y = 1 - x;
348 	if (1 - y == x)
349 		y = tgamma(y);
350 	else		/* 1-x is inexact */
351 		y = - x * tgamma(-x);
352 
353 	if (sgn < 0) y = -y;
354 	return (M_PI / (y * z));
355 }
356 /*
357  * xmax comes from lgamma(xmax) - emax * log(2) = 0.
358  * static const float  xmax = 35.040095f
359  * static const double xmax = 171.624376956302725;
360  * ld80: LD80C(0xdb718c066b352e20, 10, 1.75554834290446291689e+03L),
361  * ld128: 1.75554834290446291700388921607020320e+03L,
362  *
363  * iota is a sloppy threshold to isolate x = 0.
364  */
365 static const double xmax = 171.624376956302725;
366 static const double iota = 0x1p-56;
367 
368 double
369 tgamma(double x)
370 {
371 	struct Double u;
372 
373 	if (x >= 6) {
374 		if (x > xmax)
375 			return (x / zero);
376 		u = large_gam(x);
377 		return (__exp__D(u.a, u.b));
378 	}
379 
380 	if (x >= 1 + left + x0)
381 		return (small_gam(x));
382 
383 	if (x > iota)
384 		return (smaller_gam(x));
385 
386 	if (x > -iota) {
387 		if (x != 0.)
388 			u.a = 1 - tiny;	/* raise inexact */
389 		return (1 / x);
390 	}
391 
392 	if (!isfinite(x))
393 		return (x - x);		/* x is NaN or -Inf */
394 
395 	return (neg_gam(x));
396 }
397 
398 #if (LDBL_MANT_DIG == 53)
399 __weak_reference(tgamma, tgammal);
400 #endif
401