Print this page
11210 libm should be cstyle(1ONBLD) clean
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.
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
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 27 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
27 28 * Use is subject to license terms.
28 29 */
29 30
30 31 #pragma weak __jnl = jnl
31 32 #pragma weak __ynl = ynl
32 33
33 34 /*
34 35 * floating point Bessel's function of the 1st and 2nd kind
35 36 * of order n: jn(n,x),yn(n,x);
36 37 *
37 38 * Special cases:
38 39 * y0(0)=y1(0)=yn(n,0) = -inf with division by zero signal;
39 40 * y0(-ve)=y1(-ve)=yn(n,-ve) are NaN with invalid signal.
40 41 * Note 2. About jn(n,x), yn(n,x)
41 42 * For n=0, j0(x) is called,
42 43 * for n=1, j1(x) is called,
43 44 * for n<x, forward recursion us used starting
44 45 * from values of j0(x) and j1(x).
45 46 * for n>x, a continued fraction approximation to
46 47 * j(n,x)/j(n-1,x) is evaluated and then backward
47 48 * recursion is used starting from a supposed value
48 49 * for j(n,x). The resulting value of j(0,x) is
49 50 * compared with the actual value to correct the
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
50 51 * supposed value of j(n,x).
51 52 *
52 53 * yn(n,x) is similar in all respects, except
53 54 * that forward recursion is used for all
54 55 * values of n>1.
55 56 *
56 57 */
57 58
58 59 #include "libm.h"
59 60 #include "longdouble.h"
60 -#include <float.h> /* LDBL_MAX */
61 +#include <float.h> /* LDBL_MAX */
61 62
62 -#define GENERIC long double
63 +#define GENERIC long double
63 64
64 65 static const GENERIC
65 66 invsqrtpi = 5.641895835477562869480794515607725858441e-0001L,
66 -two = 2.0L,
67 -zero = 0.0L,
68 -one = 1.0L;
67 + two = 2.0L,
68 + zero = 0.0L,
69 + one = 1.0L;
69 70
70 71 GENERIC
71 72 jnl(int n, GENERIC x)
72 73 {
73 74 int i, sgn;
74 75 GENERIC a, b, temp = 0, z, w;
75 76
76 77 /*
77 78 * J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
78 79 * Thus, J(-n,x) = J(n,-x)
79 80 */
80 81 if (n < 0) {
81 82 n = -n;
82 83 x = -x;
83 84 }
85 +
84 86 if (n == 0)
85 87 return (j0l(x));
88 +
86 89 if (n == 1)
87 90 return (j1l(x));
91 +
88 92 if (x != x)
89 - return (x+x);
90 - if ((n&1) == 0)
91 - sgn = 0; /* even n */
93 + return (x + x);
94 +
95 + if ((n & 1) == 0)
96 + sgn = 0; /* even n */
92 97 else
93 98 sgn = signbitl(x); /* old n */
99 +
94 100 x = fabsl(x);
95 - if (x == zero || !finitel(x)) b = zero;
96 - else if ((GENERIC)n <= x) {
101 +
102 + if (x == zero || !finitel(x)) {
103 + b = zero;
104 + } else if ((GENERIC)n <= x) {
105 + /*
106 + * Safe to use
107 + * J(n+1,x)=2n/x *J(n,x)-J(n-1,x)
108 + */
109 + if (x > 1.0e91L) {
97 110 /*
98 - * Safe to use
99 - * J(n+1,x)=2n/x *J(n,x)-J(n-1,x)
111 + * x >> n**2
112 + * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
113 + * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
114 + * Let s=sin(x), c=cos(x),
115 + * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
116 + *
117 + * n sin(xn)*sqt2 cos(xn)*sqt2
118 + * ----------------------------------
119 + * 0 s-c c+s
120 + * 1 -s-c -c+s
121 + * 2 -s+c -c-s
122 + * 3 s+c c-s
100 123 */
101 - if (x > 1.0e91L) {
102 - /*
103 - * x >> n**2
104 - * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
105 - * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
106 - * Let s=sin(x), c=cos(x),
107 - * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
108 - *
109 - * n sin(xn)*sqt2 cos(xn)*sqt2
110 - * ----------------------------------
111 - * 0 s-c c+s
112 - * 1 -s-c -c+s
113 - * 2 -s+c -c-s
114 - * 3 s+c c-s
115 - */
116 - switch (n&3) {
124 + switch (n & 3) {
117 125 case 0:
118 - temp = cosl(x)+sinl(x);
126 + temp = cosl(x) + sinl(x);
119 127 break;
120 128 case 1:
121 - temp = -cosl(x)+sinl(x);
129 + temp = -cosl(x) + sinl(x);
122 130 break;
123 131 case 2:
124 - temp = -cosl(x)-sinl(x);
132 + temp = -cosl(x) - sinl(x);
125 133 break;
126 134 case 3:
127 - temp = cosl(x)-sinl(x);
135 + temp = cosl(x) - sinl(x);
128 136 break;
129 137 }
130 - b = invsqrtpi*temp/sqrtl(x);
138 +
139 + b = invsqrtpi * temp / sqrtl(x);
131 140 } else {
132 141 a = j0l(x);
133 142 b = j1l(x);
143 +
134 144 for (i = 1; i < n; i++) {
135 145 temp = b;
136 146 /* avoid underflow */
137 - b = b*((GENERIC)(i+i)/x) - a;
147 + b = b * ((GENERIC)(i + i) / x) - a;
138 148 a = temp;
139 149 }
140 150 }
141 151 } else {
142 152 if (x < 1e-17L) { /* use J(n,x) = 1/n!*(x/2)^n */
143 - b = powl(0.5L*x, (GENERIC)n);
153 + b = powl(0.5L * x, (GENERIC)n);
154 +
144 155 if (b != zero) {
145 156 for (a = one, i = 1; i <= n; i++)
146 157 a *= (GENERIC)i;
147 - b = b/a;
158 +
159 + b = b / a;
148 160 }
149 161 } else {
150 162 /* BEGIN CSTYLED */
163 +
151 164 /*
152 165 * use backward recurrence
153 166 * x x^2 x^2
154 167 * J(n,x)/J(n-1,x) = ---- ------ ------ .....
155 168 * 2n - 2(n+1) - 2(n+2)
156 169 *
157 170 * 1 1 1
158 171 * (for large x) = ---- ------ ------ .....
159 172 * 2n 2(n+1) 2(n+2)
160 173 * -- - ------ - ------ -
161 174 * x x x
162 175 *
163 176 * Let w = 2n/x and h=2/x, then the above quotient
164 177 * is equal to the continued fraction:
165 178 * 1
166 179 * = -----------------------
167 180 * 1
168 181 * w - -----------------
169 182 * 1
↓ open down ↓ |
9 lines elided |
↑ open up ↑ |
170 183 * w+h - ---------
171 184 * w+2h - ...
172 185 *
173 186 * To determine how many terms needed, let
174 187 * Q(0) = w, Q(1) = w(w+h) - 1,
175 188 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
176 189 * When Q(k) > 1e4 good for single
177 190 * When Q(k) > 1e9 good for double
178 191 * When Q(k) > 1e17 good for quaduple
179 192 */
180 - /* END CSTYLED */
181 - /* determine k */
193 +
194 + /*
195 + * END CSTYLED
196 + * determine k
197 + */
182 198 GENERIC t, v;
183 199 double q0, q1, h, tmp;
184 200 int k, m;
185 - w = (n+n)/(double)x;
186 - h = 2.0/(double)x;
201 +
202 + w = (n + n) / (double)x;
203 + h = 2.0 / (double)x;
187 204 q0 = w;
188 - z = w+h;
189 - q1 = w*z - 1.0;
205 + z = w + h;
206 + q1 = w * z - 1.0;
190 207 k = 1;
208 +
191 209 while (q1 < 1.0e17) {
192 210 k += 1;
193 211 z += h;
194 - tmp = z*q1 - q0;
212 + tmp = z * q1 - q0;
195 213 q0 = q1;
196 214 q1 = tmp;
197 215 }
198 - m = n+n;
199 - for (t = zero, i = 2*(n+k); i >= m; i -= 2)
200 - t = one/(i/x-t);
216 +
217 + m = n + n;
218 +
219 + for (t = zero, i = 2 * (n + k); i >= m; i -= 2)
220 + t = one / (i / x - t);
221 +
201 222 a = t;
202 223 b = one;
224 +
203 225 /*
204 226 * Estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
205 227 * hence, if n*(log(2n/x)) > ...
206 228 * single:
207 229 * 8.8722839355e+01
208 230 * double:
209 231 * 7.09782712893383973096e+02
210 232 * long double:
211 233 * 1.1356523406294143949491931077970765006170e+04
212 234 * then recurrent value may overflow and the result is
213 235 * likely underflow to zero.
214 236 */
215 237 tmp = n;
216 - v = two/x;
217 - tmp = tmp*logl(fabsl(v*tmp));
238 + v = two / x;
239 + tmp = tmp * logl(fabsl(v * tmp));
240 +
218 241 if (tmp < 1.1356523406294143949491931077970765e+04L) {
219 - for (i = n-1; i > 0; i--) {
242 + for (i = n - 1; i > 0; i--) {
220 243 temp = b;
221 - b = ((i+i)/x)*b - a;
244 + b = ((i + i) / x) * b - a;
222 245 a = temp;
223 246 }
224 247 } else {
225 - for (i = n-1; i > 0; i--) {
248 + for (i = n - 1; i > 0; i--) {
226 249 temp = b;
227 - b = ((i+i)/x)*b - a;
250 + b = ((i + i) / x) * b - a;
228 251 a = temp;
252 +
229 253 if (b > 1e1000L) {
230 254 a /= b;
231 255 t /= b;
232 - b = 1.0;
256 + b = 1.0;
233 257 }
234 258 }
235 259 }
236 - b = (t*j0l(x)/b);
260 +
261 + b = (t * j0l(x) / b);
237 262 }
238 263 }
264 +
239 265 if (sgn != 0)
240 266 return (-b);
241 267 else
242 268 return (b);
243 269 }
244 270
245 271 GENERIC
246 272 ynl(int n, GENERIC x)
247 273 {
248 274 int i;
249 275 int sign;
250 276 GENERIC a, b, temp = 0;
251 277
252 278 if (x != x)
253 - return (x+x);
279 + return (x + x);
280 +
254 281 if (x <= zero) {
255 282 if (x == zero)
256 - return (-one/zero);
283 + return (-one / zero);
257 284 else
258 - return (zero/zero);
285 + return (zero / zero);
259 286 }
287 +
260 288 sign = 1;
289 +
261 290 if (n < 0) {
262 291 n = -n;
263 - if ((n&1) == 1)
292 +
293 + if ((n & 1) == 1)
264 294 sign = -1;
265 295 }
296 +
266 297 if (n == 0)
267 298 return (y0l(x));
299 +
268 300 if (n == 1)
269 - return (sign*y1l(x));
301 + return (sign * y1l(x));
302 +
270 303 if (!finitel(x))
271 304 return (zero);
272 305
273 306 if (x > 1.0e91L) {
274 - /*
275 - * x >> n**2
276 - * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
277 - * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
278 - * Let s=sin(x), c=cos(x),
279 - * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
280 - *
281 - * n sin(xn)*sqt2 cos(xn)*sqt2
282 - * ----------------------------------
283 - * 0 s-c c+s
284 - * 1 -s-c -c+s
285 - * 2 -s+c -c-s
286 - * 3 s+c c-s
287 - */
288 - switch (n&3) {
307 + /*
308 + * x >> n**2
309 + * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
310 + * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
311 + * Let s=sin(x), c=cos(x),
312 + * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
313 + *
314 + * n sin(xn)*sqt2 cos(xn)*sqt2
315 + * ----------------------------------
316 + * 0 s-c c+s
317 + * 1 -s-c -c+s
318 + * 2 -s+c -c-s
319 + * 3 s+c c-s
320 + */
321 + switch (n & 3) {
289 322 case 0:
290 - temp = sinl(x)-cosl(x);
323 + temp = sinl(x) - cosl(x);
291 324 break;
292 325 case 1:
293 - temp = -sinl(x)-cosl(x);
326 + temp = -sinl(x) - cosl(x);
294 327 break;
295 328 case 2:
296 - temp = -sinl(x)+cosl(x);
329 + temp = -sinl(x) + cosl(x);
297 330 break;
298 331 case 3:
299 - temp = sinl(x)+cosl(x);
332 + temp = sinl(x) + cosl(x);
300 333 break;
301 334 }
302 - b = invsqrtpi*temp/sqrtl(x);
335 +
336 + b = invsqrtpi * temp / sqrtl(x);
303 337 } else {
304 338 a = y0l(x);
305 339 b = y1l(x);
340 +
306 341 /*
307 342 * fix 1262058 and take care of non-default rounding
308 343 */
309 344 for (i = 1; i < n; i++) {
310 345 temp = b;
311 - b *= (GENERIC) (i + i) / x;
346 + b *= (GENERIC)(i + i) / x;
347 +
312 348 if (b <= -LDBL_MAX)
313 349 break;
350 +
314 351 b -= a;
315 352 a = temp;
316 353 }
317 354 }
355 +
318 356 if (sign > 0)
319 357 return (b);
320 358 else
321 359 return (-b);
322 360 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX