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 #include <float.h>
48
49 #include "math.h"
50 #include "math_private.h"
51
52 /* Used in b_log.c and below. */
53 struct Double {
54 double a;
55 double b;
56 };
57
58 #include "b_log.c"
59 #include "b_exp.c"
60
61 /*
62 * The range is broken into several subranges. Each is handled by its
63 * helper functions.
64 *
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.
69 * -iota > x : neg_gam
70 *
71 * Special values:
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;
77 * +Inf: return +Inf;
78 * NaN: return NaN.
79 *
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.
83 * x < 0:
84 * Maximum observed error < 4ulp in 1,000,000 trials.
85 */
86
87 /*
88 * Constants for large x approximation (x in [6, Inf])
89 * (Accurate to 2.8*10^-19 absolute)
90 */
91
92 static const double zero = 0.;
93 static const volatile double tiny = 1e-300;
94 /*
95 * x >= 6
96 *
97 * Use the asymptotic approximation (Stirling's formula) adjusted fof
98 * equal-ripples:
99 *
100 * log(G(x)) ~= (x-0.5)*(log(x)-1) + 0.5(log(2*pi)-1) + 1/x*P(1/(x*x))
101 *
102 * Keep extra precision in multiplying (x-.5)(log(x)-1), to avoid
103 * premature round-off.
104 *
105 * Accurate to max(ulp(1/128) absolute, 2^-66 relative) error.
106 */
107 static const double
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 */
118
119 static struct Double
large_gam(double x)120 large_gam(double x)
121 {
122 double p, z, thi, tlo, xhi, xlo;
123 struct Double u;
124
125 z = 1 / (x * x);
126 p = Pa0 + z * (Pa1 + z * (Pa2 + z * (Pa3 + z * (Pa4 + z * (Pa5 +
127 z * (Pa6 + z * Pa7))))));
128 p = p / x;
129
130 u = __log__D(x);
131 u.a -= 1;
132
133 /* Split (x - 0.5) in high and low parts. */
134 x -= 0.5;
135 xhi = (float)x;
136 xlo = x - xhi;
137
138 /* Compute t = (x-.5)*(log(x)-1) in extra precision. */
139 thi = xhi * u.a;
140 tlo = xlo * u.a + x * u.b;
141
142 /* Compute thi + tlo + ln2pi_hi + ln2pi_lo + p. */
143 tlo += ln2pi_lo;
144 tlo += p;
145 u.a = ln2pi_hi + tlo;
146 u.a += thi;
147 u.b = thi - u.a;
148 u.b += ln2pi_hi;
149 u.b += tlo;
150 return (u);
151 }
152 /*
153 * Rational approximation, A0 + x * x * P(x) / Q(x), on the interval
154 * [1.066.., 2.066..] accurate to 4.25e-19.
155 *
156 * Returns r.a + r.b = a0 + (z + c)^2 * p / q, with r.a truncated.
157 */
158 static const double
159 #if 0
160 a0_hi = 8.8560319441088875e-1,
161 a0_lo = -4.9964270364690197e-17,
162 #else
163 a0_hi = 8.8560319441088875e-01, /* 0x3fec56dc, 0x82a74aef */
164 a0_lo = -4.9642368725563397e-17, /* 0xbc8c9deb, 0xaa64afc3 */
165 #endif
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;
180
181 static struct Double
ratfun_gam(double z,double c)182 ratfun_gam(double z, double c)
183 {
184 double p, q, thi, tlo;
185 struct Double r;
186
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)));
190 p = p / q;
191
192 /* Split z into high and low parts. */
193 thi = (float)z;
194 tlo = (z - thi) + c;
195 tlo *= (thi + z);
196
197 /* Split (z+c)^2 into high and low parts. */
198 thi *= thi;
199 q = thi;
200 thi = (float)thi;
201 tlo += (q - thi);
202
203 /* Split p/q into high and low parts. */
204 r.a = (float)p;
205 r.b = p - r.a;
206
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 */
212 }
213 /*
214 * x < 6
215 *
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.
219 *
220 * Good to < 1 ulp. (provably .90 ulp; .87 ulp on 1,000,000 runs.)
221 * It also has correct monotonicity.
222 */
223 static const double
224 left = -0.3955078125, /* left boundary for rat. approx */
225 x0 = 4.6163214496836236e-1; /* xmin - 1 */
226
227 static double
small_gam(double x)228 small_gam(double x)
229 {
230 double t, y, ym1;
231 struct Double yy, r;
232
233 y = x - 1;
234 if (y <= 1 + (left + x0)) {
235 yy = ratfun_gam(y - x0, 0);
236 return (yy.a + yy.b);
237 }
238
239 r.a = (float)y;
240 yy.a = r.a - 1;
241 y = y - 1 ;
242 r.b = yy.b = y - yy.a;
243
244 /* Argument reduction: G(x+1) = x*G(x) */
245 for (ym1 = y - 1; ym1 > left + x0; y = ym1--, yy.a--) {
246 t = r.a * yy.a;
247 r.b = r.a * yy.b + y * r.b;
248 r.a = (float)t;
249 r.b += (t - r.a);
250 }
251
252 /* Return r*tgamma(y). */
253 yy = ratfun_gam(y - x0, 0);
254 y = r.b * (yy.a + yy.b) + r.a * yy.b;
255 y += yy.a * r.a;
256 return (y);
257 }
258 /*
259 * Good on (0, 1+x0+left]. Accurate to 1 ulp.
260 */
261 static double
smaller_gam(double x)262 smaller_gam(double x)
263 {
264 double d, rhi, rlo, t, xhi, xlo;
265 struct Double r;
266
267 if (x < x0 + left) {
268 t = (float)x;
269 d = (t + x) * (x - t);
270 t *= t;
271 xhi = (float)(t + x);
272 xlo = x - xhi;
273 xlo += t;
274 xlo += d;
275 t = 1 - x0;
276 t += x;
277 d = 1 - x0;
278 d -= t;
279 d += x;
280 x = xhi + xlo;
281 } else {
282 xhi = (float)x;
283 xlo = x - xhi;
284 t = x - x0;
285 d = - x0 - t;
286 d += x;
287 }
288
289 r = ratfun_gam(t, d);
290 d = (float)(r.a / x);
291 r.a -= d * xhi;
292 r.a -= d * xlo;
293 r.a += r.b;
294
295 return (d + r.a / x);
296 }
297 /*
298 * x < 0
299 *
300 * Use reflection formula, G(x) = pi/(sin(pi*x)*x*G(x)).
301 * At negative integers, return NaN and raise invalid.
302 */
303 static double
neg_gam(double x)304 neg_gam(double x)
305 {
306 int sgn = 1;
307 struct Double lg, lsine;
308 double y, z;
309
310 y = ceil(x);
311 if (y == x) /* Negative integer. */
312 return ((x - x) / zero);
313
314 z = y - x;
315 if (z > 0.5)
316 z = 1 - z;
317
318 y = y / 2;
319 if (y == ceil(y))
320 sgn = -1;
321
322 if (z < 0.25)
323 z = sinpi(z);
324 else
325 z = cospi(0.5 - z);
326
327 /* Special case: G(1-x) = Inf; G(x) may be nonzero. */
328 if (x < -170) {
329
330 if (x < -190)
331 return (sgn * tiny * tiny);
332
333 y = 1 - x; /* exact: 128 < |x| < 255 */
334 lg = large_gam(y);
335 lsine = __log__D(M_PI / z); /* = TRUNC(log(u)) + small */
336 lg.a -= lsine.a; /* exact (opposite signs) */
337 lg.b -= lsine.b;
338 y = -(lg.a + lg.b);
339 z = (y + lg.a) + lg.b;
340 y = __exp__D(y, z);
341 if (sgn < 0) y = -y;
342 return (y);
343 }
344
345 y = 1 - x;
346 if (1 - y == x)
347 y = tgamma(y);
348 else /* 1-x is inexact */
349 y = - x * tgamma(-x);
350
351 if (sgn < 0) y = -y;
352 return (M_PI / (y * z));
353 }
354 /*
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,
360 *
361 * iota is a sloppy threshold to isolate x = 0.
362 */
363 static const double xmax = 171.624376956302725;
364 static const double iota = 0x1p-56;
365
366 double
tgamma(double x)367 tgamma(double x)
368 {
369 struct Double u;
370
371 if (x >= 6) {
372 if (x > xmax)
373 return (x / zero);
374 u = large_gam(x);
375 return (__exp__D(u.a, u.b));
376 }
377
378 if (x >= 1 + left + x0)
379 return (small_gam(x));
380
381 if (x > iota)
382 return (smaller_gam(x));
383
384 if (x > -iota) {
385 if (x != 0.)
386 u.a = 1 - tiny; /* raise inexact */
387 return (1 / x);
388 }
389
390 if (!isfinite(x))
391 return (x - x); /* x is NaN or -Inf */
392
393 return (neg_gam(x));
394 }
395
396 #if (LDBL_MANT_DIG == 53)
397 __weak_reference(tgamma, tgammal);
398 #endif
399