Print this page
11210 libm should be cstyle(1ONBLD) clean
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libm/common/C/jn.c
+++ new/usr/src/lib/libm/common/C/jn.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 __jn = jn
31 32 #pragma weak __yn = yn
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
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
49 50 * compared with the actual value to correct the
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 -#include <float.h> /* DBL_MIN */
60 -#include <values.h> /* X_TLOSS */
61 -#include "xpg6.h" /* __xpg6 */
60 +#include <float.h> /* DBL_MIN */
61 +#include <values.h> /* X_TLOSS */
62 +#include "xpg6.h" /* __xpg6 */
62 63
63 -#define GENERIC double
64 +#define GENERIC double
64 65
65 66 static const GENERIC
66 67 invsqrtpi = 5.641895835477562869480794515607725858441e-0001,
67 - two = 2.0,
68 - zero = 0.0,
69 - one = 1.0;
68 + two = 2.0,
69 + zero = 0.0,
70 + one = 1.0;
70 71
71 72 GENERIC
72 73 jn(int n, GENERIC x)
73 74 {
74 75 int i, sgn;
75 76 GENERIC a, b, temp = 0;
76 77 GENERIC z, w, ox, on;
77 78
78 79 /*
79 80 * J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
80 81 * Thus, J(-n,x) = J(n,-x)
81 82 */
82 83 ox = x;
83 84 on = (GENERIC)n;
84 85
85 86 if (n < 0) {
86 87 n = -n;
87 88 x = -x;
88 89 }
90 +
89 91 if (isnan(x))
90 - return (x*x); /* + -> * for Cheetah */
91 - if (!((int)_lib_version == libm_ieee ||
92 - (__xpg6 & _C99SUSv3_math_errexcept) != 0)) {
92 + return (x * x); /* + -> * for Cheetah */
93 +
94 + if (!((int)_lib_version == libm_ieee || (__xpg6 &
95 + _C99SUSv3_math_errexcept) != 0)) {
93 96 if (fabs(x) > X_TLOSS)
94 97 return (_SVID_libm_err(on, ox, 38));
95 98 }
99 +
96 100 if (n == 0)
97 101 return (j0(x));
102 +
98 103 if (n == 1)
99 104 return (j1(x));
100 - if ((n&1) == 0)
101 - sgn = 0; /* even n */
105 +
106 + if ((n & 1) == 0)
107 + sgn = 0; /* even n */
102 108 else
103 109 sgn = signbit(x); /* old n */
110 +
104 111 x = fabs(x);
105 - if (x == zero||!finite(x)) b = zero;
106 - else if ((GENERIC)n <= x) {
107 - /*
108 - * Safe to use
109 - * J(n+1,x)=2n/x *J(n,x)-J(n-1,x)
110 - */
112 +
113 + if (x == zero || !finite(x)) {
114 + b = zero;
115 + } else if ((GENERIC)n <= x) {
116 + /*
117 + * Safe to use
118 + * J(n+1,x)=2n/x *J(n,x)-J(n-1,x)
119 + */
111 120 if (x > 1.0e91) {
112 - /*
113 - * x >> n**2
114 - * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
115 - * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
116 - * Let s=sin(x), c=cos(x),
117 - * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
118 - *
119 - * n sin(xn)*sqt2 cos(xn)*sqt2
120 - * ----------------------------------
121 - * 0 s-c c+s
122 - * 1 -s-c -c+s
123 - * 2 -s+c -c-s
124 - * 3 s+c c-s
125 - */
126 - switch (n&3) {
121 + /*
122 + * x >> n**2
123 + * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
124 + * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
125 + * Let s=sin(x), c=cos(x),
126 + * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
127 + *
128 + * n sin(xn)*sqt2 cos(xn)*sqt2
129 + * ----------------------------------
130 + * 0 s-c c+s
131 + * 1 -s-c -c+s
132 + * 2 -s+c -c-s
133 + * 3 s+c c-s
134 + */
135 + switch (n & 3) {
127 136 case 0:
128 - temp = cos(x)+sin(x);
137 + temp = cos(x) + sin(x);
129 138 break;
130 139 case 1:
131 - temp = -cos(x)+sin(x);
140 + temp = -cos(x) + sin(x);
132 141 break;
133 142 case 2:
134 - temp = -cos(x)-sin(x);
143 + temp = -cos(x) - sin(x);
135 144 break;
136 145 case 3:
137 - temp = cos(x)-sin(x);
146 + temp = cos(x) - sin(x);
138 147 break;
139 148 }
140 - b = invsqrtpi*temp/sqrt(x);
149 +
150 + b = invsqrtpi * temp / sqrt(x);
141 151 } else {
142 152 a = j0(x);
143 153 b = j1(x);
154 +
144 155 for (i = 1; i < n; i++) {
145 156 temp = b;
146 157 /* avoid underflow */
147 - b = b*((GENERIC)(i+i)/x) - a;
158 + b = b * ((GENERIC)(i + i) / x) - a;
148 159 a = temp;
149 160 }
150 161 }
151 162 } else {
152 - if (x < 1e-9) { /* use J(n,x) = 1/n!*(x/2)^n */
153 - b = pow(0.5*x, (GENERIC) n);
163 + if (x < 1e-9) { /* use J(n,x) = 1/n!*(x/2)^n */
164 + b = pow(0.5 * x, (GENERIC)n);
165 +
154 166 if (b != zero) {
155 167 for (a = one, i = 1; i <= n; i++)
156 168 a *= (GENERIC)i;
157 - b = b/a;
169 +
170 + b = b / a;
158 171 }
159 172 } else {
160 173 /*
161 174 * use backward recurrence
162 175 * x x^2 x^2
163 176 * J(n,x)/J(n-1,x) = ---- ------ ------ .....
164 177 * 2n - 2(n+1) - 2(n+2)
165 178 *
166 179 * 1 1 1
167 180 * (for large x) = ---- ------ ------ .....
168 181 * 2n 2(n+1) 2(n+2)
169 182 * -- - ------ - ------ -
170 183 * x x x
171 184 *
172 185 * Let w = 2n/x and h = 2/x, then the above quotient
173 186 * is equal to the continued fraction:
174 187 * 1
175 188 * = -----------------------
176 189 * 1
177 190 * w - -----------------
178 191 * 1
179 192 * w+h - ---------
180 193 * w+2h - ...
181 194 *
182 195 * To determine how many terms needed, let
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
183 196 * Q(0) = w, Q(1) = w(w+h) - 1,
184 197 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
185 198 * When Q(k) > 1e4 good for single
186 199 * When Q(k) > 1e9 good for double
187 200 * When Q(k) > 1e17 good for quaduple
188 201 */
189 202 /* determine k */
190 203 GENERIC t, v;
191 204 double q0, q1, h, tmp;
192 205 int k, m;
193 - w = (n+n)/(double)x;
194 - h = 2.0/(double)x;
206 +
207 + w = (n + n) / (double)x;
208 + h = 2.0 / (double)x;
195 209 q0 = w;
196 210 z = w + h;
197 - q1 = w*z - 1.0;
211 + q1 = w * z - 1.0;
198 212 k = 1;
199 213
200 214 while (q1 < 1.0e9) {
201 215 k += 1;
202 216 z += h;
203 - tmp = z*q1 - q0;
217 + tmp = z * q1 - q0;
204 218 q0 = q1;
205 219 q1 = tmp;
206 220 }
207 - m = n+n;
208 - for (t = zero, i = 2*(n+k); i >= m; i -= 2)
209 - t = one/(i/x-t);
221 +
222 + m = n + n;
223 +
224 + for (t = zero, i = 2 * (n + k); i >= m; i -= 2)
225 + t = one / (i / x - t);
226 +
210 227 a = t;
211 228 b = one;
229 +
230 + /* BEGIN CSTYLED */
212 231 /*
213 232 * estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
214 233 * hence, if n*(log(2n/x)) > ...
215 234 * single:
216 235 * 8.8722839355e+01
217 236 * double:
218 237 * 7.09782712893383973096e+02
219 238 * long double:
220 239 * 1.1356523406294143949491931077970765006170e+04
221 240 * then recurrent value may overflow and the result is
222 241 * likely underflow to zero
223 242 */
243 + /* END CSTYLED */
224 244 tmp = n;
225 - v = two/x;
226 - tmp = tmp*log(fabs(v*tmp));
245 + v = two / x;
246 + tmp = tmp * log(fabs(v * tmp));
247 +
227 248 if (tmp < 7.09782712893383973096e+02) {
228 - for (i = n-1; i > 0; i--) {
249 + for (i = n - 1; i > 0; i--) {
229 250 temp = b;
230 - b = ((i+i)/x)*b - a;
251 + b = ((i + i) / x) * b - a;
231 252 a = temp;
232 253 }
233 254 } else {
234 - for (i = n-1; i > 0; i--) {
255 + for (i = n - 1; i > 0; i--) {
235 256 temp = b;
236 - b = ((i+i)/x)*b - a;
257 + b = ((i + i) / x) * b - a;
237 258 a = temp;
259 +
238 260 if (b > 1e100) {
239 261 a /= b;
240 262 t /= b;
241 - b = 1.0;
263 + b = 1.0;
242 264 }
243 265 }
244 266 }
245 - b = (t*j0(x)/b);
267 +
268 + b = (t * j0(x) / b);
246 269 }
247 270 }
271 +
248 272 if (sgn != 0)
249 273 return (-b);
250 274 else
251 275 return (b);
252 276 }
253 277
254 278 GENERIC
255 279 yn(int n, GENERIC x)
256 280 {
257 281 int i;
258 282 int sign;
259 283 GENERIC a, b, temp = 0, ox, on;
260 284
261 285 ox = x;
262 286 on = (GENERIC)n;
287 +
263 288 if (isnan(x))
264 - return (x*x); /* + -> * for Cheetah */
289 + return (x * x); /* + -> * for Cheetah */
290 +
265 291 if (x <= zero) {
266 292 if (x == zero) {
267 293 /* return -one/zero; */
268 294 return (_SVID_libm_err((GENERIC)n, x, 12));
269 295 } else {
270 296 /* return zero/zero; */
271 297 return (_SVID_libm_err((GENERIC)n, x, 13));
272 298 }
273 299 }
274 - if (!((int)_lib_version == libm_ieee ||
275 - (__xpg6 & _C99SUSv3_math_errexcept) != 0)) {
300 +
301 + if (!((int)_lib_version == libm_ieee || (__xpg6 &
302 + _C99SUSv3_math_errexcept) != 0)) {
276 303 if (x > X_TLOSS)
277 304 return (_SVID_libm_err(on, ox, 39));
278 305 }
306 +
279 307 sign = 1;
308 +
280 309 if (n < 0) {
281 310 n = -n;
282 - if ((n&1) == 1) sign = -1;
311 +
312 + if ((n & 1) == 1)
313 + sign = -1;
283 314 }
315 +
284 316 if (n == 0)
285 317 return (y0(x));
318 +
286 319 if (n == 1)
287 - return (sign*y1(x));
320 + return (sign * y1(x));
321 +
288 322 if (!finite(x))
289 323 return (zero);
290 324
291 325 if (x > 1.0e91) {
292 - /*
293 - * x >> n**2
294 - * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
295 - * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
296 - * Let s = sin(x), c = cos(x),
297 - * xn = x-(2n+1)*pi/4, sqt2 = sqrt(2), then
298 - *
299 - * n sin(xn)*sqt2 cos(xn)*sqt2
300 - * ----------------------------------
301 - * 0 s-c c+s
302 - * 1 -s-c -c+s
303 - * 2 -s+c -c-s
304 - * 3 s+c c-s
305 - */
306 - switch (n&3) {
326 + /*
327 + * x >> n**2
328 + * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
329 + * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
330 + * Let s = sin(x), c = cos(x),
331 + * xn = x-(2n+1)*pi/4, sqt2 = sqrt(2), then
332 + *
333 + * n sin(xn)*sqt2 cos(xn)*sqt2
334 + * ----------------------------------
335 + * 0 s-c c+s
336 + * 1 -s-c -c+s
337 + * 2 -s+c -c-s
338 + * 3 s+c c-s
339 + */
340 + switch (n & 3) {
307 341 case 0:
308 - temp = sin(x)-cos(x);
342 + temp = sin(x) - cos(x);
309 343 break;
310 344 case 1:
311 - temp = -sin(x)-cos(x);
345 + temp = -sin(x) - cos(x);
312 346 break;
313 347 case 2:
314 - temp = -sin(x)+cos(x);
348 + temp = -sin(x) + cos(x);
315 349 break;
316 350 case 3:
317 - temp = sin(x)+cos(x);
351 + temp = sin(x) + cos(x);
318 352 break;
319 353 }
320 - b = invsqrtpi*temp/sqrt(x);
354 +
355 + b = invsqrtpi * temp / sqrt(x);
321 356 } else {
322 357 a = y0(x);
323 358 b = y1(x);
359 +
324 360 /*
325 361 * fix 1262058 and take care of non-default rounding
326 362 */
327 363 for (i = 1; i < n; i++) {
328 364 temp = b;
329 - b *= (GENERIC) (i + i) / x;
365 + b *= (GENERIC)(i + i) / x;
366 +
330 367 if (b <= -DBL_MAX)
331 368 break;
369 +
332 370 b -= a;
333 371 a = temp;
334 372 }
335 373 }
374 +
336 375 if (sign > 0)
337 376 return (b);
338 377 else
339 378 return (-b);
340 379 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX