OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [newlib-1.17.0/] [newlib/] [libm/] [mathfp/] [er_lgamma.c] - Blame information for rev 148

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 148 jeremybenn
 
2
/* @(#)er_lgamma.c 5.1 93/09/24 */
3
/*
4
 * ====================================================
5
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6
 *
7
 * Developed at SunPro, a Sun Microsystems, Inc. business.
8
 * Permission to use, copy, modify, and distribute this
9
 * software is freely granted, provided that this notice
10
 * is preserved.
11
 * ====================================================
12
 *
13
 */
14
 
15
/*
16
FUNCTION
17
        <<gamma>>, <<gammaf>>, <<lgamma>>, <<lgammaf>>, <<gamma_r>>,
18
        <<gammaf_r>>, <<lgamma_r>>, <<lgammaf_r>>---logarithmic gamma
19
        function
20
INDEX
21
gamma
22
INDEX
23
gammaf
24
INDEX
25
lgamma
26
INDEX
27
lgammaf
28
INDEX
29
gamma_r
30
INDEX
31
gammaf_r
32
INDEX
33
lgamma_r
34
INDEX
35
lgammaf_r
36
 
37
ANSI_SYNOPSIS
38
#include <math.h>
39
double gamma(double <[x]>);
40
float gammaf(float <[x]>);
41
double lgamma(double <[x]>);
42
float lgammaf(float <[x]>);
43
double gamma_r(double <[x]>, int *<[signgamp]>);
44
float gammaf_r(float <[x]>, int *<[signgamp]>);
45
double lgamma_r(double <[x]>, int *<[signgamp]>);
46
float lgammaf_r(float <[x]>, int *<[signgamp]>);
47
 
48
TRAD_SYNOPSIS
49
#include <math.h>
50
double gamma(<[x]>)
51
double <[x]>;
52
float gammaf(<[x]>)
53
float <[x]>;
54
double lgamma(<[x]>)
55
double <[x]>;
56
float lgammaf(<[x]>)
57
float <[x]>;
58
double gamma_r(<[x]>, <[signgamp]>)
59
double <[x]>;
60
int <[signgamp]>;
61
float gammaf_r(<[x]>, <[signgamp]>)
62
float <[x]>;
63
int <[signgamp]>;
64
double lgamma_r(<[x]>, <[signgamp]>)
65
double <[x]>;
66
int <[signgamp]>;
67
float lgammaf_r(<[x]>, <[signgamp]>)
68
float <[x]>;
69
int <[signgamp]>;
70
 
71
DESCRIPTION
72
<<gamma>> calculates
73
@tex
74
$\mit ln\bigl(\Gamma(x)\bigr)$,
75
@end tex
76
the natural logarithm of the gamma function of <[x]>.  The gamma function
77
(<<exp(gamma(<[x]>))>>) is a generalization of factorial, and retains
78
the property that
79
@ifnottex
80
<<exp(gamma(N))>> is equivalent to <<N*exp(gamma(N-1))>>.
81
@end ifnottex
82
@tex
83
$\mit \Gamma(N)\equiv N\times\Gamma(N-1)$.
84
@end tex
85
Accordingly, the results of the gamma function itself grow very
86
quickly.  <<gamma>> is defined as
87
@tex
88
$\mit ln\bigl(\Gamma(x)\bigr)$ rather than simply $\mit \Gamma(x)$
89
@end tex
90
@ifnottex
91
the natural log of the gamma function, rather than the gamma function
92
itself,
93
@end ifnottex
94
to extend the useful range of results representable.
95
 
96
The sign of the result is returned in the global variable <<signgam>>,
97
which is declared in math.h.
98
 
99
<<gammaf>> performs the same calculation as <<gamma>>, but uses and
100
returns <<float>> values.
101
 
102
<<lgamma>> and <<lgammaf>> are alternate names for <<gamma>> and
103
<<gammaf>>.  The use of <<lgamma>> instead of <<gamma>> is a reminder
104
that these functions compute the log of the gamma function, rather
105
than the gamma function itself.
106
 
107
The functions <<gamma_r>>, <<gammaf_r>>, <<lgamma_r>>, and
108
<<lgammaf_r>> are just like <<gamma>>, <<gammaf>>, <<lgamma>>, and
109
<<lgammaf>>, respectively, but take an additional argument.  This
110
additional argument is a pointer to an integer.  This additional
111
argument is used to return the sign of the result, and the global
112
variable <<signgam>> is not used.  These functions may be used for
113
reentrant calls (but they will still set the global variable <<errno>>
114
if an error occurs).
115
 
116
RETURNS
117
Normally, the computed result is returned.
118
 
119
When <[x]> is a nonpositive integer, <<gamma>> returns <<HUGE_VAL>>
120
and <<errno>> is set to <<EDOM>>.  If the result overflows, <<gamma>>
121
returns <<HUGE_VAL>> and <<errno>> is set to <<ERANGE>>.
122
 
123
You can modify this error treatment using <<matherr>>.
124
 
125
PORTABILITY
126
Neither <<gamma>> nor <<gammaf>> is ANSI C.  */
127
 
128
/* lgamma_r(x, signgamp)
129
 * Reentrant version of the logarithm of the Gamma function
130
 * with user provide pointer for the sign of Gamma(x).
131
 *
132
 * Method:
133
 *   1. Argument Reduction for 0 < x <= 8
134
 *      Since gamma(1+s)=s*gamma(s), for x in [0,8], we may
135
 *      reduce x to a number in [1.5,2.5] by
136
 *              lgamma(1+s) = log(s) + lgamma(s)
137
 *      for example,
138
 *              lgamma(7.3) = log(6.3) + lgamma(6.3)
139
 *                          = log(6.3*5.3) + lgamma(5.3)
140
 *                          = log(6.3*5.3*4.3*3.3*2.3) + lgamma(2.3)
141
 *   2. Polynomial approximation of lgamma around its
142
 *      minimun ymin=1.461632144968362245 to maintain monotonicity.
143
 *      On [ymin-0.23, ymin+0.27] (i.e., [1.23164,1.73163]), use
144
 *              Let z = x-ymin;
145
 *              lgamma(x) = -1.214862905358496078218 + z^2*poly(z)
146
 *      where
147
 *              poly(z) is a 14 degree polynomial.
148
 *   2. Rational approximation in the primary interval [2,3]
149
 *      We use the following approximation:
150
 *              s = x-2.0;
151
 *              lgamma(x) = 0.5*s + s*P(s)/Q(s)
152
 *      with accuracy
153
 *              |P/Q - (lgamma(x)-0.5s)| < 2**-61.71
154
 *      Our algorithms are based on the following observation
155
 *
156
 *                             zeta(2)-1    2    zeta(3)-1    3
157
 * lgamma(2+s) = s*(1-Euler) + --------- * s  -  --------- * s  + ...
158
 *                                 2                 3
159
 *
160
 *      where Euler = 0.5771... is the Euler constant, which is very
161
 *      close to 0.5.
162
 *
163
 *   3. For x>=8, we have
164
 *      lgamma(x)~(x-0.5)log(x)-x+0.5*log(2pi)+1/(12x)-1/(360x**3)+....
165
 *      (better formula:
166
 *         lgamma(x)~(x-0.5)*(log(x)-1)-.5*(log(2pi)-1) + ...)
167
 *      Let z = 1/x, then we approximation
168
 *              f(z) = lgamma(x) - (x-0.5)(log(x)-1)
169
 *      by
170
 *                                  3       5             11
171
 *              w = w0 + w1*z + w2*z  + w3*z  + ... + w6*z
172
 *      where
173
 *              |w - f(z)| < 2**-58.74
174
 *
175
 *   4. For negative x, since (G is gamma function)
176
 *              -x*G(-x)*G(x) = pi/sin(pi*x),
177
 *      we have
178
 *              G(x) = pi/(sin(pi*x)*(-x)*G(-x))
179
 *      since G(-x) is positive, sign(G(x)) = sign(sin(pi*x)) for x<0
180
 *      Hence, for x<0, signgam = sign(sin(pi*x)) and
181
 *              lgamma(x) = log(|Gamma(x)|)
182
 *                        = log(pi/(|x*sin(pi*x)|)) - lgamma(-x);
183
 *      Note: one should avoid compute pi*(-x) directly in the
184
 *            computation of sin(pi*(-x)).
185
 *
186
 *   5. Special Cases
187
 *              lgamma(2+s) ~ s*(1-Euler) for tiny s
188
 *              lgamma(1)=lgamma(2)=0
189
 *              lgamma(x) ~ -log(x) for tiny x
190
 *              lgamma(0) = lgamma(inf) = inf
191
 *              lgamma(-integer) = +-inf
192
 *
193
 */
194
 
195
#include "fdlibm.h"
196
 
197
#ifdef __STDC__
198
static const double
199
#else
200
static double
201
#endif
202
two52=  4.50359962737049600000e+15, /* 0x43300000, 0x00000000 */
203
half=  5.00000000000000000000e-01, /* 0x3FE00000, 0x00000000 */
204
one =  1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */
205
pi  =  3.14159265358979311600e+00, /* 0x400921FB, 0x54442D18 */
206
a0  =  7.72156649015328655494e-02, /* 0x3FB3C467, 0xE37DB0C8 */
207
a1  =  3.22467033424113591611e-01, /* 0x3FD4A34C, 0xC4A60FAD */
208
a2  =  6.73523010531292681824e-02, /* 0x3FB13E00, 0x1A5562A7 */
209
a3  =  2.05808084325167332806e-02, /* 0x3F951322, 0xAC92547B */
210
a4  =  7.38555086081402883957e-03, /* 0x3F7E404F, 0xB68FEFE8 */
211
a5  =  2.89051383673415629091e-03, /* 0x3F67ADD8, 0xCCB7926B */
212
a6  =  1.19270763183362067845e-03, /* 0x3F538A94, 0x116F3F5D */
213
a7  =  5.10069792153511336608e-04, /* 0x3F40B6C6, 0x89B99C00 */
214
a8  =  2.20862790713908385557e-04, /* 0x3F2CF2EC, 0xED10E54D */
215
a9  =  1.08011567247583939954e-04, /* 0x3F1C5088, 0x987DFB07 */
216
a10 =  2.52144565451257326939e-05, /* 0x3EFA7074, 0x428CFA52 */
217
a11 =  4.48640949618915160150e-05, /* 0x3F07858E, 0x90A45837 */
218
tc  =  1.46163214496836224576e+00, /* 0x3FF762D8, 0x6356BE3F */
219
tf  = -1.21486290535849611461e-01, /* 0xBFBF19B9, 0xBCC38A42 */
220
/* tt = -(tail of tf) */
221
tt  = -3.63867699703950536541e-18, /* 0xBC50C7CA, 0xA48A971F */
222
t0  =  4.83836122723810047042e-01, /* 0x3FDEF72B, 0xC8EE38A2 */
223
t1  = -1.47587722994593911752e-01, /* 0xBFC2E427, 0x8DC6C509 */
224
t2  =  6.46249402391333854778e-02, /* 0x3FB08B42, 0x94D5419B */
225
t3  = -3.27885410759859649565e-02, /* 0xBFA0C9A8, 0xDF35B713 */
226
t4  =  1.79706750811820387126e-02, /* 0x3F9266E7, 0x970AF9EC */
227
t5  = -1.03142241298341437450e-02, /* 0xBF851F9F, 0xBA91EC6A */
228
t6  =  6.10053870246291332635e-03, /* 0x3F78FCE0, 0xE370E344 */
229
t7  = -3.68452016781138256760e-03, /* 0xBF6E2EFF, 0xB3E914D7 */
230
t8  =  2.25964780900612472250e-03, /* 0x3F6282D3, 0x2E15C915 */
231
t9  = -1.40346469989232843813e-03, /* 0xBF56FE8E, 0xBF2D1AF1 */
232
t10 =  8.81081882437654011382e-04, /* 0x3F4CDF0C, 0xEF61A8E9 */
233
t11 = -5.38595305356740546715e-04, /* 0xBF41A610, 0x9C73E0EC */
234
t12 =  3.15632070903625950361e-04, /* 0x3F34AF6D, 0x6C0EBBF7 */
235
t13 = -3.12754168375120860518e-04, /* 0xBF347F24, 0xECC38C38 */
236
t14 =  3.35529192635519073543e-04, /* 0x3F35FD3E, 0xE8C2D3F4 */
237
u0  = -7.72156649015328655494e-02, /* 0xBFB3C467, 0xE37DB0C8 */
238
u1  =  6.32827064025093366517e-01, /* 0x3FE4401E, 0x8B005DFF */
239
u2  =  1.45492250137234768737e+00, /* 0x3FF7475C, 0xD119BD6F */
240
u3  =  9.77717527963372745603e-01, /* 0x3FEF4976, 0x44EA8450 */
241
u4  =  2.28963728064692451092e-01, /* 0x3FCD4EAE, 0xF6010924 */
242
u5  =  1.33810918536787660377e-02, /* 0x3F8B678B, 0xBF2BAB09 */
243
v1  =  2.45597793713041134822e+00, /* 0x4003A5D7, 0xC2BD619C */
244
v2  =  2.12848976379893395361e+00, /* 0x40010725, 0xA42B18F5 */
245
v3  =  7.69285150456672783825e-01, /* 0x3FE89DFB, 0xE45050AF */
246
v4  =  1.04222645593369134254e-01, /* 0x3FBAAE55, 0xD6537C88 */
247
v5  =  3.21709242282423911810e-03, /* 0x3F6A5ABB, 0x57D0CF61 */
248
s0  = -7.72156649015328655494e-02, /* 0xBFB3C467, 0xE37DB0C8 */
249
s1  =  2.14982415960608852501e-01, /* 0x3FCB848B, 0x36E20878 */
250
s2  =  3.25778796408930981787e-01, /* 0x3FD4D98F, 0x4F139F59 */
251
s3  =  1.46350472652464452805e-01, /* 0x3FC2BB9C, 0xBEE5F2F7 */
252
s4  =  2.66422703033638609560e-02, /* 0x3F9B481C, 0x7E939961 */
253
s5  =  1.84028451407337715652e-03, /* 0x3F5E26B6, 0x7368F239 */
254
s6  =  3.19475326584100867617e-05, /* 0x3F00BFEC, 0xDD17E945 */
255
r1  =  1.39200533467621045958e+00, /* 0x3FF645A7, 0x62C4AB74 */
256
r2  =  7.21935547567138069525e-01, /* 0x3FE71A18, 0x93D3DCDC */
257
r3  =  1.71933865632803078993e-01, /* 0x3FC601ED, 0xCCFBDF27 */
258
r4  =  1.86459191715652901344e-02, /* 0x3F9317EA, 0x742ED475 */
259
r5  =  7.77942496381893596434e-04, /* 0x3F497DDA, 0xCA41A95B */
260
r6  =  7.32668430744625636189e-06, /* 0x3EDEBAF7, 0xA5B38140 */
261
w0  =  4.18938533204672725052e-01, /* 0x3FDACFE3, 0x90C97D69 */
262
w1  =  8.33333333333329678849e-02, /* 0x3FB55555, 0x5555553B */
263
w2  = -2.77777777728775536470e-03, /* 0xBF66C16C, 0x16B02E5C */
264
w3  =  7.93650558643019558500e-04, /* 0x3F4A019F, 0x98CF38B6 */
265
w4  = -5.95187557450339963135e-04, /* 0xBF4380CB, 0x8C0FE741 */
266
w5  =  8.36339918996282139126e-04, /* 0x3F4B67BA, 0x4CDAD5D1 */
267
w6  = -1.63092934096575273989e-03; /* 0xBF5AB89D, 0x0B9E43E4 */
268
 
269
#ifdef __STDC__
270
static const double zero=  0.00000000000000000000e+00;
271
#else
272
static double zero=  0.00000000000000000000e+00;
273
#endif
274
 
275
#ifdef __STDC__
276
        static double sin_pi(double x)
277
#else
278
        static double sin_pi(x)
279
        double x;
280
#endif
281
{
282
        double y,z;
283
        __int32_t n,ix;
284
 
285
        GET_HIGH_WORD(ix,x);
286
        ix &= 0x7fffffff;
287
 
288
        if(ix<0x3fd00000) return sin(pi*x);
289
        y = -x;         /* x is assume negative */
290
 
291
    /*
292
     * argument reduction, make sure inexact flag not raised if input
293
     * is an integer
294
     */
295
        z = floor(y);
296
        if(z!=y) {                              /* inexact anyway */
297
            y  *= 0.5;
298
            y   = 2.0*(y - floor(y));           /* y = |x| mod 2.0 */
299
            n   = (__int32_t) (y*4.0);
300
        } else {
301
            if(ix>=0x43400000) {
302
                y = zero; n = 0;                 /* y must be even */
303
            } else {
304
                if(ix<0x43300000) z = y+two52;  /* exact */
305
                GET_LOW_WORD(n,z);
306
                n &= 1;
307
                y  = n;
308
                n<<= 2;
309
            }
310
        }
311
        switch (n) {
312
            case 0:   y =  sin(pi*y); break;
313
            case 1:
314
            case 2:   y =  cos(pi*(0.5-y)); break;
315
            case 3:
316
            case 4:   y =  sin(pi*(one-y)); break;
317
            case 5:
318
            case 6:   y = -cos(pi*(y-1.5)); break;
319
            default:  y =  sin(pi*(y-2.0)); break;
320
            }
321
        return -y;
322
}
323
 
324
 
325
#ifdef __STDC__
326
        double lgamma_r(double x, int *signgamp)
327
#else
328
        double lgamma_r(x,signgamp)
329
        double x; int *signgamp;
330
#endif
331
{
332
        double t,y,z,nadj,p,p1,p2,p3,q,r,w;
333
        __int32_t i,hx,lx,ix;
334
 
335
        nadj = 0;
336
 
337
        EXTRACT_WORDS(hx,lx,x);
338
 
339
    /* purge off +-inf, NaN, +-0, and negative arguments */
340
        *signgamp = 1;
341
        ix = hx&0x7fffffff;
342
        if(ix>=0x7ff00000) return x*x;
343
        if((ix|lx)==0) return one/zero;
344
        if(ix<0x3b900000) {     /* |x|<2**-70, return -log(|x|) */
345
            if(hx<0) {
346
                *signgamp = -1;
347
                return -log(-x);
348
            } else return -log(x);
349
        }
350
        if(hx<0) {
351
            if(ix>=0x43300000)  /* |x|>=2**52, must be -integer */
352
                return one/zero;
353
            t = sin_pi(x);
354
            if(t==zero) return one/zero; /* -integer */
355
            nadj = log(pi/fabs(t*x));
356
            if(t<zero) *signgamp = -1;
357
            x = -x;
358
        }
359
 
360
    /* purge off 1 and 2 */
361
        if((((ix-0x3ff00000)|lx)==0)||(((ix-0x40000000)|lx)==0)) r = 0;
362
    /* for x < 2.0 */
363
        else if(ix<0x40000000) {
364
            if(ix<=0x3feccccc) {        /* lgamma(x) = lgamma(x+1)-log(x) */
365
                r = -log(x);
366
                if(ix>=0x3FE76944) {y = one-x; i= 0;}
367
                else if(ix>=0x3FCDA661) {y= x-(tc-one); i=1;}
368
                else {y = x; i=2;}
369
            } else {
370
                r = zero;
371
                if(ix>=0x3FFBB4C3) {y=2.0-x;i=0;} /* [1.7316,2] */
372
                else if(ix>=0x3FF3B4C4) {y=x-tc;i=1;} /* [1.23,1.73] */
373
                else {y=x-one;i=2;}
374
            }
375
            switch(i) {
376
              case 0:
377
                z = y*y;
378
                p1 = a0+z*(a2+z*(a4+z*(a6+z*(a8+z*a10))));
379
                p2 = z*(a1+z*(a3+z*(a5+z*(a7+z*(a9+z*a11)))));
380
                p  = y*p1+p2;
381
                r  += (p-0.5*y); break;
382
              case 1:
383
                z = y*y;
384
                w = z*y;
385
                p1 = t0+w*(t3+w*(t6+w*(t9 +w*t12)));    /* parallel comp */
386
                p2 = t1+w*(t4+w*(t7+w*(t10+w*t13)));
387
                p3 = t2+w*(t5+w*(t8+w*(t11+w*t14)));
388
                p  = z*p1-(tt-w*(p2+y*p3));
389
                r += (tf + p); break;
390
              case 2:
391
                p1 = y*(u0+y*(u1+y*(u2+y*(u3+y*(u4+y*u5)))));
392
                p2 = one+y*(v1+y*(v2+y*(v3+y*(v4+y*v5))));
393
                r += (-0.5*y + p1/p2);
394
            }
395
        }
396
        else if(ix<0x40200000) {                        /* x < 8.0 */
397
            i = (__int32_t)x;
398
            t = zero;
399
            y = x-(double)i;
400
            p = y*(s0+y*(s1+y*(s2+y*(s3+y*(s4+y*(s5+y*s6))))));
401
            q = one+y*(r1+y*(r2+y*(r3+y*(r4+y*(r5+y*r6)))));
402
            r = half*y+p/q;
403
            z = one;    /* lgamma(1+s) = log(s) + lgamma(s) */
404
            switch(i) {
405
            case 7: z *= (y+6.0);       /* FALLTHRU */
406
            case 6: z *= (y+5.0);       /* FALLTHRU */
407
            case 5: z *= (y+4.0);       /* FALLTHRU */
408
            case 4: z *= (y+3.0);       /* FALLTHRU */
409
            case 3: z *= (y+2.0);       /* FALLTHRU */
410
                    r += log(z); break;
411
            }
412
    /* 8.0 <= x < 2**58 */
413
        } else if (ix < 0x43900000) {
414
            t = log(x);
415
            z = one/x;
416
            y = z*z;
417
            w = w0+z*(w1+y*(w2+y*(w3+y*(w4+y*(w5+y*w6)))));
418
            r = (x-half)*(t-one)+w;
419
        } else
420
    /* 2**58 <= x <= inf */
421
            r =  x*(log(x)-one);
422
        if(hx<0) r = nadj - r;
423
        return r;
424
}
425
 
426
double
427
lgamma(double x)
428
{
429
  return lgamma_r(x, &(_REENT_SIGNGAM(_REENT)));
430
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.