Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libm/common/LD/jnl.c
+++ new/usr/src/lib/libm/common/LD/jnl.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 24 */
25 25 /*
26 26 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
27 27 * Use is subject to license terms.
28 28 */
29 29
30 30 #if defined(ELFOBJ)
31 31 #pragma weak jnl = __jnl
32 32 #pragma weak ynl = __ynl
33 33 #endif
34 34
35 35 /*
36 36 * floating point Bessel's function of the 1st and 2nd kind
37 37 * of order n: jn(n,x),yn(n,x);
38 38 *
39 39 * Special cases:
40 40 * y0(0)=y1(0)=yn(n,0) = -inf with division by zero signal;
41 41 * y0(-ve)=y1(-ve)=yn(n,-ve) are NaN with invalid signal.
42 42 * Note 2. About jn(n,x), yn(n,x)
43 43 * For n=0, j0(x) is called,
44 44 * for n=1, j1(x) is called,
45 45 * for n<x, forward recursion us used starting
46 46 * from values of j0(x) and j1(x).
47 47 * for n>x, a continued fraction approximation to
48 48 * j(n,x)/j(n-1,x) is evaluated and then backward
49 49 * recursion is used starting from a supposed value
50 50 * for j(n,x). The resulting value of j(0,x) is
↓ open down ↓ |
50 lines elided |
↑ open up ↑ |
51 51 * compared with the actual value to correct the
52 52 * supposed value of j(n,x).
53 53 *
54 54 * yn(n,x) is similar in all respects, except
55 55 * that forward recursion is used for all
56 56 * values of n>1.
57 57 *
58 58 */
59 59
60 60 #include "libm.h"
61 +#include "longdouble.h"
61 62 #include <float.h> /* LDBL_MAX */
62 63
63 64 #define GENERIC long double
64 65
65 66 static const GENERIC
66 67 invsqrtpi= 5.641895835477562869480794515607725858441e-0001L,
67 68 two = 2.0L,
68 69 zero = 0.0L,
69 70 one = 1.0L;
70 71
71 72 GENERIC
72 73 jnl(n,x) int n; GENERIC x;{
73 74 int i, sgn;
74 - GENERIC a, b, temp, z, w;
75 + GENERIC a, b, temp = 0, z, w;
75 76
76 77 /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
77 78 * Thus, J(-n,x) = J(n,-x)
78 79 */
79 80 if(n<0){
80 81 n = -n;
81 82 x = -x;
82 83 }
83 84 if(n==0) return(j0l(x));
84 85 if(n==1) return(j1l(x));
85 86 if(x!=x) return x+x;
86 87 if((n&1)==0)
87 88 sgn=0; /* even n */
88 89 else
89 90 sgn = signbitl(x); /* old n */
90 91 x = fabsl(x);
91 92 if(x == zero||!finitel(x)) b = zero;
92 93 else if((GENERIC)n<=x) { /* Safe to use
93 94 J(n+1,x)=2n/x *J(n,x)-J(n-1,x)
94 95 */
95 96 if(x>1.0e91L) { /* x >> n**2
96 97 Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
97 98 Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
98 99 Let s=sin(x), c=cos(x),
99 100 xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
100 101
101 102 n sin(xn)*sqt2 cos(xn)*sqt2
102 103 ----------------------------------
103 104 0 s-c c+s
104 105 1 -s-c -c+s
105 106 2 -s+c -c-s
106 107 3 s+c c-s
107 108 */
108 109 switch(n&3) {
109 110 case 0: temp = cosl(x)+sinl(x); break;
110 111 case 1: temp = -cosl(x)+sinl(x); break;
111 112 case 2: temp = -cosl(x)-sinl(x); break;
112 113 case 3: temp = cosl(x)-sinl(x); break;
113 114 }
114 115 b = invsqrtpi*temp/sqrtl(x);
115 116 } else {
116 117 a = j0l(x);
117 118 b = j1l(x);
118 119 for(i=1;i<n;i++){
119 120 temp = b;
120 121 b = b*((GENERIC)(i+i)/x) - a; /* avoid underflow */
121 122 a = temp;
122 123 }
123 124 }
124 125 } else {
125 126 if(x<1e-17L) { /* use J(n,x) = 1/n!*(x/2)^n */
126 127 b = powl(0.5L*x,(GENERIC) n);
127 128 if (b!=zero) {
128 129 for(a=one,i=1;i<=n;i++) a *= (GENERIC)i;
129 130 b = b/a;
130 131 }
131 132 } else {
132 133 /* use backward recurrence */
133 134 /* x x^2 x^2
134 135 * J(n,x)/J(n-1,x) = ---- ------ ------ .....
135 136 * 2n - 2(n+1) - 2(n+2)
136 137 *
137 138 * 1 1 1
138 139 * (for large x) = ---- ------ ------ .....
139 140 * 2n 2(n+1) 2(n+2)
140 141 * -- - ------ - ------ -
141 142 * x x x
142 143 *
143 144 * Let w = 2n/x and h=2/x, then the above quotient
144 145 * is equal to the continued fraction:
145 146 * 1
146 147 * = -----------------------
147 148 * 1
148 149 * w - -----------------
149 150 * 1
150 151 * w+h - ---------
151 152 * w+2h - ...
152 153 *
153 154 * To determine how many terms needed, let
154 155 * Q(0) = w, Q(1) = w(w+h) - 1,
155 156 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
156 157 * When Q(k) > 1e4 good for single
157 158 * When Q(k) > 1e9 good for double
158 159 * When Q(k) > 1e17 good for quaduple
159 160 */
160 161 /* determin k */
161 162 GENERIC t,v;
162 163 double q0,q1,h,tmp; int k,m;
163 164 w = (n+n)/(double)x; h = 2.0/(double)x;
164 165 q0 = w; z = w+h; q1 = w*z - 1.0; k=1;
165 166 while(q1<1.0e17) {
166 167 k += 1; z += h;
167 168 tmp = z*q1 - q0;
168 169 q0 = q1;
169 170 q1 = tmp;
170 171 }
171 172 m = n+n;
172 173 for(t=zero, i = 2*(n+k); i>=m; i -= 2) t = one/(i/x-t);
173 174 a = t;
174 175 b = one;
175 176 /* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
176 177 hence, if n*(log(2n/x)) > ...
177 178 single 8.8722839355e+01
178 179 double 7.09782712893383973096e+02
179 180 long double 1.1356523406294143949491931077970765006170e+04
180 181 then recurrent value may overflow and the result is
181 182 likely underflow to zero
182 183 */
183 184 tmp = n;
184 185 v = two/x;
185 186 tmp = tmp*logl(fabsl(v*tmp));
186 187 if(tmp<1.1356523406294143949491931077970765e+04L) {
187 188 for(i=n-1;i>0;i--){
188 189 temp = b;
189 190 b = ((i+i)/x)*b - a;
190 191 a = temp;
191 192 }
192 193 } else {
193 194 for(i=n-1;i>0;i--){
194 195 temp = b;
195 196 b = ((i+i)/x)*b - a;
196 197 a = temp;
197 198 if(b>1e1000L) {
198 199 a /= b;
199 200 t /= b;
200 201 b = 1.0;
201 202 }
202 203 }
203 204 }
↓ open down ↓ |
119 lines elided |
↑ open up ↑ |
204 205 b = (t*j0l(x)/b);
205 206 }
206 207 }
207 208 if(sgn==1) return -b; else return b;
208 209 }
209 210
210 211 GENERIC ynl(n,x)
211 212 int n; GENERIC x;{
212 213 int i;
213 214 int sign;
214 - GENERIC a, b, temp;
215 + GENERIC a, b, temp = 0;
215 216
216 - if(x!=x) return x+x;
217 - if (x <= zero)
217 + if(x!=x)
218 + return x+x;
219 + if (x <= zero) {
218 220 if(x==zero)
219 221 return -one/zero;
220 222 else
221 223 return zero/zero;
224 + }
222 225 sign = 1;
223 226 if(n<0){
224 227 n = -n;
225 228 if((n&1) == 1) sign = -1;
226 229 }
227 230 if(n==0) return(y0l(x));
228 231 if(n==1) return(sign*y1l(x));
229 232 if(!finitel(x)) return zero;
230 233
231 234 if(x>1.0e91L) { /* x >> n**2
232 235 Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
233 236 Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
234 237 Let s=sin(x), c=cos(x),
235 238 xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
236 239
237 240 n sin(xn)*sqt2 cos(xn)*sqt2
238 241 ----------------------------------
239 242 0 s-c c+s
240 243 1 -s-c -c+s
241 244 2 -s+c -c-s
242 245 3 s+c c-s
243 246 */
244 247 switch(n&3) {
245 248 case 0: temp = sinl(x)-cosl(x); break;
246 249 case 1: temp = -sinl(x)-cosl(x); break;
247 250 case 2: temp = -sinl(x)+cosl(x); break;
248 251 case 3: temp = sinl(x)+cosl(x); break;
249 252 }
250 253 b = invsqrtpi*temp/sqrtl(x);
251 254 } else {
252 255 a = y0l(x);
253 256 b = y1l(x);
254 257 /*
255 258 * fix 1262058 and take care of non-default rounding
256 259 */
257 260 for (i = 1; i < n; i++) {
258 261 temp = b;
259 262 b *= (GENERIC) (i + i) / x;
260 263 if (b <= -LDBL_MAX)
261 264 break;
262 265 b -= a;
263 266 a = temp;
264 267 }
265 268 }
266 269 if(sign>0) return b; else return -b;
267 270 }
↓ open down ↓ |
36 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX