Cygwin: (mostly) drop NT4 and Samba < 3.0 support
[newlib-cygwin.git] / winsup / cygwin / math / tgammal.c
blob910706db64df415651c914ae7add1c0aa630f3ad
1 /**
2 * This file has no copyright assigned and is placed in the Public Domain.
3 * This file is part of the mingw-w64 runtime package.
4 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
5 */
6 #include "cephes_mconf.h"
8 /*
9 gamma(x+2) = gamma(x+2) P(x)/Q(x)
10 0 <= x <= 1
11 Relative error
12 n=7, d=8
13 Peak error = 1.83e-20
14 Relative error spread = 8.4e-23
17 #if UNK
18 static const uLD P[8] = {
19 { { 4.212760487471622013093E-5L } },
20 { { 4.542931960608009155600E-4L } },
21 { { 4.092666828394035500949E-3L } },
22 { { 2.385363243461108252554E-2L } },
23 { { 1.113062816019361559013E-1L } },
24 { { 3.629515436640239168939E-1L } },
25 { { 8.378004301573126728826E-1L } },
26 { { 1.000000000000000000009E0L } }
28 static const uLD Q[9] = {
29 { { -1.397148517476170440917E-5L } },
30 { { 2.346584059160635244282E-4L } },
31 { { -1.237799246653152231188E-3L } },
32 { { -7.955933682494738320586E-4L } },
33 { { 2.773706565840072979165E-2L } },
34 { { -4.633887671244534213831E-2L } },
35 { { -2.243510905670329164562E-1L } },
36 { { 4.150160950588455434583E-1L } },
37 { { 9.999999999999999999908E-1L } }
39 #endif
40 #if IBMPC
41 static const uLD P[8] = {
42 { { 0x434a,0x3f22,0x2bda,0xb0b2,0x3ff0, 0x0, 0x0, 0x0 } },
43 { { 0xf5aa,0xe82f,0x335b,0xee2e,0x3ff3, 0x0, 0x0, 0x0 } },
44 { { 0xbe6c,0x3757,0xc717,0x861b,0x3ff7, 0x0, 0x0, 0x0 } },
45 { { 0x7f43,0x5196,0xb166,0xc368,0x3ff9, 0x0, 0x0, 0x0 } },
46 { { 0x9549,0x8eb5,0x8c3a,0xe3f4,0x3ffb, 0x0, 0x0, 0x0 } },
47 { { 0x8d75,0x23af,0xc8e4,0xb9d4,0x3ffd, 0x0, 0x0, 0x0 } },
48 { { 0x29cf,0x19b3,0x16c8,0xd67a,0x3ffe, 0x0, 0x0, 0x0 } },
49 { { 0x0000,0x0000,0x0000,0x8000,0x3fff, 0x0, 0x0, 0x0 } }
51 static const uLD Q[9] = {
52 { { 0x5473,0x2de8,0x1268,0xea67,0xbfee, 0x0, 0x0, 0x0 } },
53 { { 0x334b,0xc2f0,0xa2dd,0xf60e,0x3ff2, 0x0, 0x0, 0x0 } },
54 { { 0xbeed,0x1853,0xa691,0xa23d,0xbff5, 0x0, 0x0, 0x0 } },
55 { { 0x296e,0x7cb1,0x5dfd,0xd08f,0xbff4, 0x0, 0x0, 0x0 } },
56 { { 0x0417,0x7989,0xd7bc,0xe338,0x3ff9, 0x0, 0x0, 0x0 } },
57 { { 0x3295,0x3698,0xd580,0xbdcd,0xbffa, 0x0, 0x0, 0x0 } },
58 { { 0x75ef,0x3ab7,0x4ad3,0xe5bc,0xbffc, 0x0, 0x0, 0x0 } },
59 { { 0xe458,0x2ec7,0xfd57,0xd47c,0x3ffd, 0x0, 0x0, 0x0 } },
60 { { 0x0000,0x0000,0x0000,0x8000,0x3fff, 0x0, 0x0, 0x0 } }
62 #endif
63 #if MIEEE
64 static const uLD P[8] = {
65 { { 0x3ff00000,0xb0b22bda,0x3f22434a, 0 } },
66 { { 0x3ff30000,0xee2e335b,0xe82ff5aa, 0 } },
67 { { 0x3ff70000,0x861bc717,0x3757be6c, 0 } },
68 { { 0x3ff90000,0xc368b166,0x51967f43, 0 } },
69 { { 0x3ffb0000,0xe3f48c3a,0x8eb59549, 0 } },
70 { { 0x3ffd0000,0xb9d4c8e4,0x23af8d75, 0 } },
71 { { 0x3ffe0000,0xd67a16c8,0x19b329cf, 0 } },
72 { { 0x3fff0000,0x80000000,0x00000000, 0 } }
74 static const uLD Q[9] = {
75 { { 0xbfee0000,0xea671268,0x2de85473, 0 } },
76 { { 0x3ff20000,0xf60ea2dd,0xc2f0334b, 0 } },
77 { { 0xbff50000,0xa23da691,0x1853beed, 0 } },
78 { { 0xbff40000,0xd08f5dfd,0x7cb1296e, 0 } },
79 { { 0x3ff90000,0xe338d7bc,0x79890417, 0 } },
80 { { 0xbffa0000,0xbdcdd580,0x36983295, 0 } },
81 { { 0xbffc0000,0xe5bc4ad3,0x3ab775ef, 0 } },
82 { { 0x3ffd0000,0xd47cfd57,0x2ec7e458, 0 } },
83 { { 0x3fff0000,0x80000000,0x00000000, 0 } }
85 #endif
87 #define MAXGAML 1755.455L
88 /*static const long double LOGPI = 1.14472988584940017414L;*/
90 /* Stirling's formula for the gamma function
91 gamma(x) = sqrt(2 pi) x^(x-.5) exp(-x) (1 + 1/x P(1/x))
92 z(x) = x
93 13 <= x <= 1024
94 Relative error
95 n=8, d=0
96 Peak error = 9.44e-21
97 Relative error spread = 8.8e-4
99 #if UNK
100 static const uLD STIR[9] = {
101 { { 7.147391378143610789273E-4L } },
102 { { -2.363848809501759061727E-5L } },
103 { { -5.950237554056330156018E-4L } },
104 { { 6.989332260623193171870E-5L } },
105 { { 7.840334842744753003862E-4L } },
106 { { -2.294719747873185405699E-4L } },
107 { { -2.681327161876304418288E-3L } },
108 { { 3.472222222230075327854E-3L } },
109 { { 8.333333333333331800504E-2L } }
111 #endif
112 #if IBMPC
113 static const uLD STIR[9] = {
114 { { 0x6ede,0x69f7,0x54e3,0xbb5d,0x3ff4, 0, 0, 0 } },
115 { { 0xc395,0x0295,0x4443,0xc64b,0xbfef, 0, 0, 0 } },
116 { { 0xba6f,0x7c59,0x5e47,0x9bfb,0xbff4, 0, 0, 0 } },
117 { { 0x5704,0x1a39,0xb11d,0x9293,0x3ff1, 0, 0, 0 } },
118 { { 0x30b7,0x1a21,0x98b2,0xcd87,0x3ff4, 0, 0, 0 } },
119 { { 0xbef3,0x7023,0x6a08,0xf09e,0xbff2, 0, 0, 0 } },
120 { { 0x3a1c,0x5ac8,0x3478,0xafb9,0xbff6, 0, 0, 0 } },
121 { { 0xc3c9,0x906e,0x38e3,0xe38e,0x3ff6, 0, 0, 0 } },
122 { { 0xa1d5,0xaaaa,0xaaaa,0xaaaa,0x3ffb, 0, 0, 0 } }
124 #endif
125 #if MIEEE
126 static const uLD STIR[9] = {
127 { { 0x3ff40000,0xbb5d54e3,0x69f76ede, 0 } },
128 { { 0xbfef0000,0xc64b4443,0x0295c395, 0 } },
129 { { 0xbff40000,0x9bfb5e47,0x7c59ba6f, 0 } },
130 { { 0x3ff10000,0x9293b11d,0x1a395704, 0 } },
131 { { 0x3ff40000,0xcd8798b2,0x1a2130b7, 0 } },
132 { { 0xbff20000,0xf09e6a08,0x7023bef3, 0 } },
133 { { 0xbff60000,0xafb93478,0x5ac83a1c, 0 } },
134 { { 0x3ff60000,0xe38e38e3,0x906ec3c9, 0 } },
135 { { 0x3ffb0000,0xaaaaaaaa,0xaaaaa1d5, 0 } }
137 #endif
138 #define MAXSTIR 1024.0L
139 static const long double SQTPI = 2.50662827463100050242E0L;
141 /* 1/gamma(x) = z P(z)
142 * z(x) = 1/x
143 * 0 < x < 0.03125
144 * Peak relative error 4.2e-23
146 #if UNK
147 static const uLD S[9] = {
148 { { -1.193945051381510095614E-3L } },
149 { { 7.220599478036909672331E-3L } },
150 { { -9.622023360406271645744E-3L } },
151 { { -4.219773360705915470089E-2L } },
152 { { 1.665386113720805206758E-1L } },
153 { { -4.200263503403344054473E-2L } },
154 { { -6.558780715202540684668E-1L } },
155 { { 5.772156649015328608253E-1L } },
156 { { 1.000000000000000000000E0L } }
158 #endif
159 #if IBMPC
160 static const uLD S[9] = {
161 { { 0xbaeb,0xd6d3,0x25e5,0x9c7e,0xbff5, 0, 0, 0 } },
162 { { 0xfe9a,0xceb4,0xc74e,0xec9a,0x3ff7, 0, 0, 0 } },
163 { { 0x9225,0xdfef,0xb0e9,0x9da5,0xbff8, 0, 0, 0 } },
164 { { 0x10b0,0xec17,0x87dc,0xacd7,0xbffa, 0, 0, 0 } },
165 { { 0x6b8d,0x7515,0x1905,0xaa89,0x3ffc, 0, 0, 0 } },
166 { { 0xf183,0x126b,0xf47d,0xac0a,0xbffa, 0, 0, 0 } },
167 { { 0x7bf6,0x57d1,0xa013,0xa7e7,0xbffe, 0, 0, 0 } },
168 { { 0xc7a9,0x7db0,0x67e3,0x93c4,0x3ffe, 0, 0, 0 } },
169 { { 0x0000,0x0000,0x0000,0x8000,0x3fff, 0, 0, 0 } }
171 #endif
172 #if MIEEE
173 static const long S[9] = {
174 { { 0xbff50000,0x9c7e25e5,0xd6d3baeb, 0 } },
175 { { 0x3ff70000,0xec9ac74e,0xceb4fe9a, 0 } },
176 { { 0xbff80000,0x9da5b0e9,0xdfef9225, 0 } },
177 { { 0xbffa0000,0xacd787dc,0xec1710b0, 0 } },
178 { { 0x3ffc0000,0xaa891905,0x75156b8d, 0 } },
179 { { 0xbffa0000,0xac0af47d,0x126bf183, 0 } },
180 { { 0xbffe0000,0xa7e7a013,0x57d17bf6, 0 } },
181 { { 0x3ffe0000,0x93c467e3,0x7db0c7a9, 0 } },
182 { { 0x3fff0000,0x80000000,0x00000000, 0 } }
184 #endif
185 /* 1/gamma(-x) = z P(z)
186 * z(x) = 1/x
187 * 0 < x < 0.03125
188 * Peak relative error 5.16e-23
189 * Relative error spread = 2.5e-24
191 #if UNK
192 static const uLD SN[9] = {
193 { { 1.133374167243894382010E-3L } },
194 { { 7.220837261893170325704E-3L } },
195 { { 9.621911155035976733706E-3L } },
196 { { -4.219773343731191721664E-2L } },
197 { { -1.665386113944413519335E-1L } },
198 { { -4.200263503402112910504E-2L } },
199 { { 6.558780715202536547116E-1L } },
200 { { 5.772156649015328608727E-1L } },
201 { { -1.000000000000000000000E0L } }
203 #endif
204 #if IBMPC
205 static const uLD SN[9] = {
206 { { 0x5dd1,0x02de,0xb9f7,0x948d,0x3ff5, 0, 0, 0 } },
207 { { 0x989b,0xdd68,0xc5f1,0xec9c,0x3ff7, 0, 0, 0 } },
208 { { 0x2ca1,0x18f0,0x386f,0x9da5,0x3ff8, 0, 0, 0 } },
209 { { 0x783f,0x41dd,0x87d1,0xacd7,0xbffa, 0, 0, 0 } },
210 { { 0x7a5b,0xd76d,0x1905,0xaa89,0xbffc, 0, 0, 0 } },
211 { { 0x7f64,0x1234,0xf47d,0xac0a,0xbffa, 0, 0, 0 } },
212 { { 0x5e26,0x57d1,0xa013,0xa7e7,0x3ffe, 0, 0, 0 } },
213 { { 0xc7aa,0x7db0,0x67e3,0x93c4,0x3ffe, 0, 0, 0 } },
214 { { 0x0000,0x0000,0x0000,0x8000,0xbfff, 0, 0, 0 } }
216 #endif
217 #if MIEEE
218 static const uLD SN[9] = {
219 { { 0x3ff50000,0x948db9f7,0x02de5dd1, 0 } },
220 { { 0x3ff70000,0xec9cc5f1,0xdd68989b, 0 } },
221 { { 0x3ff80000,0x9da5386f,0x18f02ca1, 0 } },
222 { { 0xbffa0000,0xacd787d1,0x41dd783f, 0 } },
223 { { 0xbffc0000,0xaa891905,0xd76d7a5b, 0 } },
224 { { 0xbffa0000,0xac0af47d,0x12347f64, 0 } },
225 { { 0x3ffe0000,0xa7e7a013,0x57d15e26, 0 } },
226 { { 0x3ffe0000,0x93c467e3,0x7db0c7aa, 0 } },
227 { { 0xbfff0000,0x80000000,0x00000000, 0 } }
229 #endif
231 static long double stirf (long double);
233 /* Gamma function computed by Stirling's formula. */
235 static long double stirf(long double x)
237 long double y, w, v;
239 w = 1.0L/x;
240 /* For large x, use rational coefficients from the analytical expansion. */
241 if (x > 1024.0L)
242 w = (((((6.97281375836585777429E-5L * w
243 + 7.84039221720066627474E-4L) * w
244 - 2.29472093621399176955E-4L) * w
245 - 2.68132716049382716049E-3L) * w
246 + 3.47222222222222222222E-3L) * w
247 + 8.33333333333333333333E-2L) * w
248 + 1.0L;
249 else
250 w = 1.0L + w * polevll( w, STIR, 8 );
251 y = expl(x);
252 if (x > MAXSTIR)
253 { /* Avoid overflow in pow() */
254 v = powl(x, 0.5L * x - 0.25L);
255 y = v * (v / y);
257 else
259 y = powl(x, x - 0.5L) / y;
261 y = SQTPI * y * w;
262 return (y);
265 long double __tgammal_r(long double, int *);
267 long double __tgammal_r(long double x, int* sgngaml)
269 long double p, q, z;
270 int i;
272 *sgngaml = 1;
273 #ifdef NANS
274 if (isnanl(x))
275 return x;
276 #endif
277 #ifdef INFINITIES
278 #ifdef NANS
279 if (x == INFINITYL)
280 return (x);
281 if (x == -INFINITYL)
282 return (NANL);
283 #else
284 if (!isfinite(x))
285 return (x);
286 #endif
287 #endif
288 if (x == 0.0L)
289 return copysignl(HUGE_VALL, x);
291 q = fabsl(x);
293 if (q > 13.0L)
295 if (q > MAXGAML)
296 goto goverf;
297 if (x < 0.0L)
299 p = floorl(q);
300 if (p == q)
302 gsing:
303 _SET_ERRNO(EDOM);
304 mtherr("tgammal", SING);
305 #ifdef NANS
306 return (NAN);
307 #else
308 return (*sgngaml * MAXNUML);
309 #endif
311 i = p;
312 if ((i & 1) == 0)
313 *sgngaml = -1;
314 z = q - p;
315 if (z > 0.5L)
317 p += 1.0L;
318 z = q - p;
320 z = q * sinl(PIL * z);
321 z = fabsl(z) * stirf(q);
322 if (z <= PIL/MAXNUML)
324 goverf:
325 _SET_ERRNO(ERANGE);
326 mtherr("tgammal", OVERFLOW);
327 #ifdef INFINITIES
328 return(*sgngaml * INFINITYL);
329 #else
330 return(*sgngaml * MAXNUML);
331 #endif
333 z = PIL/z;
335 else
337 z = stirf(x);
339 return (*sgngaml * z);
342 z = 1.0L;
343 while (x >= 3.0L)
345 x -= 1.0L;
346 z *= x;
349 while (x < -0.03125L)
351 z /= x;
352 x += 1.0L;
355 if (x <= 0.03125L)
356 goto Small;
358 while (x < 2.0L)
360 z /= x;
361 x += 1.0L;
364 if (x == 2.0L)
365 return (z);
367 x -= 2.0L;
368 p = polevll( x, P, 7 );
369 q = polevll( x, Q, 8 );
370 return (z * p / q);
372 Small:
373 if (x == 0.0L)
375 goto gsing;
377 else
379 if (x < 0.0L)
381 x = -x;
382 q = z / (x * polevll(x, SN, 8));
384 else
385 q = z / (x * polevll(x, S, 8));
387 return q;
390 /* This is the C99 version. */
391 long double tgammal(long double x)
393 int local_sgngaml = 0;
394 return (__tgammal_r(x, &local_sgngaml));