blob: 2b354a7c13245a4dba257a298581a0fa24bac4cf [file] [log] [blame]
Rich Felkerb69f6952012-03-13 01:17:53 -04001/* origin: OpenBSD /usr/src/lib/libm/src/ld80/e_lgammal.c */
2/*
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5 *
6 * Developed at SunPro, a Sun Microsystems, Inc. business.
7 * Permission to use, copy, modify, and distribute this
8 * software is freely granted, provided that this notice
9 * is preserved.
10 * ====================================================
11 */
12/*
13 * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
14 *
15 * Permission to use, copy, modify, and distribute this software for any
16 * purpose with or without fee is hereby granted, provided that the above
17 * copyright notice and this permission notice appear in all copies.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
20 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
22 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
23 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
24 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
25 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 */
27/* lgammal(x)
28 * Reentrant version of the logarithm of the Gamma function
29 * with user provide pointer for the sign of Gamma(x).
30 *
31 * Method:
32 * 1. Argument Reduction for 0 < x <= 8
33 * Since gamma(1+s)=s*gamma(s), for x in [0,8], we may
34 * reduce x to a number in [1.5,2.5] by
35 * lgamma(1+s) = log(s) + lgamma(s)
36 * for example,
37 * lgamma(7.3) = log(6.3) + lgamma(6.3)
38 * = log(6.3*5.3) + lgamma(5.3)
39 * = log(6.3*5.3*4.3*3.3*2.3) + lgamma(2.3)
40 * 2. Polynomial approximation of lgamma around its
41 * minimun ymin=1.461632144968362245 to maintain monotonicity.
42 * On [ymin-0.23, ymin+0.27] (i.e., [1.23164,1.73163]), use
43 * Let z = x-ymin;
44 * lgamma(x) = -1.214862905358496078218 + z^2*poly(z)
45 * 2. Rational approximation in the primary interval [2,3]
46 * We use the following approximation:
47 * s = x-2.0;
48 * lgamma(x) = 0.5*s + s*P(s)/Q(s)
49 * Our algorithms are based on the following observation
50 *
51 * zeta(2)-1 2 zeta(3)-1 3
52 * lgamma(2+s) = s*(1-Euler) + --------- * s - --------- * s + ...
53 * 2 3
54 *
55 * where Euler = 0.5771... is the Euler constant, which is very
56 * close to 0.5.
57 *
58 * 3. For x>=8, we have
59 * lgamma(x)~(x-0.5)log(x)-x+0.5*log(2pi)+1/(12x)-1/(360x**3)+....
60 * (better formula:
61 * lgamma(x)~(x-0.5)*(log(x)-1)-.5*(log(2pi)-1) + ...)
62 * Let z = 1/x, then we approximation
63 * f(z) = lgamma(x) - (x-0.5)(log(x)-1)
64 * by
65 * 3 5 11
66 * w = w0 + w1*z + w2*z + w3*z + ... + w6*z
67 *
68 * 4. For negative x, since (G is gamma function)
69 * -x*G(-x)*G(x) = pi/sin(pi*x),
70 * we have
71 * G(x) = pi/(sin(pi*x)*(-x)*G(-x))
72 * since G(-x) is positive, sign(G(x)) = sign(sin(pi*x)) for x<0
73 * Hence, for x<0, signgam = sign(sin(pi*x)) and
74 * lgamma(x) = log(|Gamma(x)|)
75 * = log(pi/(|x*sin(pi*x)|)) - lgamma(-x);
76 * Note: one should avoid compute pi*(-x) directly in the
77 * computation of sin(pi*(-x)).
78 *
79 * 5. Special Cases
80 * lgamma(2+s) ~ s*(1-Euler) for tiny s
81 * lgamma(1)=lgamma(2)=0
82 * lgamma(x) ~ -log(x) for tiny x
83 * lgamma(0) = lgamma(inf) = inf
84 * lgamma(-integer) = +-inf
85 *
86 */
87
nsz40305f72012-03-15 09:29:53 +010088#define _GNU_SOURCE
Rich Felkerb69f6952012-03-13 01:17:53 -040089#include "libm.h"
Szabolcs Nagyafa2aac2013-09-05 14:03:10 +000090#include "libc.h"
Rich Felkerb69f6952012-03-13 01:17:53 -040091
Rich Felkerb69f6952012-03-13 01:17:53 -040092#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
nsz1b229a22012-03-27 22:12:20 +020093double __lgamma_r(double x, int *sg);
94
Rich Felkerde7db6e2012-03-16 21:16:32 -040095long double __lgammal_r(long double x, int *sg)
Rich Felkerb69f6952012-03-13 01:17:53 -040096{
Rich Felkerde7db6e2012-03-16 21:16:32 -040097 return __lgamma_r(x, sg);
Rich Felkerb69f6952012-03-13 01:17:53 -040098}
99#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384
100static const long double
Rich Felkerb69f6952012-03-13 01:17:53 -0400101pi = 3.14159265358979323846264L,
Rich Felkerb69f6952012-03-13 01:17:53 -0400102
103/* lgam(1+x) = 0.5 x + x a(x)/b(x)
104 -0.268402099609375 <= x <= 0
105 peak relative error 6.6e-22 */
106a0 = -6.343246574721079391729402781192128239938E2L,
107a1 = 1.856560238672465796768677717168371401378E3L,
108a2 = 2.404733102163746263689288466865843408429E3L,
109a3 = 8.804188795790383497379532868917517596322E2L,
110a4 = 1.135361354097447729740103745999661157426E2L,
111a5 = 3.766956539107615557608581581190400021285E0L,
112
113b0 = 8.214973713960928795704317259806842490498E3L,
114b1 = 1.026343508841367384879065363925870888012E4L,
115b2 = 4.553337477045763320522762343132210919277E3L,
116b3 = 8.506975785032585797446253359230031874803E2L,
117b4 = 6.042447899703295436820744186992189445813E1L,
118/* b5 = 1.000000000000000000000000000000000000000E0 */
119
120
121tc = 1.4616321449683623412626595423257213284682E0L,
122tf = -1.2148629053584961146050602565082954242826E-1, /* double precision */
123/* tt = (tail of tf), i.e. tf + tt has extended precision. */
124tt = 3.3649914684731379602768989080467587736363E-18L,
125/* lgam ( 1.4616321449683623412626595423257213284682E0 ) =
126-1.2148629053584960809551455717769158215135617312999903886372437313313530E-1 */
127
128/* lgam (x + tc) = tf + tt + x g(x)/h(x)
129 -0.230003726999612341262659542325721328468 <= x
130 <= 0.2699962730003876587373404576742786715318
131 peak relative error 2.1e-21 */
132g0 = 3.645529916721223331888305293534095553827E-18L,
133g1 = 5.126654642791082497002594216163574795690E3L,
134g2 = 8.828603575854624811911631336122070070327E3L,
135g3 = 5.464186426932117031234820886525701595203E3L,
136g4 = 1.455427403530884193180776558102868592293E3L,
137g5 = 1.541735456969245924860307497029155838446E2L,
138g6 = 4.335498275274822298341872707453445815118E0L,
139
140h0 = 1.059584930106085509696730443974495979641E4L,
141h1 = 2.147921653490043010629481226937850618860E4L,
142h2 = 1.643014770044524804175197151958100656728E4L,
143h3 = 5.869021995186925517228323497501767586078E3L,
144h4 = 9.764244777714344488787381271643502742293E2L,
145h5 = 6.442485441570592541741092969581997002349E1L,
146/* h6 = 1.000000000000000000000000000000000000000E0 */
147
148
149/* lgam (x+1) = -0.5 x + x u(x)/v(x)
150 -0.100006103515625 <= x <= 0.231639862060546875
151 peak relative error 1.3e-21 */
152u0 = -8.886217500092090678492242071879342025627E1L,
153u1 = 6.840109978129177639438792958320783599310E2L,
154u2 = 2.042626104514127267855588786511809932433E3L,
155u3 = 1.911723903442667422201651063009856064275E3L,
156u4 = 7.447065275665887457628865263491667767695E2L,
157u5 = 1.132256494121790736268471016493103952637E2L,
158u6 = 4.484398885516614191003094714505960972894E0L,
159
160v0 = 1.150830924194461522996462401210374632929E3L,
161v1 = 3.399692260848747447377972081399737098610E3L,
162v2 = 3.786631705644460255229513563657226008015E3L,
163v3 = 1.966450123004478374557778781564114347876E3L,
164v4 = 4.741359068914069299837355438370682773122E2L,
165v5 = 4.508989649747184050907206782117647852364E1L,
166/* v6 = 1.000000000000000000000000000000000000000E0 */
167
168
169/* lgam (x+2) = .5 x + x s(x)/r(x)
170 0 <= x <= 1
171 peak relative error 7.2e-22 */
172s0 = 1.454726263410661942989109455292824853344E6L,
173s1 = -3.901428390086348447890408306153378922752E6L,
174s2 = -6.573568698209374121847873064292963089438E6L,
175s3 = -3.319055881485044417245964508099095984643E6L,
176s4 = -7.094891568758439227560184618114707107977E5L,
177s5 = -6.263426646464505837422314539808112478303E4L,
178s6 = -1.684926520999477529949915657519454051529E3L,
179
180r0 = -1.883978160734303518163008696712983134698E7L,
181r1 = -2.815206082812062064902202753264922306830E7L,
182r2 = -1.600245495251915899081846093343626358398E7L,
183r3 = -4.310526301881305003489257052083370058799E6L,
184r4 = -5.563807682263923279438235987186184968542E5L,
185r5 = -3.027734654434169996032905158145259713083E4L,
186r6 = -4.501995652861105629217250715790764371267E2L,
187/* r6 = 1.000000000000000000000000000000000000000E0 */
188
189
190/* lgam(x) = ( x - 0.5 ) * log(x) - x + LS2PI + 1/x w(1/x^2)
191 x >= 8
192 Peak relative error 1.51e-21
193w0 = LS2PI - 0.5 */
194w0 = 4.189385332046727417803e-1L,
195w1 = 8.333333333333331447505E-2L,
196w2 = -2.777777777750349603440E-3L,
197w3 = 7.936507795855070755671E-4L,
198w4 = -5.952345851765688514613E-4L,
199w5 = 8.412723297322498080632E-4L,
200w6 = -1.880801938119376907179E-3L,
201w7 = 4.885026142432270781165E-3L;
202
Szabolcs Nagyebbaf212013-11-21 01:01:57 +0000203/* sin(pi*x) assuming x > 2^-1000, if sin(pi*x)==0 the sign is arbitrary */
Rich Felkerb69f6952012-03-13 01:17:53 -0400204static long double sin_pi(long double x)
205{
Szabolcs Nagy34660d72013-09-04 15:52:23 +0000206 int n;
Rich Felkerb69f6952012-03-13 01:17:53 -0400207
Szabolcs Nagyebbaf212013-11-21 01:01:57 +0000208 /* spurious inexact if odd int */
209 x *= 0.5;
210 x = 2.0*(x - floorl(x)); /* x mod 2.0 */
Rich Felkerb69f6952012-03-13 01:17:53 -0400211
Szabolcs Nagyebbaf212013-11-21 01:01:57 +0000212 n = (int)(x*4.0);
213 n = (n+1)/2;
214 x -= n*0.5f;
215 x *= pi;
Rich Felkerb69f6952012-03-13 01:17:53 -0400216
217 switch (n) {
Szabolcs Nagyebbaf212013-11-21 01:01:57 +0000218 default: /* case 4: */
219 case 0: return __sinl(x, 0.0, 0);
220 case 1: return __cosl(x, 0.0);
221 case 2: return __sinl(-x, 0.0, 0);
222 case 3: return -__cosl(x, 0.0);
Rich Felkerb69f6952012-03-13 01:17:53 -0400223 }
Rich Felkerb69f6952012-03-13 01:17:53 -0400224}
225
Rich Felkerde7db6e2012-03-16 21:16:32 -0400226long double __lgammal_r(long double x, int *sg) {
Rich Felkerb69f6952012-03-13 01:17:53 -0400227 long double t, y, z, nadj, p, p1, p2, q, r, w;
Szabolcs Nagy34660d72013-09-04 15:52:23 +0000228 union ldshape u = {x};
229 uint32_t ix = (u.i.se & 0x7fffU)<<16 | u.i.m>>48;
230 int sign = u.i.se >> 15;
231 int i;
Rich Felkerb69f6952012-03-13 01:17:53 -0400232
233 *sg = 1;
Rich Felkerb69f6952012-03-13 01:17:53 -0400234
Szabolcs Nagyebbaf212013-11-21 01:01:57 +0000235 /* purge off +-inf, NaN, +-0, tiny and negative arguments */
Rich Felkerb69f6952012-03-13 01:17:53 -0400236 if (ix >= 0x7fff0000)
237 return x * x;
Rich Felkerb69f6952012-03-13 01:17:53 -0400238 if (ix < 0x3fc08000) { /* |x|<2**-63, return -log(|x|) */
Szabolcs Nagy34660d72013-09-04 15:52:23 +0000239 if (sign) {
Rich Felkerb69f6952012-03-13 01:17:53 -0400240 *sg = -1;
Szabolcs Nagyebbaf212013-11-21 01:01:57 +0000241 x = -x;
Rich Felkerb69f6952012-03-13 01:17:53 -0400242 }
243 return -logl(x);
244 }
Szabolcs Nagy34660d72013-09-04 15:52:23 +0000245 if (sign) {
Rich Felkerb69f6952012-03-13 01:17:53 -0400246 x = -x;
Szabolcs Nagyebbaf212013-11-21 01:01:57 +0000247 t = sin_pi(x);
248 if (t == 0.0)
249 return 1.0 / (x-x); /* -integer */
250 if (t > 0.0)
251 *sg = -1;
252 else
253 t = -t;
254 nadj = logl(pi / (t * x));
Rich Felkerb69f6952012-03-13 01:17:53 -0400255 }
256
Szabolcs Nagyebbaf212013-11-21 01:01:57 +0000257 /* purge off 1 and 2 (so the sign is ok with downward rounding) */
258 if ((ix == 0x3fff8000 || ix == 0x40008000) && u.i.m == 0) {
259 r = 0;
260 } else if (ix < 0x40008000) { /* x < 2.0 */
Rich Felkerb69f6952012-03-13 01:17:53 -0400261 if (ix <= 0x3ffee666) { /* 8.99993896484375e-1 */
262 /* lgamma(x) = lgamma(x+1) - log(x) */
nsz0cbb6542012-03-19 23:41:19 +0100263 r = -logl(x);
Rich Felkerb69f6952012-03-13 01:17:53 -0400264 if (ix >= 0x3ffebb4a) { /* 7.31597900390625e-1 */
nsz0cbb6542012-03-19 23:41:19 +0100265 y = x - 1.0;
Rich Felkerb69f6952012-03-13 01:17:53 -0400266 i = 0;
267 } else if (ix >= 0x3ffced33) { /* 2.31639862060546875e-1 */
nsz0cbb6542012-03-19 23:41:19 +0100268 y = x - (tc - 1.0);
Rich Felkerb69f6952012-03-13 01:17:53 -0400269 i = 1;
270 } else { /* x < 0.23 */
271 y = x;
272 i = 2;
273 }
274 } else {
nsz0cbb6542012-03-19 23:41:19 +0100275 r = 0.0;
Rich Felkerb69f6952012-03-13 01:17:53 -0400276 if (ix >= 0x3fffdda6) { /* 1.73162841796875 */
277 /* [1.7316,2] */
278 y = x - 2.0;
279 i = 0;
280 } else if (ix >= 0x3fff9da6) { /* 1.23162841796875 */
281 /* [1.23,1.73] */
282 y = x - tc;
283 i = 1;
284 } else {
285 /* [0.9, 1.23] */
nsz0cbb6542012-03-19 23:41:19 +0100286 y = x - 1.0;
Rich Felkerb69f6952012-03-13 01:17:53 -0400287 i = 2;
288 }
289 }
290 switch (i) {
291 case 0:
292 p1 = a0 + y * (a1 + y * (a2 + y * (a3 + y * (a4 + y * a5))));
293 p2 = b0 + y * (b1 + y * (b2 + y * (b3 + y * (b4 + y))));
nsz0cbb6542012-03-19 23:41:19 +0100294 r += 0.5 * y + y * p1/p2;
Rich Felkerb69f6952012-03-13 01:17:53 -0400295 break;
296 case 1:
297 p1 = g0 + y * (g1 + y * (g2 + y * (g3 + y * (g4 + y * (g5 + y * g6)))));
298 p2 = h0 + y * (h1 + y * (h2 + y * (h3 + y * (h4 + y * (h5 + y)))));
299 p = tt + y * p1/p2;
300 r += (tf + p);
301 break;
302 case 2:
303 p1 = y * (u0 + y * (u1 + y * (u2 + y * (u3 + y * (u4 + y * (u5 + y * u6))))));
304 p2 = v0 + y * (v1 + y * (v2 + y * (v3 + y * (v4 + y * (v5 + y)))));
nsz0cbb6542012-03-19 23:41:19 +0100305 r += (-0.5 * y + p1 / p2);
Rich Felkerb69f6952012-03-13 01:17:53 -0400306 }
307 } else if (ix < 0x40028000) { /* 8.0 */
308 /* x < 8.0 */
309 i = (int)x;
Rich Felkerb69f6952012-03-13 01:17:53 -0400310 y = x - (double)i;
311 p = y * (s0 + y * (s1 + y * (s2 + y * (s3 + y * (s4 + y * (s5 + y * s6))))));
312 q = r0 + y * (r1 + y * (r2 + y * (r3 + y * (r4 + y * (r5 + y * (r6 + y))))));
nsz0cbb6542012-03-19 23:41:19 +0100313 r = 0.5 * y + p / q;
Szabolcs Nagye71981a2013-10-04 18:08:16 +0000314 z = 1.0;
315 /* lgamma(1+s) = log(s) + lgamma(s) */
Rich Felkerb69f6952012-03-13 01:17:53 -0400316 switch (i) {
317 case 7:
318 z *= (y + 6.0); /* FALLTHRU */
319 case 6:
320 z *= (y + 5.0); /* FALLTHRU */
321 case 5:
322 z *= (y + 4.0); /* FALLTHRU */
323 case 4:
324 z *= (y + 3.0); /* FALLTHRU */
325 case 3:
326 z *= (y + 2.0); /* FALLTHRU */
nsz0cbb6542012-03-19 23:41:19 +0100327 r += logl(z);
Rich Felkerb69f6952012-03-13 01:17:53 -0400328 break;
329 }
330 } else if (ix < 0x40418000) { /* 2^66 */
331 /* 8.0 <= x < 2**66 */
nsz0cbb6542012-03-19 23:41:19 +0100332 t = logl(x);
333 z = 1.0 / x;
Rich Felkerb69f6952012-03-13 01:17:53 -0400334 y = z * z;
335 w = w0 + z * (w1 + y * (w2 + y * (w3 + y * (w4 + y * (w5 + y * (w6 + y * w7))))));
nsz0cbb6542012-03-19 23:41:19 +0100336 r = (x - 0.5) * (t - 1.0) + w;
Rich Felkerb69f6952012-03-13 01:17:53 -0400337 } else /* 2**66 <= x <= inf */
nsz0cbb6542012-03-19 23:41:19 +0100338 r = x * (logl(x) - 1.0);
Szabolcs Nagy34660d72013-09-04 15:52:23 +0000339 if (sign)
Rich Felkerb69f6952012-03-13 01:17:53 -0400340 r = nadj - r;
341 return r;
342}
Szabolcs Nagyf4e46322015-03-10 20:01:20 +0000343#elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384
344// TODO: broken implementation to make things compile
345double __lgamma_r(double x, int *sg);
346
347long double __lgammal_r(long double x, int *sg)
348{
349 return __lgamma_r(x, sg);
350}
Rich Felkerb69f6952012-03-13 01:17:53 -0400351#endif
Rich Felkerde7db6e2012-03-16 21:16:32 -0400352
Rich Felker8c071f82012-03-16 21:20:53 -0400353extern int __signgam;
354
Rich Felkerde7db6e2012-03-16 21:16:32 -0400355long double lgammal(long double x)
356{
Rich Felker8c071f82012-03-16 21:20:53 -0400357 return __lgammal_r(x, &__signgam);
Rich Felkerde7db6e2012-03-16 21:16:32 -0400358}
359
360weak_alias(__lgammal_r, lgammal_r);