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