xref: /openbsd/lib/libm/src/ld80/e_lgammal.c (revision 2c53affb)
149393c00Smartynas /*
249393c00Smartynas  * ====================================================
349393c00Smartynas  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
449393c00Smartynas  *
549393c00Smartynas  * Developed at SunPro, a Sun Microsystems, Inc. business.
649393c00Smartynas  * Permission to use, copy, modify, and distribute this
749393c00Smartynas  * software is freely granted, provided that this notice
849393c00Smartynas  * is preserved.
949393c00Smartynas  * ====================================================
1049393c00Smartynas  */
1149393c00Smartynas 
1249393c00Smartynas /*
1349393c00Smartynas  * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
1449393c00Smartynas  *
1549393c00Smartynas  * Permission to use, copy, modify, and distribute this software for any
1649393c00Smartynas  * purpose with or without fee is hereby granted, provided that the above
1749393c00Smartynas  * copyright notice and this permission notice appear in all copies.
1849393c00Smartynas  *
1949393c00Smartynas  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
2049393c00Smartynas  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
2149393c00Smartynas  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
2249393c00Smartynas  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
2349393c00Smartynas  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
2449393c00Smartynas  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
2549393c00Smartynas  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2649393c00Smartynas  */
2749393c00Smartynas 
2849393c00Smartynas /* lgammal(x)
2949393c00Smartynas  * Reentrant version of the logarithm of the Gamma function
3049393c00Smartynas  * with user provide pointer for the sign of Gamma(x).
3149393c00Smartynas  *
3249393c00Smartynas  * Method:
3349393c00Smartynas  *   1. Argument Reduction for 0 < x <= 8
3449393c00Smartynas  *	Since gamma(1+s)=s*gamma(s), for x in [0,8], we may
3549393c00Smartynas  *	reduce x to a number in [1.5,2.5] by
3649393c00Smartynas  *		lgamma(1+s) = log(s) + lgamma(s)
3749393c00Smartynas  *	for example,
3849393c00Smartynas  *		lgamma(7.3) = log(6.3) + lgamma(6.3)
3949393c00Smartynas  *			    = log(6.3*5.3) + lgamma(5.3)
4049393c00Smartynas  *			    = log(6.3*5.3*4.3*3.3*2.3) + lgamma(2.3)
4149393c00Smartynas  *   2. Polynomial approximation of lgamma around its
42*2c53affbSjmc  *	minimum ymin=1.461632144968362245 to maintain monotonicity.
4349393c00Smartynas  *	On [ymin-0.23, ymin+0.27] (i.e., [1.23164,1.73163]), use
4449393c00Smartynas  *		Let z = x-ymin;
4549393c00Smartynas  *		lgamma(x) = -1.214862905358496078218 + z^2*poly(z)
4649393c00Smartynas  *   2. Rational approximation in the primary interval [2,3]
4749393c00Smartynas  *	We use the following approximation:
4849393c00Smartynas  *		s = x-2.0;
4949393c00Smartynas  *		lgamma(x) = 0.5*s + s*P(s)/Q(s)
5049393c00Smartynas  *	Our algorithms are based on the following observation
5149393c00Smartynas  *
5249393c00Smartynas  *                             zeta(2)-1    2    zeta(3)-1    3
5349393c00Smartynas  * lgamma(2+s) = s*(1-Euler) + --------- * s  -  --------- * s  + ...
5449393c00Smartynas  *                                 2                 3
5549393c00Smartynas  *
5649393c00Smartynas  *	where Euler = 0.5771... is the Euler constant, which is very
5749393c00Smartynas  *	close to 0.5.
5849393c00Smartynas  *
5949393c00Smartynas  *   3. For x>=8, we have
6049393c00Smartynas  *	lgamma(x)~(x-0.5)log(x)-x+0.5*log(2pi)+1/(12x)-1/(360x**3)+....
6149393c00Smartynas  *	(better formula:
6249393c00Smartynas  *	   lgamma(x)~(x-0.5)*(log(x)-1)-.5*(log(2pi)-1) + ...)
6349393c00Smartynas  *	Let z = 1/x, then we approximation
6449393c00Smartynas  *		f(z) = lgamma(x) - (x-0.5)(log(x)-1)
6549393c00Smartynas  *	by
6649393c00Smartynas  *				    3       5             11
6749393c00Smartynas  *		w = w0 + w1*z + w2*z  + w3*z  + ... + w6*z
6849393c00Smartynas  *
6949393c00Smartynas  *   4. For negative x, since (G is gamma function)
7049393c00Smartynas  *		-x*G(-x)*G(x) = pi/sin(pi*x),
7149393c00Smartynas  *	we have
7249393c00Smartynas  *		G(x) = pi/(sin(pi*x)*(-x)*G(-x))
7349393c00Smartynas  *	since G(-x) is positive, sign(G(x)) = sign(sin(pi*x)) for x<0
7449393c00Smartynas  *	Hence, for x<0, signgam = sign(sin(pi*x)) and
7549393c00Smartynas  *		lgamma(x) = log(|Gamma(x)|)
7649393c00Smartynas  *			  = log(pi/(|x*sin(pi*x)|)) - lgamma(-x);
7749393c00Smartynas  *	Note: one should avoid compute pi*(-x) directly in the
7849393c00Smartynas  *	      computation of sin(pi*(-x)).
7949393c00Smartynas  *
8049393c00Smartynas  *   5. Special Cases
8149393c00Smartynas  *		lgamma(2+s) ~ s*(1-Euler) for tiny s
8249393c00Smartynas  *		lgamma(1)=lgamma(2)=0
8349393c00Smartynas  *		lgamma(x) ~ -log(x) for tiny x
8449393c00Smartynas  *		lgamma(0) = lgamma(inf) = inf
8549393c00Smartynas  *		lgamma(-integer) = +-inf
8649393c00Smartynas  *
8749393c00Smartynas  */
8849393c00Smartynas 
8949393c00Smartynas #include <math.h>
9049393c00Smartynas 
9149393c00Smartynas #include "math_private.h"
9249393c00Smartynas 
9349393c00Smartynas static const long double
9449393c00Smartynas   half = 0.5L,
9549393c00Smartynas   one = 1.0L,
9649393c00Smartynas   pi = 3.14159265358979323846264L,
9749393c00Smartynas   two63 = 9.223372036854775808e18L,
9849393c00Smartynas 
9949393c00Smartynas   /* lgam(1+x) = 0.5 x + x a(x)/b(x)
10049393c00Smartynas      -0.268402099609375 <= x <= 0
10149393c00Smartynas      peak relative error 6.6e-22 */
10249393c00Smartynas   a0 = -6.343246574721079391729402781192128239938E2L,
10349393c00Smartynas   a1 =  1.856560238672465796768677717168371401378E3L,
10449393c00Smartynas   a2 =  2.404733102163746263689288466865843408429E3L,
10549393c00Smartynas   a3 =  8.804188795790383497379532868917517596322E2L,
10649393c00Smartynas   a4 =  1.135361354097447729740103745999661157426E2L,
10749393c00Smartynas   a5 =  3.766956539107615557608581581190400021285E0L,
10849393c00Smartynas 
10949393c00Smartynas   b0 =  8.214973713960928795704317259806842490498E3L,
11049393c00Smartynas   b1 =  1.026343508841367384879065363925870888012E4L,
11149393c00Smartynas   b2 =  4.553337477045763320522762343132210919277E3L,
11249393c00Smartynas   b3 =  8.506975785032585797446253359230031874803E2L,
11349393c00Smartynas   b4 =  6.042447899703295436820744186992189445813E1L,
11449393c00Smartynas   /* b5 =  1.000000000000000000000000000000000000000E0 */
11549393c00Smartynas 
11649393c00Smartynas 
11749393c00Smartynas   tc =  1.4616321449683623412626595423257213284682E0L,
11849393c00Smartynas   tf = -1.2148629053584961146050602565082954242826E-1,/* double precision */
11949393c00Smartynas /* tt = (tail of tf), i.e. tf + tt has extended precision. */
12049393c00Smartynas   tt = 3.3649914684731379602768989080467587736363E-18L,
12149393c00Smartynas   /* lgam ( 1.4616321449683623412626595423257213284682E0 ) =
12249393c00Smartynas -1.2148629053584960809551455717769158215135617312999903886372437313313530E-1 */
12349393c00Smartynas 
12449393c00Smartynas   /* lgam (x + tc) = tf + tt + x g(x)/h(x)
12549393c00Smartynas      - 0.230003726999612341262659542325721328468 <= x
12649393c00Smartynas      <= 0.2699962730003876587373404576742786715318
12749393c00Smartynas      peak relative error 2.1e-21 */
12849393c00Smartynas   g0 = 3.645529916721223331888305293534095553827E-18L,
12949393c00Smartynas   g1 = 5.126654642791082497002594216163574795690E3L,
13049393c00Smartynas   g2 = 8.828603575854624811911631336122070070327E3L,
13149393c00Smartynas   g3 = 5.464186426932117031234820886525701595203E3L,
13249393c00Smartynas   g4 = 1.455427403530884193180776558102868592293E3L,
13349393c00Smartynas   g5 = 1.541735456969245924860307497029155838446E2L,
13449393c00Smartynas   g6 = 4.335498275274822298341872707453445815118E0L,
13549393c00Smartynas 
13649393c00Smartynas   h0 = 1.059584930106085509696730443974495979641E4L,
13749393c00Smartynas   h1 =  2.147921653490043010629481226937850618860E4L,
13849393c00Smartynas   h2 = 1.643014770044524804175197151958100656728E4L,
13949393c00Smartynas   h3 =  5.869021995186925517228323497501767586078E3L,
14049393c00Smartynas   h4 =  9.764244777714344488787381271643502742293E2L,
14149393c00Smartynas   h5 =  6.442485441570592541741092969581997002349E1L,
14249393c00Smartynas   /* h6 = 1.000000000000000000000000000000000000000E0 */
14349393c00Smartynas 
14449393c00Smartynas 
14549393c00Smartynas   /* lgam (x+1) = -0.5 x + x u(x)/v(x)
14649393c00Smartynas      -0.100006103515625 <= x <= 0.231639862060546875
14749393c00Smartynas      peak relative error 1.3e-21 */
14849393c00Smartynas   u0 = -8.886217500092090678492242071879342025627E1L,
14949393c00Smartynas   u1 =  6.840109978129177639438792958320783599310E2L,
15049393c00Smartynas   u2 =  2.042626104514127267855588786511809932433E3L,
15149393c00Smartynas   u3 =  1.911723903442667422201651063009856064275E3L,
15249393c00Smartynas   u4 =  7.447065275665887457628865263491667767695E2L,
15349393c00Smartynas   u5 =  1.132256494121790736268471016493103952637E2L,
15449393c00Smartynas   u6 =  4.484398885516614191003094714505960972894E0L,
15549393c00Smartynas 
15649393c00Smartynas   v0 =  1.150830924194461522996462401210374632929E3L,
15749393c00Smartynas   v1 =  3.399692260848747447377972081399737098610E3L,
15849393c00Smartynas   v2 =  3.786631705644460255229513563657226008015E3L,
15949393c00Smartynas   v3 =  1.966450123004478374557778781564114347876E3L,
16049393c00Smartynas   v4 =  4.741359068914069299837355438370682773122E2L,
16149393c00Smartynas   v5 =  4.508989649747184050907206782117647852364E1L,
16249393c00Smartynas   /* v6 =  1.000000000000000000000000000000000000000E0 */
16349393c00Smartynas 
16449393c00Smartynas 
16549393c00Smartynas   /* lgam (x+2) = .5 x + x s(x)/r(x)
16649393c00Smartynas      0 <= x <= 1
16749393c00Smartynas      peak relative error 7.2e-22 */
16849393c00Smartynas   s0 =  1.454726263410661942989109455292824853344E6L,
16949393c00Smartynas   s1 = -3.901428390086348447890408306153378922752E6L,
17049393c00Smartynas   s2 = -6.573568698209374121847873064292963089438E6L,
17149393c00Smartynas   s3 = -3.319055881485044417245964508099095984643E6L,
17249393c00Smartynas   s4 = -7.094891568758439227560184618114707107977E5L,
17349393c00Smartynas   s5 = -6.263426646464505837422314539808112478303E4L,
17449393c00Smartynas   s6 = -1.684926520999477529949915657519454051529E3L,
17549393c00Smartynas 
17649393c00Smartynas   r0 = -1.883978160734303518163008696712983134698E7L,
17749393c00Smartynas   r1 = -2.815206082812062064902202753264922306830E7L,
17849393c00Smartynas   r2 = -1.600245495251915899081846093343626358398E7L,
17949393c00Smartynas   r3 = -4.310526301881305003489257052083370058799E6L,
18049393c00Smartynas   r4 = -5.563807682263923279438235987186184968542E5L,
18149393c00Smartynas   r5 = -3.027734654434169996032905158145259713083E4L,
18249393c00Smartynas   r6 = -4.501995652861105629217250715790764371267E2L,
18349393c00Smartynas   /* r6 =  1.000000000000000000000000000000000000000E0 */
18449393c00Smartynas 
18549393c00Smartynas 
18649393c00Smartynas /* lgam(x) = ( x - 0.5 ) * log(x) - x + LS2PI + 1/x w(1/x^2)
18749393c00Smartynas    x >= 8
18849393c00Smartynas    Peak relative error 1.51e-21
18949393c00Smartynas    w0 = LS2PI - 0.5 */
19049393c00Smartynas   w0 =  4.189385332046727417803e-1L,
19149393c00Smartynas   w1 =  8.333333333333331447505E-2L,
19249393c00Smartynas   w2 = -2.777777777750349603440E-3L,
19349393c00Smartynas   w3 =  7.936507795855070755671E-4L,
19449393c00Smartynas   w4 = -5.952345851765688514613E-4L,
19549393c00Smartynas   w5 =  8.412723297322498080632E-4L,
19649393c00Smartynas   w6 = -1.880801938119376907179E-3L,
19749393c00Smartynas   w7 =  4.885026142432270781165E-3L;
19849393c00Smartynas 
19949393c00Smartynas static const long double zero = 0.0L;
20049393c00Smartynas 
20149393c00Smartynas static long double
sin_pi(long double x)20249393c00Smartynas sin_pi(long double x)
20349393c00Smartynas {
20449393c00Smartynas   long double y, z;
20549393c00Smartynas   int n, ix;
20649393c00Smartynas   u_int32_t se, i0, i1;
20749393c00Smartynas 
20849393c00Smartynas   GET_LDOUBLE_WORDS (se, i0, i1, x);
20949393c00Smartynas   ix = se & 0x7fff;
21049393c00Smartynas   ix = (ix << 16) | (i0 >> 16);
21149393c00Smartynas   if (ix < 0x3ffd8000) /* 0.25 */
21249393c00Smartynas     return sinl (pi * x);
21349393c00Smartynas   y = -x;			/* x is assume negative */
21449393c00Smartynas 
21549393c00Smartynas   /*
21649393c00Smartynas    * argument reduction, make sure inexact flag not raised if input
21749393c00Smartynas    * is an integer
21849393c00Smartynas    */
21949393c00Smartynas   z = floorl (y);
22049393c00Smartynas   if (z != y)
22149393c00Smartynas     {				/* inexact anyway */
22249393c00Smartynas       y  *= 0.5;
22349393c00Smartynas       y = 2.0*(y - floorl(y));		/* y = |x| mod 2.0 */
22449393c00Smartynas       n = (int) (y*4.0);
22549393c00Smartynas     }
22649393c00Smartynas   else
22749393c00Smartynas     {
22849393c00Smartynas       if (ix >= 0x403f8000)  /* 2^64 */
22949393c00Smartynas 	{
23049393c00Smartynas 	  y = zero; n = 0;		/* y must be even */
23149393c00Smartynas 	}
23249393c00Smartynas       else
23349393c00Smartynas 	{
23449393c00Smartynas 	if (ix < 0x403e8000)  /* 2^63 */
23549393c00Smartynas 	  z = y + two63;	/* exact */
23649393c00Smartynas 	GET_LDOUBLE_WORDS (se, i0, i1, z);
23749393c00Smartynas 	n = i1 & 1;
23849393c00Smartynas 	y  = n;
23949393c00Smartynas 	n <<= 2;
24049393c00Smartynas       }
24149393c00Smartynas     }
24249393c00Smartynas 
24349393c00Smartynas   switch (n)
24449393c00Smartynas     {
24549393c00Smartynas     case 0:
24649393c00Smartynas       y = sinl (pi * y);
24749393c00Smartynas       break;
24849393c00Smartynas     case 1:
24949393c00Smartynas     case 2:
25049393c00Smartynas       y = cosl (pi * (half - y));
25149393c00Smartynas       break;
25249393c00Smartynas     case 3:
25349393c00Smartynas     case 4:
25449393c00Smartynas       y = sinl (pi * (one - y));
25549393c00Smartynas       break;
25649393c00Smartynas     case 5:
25749393c00Smartynas     case 6:
25849393c00Smartynas       y = -cosl (pi * (y - 1.5));
25949393c00Smartynas       break;
26049393c00Smartynas     default:
26149393c00Smartynas       y = sinl (pi * (y - 2.0));
26249393c00Smartynas       break;
26349393c00Smartynas     }
26449393c00Smartynas   return -y;
26549393c00Smartynas }
26649393c00Smartynas 
26749393c00Smartynas 
26849393c00Smartynas long double
lgammal(long double x)26949393c00Smartynas lgammal(long double x)
27049393c00Smartynas {
27149393c00Smartynas   long double t, y, z, nadj, p, p1, p2, q, r, w;
27249393c00Smartynas   int i, ix;
27349393c00Smartynas   u_int32_t se, i0, i1;
27449393c00Smartynas 
27549393c00Smartynas   signgam = 1;
27649393c00Smartynas   GET_LDOUBLE_WORDS (se, i0, i1, x);
27749393c00Smartynas   ix = se & 0x7fff;
27849393c00Smartynas 
27949393c00Smartynas   if ((ix | i0 | i1) == 0)
28049393c00Smartynas     {
28149393c00Smartynas       if (se & 0x8000)
28249393c00Smartynas 	signgam = -1;
28349393c00Smartynas       return one / fabsl (x);
28449393c00Smartynas     }
28549393c00Smartynas 
28649393c00Smartynas   ix = (ix << 16) | (i0 >> 16);
28749393c00Smartynas 
28849393c00Smartynas   /* purge off +-inf, NaN, +-0, and negative arguments */
28949393c00Smartynas   if (ix >= 0x7fff0000)
29049393c00Smartynas     return x * x;
29149393c00Smartynas 
29249393c00Smartynas   if (ix < 0x3fc08000) /* 2^-63 */
29349393c00Smartynas     {				/* |x|<2**-63, return -log(|x|) */
29449393c00Smartynas       if (se & 0x8000)
29549393c00Smartynas 	{
29649393c00Smartynas 	  signgam = -1;
29749393c00Smartynas 	  return -logl (-x);
29849393c00Smartynas 	}
29949393c00Smartynas       else
30049393c00Smartynas 	return -logl (x);
30149393c00Smartynas     }
30249393c00Smartynas   if (se & 0x8000)
30349393c00Smartynas     {
30449393c00Smartynas       t = sin_pi (x);
30549393c00Smartynas       if (t == zero)
30649393c00Smartynas 	return one / fabsl (t);	/* -integer */
30749393c00Smartynas       nadj = logl (pi / fabsl (t * x));
30849393c00Smartynas       if (t < zero)
30949393c00Smartynas 	signgam = -1;
31049393c00Smartynas       x = -x;
31149393c00Smartynas     }
31249393c00Smartynas 
31349393c00Smartynas   /* purge off 1 and 2 */
31449393c00Smartynas   if ((((ix - 0x3fff8000) | i0 | i1) == 0)
31549393c00Smartynas       || (((ix - 0x40008000) | i0 | i1) == 0))
31649393c00Smartynas     r = 0;
31749393c00Smartynas   else if (ix < 0x40008000) /* 2.0 */
31849393c00Smartynas     {
31949393c00Smartynas       /* x < 2.0 */
32049393c00Smartynas       if (ix <= 0x3ffee666) /* 8.99993896484375e-1 */
32149393c00Smartynas 	{
32249393c00Smartynas 	  /* lgamma(x) = lgamma(x+1) - log(x) */
32349393c00Smartynas 	  r = -logl (x);
32449393c00Smartynas 	  if (ix >= 0x3ffebb4a) /* 7.31597900390625e-1 */
32549393c00Smartynas 	    {
32649393c00Smartynas 	      y = x - one;
32749393c00Smartynas 	      i = 0;
32849393c00Smartynas 	    }
32949393c00Smartynas 	  else if (ix >= 0x3ffced33)/* 2.31639862060546875e-1 */
33049393c00Smartynas 	    {
33149393c00Smartynas 	      y = x - (tc - one);
33249393c00Smartynas 	      i = 1;
33349393c00Smartynas 	    }
33449393c00Smartynas 	  else
33549393c00Smartynas 	    {
33649393c00Smartynas 	      /* x < 0.23 */
33749393c00Smartynas 	      y = x;
33849393c00Smartynas 	      i = 2;
33949393c00Smartynas 	    }
34049393c00Smartynas 	}
34149393c00Smartynas       else
34249393c00Smartynas 	{
34349393c00Smartynas 	  r = zero;
34449393c00Smartynas 	  if (ix >= 0x3fffdda6) /* 1.73162841796875 */
34549393c00Smartynas 	    {
34649393c00Smartynas 	      /* [1.7316,2] */
34749393c00Smartynas 	      y = x - 2.0;
34849393c00Smartynas 	      i = 0;
34949393c00Smartynas 	    }
35049393c00Smartynas 	  else if (ix >= 0x3fff9da6)/* 1.23162841796875 */
35149393c00Smartynas 	    {
35249393c00Smartynas 	      /* [1.23,1.73] */
35349393c00Smartynas 	      y = x - tc;
35449393c00Smartynas 	      i = 1;
35549393c00Smartynas 	    }
35649393c00Smartynas 	  else
35749393c00Smartynas 	    {
35849393c00Smartynas 	      /* [0.9, 1.23] */
35949393c00Smartynas 	      y = x - one;
36049393c00Smartynas 	      i = 2;
36149393c00Smartynas 	    }
36249393c00Smartynas 	}
36349393c00Smartynas       switch (i)
36449393c00Smartynas 	{
36549393c00Smartynas 	case 0:
36649393c00Smartynas 	  p1 = a0 + y * (a1 + y * (a2 + y * (a3 + y * (a4 + y * a5))));
36749393c00Smartynas 	  p2 = b0 + y * (b1 + y * (b2 + y * (b3 + y * (b4 + y))));
36849393c00Smartynas 	  r += half * y + y * p1/p2;
36949393c00Smartynas 	  break;
37049393c00Smartynas 	case 1:
37149393c00Smartynas     p1 = g0 + y * (g1 + y * (g2 + y * (g3 + y * (g4 + y * (g5 + y * g6)))));
37249393c00Smartynas     p2 = h0 + y * (h1 + y * (h2 + y * (h3 + y * (h4 + y * (h5 + y)))));
37349393c00Smartynas     p = tt + y * p1/p2;
37449393c00Smartynas 	  r += (tf + p);
37549393c00Smartynas 	  break;
37649393c00Smartynas 	case 2:
37749393c00Smartynas  p1 = y * (u0 + y * (u1 + y * (u2 + y * (u3 + y * (u4 + y * (u5 + y * u6))))));
37849393c00Smartynas       p2 = v0 + y * (v1 + y * (v2 + y * (v3 + y * (v4 + y * (v5 + y)))));
37949393c00Smartynas 	  r += (-half * y + p1 / p2);
38049393c00Smartynas 	}
38149393c00Smartynas     }
38249393c00Smartynas   else if (ix < 0x40028000) /* 8.0 */
38349393c00Smartynas     {
38449393c00Smartynas       /* x < 8.0 */
38549393c00Smartynas       i = (int) x;
38649393c00Smartynas       t = zero;
38749393c00Smartynas       y = x - (double) i;
38849393c00Smartynas   p = y *
38949393c00Smartynas      (s0 + y * (s1 + y * (s2 + y * (s3 + y * (s4 + y * (s5 + y * s6))))));
39049393c00Smartynas   q = r0 + y * (r1 + y * (r2 + y * (r3 + y * (r4 + y * (r5 + y * (r6 + y))))));
39149393c00Smartynas       r = half * y + p / q;
39249393c00Smartynas       z = one;			/* lgamma(1+s) = log(s) + lgamma(s) */
39349393c00Smartynas       switch (i)
39449393c00Smartynas 	{
39549393c00Smartynas 	case 7:
39649393c00Smartynas 	  z *= (y + 6.0);	/* FALLTHRU */
39749393c00Smartynas 	case 6:
39849393c00Smartynas 	  z *= (y + 5.0);	/* FALLTHRU */
39949393c00Smartynas 	case 5:
40049393c00Smartynas 	  z *= (y + 4.0);	/* FALLTHRU */
40149393c00Smartynas 	case 4:
40249393c00Smartynas 	  z *= (y + 3.0);	/* FALLTHRU */
40349393c00Smartynas 	case 3:
40449393c00Smartynas 	  z *= (y + 2.0);	/* FALLTHRU */
40549393c00Smartynas 	  r += logl (z);
40649393c00Smartynas 	  break;
40749393c00Smartynas 	}
40849393c00Smartynas     }
40949393c00Smartynas   else if (ix < 0x40418000) /* 2^66 */
41049393c00Smartynas     {
41149393c00Smartynas       /* 8.0 <= x < 2**66 */
41249393c00Smartynas       t = logl (x);
41349393c00Smartynas       z = one / x;
41449393c00Smartynas       y = z * z;
41549393c00Smartynas       w = w0 + z * (w1
41649393c00Smartynas 	  + y * (w2 + y * (w3 + y * (w4 + y * (w5 + y * (w6 + y * w7))))));
41749393c00Smartynas       r = (x - half) * (t - one) + w;
41849393c00Smartynas     }
41949393c00Smartynas   else
42049393c00Smartynas     /* 2**66 <= x <= inf */
42149393c00Smartynas     r = x * (logl (x) - one);
42249393c00Smartynas   if (se & 0x8000)
42349393c00Smartynas     r = nadj - r;
42449393c00Smartynas   return r;
42549393c00Smartynas }
4262f2c0062Sguenther DEF_STD(lgammal);
427