1*4887Schin #include "FEATURE/uwin"
2*4887Schin
3*4887Schin #if !_UWIN || _lib_lgamma
4*4887Schin
_STUB_lgamma()5*4887Schin void _STUB_lgamma(){}
6*4887Schin
7*4887Schin #else
8*4887Schin
9*4887Schin /*-
10*4887Schin * Copyright (c) 1992, 1993
11*4887Schin * The Regents of the University of California. All rights reserved.
12*4887Schin *
13*4887Schin * Redistribution and use in source and binary forms, with or without
14*4887Schin * modification, are permitted provided that the following conditions
15*4887Schin * are met:
16*4887Schin * 1. Redistributions of source code must retain the above copyright
17*4887Schin * notice, this list of conditions and the following disclaimer.
18*4887Schin * 2. Redistributions in binary form must reproduce the above copyright
19*4887Schin * notice, this list of conditions and the following disclaimer in the
20*4887Schin * documentation and/or other materials provided with the distribution.
21*4887Schin * 3. Neither the name of the University nor the names of its contributors
22*4887Schin * may be used to endorse or promote products derived from this software
23*4887Schin * without specific prior written permission.
24*4887Schin *
25*4887Schin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26*4887Schin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27*4887Schin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28*4887Schin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29*4887Schin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30*4887Schin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31*4887Schin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32*4887Schin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33*4887Schin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34*4887Schin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35*4887Schin * SUCH DAMAGE.
36*4887Schin */
37*4887Schin
38*4887Schin #ifndef lint
39*4887Schin static char sccsid[] = "@(#)lgamma.c 8.2 (Berkeley) 11/30/93";
40*4887Schin #endif /* not lint */
41*4887Schin
42*4887Schin /*
43*4887Schin * Coded by Peter McIlroy, Nov 1992;
44*4887Schin *
45*4887Schin * The financial support of UUNET Communications Services is greatfully
46*4887Schin * acknowledged.
47*4887Schin */
48*4887Schin
49*4887Schin #define gamma ______gamma
50*4887Schin #define lgamma ______lgamma
51*4887Schin
52*4887Schin #include <math.h>
53*4887Schin #include <errno.h>
54*4887Schin #include "mathimpl.h"
55*4887Schin
56*4887Schin #undef gamma
57*4887Schin #undef lgamma
58*4887Schin
59*4887Schin /* Log gamma function.
60*4887Schin * Error: x > 0 error < 1.3ulp.
61*4887Schin * x > 4, error < 1ulp.
62*4887Schin * x > 9, error < .6ulp.
63*4887Schin * x < 0, all bets are off. (When G(x) ~ 1, log(G(x)) ~ 0)
64*4887Schin * Method:
65*4887Schin * x > 6:
66*4887Schin * Use the asymptotic expansion (Stirling's Formula)
67*4887Schin * 0 < x < 6:
68*4887Schin * Use gamma(x+1) = x*gamma(x) for argument reduction.
69*4887Schin * Use rational approximation in
70*4887Schin * the range 1.2, 2.5
71*4887Schin * Two approximations are used, one centered at the
72*4887Schin * minimum to ensure monotonicity; one centered at 2
73*4887Schin * to maintain small relative error.
74*4887Schin * x < 0:
75*4887Schin * Use the reflection formula,
76*4887Schin * G(1-x)G(x) = PI/sin(PI*x)
77*4887Schin * Special values:
78*4887Schin * non-positive integer returns +Inf.
79*4887Schin * NaN returns NaN
80*4887Schin */
81*4887Schin static int endian;
82*4887Schin #if defined(vax) || defined(tahoe)
83*4887Schin #define _IEEE 0
84*4887Schin /* double and float have same size exponent field */
85*4887Schin #define TRUNC(x) x = (double) (float) (x)
86*4887Schin #else
87*4887Schin #define _IEEE 1
88*4887Schin #define TRUNC(x) *(((int *) &x) + endian) &= 0xf8000000
89*4887Schin #define infnan(x) 0.0
90*4887Schin #endif
91*4887Schin
92*4887Schin static double small_lgam(double);
93*4887Schin static double large_lgam(double);
94*4887Schin static double neg_lgam(double);
95*4887Schin static double zero = 0.0, one = 1.0;
96*4887Schin int signgam;
97*4887Schin
98*4887Schin #define UNDERFL (1e-1020 * 1e-1020)
99*4887Schin
100*4887Schin #define LEFT (1.0 - (x0 + .25))
101*4887Schin #define RIGHT (x0 - .218)
102*4887Schin /*
103*4887Schin * Constants for approximation in [1.244,1.712]
104*4887Schin */
105*4887Schin #define x0 0.461632144968362356785
106*4887Schin #define x0_lo -.000000000000000015522348162858676890521
107*4887Schin #define a0_hi -0.12148629128932952880859
108*4887Schin #define a0_lo .0000000007534799204229502
109*4887Schin #define r0 -2.771227512955130520e-002
110*4887Schin #define r1 -2.980729795228150847e-001
111*4887Schin #define r2 -3.257411333183093394e-001
112*4887Schin #define r3 -1.126814387531706041e-001
113*4887Schin #define r4 -1.129130057170225562e-002
114*4887Schin #define r5 -2.259650588213369095e-005
115*4887Schin #define s0 1.714457160001714442e+000
116*4887Schin #define s1 2.786469504618194648e+000
117*4887Schin #define s2 1.564546365519179805e+000
118*4887Schin #define s3 3.485846389981109850e-001
119*4887Schin #define s4 2.467759345363656348e-002
120*4887Schin /*
121*4887Schin * Constants for approximation in [1.71, 2.5]
122*4887Schin */
123*4887Schin #define a1_hi 4.227843350984671344505727574870e-01
124*4887Schin #define a1_lo 4.670126436531227189e-18
125*4887Schin #define p0 3.224670334241133695662995251041e-01
126*4887Schin #define p1 3.569659696950364669021382724168e-01
127*4887Schin #define p2 1.342918716072560025853732668111e-01
128*4887Schin #define p3 1.950702176409779831089963408886e-02
129*4887Schin #define p4 8.546740251667538090796227834289e-04
130*4887Schin #define q0 1.000000000000000444089209850062e+00
131*4887Schin #define q1 1.315850076960161985084596381057e+00
132*4887Schin #define q2 6.274644311862156431658377186977e-01
133*4887Schin #define q3 1.304706631926259297049597307705e-01
134*4887Schin #define q4 1.102815279606722369265536798366e-02
135*4887Schin #define q5 2.512690594856678929537585620579e-04
136*4887Schin #define q6 -1.003597548112371003358107325598e-06
137*4887Schin /*
138*4887Schin * Stirling's Formula, adjusted for equal-ripple. x in [6,Inf].
139*4887Schin */
140*4887Schin #define lns2pi .418938533204672741780329736405
141*4887Schin #define pb0 8.33333333333333148296162562474e-02
142*4887Schin #define pb1 -2.77777777774548123579378966497e-03
143*4887Schin #define pb2 7.93650778754435631476282786423e-04
144*4887Schin #define pb3 -5.95235082566672847950717262222e-04
145*4887Schin #define pb4 8.41428560346653702135821806252e-04
146*4887Schin #define pb5 -1.89773526463879200348872089421e-03
147*4887Schin #define pb6 5.69394463439411649408050664078e-03
148*4887Schin #define pb7 -1.44705562421428915453880392761e-02
149*4887Schin
lgamma(double x)150*4887Schin extern __pure double lgamma(double x)
151*4887Schin {
152*4887Schin double r;
153*4887Schin
154*4887Schin signgam = 1;
155*4887Schin endian = ((*(int *) &one)) ? 1 : 0;
156*4887Schin
157*4887Schin if (!finite(x))
158*4887Schin if (_IEEE)
159*4887Schin return (x+x);
160*4887Schin else return (infnan(EDOM));
161*4887Schin
162*4887Schin if (x > 6 + RIGHT) {
163*4887Schin r = large_lgam(x);
164*4887Schin return (r);
165*4887Schin } else if (x > 1e-16)
166*4887Schin return (small_lgam(x));
167*4887Schin else if (x > -1e-16) {
168*4887Schin if (x < 0)
169*4887Schin signgam = -1, x = -x;
170*4887Schin return (-log(x));
171*4887Schin } else
172*4887Schin return (neg_lgam(x));
173*4887Schin }
174*4887Schin
175*4887Schin static double
large_lgam(double x)176*4887Schin large_lgam(double x)
177*4887Schin {
178*4887Schin double z, p, x1;
179*4887Schin struct Double t, u, v;
180*4887Schin u = __log__D(x);
181*4887Schin u.a -= 1.0;
182*4887Schin if (x > 1e15) {
183*4887Schin v.a = x - 0.5;
184*4887Schin TRUNC(v.a);
185*4887Schin v.b = (x - v.a) - 0.5;
186*4887Schin t.a = u.a*v.a;
187*4887Schin t.b = x*u.b + v.b*u.a;
188*4887Schin if (_IEEE == 0 && !finite(t.a))
189*4887Schin return(infnan(ERANGE));
190*4887Schin return(t.a + t.b);
191*4887Schin }
192*4887Schin x1 = 1./x;
193*4887Schin z = x1*x1;
194*4887Schin p = pb0+z*(pb1+z*(pb2+z*(pb3+z*(pb4+z*(pb5+z*(pb6+z*pb7))))));
195*4887Schin /* error in approximation = 2.8e-19 */
196*4887Schin
197*4887Schin p = p*x1; /* error < 2.3e-18 absolute */
198*4887Schin /* 0 < p < 1/64 (at x = 5.5) */
199*4887Schin v.a = x = x - 0.5;
200*4887Schin TRUNC(v.a); /* truncate v.a to 26 bits. */
201*4887Schin v.b = x - v.a;
202*4887Schin t.a = v.a*u.a; /* t = (x-.5)*(log(x)-1) */
203*4887Schin t.b = v.b*u.a + x*u.b;
204*4887Schin t.b += p; t.b += lns2pi; /* return t + lns2pi + p */
205*4887Schin return (t.a + t.b);
206*4887Schin }
207*4887Schin
208*4887Schin static double
small_lgam(double x)209*4887Schin small_lgam(double x)
210*4887Schin {
211*4887Schin int x_int;
212*4887Schin double y, z, t, r = 0, p, q, hi, lo;
213*4887Schin struct Double rr;
214*4887Schin x_int = (int)(x + .5);
215*4887Schin y = x - x_int;
216*4887Schin if (x_int <= 2 && y > RIGHT) {
217*4887Schin t = y - x0;
218*4887Schin y--; x_int++;
219*4887Schin goto CONTINUE;
220*4887Schin } else if (y < -LEFT) {
221*4887Schin t = y +(1.0-x0);
222*4887Schin CONTINUE:
223*4887Schin z = t - x0_lo;
224*4887Schin p = r0+z*(r1+z*(r2+z*(r3+z*(r4+z*r5))));
225*4887Schin q = s0+z*(s1+z*(s2+z*(s3+z*s4)));
226*4887Schin r = t*(z*(p/q) - x0_lo);
227*4887Schin t = .5*t*t;
228*4887Schin z = 1.0;
229*4887Schin switch (x_int) {
230*4887Schin case 6: z = (y + 5);
231*4887Schin case 5: z *= (y + 4);
232*4887Schin case 4: z *= (y + 3);
233*4887Schin case 3: z *= (y + 2);
234*4887Schin rr = __log__D(z);
235*4887Schin rr.b += a0_lo; rr.a += a0_hi;
236*4887Schin return(((r+rr.b)+t+rr.a));
237*4887Schin case 2: return(((r+a0_lo)+t)+a0_hi);
238*4887Schin case 0: r -= log1p(x);
239*4887Schin default: rr = __log__D(x);
240*4887Schin rr.a -= a0_hi; rr.b -= a0_lo;
241*4887Schin return(((r - rr.b) + t) - rr.a);
242*4887Schin }
243*4887Schin } else {
244*4887Schin p = p0+y*(p1+y*(p2+y*(p3+y*p4)));
245*4887Schin q = q0+y*(q1+y*(q2+y*(q3+y*(q4+y*(q5+y*q6)))));
246*4887Schin p = p*(y/q);
247*4887Schin t = (double)(float) y;
248*4887Schin z = y-t;
249*4887Schin hi = (double)(float) (p+a1_hi);
250*4887Schin lo = a1_hi - hi; lo += p; lo += a1_lo;
251*4887Schin r = lo*y + z*hi; /* q + r = y*(a0+p/q) */
252*4887Schin q = hi*t;
253*4887Schin z = 1.0;
254*4887Schin switch (x_int) {
255*4887Schin case 6: z = (y + 5);
256*4887Schin case 5: z *= (y + 4);
257*4887Schin case 4: z *= (y + 3);
258*4887Schin case 3: z *= (y + 2);
259*4887Schin rr = __log__D(z);
260*4887Schin r += rr.b; r += q;
261*4887Schin return(rr.a + r);
262*4887Schin case 2: return (q+ r);
263*4887Schin case 0: rr = __log__D(x);
264*4887Schin r -= rr.b; r -= log1p(x);
265*4887Schin r += q; r-= rr.a;
266*4887Schin return(r);
267*4887Schin default: rr = __log__D(x);
268*4887Schin r -= rr.b;
269*4887Schin q -= rr.a;
270*4887Schin return (r+q);
271*4887Schin }
272*4887Schin }
273*4887Schin }
274*4887Schin
275*4887Schin static double
neg_lgam(double x)276*4887Schin neg_lgam(double x)
277*4887Schin {
278*4887Schin int xi;
279*4887Schin double y, z, one = 1.0, zero = 0.0;
280*4887Schin extern double gamma();
281*4887Schin
282*4887Schin /* avoid destructive cancellation as much as possible */
283*4887Schin if (x > -170) {
284*4887Schin xi = (int)x;
285*4887Schin if (xi == x)
286*4887Schin if (_IEEE)
287*4887Schin return(one/zero);
288*4887Schin else
289*4887Schin return(infnan(ERANGE));
290*4887Schin y = gamma(x);
291*4887Schin if (y < 0)
292*4887Schin y = -y, signgam = -1;
293*4887Schin return (log(y));
294*4887Schin }
295*4887Schin z = floor(x + .5);
296*4887Schin if (z == x) { /* convention: G(-(integer)) -> +Inf */
297*4887Schin if (_IEEE)
298*4887Schin return (one/zero);
299*4887Schin else
300*4887Schin return (infnan(ERANGE));
301*4887Schin }
302*4887Schin y = .5*ceil(x);
303*4887Schin if (y == ceil(y))
304*4887Schin signgam = -1;
305*4887Schin x = -x;
306*4887Schin z = fabs(x + z); /* 0 < z <= .5 */
307*4887Schin if (z < .25)
308*4887Schin z = sin(M_PI*z);
309*4887Schin else
310*4887Schin z = cos(M_PI*(0.5-z));
311*4887Schin z = log(M_PI/(z*x));
312*4887Schin y = large_lgam(x);
313*4887Schin return (z - y);
314*4887Schin }
315*4887Schin
316*4887Schin #endif
317