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