1/* origin: OpenBSD /usr/src/lib/libm/src/ld80/e_tgammal.c */
2/*
3 * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17/*
18 *      Gamma function
19 *
20 *
21 * SYNOPSIS:
22 *
23 * long double x, y, tgammal();
24 *
25 * y = tgammal( x );
26 *
27 *
28 * DESCRIPTION:
29 *
30 * Returns gamma function of the argument.  The result is
31 * correctly signed.
32 *
33 * Arguments |x| <= 13 are reduced by recurrence and the function
34 * approximated by a rational function of degree 7/8 in the
35 * interval (2,3).  Large arguments are handled by Stirling's
36 * formula. Large negative arguments are made positive using
37 * a reflection formula.
38 *
39 *
40 * ACCURACY:
41 *
42 *                      Relative error:
43 * arithmetic   domain     # trials      peak         rms
44 *    IEEE     -40,+40      10000       3.6e-19     7.9e-20
45 *    IEEE    -1755,+1755   10000       4.8e-18     6.5e-19
46 *
47 * Accuracy for large arguments is dominated by error in powl().
48 *
49 */
50
51#include "libm.h"
52
53#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
54long double tgammal(long double x) {
55    return tgamma(x);
56}
57#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384
58/*
59tgamma(x+2) = tgamma(x+2) P(x)/Q(x)
600 <= x <= 1
61Relative error
62n=7, d=8
63Peak error =  1.83e-20
64Relative error spread =  8.4e-23
65*/
66static const long double P[8] = {
67    4.212760487471622013093E-5L, 4.542931960608009155600E-4L, 4.092666828394035500949E-3L,
68    2.385363243461108252554E-2L, 1.113062816019361559013E-1L, 3.629515436640239168939E-1L,
69    8.378004301573126728826E-1L, 1.000000000000000000009E0L,
70};
71static const long double Q[9] = {
72    -1.397148517476170440917E-5L, 2.346584059160635244282E-4L, -1.237799246653152231188E-3L,
73    -7.955933682494738320586E-4L, 2.773706565840072979165E-2L, -4.633887671244534213831E-2L,
74    -2.243510905670329164562E-1L, 4.150160950588455434583E-1L, 9.999999999999999999908E-1L,
75};
76
77/*
78static const long double P[] = {
79-3.01525602666895735709e0L,
80-3.25157411956062339893e1L,
81-2.92929976820724030353e2L,
82-1.70730828800510297666e3L,
83-7.96667499622741999770e3L,
84-2.59780216007146401957e4L,
85-5.99650230220855581642e4L,
86-7.15743521530849602425e4L
87};
88static const long double Q[] = {
89 1.00000000000000000000e0L,
90-1.67955233807178858919e1L,
91 8.85946791747759881659e1L,
92 5.69440799097468430177e1L,
93-1.98526250512761318471e3L,
94 3.31667508019495079814e3L,
95 1.60577839621734713377e4L,
96-2.97045081369399940529e4L,
97-7.15743521530849602412e4L
98};
99*/
100#define MAXGAML 1755.455L
101/*static const long double LOGPI = 1.14472988584940017414L;*/
102
103/* Stirling's formula for the gamma function
104tgamma(x) = sqrt(2 pi) x^(x-.5) exp(-x) (1 + 1/x P(1/x))
105z(x) = x
10613 <= x <= 1024
107Relative error
108n=8, d=0
109Peak error =  9.44e-21
110Relative error spread =  8.8e-4
111*/
112static const long double STIR[9] = {
113    7.147391378143610789273E-4L, -2.363848809501759061727E-5L, -5.950237554056330156018E-4L,
114    6.989332260623193171870E-5L, 7.840334842744753003862E-4L, -2.294719747873185405699E-4L,
115    -2.681327161876304418288E-3L, 3.472222222230075327854E-3L, 8.333333333333331800504E-2L,
116};
117
118#define MAXSTIR 1024.0L
119static const long double SQTPI = 2.50662827463100050242E0L;
120
121/* 1/tgamma(x) = z P(z)
122 * z(x) = 1/x
123 * 0 < x < 0.03125
124 * Peak relative error 4.2e-23
125 */
126static const long double S[9] = {
127    -1.193945051381510095614E-3L, 7.220599478036909672331E-3L, -9.622023360406271645744E-3L,
128    -4.219773360705915470089E-2L, 1.665386113720805206758E-1L, -4.200263503403344054473E-2L,
129    -6.558780715202540684668E-1L, 5.772156649015328608253E-1L, 1.000000000000000000000E0L,
130};
131
132/* 1/tgamma(-x) = z P(z)
133 * z(x) = 1/x
134 * 0 < x < 0.03125
135 * Peak relative error 5.16e-23
136 * Relative error spread =  2.5e-24
137 */
138static const long double SN[9] = {
139    1.133374167243894382010E-3L, 7.220837261893170325704E-3L, 9.621911155035976733706E-3L,
140    -4.219773343731191721664E-2L, -1.665386113944413519335E-1L, -4.200263503402112910504E-2L,
141    6.558780715202536547116E-1L, 5.772156649015328608727E-1L, -1.000000000000000000000E0L,
142};
143
144static const long double PIL = 3.1415926535897932384626L;
145
146/* Gamma function computed by Stirling's formula.
147 */
148static long double stirf(long double x) {
149    long double y, w, v;
150
151    w = 1.0 / x;
152    /* For large x, use rational coefficients from the analytical expansion.  */
153    if (x > 1024.0)
154        w = (((((6.97281375836585777429E-5L * w + 7.84039221720066627474E-4L) * w -
155                2.29472093621399176955E-4L) *
156                   w -
157               2.68132716049382716049E-3L) *
158                  w +
159              3.47222222222222222222E-3L) *
160                 w +
161             8.33333333333333333333E-2L) *
162                w +
163            1.0;
164    else
165        w = 1.0 + w * __polevll(w, STIR, 8);
166    y = expl(x);
167    if (x > MAXSTIR) { /* Avoid overflow in pow() */
168        v = powl(x, 0.5L * x - 0.25L);
169        y = v * (v / y);
170    } else {
171        y = powl(x, x - 0.5L) / y;
172    }
173    y = SQTPI * y * w;
174    return y;
175}
176
177long double tgammal(long double x) {
178    long double p, q, z;
179
180    if (!isfinite(x))
181        return x + INFINITY;
182
183    q = fabsl(x);
184    if (q > 13.0) {
185        if (x < 0.0) {
186            p = floorl(q);
187            z = q - p;
188            if (z == 0)
189                return 0 / z;
190            if (q > MAXGAML) {
191                z = 0;
192            } else {
193                if (z > 0.5) {
194                    p += 1.0;
195                    z = q - p;
196                }
197                z = q * sinl(PIL * z);
198                z = fabsl(z) * stirf(q);
199                z = PIL / z;
200            }
201            if (0.5 * p == floorl(q * 0.5))
202                z = -z;
203        } else if (x > MAXGAML) {
204            z = x * 0x1p16383L;
205        } else {
206            z = stirf(x);
207        }
208        return z;
209    }
210
211    z = 1.0;
212    while (x >= 3.0) {
213        x -= 1.0;
214        z *= x;
215    }
216    while (x < -0.03125L) {
217        z /= x;
218        x += 1.0;
219    }
220    if (x <= 0.03125L)
221        goto small;
222    while (x < 2.0) {
223        z /= x;
224        x += 1.0;
225    }
226    if (x == 2.0)
227        return z;
228
229    x -= 2.0;
230    p = __polevll(x, P, 7);
231    q = __polevll(x, Q, 8);
232    z = z * p / q;
233    return z;
234
235small:
236    /* z==1 if x was originally +-0 */
237    if (x == 0 && z != 1)
238        return x / x;
239    if (x < 0.0) {
240        x = -x;
241        q = z / (x * __polevll(x, SN, 8));
242    } else
243        q = z / (x * __polevll(x, S, 8));
244    return q;
245}
246#elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384
247// TODO: broken implementation to make things compile
248long double tgammal(long double x) {
249    return tgamma(x);
250}
251#endif
252