xref: /freebsd/lib/msun/bsdsrc/b_tgamma.c (revision 397e83df75e0fcd0d3fcb95ae4d794cb7600fc89)
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 #include <float.h>
46 
47 #include "math.h"
48 #include "math_private.h"
49 
50 /* Used in b_log.c and below. */
51 struct Double {
52 	double a;
53 	double b;
54 };
55 
56 #include "b_log.c"
57 #include "b_exp.c"
58 
59 /*
60  * The range is broken into several subranges.  Each is handled by its
61  * helper functions.
62  *
63  *         x >=   6.0: large_gam(x)
64  *   6.0 > x >= xleft: small_gam(x) where xleft = 1 + left + x0.
65  * xleft > x >   iota: smaller_gam(x) where iota = 1e-17.
66  *  iota > x >  -itoa: Handle x near 0.
67  * -iota > x         : neg_gam
68  *
69  * Special values:
70  *	-Inf:			return NaN and raise invalid;
71  *	negative integer:	return NaN and raise invalid;
72  *	other x ~< 177.79:	return +-0 and raise underflow;
73  *	+-0:			return +-Inf and raise divide-by-zero;
74  *	finite x ~> 171.63:	return +Inf and raise overflow;
75  *	+Inf:			return +Inf;
76  *	NaN: 			return NaN.
77  *
78  * Accuracy: tgamma(x) is accurate to within
79  *	x > 0:  error provably < 0.9ulp.
80  *	Maximum observed in 1,000,000 trials was .87ulp.
81  *	x < 0:
82  *	Maximum observed error < 4ulp in 1,000,000 trials.
83  */
84 
85 /*
86  * Constants for large x approximation (x in [6, Inf])
87  * (Accurate to 2.8*10^-19 absolute)
88  */
89 
90 static const double zero = 0.;
91 static const volatile double tiny = 1e-300;
92 /*
93  * x >= 6
94  *
95  * Use the asymptotic approximation (Stirling's formula) adjusted fof
96  * equal-ripples:
97  *
98  * log(G(x)) ~= (x-0.5)*(log(x)-1) + 0.5(log(2*pi)-1) + 1/x*P(1/(x*x))
99  *
100  * Keep extra precision in multiplying (x-.5)(log(x)-1), to avoid
101  * premature round-off.
102  *
103  * Accurate to max(ulp(1/128) absolute, 2^-66 relative) error.
104  */
105 static const double
106     ln2pi_hi =  0.41894531250000000,
107     ln2pi_lo = -6.7792953272582197e-6,
108     Pa0 =  8.3333333333333329e-02, /* 0x3fb55555, 0x55555555 */
109     Pa1 = -2.7777777777735404e-03, /* 0xbf66c16c, 0x16c145ec */
110     Pa2 =  7.9365079044114095e-04, /* 0x3f4a01a0, 0x183de82d */
111     Pa3 = -5.9523715464225254e-04, /* 0xbf438136, 0x0e681f62 */
112     Pa4 =  8.4161391899445698e-04, /* 0x3f4b93f8, 0x21042a13 */
113     Pa5 = -1.9065246069191080e-03, /* 0xbf5f3c8b, 0x357cb64e */
114     Pa6 =  5.9047708485785158e-03, /* 0x3f782f99, 0xdaf5d65f */
115     Pa7 = -1.6484018705183290e-02; /* 0xbf90e12f, 0xc4fb4df0 */
116 
117 static struct Double
118 large_gam(double x)
119 {
120 	double p, z, thi, tlo, xhi, xlo;
121 	struct Double u;
122 
123 	z = 1 / (x * x);
124 	p = Pa0 + z * (Pa1 + z * (Pa2 + z * (Pa3 + z * (Pa4 + z * (Pa5 +
125 	    z * (Pa6 + z * Pa7))))));
126 	p = p / x;
127 
128 	u = __log__D(x);
129 	u.a -= 1;
130 
131 	/* Split (x - 0.5) in high and low parts. */
132 	x -= 0.5;
133 	xhi = (float)x;
134 	xlo = x - xhi;
135 
136 	/* Compute  t = (x-.5)*(log(x)-1) in extra precision. */
137 	thi = xhi * u.a;
138 	tlo = xlo * u.a + x * u.b;
139 
140 	/* Compute thi + tlo + ln2pi_hi + ln2pi_lo + p. */
141 	tlo += ln2pi_lo;
142 	tlo += p;
143 	u.a = ln2pi_hi + tlo;
144 	u.a += thi;
145 	u.b = thi - u.a;
146 	u.b += ln2pi_hi;
147 	u.b += tlo;
148 	return (u);
149 }
150 /*
151  * Rational approximation, A0 + x * x * P(x) / Q(x), on the interval
152  * [1.066.., 2.066..] accurate to 4.25e-19.
153  *
154  * Returns r.a + r.b = a0 + (z + c)^2 * p / q, with r.a truncated.
155  */
156 static const double
157 #if 0
158     a0_hi =  8.8560319441088875e-1,
159     a0_lo = -4.9964270364690197e-17,
160 #else
161     a0_hi =  8.8560319441088875e-01, /* 0x3fec56dc, 0x82a74aef */
162     a0_lo = -4.9642368725563397e-17, /* 0xbc8c9deb, 0xaa64afc3 */
163 #endif
164     P0 =  6.2138957182182086e-1,
165     P1 =  2.6575719865153347e-1,
166     P2 =  5.5385944642991746e-3,
167     P3 =  1.3845669830409657e-3,
168     P4 =  2.4065995003271137e-3,
169     Q0 =  1.4501953125000000e+0,
170     Q1 =  1.0625852194801617e+0,
171     Q2 = -2.0747456194385994e-1,
172     Q3 = -1.4673413178200542e-1,
173     Q4 =  3.0787817615617552e-2,
174     Q5 =  5.1244934798066622e-3,
175     Q6 = -1.7601274143166700e-3,
176     Q7 =  9.3502102357378894e-5,
177     Q8 =  6.1327550747244396e-6;
178 
179 static struct Double
180 ratfun_gam(double z, double c)
181 {
182 	double p, q, thi, tlo;
183 	struct Double r;
184 
185 	q = Q0 + z * (Q1 + z * (Q2 + z * (Q3 + z * (Q4 + z * (Q5 +
186 	    z * (Q6 + z * (Q7 + z * Q8)))))));
187 	p = P0 + z * (P1 + z * (P2 + z * (P3 + z * P4)));
188 	p = p / q;
189 
190 	/* Split z into high and low parts. */
191 	thi = (float)z;
192 	tlo = (z - thi) + c;
193 	tlo *= (thi + z);
194 
195 	/* Split (z+c)^2 into high and low parts. */
196 	thi *= thi;
197 	q = thi;
198 	thi = (float)thi;
199 	tlo += (q - thi);
200 
201 	/* Split p/q into high and low parts. */
202 	r.a = (float)p;
203 	r.b = p - r.a;
204 
205 	tlo = tlo * p + thi * r.b + a0_lo;
206 	thi *= r.a;				/* t = (z+c)^2*(P/Q) */
207 	r.a = (float)(thi + a0_hi);
208 	r.b = ((a0_hi - r.a) + thi) + tlo;
209 	return (r);				/* r = a0 + t */
210 }
211 /*
212  * x < 6
213  *
214  * Use argument reduction G(x+1) = xG(x) to reach the range [1.066124,
215  * 2.066124].  Use a rational approximation centered at the minimum
216  * (x0+1) to ensure monotonicity.
217  *
218  * Good to < 1 ulp.  (provably .90 ulp; .87 ulp on 1,000,000 runs.)
219  * It also has correct monotonicity.
220  */
221 static const double
222     left = -0.3955078125,	/* left boundary for rat. approx */
223     x0 = 4.6163214496836236e-1;	/* xmin - 1 */
224 
225 static double
226 small_gam(double x)
227 {
228 	double t, y, ym1;
229 	struct Double yy, r;
230 
231 	y = x - 1;
232 	if (y <= 1 + (left + x0)) {
233 		yy = ratfun_gam(y - x0, 0);
234 		return (yy.a + yy.b);
235 	}
236 
237 	r.a = (float)y;
238 	yy.a = r.a - 1;
239 	y = y - 1 ;
240 	r.b = yy.b = y - yy.a;
241 
242 	/* Argument reduction: G(x+1) = x*G(x) */
243 	for (ym1 = y - 1; ym1 > left + x0; y = ym1--, yy.a--) {
244 		t = r.a * yy.a;
245 		r.b = r.a * yy.b + y * r.b;
246 		r.a = (float)t;
247 		r.b += (t - r.a);
248 	}
249 
250 	/* Return r*tgamma(y). */
251 	yy = ratfun_gam(y - x0, 0);
252 	y = r.b * (yy.a + yy.b) + r.a * yy.b;
253 	y += yy.a * r.a;
254 	return (y);
255 }
256 /*
257  * Good on (0, 1+x0+left].  Accurate to 1 ulp.
258  */
259 static double
260 smaller_gam(double x)
261 {
262 	double d, rhi, rlo, t, xhi, xlo;
263 	struct Double r;
264 
265 	if (x < x0 + left) {
266 		t = (float)x;
267 		d = (t + x) * (x - t);
268 		t *= t;
269 		xhi = (float)(t + x);
270 		xlo = x - xhi;
271 		xlo += t;
272 		xlo += d;
273 		t = 1 - x0;
274 		t += x;
275 		d = 1 - x0;
276 		d -= t;
277 		d += x;
278 		x = xhi + xlo;
279 	} else {
280 		xhi = (float)x;
281 		xlo = x - xhi;
282 		t = x - x0;
283 		d = - x0 - t;
284 		d += x;
285 	}
286 
287 	r = ratfun_gam(t, d);
288 	d = (float)(r.a / x);
289 	r.a -= d * xhi;
290 	r.a -= d * xlo;
291 	r.a += r.b;
292 
293 	return (d + r.a / x);
294 }
295 /*
296  * x < 0
297  *
298  * Use reflection formula, G(x) = pi/(sin(pi*x)*x*G(x)).
299  * At negative integers, return NaN and raise invalid.
300  */
301 static double
302 neg_gam(double x)
303 {
304 	int sgn = 1;
305 	struct Double lg, lsine;
306 	double y, z;
307 
308 	y = ceil(x);
309 	if (y == x)		/* Negative integer. */
310 		return ((x - x) / zero);
311 
312 	z = y - x;
313 	if (z > 0.5)
314 		z = 1 - z;
315 
316 	y = y / 2;
317 	if (y == ceil(y))
318 		sgn = -1;
319 
320 	if (z < 0.25)
321 		z = sinpi(z);
322 	else
323 		z = cospi(0.5 - z);
324 
325 	/* Special case: G(1-x) = Inf; G(x) may be nonzero. */
326 	if (x < -170) {
327 
328 		if (x < -190)
329 			return (sgn * tiny * tiny);
330 
331 		y = 1 - x;			/* exact: 128 < |x| < 255 */
332 		lg = large_gam(y);
333 		lsine = __log__D(M_PI / z);	/* = TRUNC(log(u)) + small */
334 		lg.a -= lsine.a;		/* exact (opposite signs) */
335 		lg.b -= lsine.b;
336 		y = -(lg.a + lg.b);
337 		z = (y + lg.a) + lg.b;
338 		y = __exp__D(y, z);
339 		if (sgn < 0) y = -y;
340 		return (y);
341 	}
342 
343 	y = 1 - x;
344 	if (1 - y == x)
345 		y = tgamma(y);
346 	else		/* 1-x is inexact */
347 		y = - x * tgamma(-x);
348 
349 	if (sgn < 0) y = -y;
350 	return (M_PI / (y * z));
351 }
352 /*
353  * xmax comes from lgamma(xmax) - emax * log(2) = 0.
354  * static const float  xmax = 35.040095f
355  * static const double xmax = 171.624376956302725;
356  * ld80: LD80C(0xdb718c066b352e20, 10, 1.75554834290446291689e+03L),
357  * ld128: 1.75554834290446291700388921607020320e+03L,
358  *
359  * iota is a sloppy threshold to isolate x = 0.
360  */
361 static const double xmax = 171.624376956302725;
362 static const double iota = 0x1p-56;
363 
364 double
365 tgamma(double x)
366 {
367 	struct Double u;
368 
369 	if (x >= 6) {
370 		if (x > xmax)
371 			return (x / zero);
372 		u = large_gam(x);
373 		return (__exp__D(u.a, u.b));
374 	}
375 
376 	if (x >= 1 + left + x0)
377 		return (small_gam(x));
378 
379 	if (x > iota)
380 		return (smaller_gam(x));
381 
382 	if (x > -iota) {
383 		if (x != 0.)
384 			u.a = 1 - tiny;	/* raise inexact */
385 		return (1 / x);
386 	}
387 
388 	if (!isfinite(x))
389 		return (x - x);		/* x is NaN or -Inf */
390 
391 	return (neg_gam(x));
392 }
393 
394 #if (LDBL_MANT_DIG == 53)
395 __weak_reference(tgamma, tgammal);
396 #endif
397