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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [newlib-1.17.0/] [newlib/] [libm/] [math/] [e_jn.c] - Blame information for rev 862

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

Line No. Rev Author Line
1 148 jeremybenn
 
2
/* @(#)e_jn.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
 * __ieee754_jn(n, x), __ieee754_yn(n, x)
16
 * floating point Bessel's function of the 1st and 2nd kind
17
 * of order n
18
 *
19
 * Special cases:
20
 *      y0(0)=y1(0)=yn(n,0) = -inf with division by zero signal;
21
 *      y0(-ve)=y1(-ve)=yn(n,-ve) are NaN with invalid signal.
22
 * Note 2. About jn(n,x), yn(n,x)
23
 *      For n=0, j0(x) is called,
24
 *      for n=1, j1(x) is called,
25
 *      for n<x, forward recursion us used starting
26
 *      from values of j0(x) and j1(x).
27
 *      for n>x, a continued fraction approximation to
28
 *      j(n,x)/j(n-1,x) is evaluated and then backward
29
 *      recursion is used starting from a supposed value
30
 *      for j(n,x). The resulting value of j(0,x) is
31
 *      compared with the actual value to correct the
32
 *      supposed value of j(n,x).
33
 *
34
 *      yn(n,x) is similar in all respects, except
35
 *      that forward recursion is used for all
36
 *      values of n>1.
37
 *
38
 */
39
 
40
#include "fdlibm.h"
41
 
42
#ifndef _DOUBLE_IS_32BITS
43
 
44
#ifdef __STDC__
45
static const double
46
#else
47
static double
48
#endif
49
invsqrtpi=  5.64189583547756279280e-01, /* 0x3FE20DD7, 0x50429B6D */
50
two   =  2.00000000000000000000e+00, /* 0x40000000, 0x00000000 */
51
one   =  1.00000000000000000000e+00; /* 0x3FF00000, 0x00000000 */
52
 
53
#ifdef __STDC__
54
static const double zero  =  0.00000000000000000000e+00;
55
#else
56
static double zero  =  0.00000000000000000000e+00;
57
#endif
58
 
59
#ifdef __STDC__
60
        double __ieee754_jn(int n, double x)
61
#else
62
        double __ieee754_jn(n,x)
63
        int n; double x;
64
#endif
65
{
66
        __int32_t i,hx,ix,lx, sgn;
67
        double a, b, temp, di;
68
        double z, w;
69
 
70
    /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
71
     * Thus, J(-n,x) = J(n,-x)
72
     */
73
        EXTRACT_WORDS(hx,lx,x);
74
        ix = 0x7fffffff&hx;
75
    /* if J(n,NaN) is NaN */
76
        if((ix|((__uint32_t)(lx|-lx))>>31)>0x7ff00000) return x+x;
77
        if(n<0){
78
                n = -n;
79
                x = -x;
80
                hx ^= 0x80000000;
81
        }
82
        if(n==0) return(__ieee754_j0(x));
83
        if(n==1) return(__ieee754_j1(x));
84
        sgn = (n&1)&(hx>>31);   /* even n -- 0, odd n -- sign(x) */
85
        x = fabs(x);
86
        if((ix|lx)==0||ix>=0x7ff00000)   /* if x is 0 or inf */
87
            b = zero;
88
        else if((double)n<=x) {
89
                /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
90
            if(ix>=0x52D00000) { /* x > 2**302 */
91
    /* (x >> n**2)
92
     *      Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
93
     *      Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
94
     *      Let s=sin(x), c=cos(x),
95
     *          xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
96
     *
97
     *             n    sin(xn)*sqt2    cos(xn)*sqt2
98
     *          ----------------------------------
99
     *             0     s-c             c+s
100
     *             1    -s-c            -c+s
101
     *             2    -s+c            -c-s
102
     *             3     s+c             c-s
103
     */
104
                switch(n&3) {
105
                    case 0: temp =  cos(x)+sin(x); break;
106
                    case 1: temp = -cos(x)+sin(x); break;
107
                    case 2: temp = -cos(x)-sin(x); break;
108
                    case 3: temp =  cos(x)-sin(x); break;
109
                }
110
                b = invsqrtpi*temp/__ieee754_sqrt(x);
111
            } else {
112
                a = __ieee754_j0(x);
113
                b = __ieee754_j1(x);
114
                for(i=1;i<n;i++){
115
                    temp = b;
116
                    b = b*((double)(i+i)/x) - a; /* avoid underflow */
117
                    a = temp;
118
                }
119
            }
120
        } else {
121
            if(ix<0x3e100000) { /* x < 2**-29 */
122
    /* x is tiny, return the first Taylor expansion of J(n,x)
123
     * J(n,x) = 1/n!*(x/2)^n  - ...
124
     */
125
                if(n>33)        /* underflow */
126
                    b = zero;
127
                else {
128
                    temp = x*0.5; b = temp;
129
                    for (a=one,i=2;i<=n;i++) {
130
                        a *= (double)i;         /* a = n! */
131
                        b *= temp;              /* b = (x/2)^n */
132
                    }
133
                    b = b/a;
134
                }
135
            } else {
136
                /* use backward recurrence */
137
                /*                      x      x^2      x^2
138
                 *  J(n,x)/J(n-1,x) =  ----   ------   ------   .....
139
                 *                      2n  - 2(n+1) - 2(n+2)
140
                 *
141
                 *                      1      1        1
142
                 *  (for large x)   =  ----  ------   ------   .....
143
                 *                      2n   2(n+1)   2(n+2)
144
                 *                      -- - ------ - ------ -
145
                 *                       x     x         x
146
                 *
147
                 * Let w = 2n/x and h=2/x, then the above quotient
148
                 * is equal to the continued fraction:
149
                 *                  1
150
                 *      = -----------------------
151
                 *                     1
152
                 *         w - -----------------
153
                 *                        1
154
                 *              w+h - ---------
155
                 *                     w+2h - ...
156
                 *
157
                 * To determine how many terms needed, let
158
                 * Q(0) = w, Q(1) = w(w+h) - 1,
159
                 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
160
                 * When Q(k) > 1e4      good for single
161
                 * When Q(k) > 1e9      good for double
162
                 * When Q(k) > 1e17     good for quadruple
163
                 */
164
            /* determine k */
165
                double t,v;
166
                double q0,q1,h,tmp; __int32_t k,m;
167
                w  = (n+n)/(double)x; h = 2.0/(double)x;
168
                q0 = w;  z = w+h; q1 = w*z - 1.0; k=1;
169
                while(q1<1.0e9) {
170
                        k += 1; z += h;
171
                        tmp = z*q1 - q0;
172
                        q0 = q1;
173
                        q1 = tmp;
174
                }
175
                m = n+n;
176
                for(t=zero, i = 2*(n+k); i>=m; i -= 2) t = one/(i/x-t);
177
                a = t;
178
                b = one;
179
                /*  estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
180
                 *  Hence, if n*(log(2n/x)) > ...
181
                 *  single 8.8722839355e+01
182
                 *  double 7.09782712893383973096e+02
183
                 *  long double 1.1356523406294143949491931077970765006170e+04
184
                 *  then recurrent value may overflow and the result is
185
                 *  likely underflow to zero
186
                 */
187
                tmp = n;
188
                v = two/x;
189
                tmp = tmp*__ieee754_log(fabs(v*tmp));
190
                if(tmp<7.09782712893383973096e+02) {
191
                    for(i=n-1,di=(double)(i+i);i>0;i--){
192
                        temp = b;
193
                        b *= di;
194
                        b  = b/x - a;
195
                        a = temp;
196
                        di -= two;
197
                    }
198
                } else {
199
                    for(i=n-1,di=(double)(i+i);i>0;i--){
200
                        temp = b;
201
                        b *= di;
202
                        b  = b/x - a;
203
                        a = temp;
204
                        di -= two;
205
                    /* scale b to avoid spurious overflow */
206
                        if(b>1e100) {
207
                            a /= b;
208
                            t /= b;
209
                            b  = one;
210
                        }
211
                    }
212
                }
213
                b = (t*__ieee754_j0(x)/b);
214
            }
215
        }
216
        if(sgn==1) return -b; else return b;
217
}
218
 
219
#ifdef __STDC__
220
        double __ieee754_yn(int n, double x)
221
#else
222
        double __ieee754_yn(n,x)
223
        int n; double x;
224
#endif
225
{
226
        __int32_t i,hx,ix,lx;
227
        __int32_t sign;
228
        double a, b, temp;
229
 
230
        EXTRACT_WORDS(hx,lx,x);
231
        ix = 0x7fffffff&hx;
232
    /* if Y(n,NaN) is NaN */
233
        if((ix|((__uint32_t)(lx|-lx))>>31)>0x7ff00000) return x+x;
234
        if((ix|lx)==0) return -one/zero;
235
        if(hx<0) return zero/zero;
236
        sign = 1;
237
        if(n<0){
238
                n = -n;
239
                sign = 1 - ((n&1)<<1);
240
        }
241
        if(n==0) return(__ieee754_y0(x));
242
        if(n==1) return(sign*__ieee754_y1(x));
243
        if(ix==0x7ff00000) return zero;
244
        if(ix>=0x52D00000) { /* x > 2**302 */
245
    /* (x >> n**2)
246
     *      Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
247
     *      Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
248
     *      Let s=sin(x), c=cos(x),
249
     *          xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
250
     *
251
     *             n    sin(xn)*sqt2    cos(xn)*sqt2
252
     *          ----------------------------------
253
     *             0     s-c             c+s
254
     *             1    -s-c            -c+s
255
     *             2    -s+c            -c-s
256
     *             3     s+c             c-s
257
     */
258
                switch(n&3) {
259
                    case 0: temp =  sin(x)-cos(x); break;
260
                    case 1: temp = -sin(x)-cos(x); break;
261
                    case 2: temp = -sin(x)+cos(x); break;
262
                    case 3: temp =  sin(x)+cos(x); break;
263
                }
264
                b = invsqrtpi*temp/__ieee754_sqrt(x);
265
        } else {
266
            __uint32_t high;
267
            a = __ieee754_y0(x);
268
            b = __ieee754_y1(x);
269
        /* quit if b is -inf */
270
            GET_HIGH_WORD(high,b);
271
            for(i=1;i<n&&high!=0xfff00000;i++){
272
                temp = b;
273
                b = ((double)(i+i)/x)*b - a;
274
                GET_HIGH_WORD(high,b);
275
                a = temp;
276
            }
277
        }
278
        if(sign>0) return b; else return -b;
279
}
280
 
281
#endif /* defined(_DOUBLE_IS_32BITS) */

powered by: WebSVN 2.1.0

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