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