2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
33 * The original code, FreeBSD's old svn r93211, contained the following
36 * This code by P. McIlroy, Oct 1992;
38 * The financial support of UUNET Communications Services is greatfully
41 * The algorithm remains, but the code has been re-arranged to facilitate
42 * porting to other precisions.
45 /* @(#)gamma.c 8.1 (Berkeley) 6/4/93 */
46 #include <sys/cdefs.h>
50 #include "math_private.h"
52 /* Used in b_log.c and below. */
62 * The range is broken into several subranges. Each is handled by its
65 * x >= 6.0: large_gam(x)
66 * 6.0 > x >= xleft: small_gam(x) where xleft = 1 + left + x0.
67 * xleft > x > iota: smaller_gam(x) where iota = 1e-17.
68 * iota > x > -itoa: Handle x near 0.
72 * -Inf: return NaN and raise invalid;
73 * negative integer: return NaN and raise invalid;
74 * other x ~< 177.79: return +-0 and raise underflow;
75 * +-0: return +-Inf and raise divide-by-zero;
76 * finite x ~> 171.63: return +Inf and raise overflow;
80 * Accuracy: tgamma(x) is accurate to within
81 * x > 0: error provably < 0.9ulp.
82 * Maximum observed in 1,000,000 trials was .87ulp.
84 * Maximum observed error < 4ulp in 1,000,000 trials.
88 * Constants for large x approximation (x in [6, Inf])
89 * (Accurate to 2.8*10^-19 absolute)
92 static const double zero = 0.;
93 static const volatile double tiny = 1e-300;
97 * Use the asymptotic approximation (Stirling's formula) adjusted fof
100 * log(G(x)) ~= (x-0.5)*(log(x)-1) + 0.5(log(2*pi)-1) + 1/x*P(1/(x*x))
102 * Keep extra precision in multiplying (x-.5)(log(x)-1), to avoid
103 * premature round-off.
105 * Accurate to max(ulp(1/128) absolute, 2^-66 relative) error.
108 ln2pi_hi = 0.41894531250000000,
109 ln2pi_lo = -6.7792953272582197e-6,
110 Pa0 = 8.3333333333333329e-02, /* 0x3fb55555, 0x55555555 */
111 Pa1 = -2.7777777777735404e-03, /* 0xbf66c16c, 0x16c145ec */
112 Pa2 = 7.9365079044114095e-04, /* 0x3f4a01a0, 0x183de82d */
113 Pa3 = -5.9523715464225254e-04, /* 0xbf438136, 0x0e681f62 */
114 Pa4 = 8.4161391899445698e-04, /* 0x3f4b93f8, 0x21042a13 */
115 Pa5 = -1.9065246069191080e-03, /* 0xbf5f3c8b, 0x357cb64e */
116 Pa6 = 5.9047708485785158e-03, /* 0x3f782f99, 0xdaf5d65f */
117 Pa7 = -1.6484018705183290e-02; /* 0xbf90e12f, 0xc4fb4df0 */
122 double p, z, thi, tlo, xhi, xlo;
126 p = Pa0 + z * (Pa1 + z * (Pa2 + z * (Pa3 + z * (Pa4 + z * (Pa5 +
127 z * (Pa6 + z * Pa7))))));
133 /* Split (x - 0.5) in high and low parts. */
138 /* Compute t = (x-.5)*(log(x)-1) in extra precision. */
140 tlo = xlo * u.a + x * u.b;
142 /* Compute thi + tlo + ln2pi_hi + ln2pi_lo + p. */
145 u.a = ln2pi_hi + tlo;
153 * Rational approximation, A0 + x * x * P(x) / Q(x), on the interval
154 * [1.066.., 2.066..] accurate to 4.25e-19.
156 * Returns r.a + r.b = a0 + (z + c)^2 * p / q, with r.a truncated.
160 a0_hi = 8.8560319441088875e-1,
161 a0_lo = -4.9964270364690197e-17,
163 a0_hi = 8.8560319441088875e-01, /* 0x3fec56dc, 0x82a74aef */
164 a0_lo = -4.9642368725563397e-17, /* 0xbc8c9deb, 0xaa64afc3 */
166 P0 = 6.2138957182182086e-1,
167 P1 = 2.6575719865153347e-1,
168 P2 = 5.5385944642991746e-3,
169 P3 = 1.3845669830409657e-3,
170 P4 = 2.4065995003271137e-3,
171 Q0 = 1.4501953125000000e+0,
172 Q1 = 1.0625852194801617e+0,
173 Q2 = -2.0747456194385994e-1,
174 Q3 = -1.4673413178200542e-1,
175 Q4 = 3.0787817615617552e-2,
176 Q5 = 5.1244934798066622e-3,
177 Q6 = -1.7601274143166700e-3,
178 Q7 = 9.3502102357378894e-5,
179 Q8 = 6.1327550747244396e-6;
182 ratfun_gam(double z, double c)
184 double p, q, thi, tlo;
187 q = Q0 + z * (Q1 + z * (Q2 + z * (Q3 + z * (Q4 + z * (Q5 +
188 z * (Q6 + z * (Q7 + z * Q8)))))));
189 p = P0 + z * (P1 + z * (P2 + z * (P3 + z * P4)));
192 /* Split z into high and low parts. */
197 /* Split (z+c)^2 into high and low parts. */
203 /* Split p/q into high and low parts. */
207 tlo = tlo * p + thi * r.b + a0_lo;
208 thi *= r.a; /* t = (z+c)^2*(P/Q) */
209 r.a = (float)(thi + a0_hi);
210 r.b = ((a0_hi - r.a) + thi) + tlo;
211 return (r); /* r = a0 + t */
216 * Use argument reduction G(x+1) = xG(x) to reach the range [1.066124,
217 * 2.066124]. Use a rational approximation centered at the minimum
218 * (x0+1) to ensure monotonicity.
220 * Good to < 1 ulp. (provably .90 ulp; .87 ulp on 1,000,000 runs.)
221 * It also has correct monotonicity.
224 left = -0.3955078125, /* left boundary for rat. approx */
225 x0 = 4.6163214496836236e-1; /* xmin - 1 */
234 if (y <= 1 + (left + x0)) {
235 yy = ratfun_gam(y - x0, 0);
236 return (yy.a + yy.b);
242 r.b = yy.b = y - yy.a;
244 /* Argument reduction: G(x+1) = x*G(x) */
245 for (ym1 = y - 1; ym1 > left + x0; y = ym1--, yy.a--) {
247 r.b = r.a * yy.b + y * r.b;
252 /* Return r*tgamma(y). */
253 yy = ratfun_gam(y - x0, 0);
254 y = r.b * (yy.a + yy.b) + r.a * yy.b;
259 * Good on (0, 1+x0+left]. Accurate to 1 ulp.
262 smaller_gam(double x)
264 double d, rhi, rlo, t, xhi, xlo;
269 d = (t + x) * (x - t);
271 xhi = (float)(t + x);
289 r = ratfun_gam(t, d);
290 d = (float)(r.a / x);
295 return (d + r.a / x);
300 * Use reflection formula, G(x) = pi/(sin(pi*x)*x*G(x)).
301 * At negative integers, return NaN and raise invalid.
307 struct Double lg, lsine;
311 if (y == x) /* Negative integer. */
312 return ((x - x) / zero);
327 /* Special case: G(1-x) = Inf; G(x) may be nonzero. */
331 return (sgn * tiny * tiny);
333 y = 1 - x; /* exact: 128 < |x| < 255 */
335 lsine = __log__D(M_PI / z); /* = TRUNC(log(u)) + small */
336 lg.a -= lsine.a; /* exact (opposite signs) */
339 z = (y + lg.a) + lg.b;
348 else /* 1-x is inexact */
349 y = - x * tgamma(-x);
352 return (M_PI / (y * z));
355 * xmax comes from lgamma(xmax) - emax * log(2) = 0.
356 * static const float xmax = 35.040095f
357 * static const double xmax = 171.624376956302725;
358 * ld80: LD80C(0xdb718c066b352e20, 10, 1.75554834290446291689e+03L),
359 * ld128: 1.75554834290446291700388921607020320e+03L,
361 * iota is a sloppy threshold to isolate x = 0.
363 static const double xmax = 171.624376956302725;
364 static const double iota = 0x1p-56;
375 return (__exp__D(u.a, u.b));
378 if (x >= 1 + left + x0)
379 return (small_gam(x));
382 return (smaller_gam(x));
386 u.a = 1 - tiny; /* raise inexact */
391 return (x - x); /* x is NaN or -Inf */
396 #if (LDBL_MANT_DIG == 53)
397 __weak_reference(tgamma, tgammal);