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