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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [language/] [c/] [libm/] [current/] [src/] [double/] [ieee754-core/] [e_jn.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//===========================================================================
2
//
3
//      e_jn.c
4
//
5
//      Part of the standard mathematical function library
6
//
7
//===========================================================================
8
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
9
// -------------------------------------------                              
10
// This file is part of eCos, the Embedded Configurable Operating System.   
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under    
14
// the terms of the GNU General Public License as published by the Free     
15
// Software Foundation; either version 2 or (at your option) any later      
16
// version.                                                                 
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT      
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
21
// for more details.                                                        
22
//
23
// You should have received a copy of the GNU General Public License        
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
26
//
27
// As a special exception, if other files instantiate templates or use      
28
// macros or inline functions from this file, or you compile this file      
29
// and link it with other works to produce a work based on this file,       
30
// this file does not by itself cause the resulting work to be covered by   
31
// the GNU General Public License. However the source code for this file    
32
// must still be made available in accordance with section (3) of the GNU   
33
// General Public License v2.                                               
34
//
35
// This exception does not invalidate any other reasons why a work based    
36
// on this file might be covered by the GNU General Public License.         
37
// -------------------------------------------                              
38
// ####ECOSGPLCOPYRIGHTEND####                                              
39
//===========================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):   jlarmour
43
// Contributors:  jlarmour
44
// Date:        1998-02-13
45
// Purpose:     
46
// Description: 
47
// Usage:       
48
//
49
//####DESCRIPTIONEND####
50
//
51
//===========================================================================
52
 
53
// CONFIGURATION
54
 
55
#include <pkgconf/libm.h>   // Configuration header
56
 
57
// Include the Math library?
58
#ifdef CYGPKG_LIBM     
59
 
60
// Derived from code with the following copyright
61
 
62
 
63
/* @(#)e_jn.c 1.4 95/01/18 */
64
/*
65
 * ====================================================
66
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
67
 *
68
 * Developed at SunSoft, a Sun Microsystems, Inc. business.
69
 * Permission to use, copy, modify, and distribute this
70
 * software is freely granted, provided that this notice
71
 * is preserved.
72
 * ====================================================
73
 */
74
 
75
/*
76
 * __ieee754_jn(n, x), __ieee754_yn(n, x)
77
 * floating point Bessel's function of the 1st and 2nd kind
78
 * of order n
79
 *
80
 * Special cases:
81
 *      y0(0)=y1(0)=yn(n,0) = -inf with division by zero signal;
82
 *      y0(-ve)=y1(-ve)=yn(n,-ve) are NaN with invalid signal.
83
 * Note 2. About jn(n,x), yn(n,x)
84
 *      For n=0, j0(x) is called,
85
 *      for n=1, j1(x) is called,
86
 *      for n<x, forward recursion us used starting
87
 *      from values of j0(x) and j1(x).
88
 *      for n>x, a continued fraction approximation to
89
 *      j(n,x)/j(n-1,x) is evaluated and then backward
90
 *      recursion is used starting from a supposed value
91
 *      for j(n,x). The resulting value of j(0,x) is
92
 *      compared with the actual value to correct the
93
 *      supposed value of j(n,x).
94
 *
95
 *      yn(n,x) is similar in all respects, except
96
 *      that forward recursion is used for all
97
 *      values of n>1.
98
 *
99
 */
100
 
101
#include "mathincl/fdlibm.h"
102
 
103
static const double
104
invsqrtpi=  5.64189583547756279280e-01, /* 0x3FE20DD7, 0x50429B6D */
105
two   =  2.00000000000000000000e+00, /* 0x40000000, 0x00000000 */
106
one   =  1.00000000000000000000e+00; /* 0x3FF00000, 0x00000000 */
107
 
108
static double zero  =  0.00000000000000000000e+00;
109
 
110
        double __ieee754_jn(int n, double x)
111
{
112
        int i,hx,ix,lx, sgn;
113
        double a, b, temp, di;
114
        double z, w;
115
 
116
    /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
117
     * Thus, J(-n,x) = J(n,-x)
118
     */
119
        hx = CYG_LIBM_HI(x);
120
        ix = 0x7fffffff&hx;
121
        lx = CYG_LIBM_LO(x);
122
    /* if J(n,NaN) is NaN */
123
        if((ix|((unsigned)(lx|-lx))>>31)>0x7ff00000) return x+x;
124
        if(n<0){
125
                n = -n;
126
                x = -x;
127
                hx ^= 0x80000000;
128
        }
129
        if(n==0) return(__ieee754_j0(x));
130
        if(n==1) return(__ieee754_j1(x));
131
        sgn = (n&1)&(hx>>31);   /* even n -- 0, odd n -- sign(x) */
132
        x = fabs(x);
133
        if((ix|lx)==0||ix>=0x7ff00000)  /* if x is 0 or inf */
134
            b = zero;
135
        else if((double)n<=x) {
136
                /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
137
            if(ix>=0x52D00000) { /* x > 2**302 */
138
    /* (x >> n**2)
139
     *      Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
140
     *      Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
141
     *      Let s=sin(x), c=cos(x),
142
     *          xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
143
     *
144
     *             n    sin(xn)*sqt2    cos(xn)*sqt2
145
     *          ----------------------------------
146
     *             0     s-c             c+s
147
     *             1    -s-c            -c+s
148
     *             2    -s+c            -c-s
149
     *             3     s+c             c-s
150
     */
151
                switch(n&3) {
152
                    case 0: temp =  cos(x)+sin(x); break;
153
                    case 1: temp = -cos(x)+sin(x); break;
154
                    case 2: temp = -cos(x)-sin(x); break;
155
                    case 3: temp =  cos(x)-sin(x); break;
156
                    default: temp = 0.0; break; /* not used - purely to
157
                                                 * placate compiler */
158
                }
159
                b = invsqrtpi*temp/sqrt(x);
160
            } else {
161
                a = __ieee754_j0(x);
162
                b = __ieee754_j1(x);
163
                for(i=1;i<n;i++){
164
                    temp = b;
165
                    b = b*((double)(i+i)/x) - a; /* avoid underflow */
166
                    a = temp;
167
                }
168
            }
169
        } else {
170
            if(ix<0x3e100000) { /* x < 2**-29 */
171
    /* x is tiny, return the first Taylor expansion of J(n,x)
172
     * J(n,x) = 1/n!*(x/2)^n  - ...
173
     */
174
                if(n>33)        /* underflow */
175
                    b = zero;
176
                else {
177
                    temp = x*0.5; b = temp;
178
                    for (a=one,i=2;i<=n;i++) {
179
                        a *= (double)i;         /* a = n! */
180
                        b *= temp;              /* b = (x/2)^n */
181
                    }
182
                    b = b/a;
183
                }
184
            } else {
185
                /* use backward recurrence */
186
                /*                      x      x^2      x^2
187
                 *  J(n,x)/J(n-1,x) =  ----   ------   ------   .....
188
                 *                      2n  - 2(n+1) - 2(n+2)
189
                 *
190
                 *                      1      1        1
191
                 *  (for large x)   =  ----  ------   ------   .....
192
                 *                      2n   2(n+1)   2(n+2)
193
                 *                      -- - ------ - ------ -
194
                 *                       x     x         x
195
                 *
196
                 * Let w = 2n/x and h=2/x, then the above quotient
197
                 * is equal to the continued fraction:
198
                 *                  1
199
                 *      = -----------------------
200
                 *                     1
201
                 *         w - -----------------
202
                 *                        1
203
                 *              w+h - ---------
204
                 *                     w+2h - ...
205
                 *
206
                 * To determine how many terms needed, let
207
                 * Q(0) = w, Q(1) = w(w+h) - 1,
208
                 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
209
                 * When Q(k) > 1e4      good for single
210
                 * When Q(k) > 1e9      good for double
211
                 * When Q(k) > 1e17     good for quadruple
212
                 */
213
            /* determine k */
214
                double t,v;
215
                double q0,q1,h,tmp; int k,m;
216
                w  = (n+n)/(double)x; h = 2.0/(double)x;
217
                q0 = w;  z = w+h; q1 = w*z - 1.0; k=1;
218
                while(q1<1.0e9) {
219
                        k += 1; z += h;
220
                        tmp = z*q1 - q0;
221
                        q0 = q1;
222
                        q1 = tmp;
223
                }
224
                m = n+n;
225
                for(t=zero, i = 2*(n+k); i>=m; i -= 2) t = one/(i/x-t);
226
                a = t;
227
                b = one;
228
                /*  estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
229
                 *  Hence, if n*(log(2n/x)) > ...
230
                 *  single 8.8722839355e+01
231
                 *  double 7.09782712893383973096e+02
232
                 *  long double 1.1356523406294143949491931077970765006170e+04
233
                 *  then recurrent value may overflow and the result is
234
                 *  likely underflow to zero
235
                 */
236
                tmp = n;
237
                v = two/x;
238
                tmp = tmp*__ieee754_log(fabs(v*tmp));
239
                if(tmp<7.09782712893383973096e+02) {
240
                    for(i=n-1,di=(double)(i+i);i>0;i--){
241
                        temp = b;
242
                        b *= di;
243
                        b  = b/x - a;
244
                        a = temp;
245
                        di -= two;
246
                    }
247
                } else {
248
                    for(i=n-1,di=(double)(i+i);i>0;i--){
249
                        temp = b;
250
                        b *= di;
251
                        b  = b/x - a;
252
                        a = temp;
253
                        di -= two;
254
                    /* scale b to avoid spurious overflow */
255
                        if(b>1e100) {
256
                            a /= b;
257
                            t /= b;
258
                            b  = one;
259
                        }
260
                    }
261
                }
262
                b = (t*__ieee754_j0(x)/b);
263
            }
264
        }
265
        if(sgn==1) return -b; else return b;
266
}
267
 
268
        double __ieee754_yn(int n, double x)
269
{
270
        int i,hx,ix,lx;
271
        int sign;
272
        double a, b, temp;
273
 
274
        hx = CYG_LIBM_HI(x);
275
        ix = 0x7fffffff&hx;
276
        lx = CYG_LIBM_LO(x);
277
    /* if Y(n,NaN) is NaN */
278
        if((ix|((unsigned)(lx|-lx))>>31)>0x7ff00000) return x+x;
279
        if((ix|lx)==0) return -one/zero;
280
        if(hx<0) return zero/zero;
281
        sign = 1;
282
        if(n<0){
283
                n = -n;
284
                sign = 1 - ((n&1)<<1);
285
        }
286
        if(n==0) return(__ieee754_y0(x));
287
        if(n==1) return(sign*__ieee754_y1(x));
288
        if(ix==0x7ff00000) return zero;
289
        if(ix>=0x52D00000) { /* x > 2**302 */
290
    /* (x >> n**2)
291
     *      Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
292
     *      Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
293
     *      Let s=sin(x), c=cos(x),
294
     *          xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
295
     *
296
     *             n    sin(xn)*sqt2    cos(xn)*sqt2
297
     *          ----------------------------------
298
     *             0     s-c             c+s
299
     *             1    -s-c            -c+s
300
     *             2    -s+c            -c-s
301
     *             3     s+c             c-s
302
     */
303
                switch(n&3) {
304
                    case 0: temp =  sin(x)-cos(x); break;
305
                    case 1: temp = -sin(x)-cos(x); break;
306
                    case 2: temp = -sin(x)+cos(x); break;
307
                    case 3: temp =  sin(x)+cos(x); break;
308
                    default: temp = 0.0; break; /* not used - purely to
309
                                                 * placate compiler */
310
                }
311
                b = invsqrtpi*temp/sqrt(x);
312
        } else {
313
            a = __ieee754_y0(x);
314
            b = __ieee754_y1(x);
315
        /* quit if b is -inf */
316
            for(i=1;i<n&&((unsigned)CYG_LIBM_HI(b) != 0xfff00000);i++){
317
                temp = b;
318
                b = ((double)(i+i)/x)*b - a;
319
                a = temp;
320
            }
321
        }
322
        if(sign>0) return b; else return -b;
323
}
324
 
325
#endif // ifdef CYGPKG_LIBM     
326
 
327
// EOF e_jn.c

powered by: WebSVN 2.1.0

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