1cfe182f3Schristos /*-
2cfe182f3Schristos * SPDX-License-Identifier: BSD-3-Clause
3cfe182f3Schristos *
4cfe182f3Schristos * Copyright (c) 1992, 1993
5cfe182f3Schristos * The Regents of the University of California. All rights reserved.
6cfe182f3Schristos *
7cfe182f3Schristos * Redistribution and use in source and binary forms, with or without
8cfe182f3Schristos * modification, are permitted provided that the following conditions
9cfe182f3Schristos * are met:
10cfe182f3Schristos * 1. Redistributions of source code must retain the above copyright
11cfe182f3Schristos * notice, this list of conditions and the following disclaimer.
12cfe182f3Schristos * 2. Redistributions in binary form must reproduce the above copyright
13cfe182f3Schristos * notice, this list of conditions and the following disclaimer in the
14cfe182f3Schristos * documentation and/or other materials provided with the distribution.
15cfe182f3Schristos * 3. Neither the name of the University nor the names of its contributors
16cfe182f3Schristos * may be used to endorse or promote products derived from this software
17cfe182f3Schristos * without specific prior written permission.
18cfe182f3Schristos *
19cfe182f3Schristos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20cfe182f3Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21cfe182f3Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22cfe182f3Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23cfe182f3Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24cfe182f3Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25cfe182f3Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26cfe182f3Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27cfe182f3Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28cfe182f3Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29cfe182f3Schristos * SUCH DAMAGE.
30cfe182f3Schristos */
31cfe182f3Schristos
32cfe182f3Schristos /*
33cfe182f3Schristos * The original code, FreeBSD's old svn r93211, contain the following
34cfe182f3Schristos * attribution:
35cfe182f3Schristos *
36cfe182f3Schristos * This code by P. McIlroy, Oct 1992;
37cfe182f3Schristos *
38cfe182f3Schristos * The financial support of UUNET Communications Services is greatfully
39cfe182f3Schristos * acknowledged.
40cfe182f3Schristos *
41cfe182f3Schristos * bsdrc/b_tgamma.c converted to long double by Steven G. Kargl.
42cfe182f3Schristos */
43cfe182f3Schristos
44*1bb3b9e1Skre #include <sys/cdefs.h>
45*1bb3b9e1Skre
46cfe182f3Schristos /*
47cfe182f3Schristos * See bsdsrc/t_tgamma.c for implementation details.
48cfe182f3Schristos */
49cfe182f3Schristos
50cfe182f3Schristos #include <float.h>
51cfe182f3Schristos
52cfe182f3Schristos #if LDBL_MAX_EXP != 0x4000
53cfe182f3Schristos #error "Unsupported long double format"
54cfe182f3Schristos #endif
55cfe182f3Schristos
56cfe182f3Schristos #include "math.h"
57cfe182f3Schristos #include "math_private.h"
58cfe182f3Schristos
59cfe182f3Schristos /* Used in b_log.c and below. */
60cfe182f3Schristos struct LDouble {
61cfe182f3Schristos long double a;
62cfe182f3Schristos long double b;
63cfe182f3Schristos };
64cfe182f3Schristos
65cfe182f3Schristos #include "b_logl.c"
66cfe182f3Schristos #include "b_expl.c"
67cfe182f3Schristos
68cfe182f3Schristos static const double zero = 0.;
69cfe182f3Schristos static const volatile double tiny = 1e-300;
70cfe182f3Schristos /*
71cfe182f3Schristos * x >= 6
72cfe182f3Schristos *
73cfe182f3Schristos * Use the asymptotic approximation (Stirling's formula) adjusted for
74cfe182f3Schristos * equal-ripples:
75cfe182f3Schristos *
76cfe182f3Schristos * log(G(x)) ~= (x-0.5)*(log(x)-1) + 0.5(log(2*pi)-1) + 1/x*P(1/(x*x))
77cfe182f3Schristos *
78cfe182f3Schristos * Keep extra precision in multiplying (x-.5)(log(x)-1), to avoid
79cfe182f3Schristos * premature round-off.
80cfe182f3Schristos *
81cfe182f3Schristos * Accurate to max(ulp(1/128) absolute, 2^-66 relative) error.
82cfe182f3Schristos */
83cfe182f3Schristos
84cfe182f3Schristos /*
85cfe182f3Schristos * The following is a decomposition of 0.5 * (log(2*pi) - 1) into the
86cfe182f3Schristos * first 12 bits in ln2pi_hi and the trailing 64 bits in ln2pi_lo. The
87cfe182f3Schristos * variables are clearly misnamed.
88cfe182f3Schristos */
89cfe182f3Schristos static const union ieee_ext_u
90cfe182f3Schristos ln2pi_hiu = LD80C(0xd680000000000000, -2, 4.18945312500000000000e-01L),
91cfe182f3Schristos ln2pi_lou = LD80C(0xe379b414b596d687, -18, -6.77929532725821967032e-06L);
92cfe182f3Schristos #define ln2pi_hi (ln2pi_hiu.extu_ld)
93cfe182f3Schristos #define ln2pi_lo (ln2pi_lou.extu_ld)
94cfe182f3Schristos
95cfe182f3Schristos static const union ieee_ext_u
96cfe182f3Schristos Pa0u = LD80C(0xaaaaaaaaaaaaaaaa, -4, 8.33333333333333333288e-02L),
97cfe182f3Schristos Pa1u = LD80C(0xb60b60b60b5fcd59, -9, -2.77777777777776516326e-03L),
98cfe182f3Schristos Pa2u = LD80C(0xd00d00cffbb47014, -11, 7.93650793635429639018e-04L),
99cfe182f3Schristos Pa3u = LD80C(0x9c09c07c0805343e, -11, -5.95238087960599252215e-04L),
100cfe182f3Schristos Pa4u = LD80C(0xdca8d31f8e6e5e8f, -11, 8.41749082509607342883e-04L),
101cfe182f3Schristos Pa5u = LD80C(0xfb4d4289632f1638, -10, -1.91728055205541624556e-03L),
102cfe182f3Schristos Pa6u = LD80C(0xd15a4ba04078d3f8, -8, 6.38893788027752396194e-03L),
103cfe182f3Schristos Pa7u = LD80C(0xe877283110bcad95, -6, -2.83771309846297590312e-02L),
104cfe182f3Schristos Pa8u = LD80C(0x8da97eed13717af8, -3, 1.38341887683837576925e-01L),
105cfe182f3Schristos Pa9u = LD80C(0xf093b1c1584e30ce, -2, -4.69876818515470146031e-01L);
106cfe182f3Schristos #define Pa0 (Pa0u.extu_ld)
107cfe182f3Schristos #define Pa1 (Pa1u.extu_ld)
108cfe182f3Schristos #define Pa2 (Pa2u.extu_ld)
109cfe182f3Schristos #define Pa3 (Pa3u.extu_ld)
110cfe182f3Schristos #define Pa4 (Pa4u.extu_ld)
111cfe182f3Schristos #define Pa5 (Pa5u.extu_ld)
112cfe182f3Schristos #define Pa6 (Pa6u.extu_ld)
113cfe182f3Schristos #define Pa7 (Pa7u.extu_ld)
114cfe182f3Schristos #define Pa8 (Pa8u.extu_ld)
115cfe182f3Schristos #define Pa9 (Pa9u.extu_ld)
116cfe182f3Schristos
117cfe182f3Schristos static struct LDouble
large_gam(long double x)118cfe182f3Schristos large_gam(long double x)
119cfe182f3Schristos {
120cfe182f3Schristos long double p, z, thi, tlo, xhi, xlo;
121cfe182f3Schristos struct LDouble u;
122cfe182f3Schristos
123cfe182f3Schristos z = 1 / (x * x);
124cfe182f3Schristos p = Pa0 + z * (Pa1 + z * (Pa2 + z * (Pa3 + z * (Pa4 + z * (Pa5 +
125cfe182f3Schristos z * (Pa6 + z * (Pa7 + z * (Pa8 + z * Pa9))))))));
126cfe182f3Schristos p = p / x;
127cfe182f3Schristos
128cfe182f3Schristos u = __log__LD(x);
129cfe182f3Schristos u.a -= 1;
130cfe182f3Schristos
131cfe182f3Schristos /* Split (x - 0.5) in high and low parts. */
132cfe182f3Schristos x -= 0.5L;
133cfe182f3Schristos xhi = (float)x;
134cfe182f3Schristos xlo = x - xhi;
135cfe182f3Schristos
136cfe182f3Schristos /* Compute t = (x-.5)*(log(x)-1) in extra precision. */
137cfe182f3Schristos thi = xhi * u.a;
138cfe182f3Schristos tlo = xlo * u.a + x * u.b;
139cfe182f3Schristos
140cfe182f3Schristos /* Compute thi + tlo + ln2pi_hi + ln2pi_lo + p. */
141cfe182f3Schristos tlo += ln2pi_lo;
142cfe182f3Schristos tlo += p;
143cfe182f3Schristos u.a = ln2pi_hi + tlo;
144cfe182f3Schristos u.a += thi;
145cfe182f3Schristos u.b = thi - u.a;
146cfe182f3Schristos u.b += ln2pi_hi;
147cfe182f3Schristos u.b += tlo;
148cfe182f3Schristos return (u);
149cfe182f3Schristos }
150cfe182f3Schristos /*
151cfe182f3Schristos * Rational approximation, A0 + x * x * P(x) / Q(x), on the interval
152cfe182f3Schristos * [1.066.., 2.066..] accurate to 4.25e-19.
153cfe182f3Schristos *
154cfe182f3Schristos * Returns r.a + r.b = a0 + (z + c)^2 * p / q, with r.a truncated.
155cfe182f3Schristos */
156cfe182f3Schristos static const union ieee_ext_u
157cfe182f3Schristos a0_hiu = LD80C(0xe2b6e4153a57746c, -1, 8.85603194410888700265e-01L),
158cfe182f3Schristos a0_lou = LD80C(0x851566d40f32c76d, -66, 1.40907742727049706207e-20L);
159cfe182f3Schristos #define a0_hi (a0_hiu.extu_ld)
160cfe182f3Schristos #define a0_lo (a0_lou.extu_ld)
161cfe182f3Schristos
162cfe182f3Schristos static const union ieee_ext_u
163cfe182f3Schristos P0u = LD80C(0xdb629fb9bbdc1c1d, -2, 4.28486815855585429733e-01L),
164cfe182f3Schristos P1u = LD80C(0xe6f4f9f5641aa6be, -3, 2.25543885805587730552e-01L),
165cfe182f3Schristos P2u = LD80C(0xead1bd99fdaf7cc1, -6, 2.86644652514293482381e-02L),
166cfe182f3Schristos P3u = LD80C(0x9ccc8b25838ab1e0, -8, 4.78512567772456362048e-03L),
167cfe182f3Schristos P4u = LD80C(0x8f0c4383ef9ce72a, -9, 2.18273781132301146458e-03L),
168cfe182f3Schristos P5u = LD80C(0xe732ab2c0a2778da, -13, 2.20487522485636008928e-04L),
169cfe182f3Schristos P6u = LD80C(0xce70b27ca822b297, -16, 2.46095923774929264284e-05L),
170cfe182f3Schristos P7u = LD80C(0xa309e2e16fb63663, -19, 2.42946473022376182921e-06L),
171cfe182f3Schristos P8u = LD80C(0xaf9c110efb2c633d, -23, 1.63549217667765869987e-07L),
172cfe182f3Schristos Q1u = LD80C(0xd4d7422719f48f15, -1, 8.31409582658993993626e-01L),
173cfe182f3Schristos Q2u = LD80C(0xe13138ea404f1268, -5, -5.49785826915643198508e-02L),
174cfe182f3Schristos Q3u = LD80C(0xd1c6cc91989352c0, -4, -1.02429960435139887683e-01L),
175cfe182f3Schristos Q4u = LD80C(0xa7e9435a84445579, -7, 1.02484853505908820524e-02L),
176cfe182f3Schristos Q5u = LD80C(0x83c7c34db89b7bda, -8, 4.02161632832052872697e-03L),
177cfe182f3Schristos Q6u = LD80C(0xbed06bf6e1c14e5b, -11, -7.27898206351223022157e-04L),
178cfe182f3Schristos Q7u = LD80C(0xef05bf841d4504c0, -18, 7.12342421869453515194e-06L),
179cfe182f3Schristos Q8u = LD80C(0xf348d08a1ff53cb1, -19, 3.62522053809474067060e-06L);
180cfe182f3Schristos #define P0 (P0u.extu_ld)
181cfe182f3Schristos #define P1 (P1u.extu_ld)
182cfe182f3Schristos #define P2 (P2u.extu_ld)
183cfe182f3Schristos #define P3 (P3u.extu_ld)
184cfe182f3Schristos #define P4 (P4u.extu_ld)
185cfe182f3Schristos #define P5 (P5u.extu_ld)
186cfe182f3Schristos #define P6 (P6u.extu_ld)
187cfe182f3Schristos #define P7 (P7u.extu_ld)
188cfe182f3Schristos #define P8 (P8u.extu_ld)
189cfe182f3Schristos #define Q1 (Q1u.extu_ld)
190cfe182f3Schristos #define Q2 (Q2u.extu_ld)
191cfe182f3Schristos #define Q3 (Q3u.extu_ld)
192cfe182f3Schristos #define Q4 (Q4u.extu_ld)
193cfe182f3Schristos #define Q5 (Q5u.extu_ld)
194cfe182f3Schristos #define Q6 (Q6u.extu_ld)
195cfe182f3Schristos #define Q7 (Q7u.extu_ld)
196cfe182f3Schristos #define Q8 (Q8u.extu_ld)
197cfe182f3Schristos
198cfe182f3Schristos static struct LDouble
ratfun_gam(long double z,long double c)199cfe182f3Schristos ratfun_gam(long double z, long double c)
200cfe182f3Schristos {
201cfe182f3Schristos long double p, q, thi, tlo;
202cfe182f3Schristos struct LDouble r;
203cfe182f3Schristos
204cfe182f3Schristos q = 1 + z * (Q1 + z * (Q2 + z * (Q3 + z * (Q4 + z * (Q5 +
205cfe182f3Schristos z * (Q6 + z * (Q7 + z * Q8)))))));
206cfe182f3Schristos p = P0 + z * (P1 + z * (P2 + z * (P3 + z * (P4 + z * (P5 +
207cfe182f3Schristos z * (P6 + z * (P7 + z * P8)))))));
208cfe182f3Schristos p = p / q;
209cfe182f3Schristos
210cfe182f3Schristos /* Split z into high and low parts. */
211cfe182f3Schristos thi = (float)z;
212cfe182f3Schristos tlo = (z - thi) + c;
213cfe182f3Schristos tlo *= (thi + z);
214cfe182f3Schristos
215cfe182f3Schristos /* Split (z+c)^2 into high and low parts. */
216cfe182f3Schristos thi *= thi;
217cfe182f3Schristos q = thi;
218cfe182f3Schristos thi = (float)thi;
219cfe182f3Schristos tlo += (q - thi);
220cfe182f3Schristos
221cfe182f3Schristos /* Split p/q into high and low parts. */
222cfe182f3Schristos r.a = (float)p;
223cfe182f3Schristos r.b = p - r.a;
224cfe182f3Schristos
225cfe182f3Schristos tlo = tlo * p + thi * r.b + a0_lo;
226cfe182f3Schristos thi *= r.a; /* t = (z+c)^2*(P/Q) */
227cfe182f3Schristos r.a = (float)(thi + a0_hi);
228cfe182f3Schristos r.b = ((a0_hi - r.a) + thi) + tlo;
229cfe182f3Schristos return (r); /* r = a0 + t */
230cfe182f3Schristos }
231cfe182f3Schristos /*
232cfe182f3Schristos * x < 6
233cfe182f3Schristos *
234cfe182f3Schristos * Use argument reduction G(x+1) = xG(x) to reach the range [1.066124,
235cfe182f3Schristos * 2.066124]. Use a rational approximation centered at the minimum
236cfe182f3Schristos * (x0+1) to ensure monotonicity.
237cfe182f3Schristos *
238cfe182f3Schristos * Good to < 1 ulp. (provably .90 ulp; .87 ulp on 1,000,000 runs.)
239cfe182f3Schristos * It also has correct monotonicity.
240cfe182f3Schristos */
241cfe182f3Schristos static const union ieee_ext_u
242cfe182f3Schristos xm1u = LD80C(0xec5b0c6ad7c7edc3, -2, 4.61632144968362341254e-01L);
243cfe182f3Schristos #define x0 (xm1u.extu_ld)
244cfe182f3Schristos
245cfe182f3Schristos static const double
246cfe182f3Schristos left = -0.3955078125; /* left boundary for rat. approx */
247cfe182f3Schristos
248cfe182f3Schristos static long double
small_gam(long double x)249cfe182f3Schristos small_gam(long double x)
250cfe182f3Schristos {
251cfe182f3Schristos long double t, y, ym1;
252cfe182f3Schristos struct LDouble yy, r;
253cfe182f3Schristos
254cfe182f3Schristos y = x - 1;
255cfe182f3Schristos
256cfe182f3Schristos if (y <= 1 + (left + x0)) {
257cfe182f3Schristos yy = ratfun_gam(y - x0, 0);
258cfe182f3Schristos return (yy.a + yy.b);
259cfe182f3Schristos }
260cfe182f3Schristos
261cfe182f3Schristos r.a = (float)y;
262cfe182f3Schristos yy.a = r.a - 1;
263cfe182f3Schristos y = y - 1 ;
264cfe182f3Schristos r.b = yy.b = y - yy.a;
265cfe182f3Schristos
266cfe182f3Schristos /* Argument reduction: G(x+1) = x*G(x) */
267cfe182f3Schristos for (ym1 = y - 1; ym1 > left + x0; y = ym1--, yy.a--) {
268cfe182f3Schristos t = r.a * yy.a;
269cfe182f3Schristos r.b = r.a * yy.b + y * r.b;
270cfe182f3Schristos r.a = (float)t;
271cfe182f3Schristos r.b += (t - r.a);
272cfe182f3Schristos }
273cfe182f3Schristos
274cfe182f3Schristos /* Return r*tgamma(y). */
275cfe182f3Schristos yy = ratfun_gam(y - x0, 0);
276cfe182f3Schristos y = r.b * (yy.a + yy.b) + r.a * yy.b;
277cfe182f3Schristos y += yy.a * r.a;
278cfe182f3Schristos return (y);
279cfe182f3Schristos }
280cfe182f3Schristos /*
281cfe182f3Schristos * Good on (0, 1+x0+left]. Accurate to 1 ulp.
282cfe182f3Schristos */
283cfe182f3Schristos static long double
smaller_gam(long double x)284cfe182f3Schristos smaller_gam(long double x)
285cfe182f3Schristos {
286cfe182f3Schristos long double d, t, xhi, xlo;
287cfe182f3Schristos struct LDouble r;
288cfe182f3Schristos
289cfe182f3Schristos if (x < x0 + left) {
290cfe182f3Schristos t = (float)x;
291cfe182f3Schristos d = (t + x) * (x - t);
292cfe182f3Schristos t *= t;
293cfe182f3Schristos xhi = (float)(t + x);
294cfe182f3Schristos xlo = x - xhi;
295cfe182f3Schristos xlo += t;
296cfe182f3Schristos xlo += d;
297cfe182f3Schristos t = 1 - x0;
298cfe182f3Schristos t += x;
299cfe182f3Schristos d = 1 - x0;
300cfe182f3Schristos d -= t;
301cfe182f3Schristos d += x;
302cfe182f3Schristos x = xhi + xlo;
303cfe182f3Schristos } else {
304cfe182f3Schristos xhi = (float)x;
305cfe182f3Schristos xlo = x - xhi;
306cfe182f3Schristos t = x - x0;
307cfe182f3Schristos d = - x0 - t;
308cfe182f3Schristos d += x;
309cfe182f3Schristos }
310cfe182f3Schristos
311cfe182f3Schristos r = ratfun_gam(t, d);
312cfe182f3Schristos d = (float)(r.a / x);
313cfe182f3Schristos r.a -= d * xhi;
314cfe182f3Schristos r.a -= d * xlo;
315cfe182f3Schristos r.a += r.b;
316cfe182f3Schristos
317cfe182f3Schristos return (d + r.a / x);
318cfe182f3Schristos }
319cfe182f3Schristos /*
320cfe182f3Schristos * x < 0
321cfe182f3Schristos *
322cfe182f3Schristos * Use reflection formula, G(x) = pi/(sin(pi*x)*x*G(x)).
323cfe182f3Schristos * At negative integers, return NaN and raise invalid.
324cfe182f3Schristos */
325cfe182f3Schristos static const union ieee_ext_u
326cfe182f3Schristos piu = LD80C(0xc90fdaa22168c235, 1, 3.14159265358979323851e+00L);
327cfe182f3Schristos #define pi (piu.extu_ld)
328cfe182f3Schristos
329cfe182f3Schristos static long double
neg_gam(long double x)330cfe182f3Schristos neg_gam(long double x)
331cfe182f3Schristos {
332cfe182f3Schristos int sgn = 1;
333cfe182f3Schristos long double y, z;
334cfe182f3Schristos
335cfe182f3Schristos y = ceill(x);
336cfe182f3Schristos if (y == x) /* Negative integer. */
337cfe182f3Schristos return ((x - x) / zero);
338cfe182f3Schristos
339cfe182f3Schristos z = y - x;
340cfe182f3Schristos if (z > 0.5)
341cfe182f3Schristos z = 1 - z;
342cfe182f3Schristos
343cfe182f3Schristos y = y / 2;
344cfe182f3Schristos if (y == ceill(y))
345cfe182f3Schristos sgn = -1;
346cfe182f3Schristos
347cfe182f3Schristos if (z < 0.25)
348cfe182f3Schristos z = sinpil(z);
349cfe182f3Schristos else
350cfe182f3Schristos z = cospil(0.5 - z);
351cfe182f3Schristos
352cfe182f3Schristos /* Special case: G(1-x) = Inf; G(x) may be nonzero. */
353cfe182f3Schristos if (x < -1753) {
354cfe182f3Schristos
355cfe182f3Schristos if (x < -1760)
356cfe182f3Schristos return (sgn * tiny * tiny);
357cfe182f3Schristos y = expl(lgammal(x) / 2);
358cfe182f3Schristos y *= y;
359cfe182f3Schristos return (sgn < 0 ? -y : y);
360cfe182f3Schristos }
361cfe182f3Schristos
362cfe182f3Schristos
363cfe182f3Schristos y = 1 - x;
364cfe182f3Schristos if (1 - y == x)
365cfe182f3Schristos y = tgammal(y);
366cfe182f3Schristos else /* 1-x is inexact */
367cfe182f3Schristos y = - x * tgammal(-x);
368cfe182f3Schristos
369cfe182f3Schristos if (sgn < 0) y = -y;
370cfe182f3Schristos return (pi / (y * z));
371cfe182f3Schristos }
372cfe182f3Schristos /*
373cfe182f3Schristos * xmax comes from lgamma(xmax) - emax * log(2) = 0.
374cfe182f3Schristos * static const float xmax = 35.040095f
375cfe182f3Schristos * static const double xmax = 171.624376956302725;
376cfe182f3Schristos * ld80: LD80C(0xdb718c066b352e20, 10, 1.75554834290446291689e+03L),
377cfe182f3Schristos * ld128: 1.75554834290446291700388921607020320e+03L,
378cfe182f3Schristos *
379cfe182f3Schristos * iota is a sloppy threshold to isolate x = 0.
380cfe182f3Schristos */
381cfe182f3Schristos static const double xmax = 1755.54834290446291689;
382cfe182f3Schristos static const double iota = 0x1p-116;
383cfe182f3Schristos
384cfe182f3Schristos long double
tgammal(long double x)385cfe182f3Schristos tgammal(long double x)
386cfe182f3Schristos {
387cfe182f3Schristos struct LDouble u;
388cfe182f3Schristos
389cfe182f3Schristos ENTERI();
390cfe182f3Schristos
391cfe182f3Schristos if (x >= 6) {
392cfe182f3Schristos if (x > xmax)
393cfe182f3Schristos RETURNI(x / zero);
394cfe182f3Schristos u = large_gam(x);
395cfe182f3Schristos RETURNI(__exp__LD(u.a, u.b));
396cfe182f3Schristos }
397cfe182f3Schristos
398cfe182f3Schristos if (x >= 1 + left + x0)
399cfe182f3Schristos RETURNI(small_gam(x));
400cfe182f3Schristos
401cfe182f3Schristos if (x > iota)
402cfe182f3Schristos RETURNI(smaller_gam(x));
403cfe182f3Schristos
404cfe182f3Schristos if (x > -iota) {
405cfe182f3Schristos if (x != 0)
406cfe182f3Schristos u.a = 1 - tiny; /* raise inexact */
407cfe182f3Schristos RETURNI(1 / x);
408cfe182f3Schristos }
409cfe182f3Schristos
410cfe182f3Schristos if (!isfinite(x))
411cfe182f3Schristos RETURNI(x - x); /* x is NaN or -Inf */
412cfe182f3Schristos
413cfe182f3Schristos RETURNI(neg_gam(x));
414cfe182f3Schristos }
415