Print this page
5261 libm should stop using synonyms.h
5298 fabs is 0-sized, confuses dis(1) and others
Reviewed by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
Approved by: Gordon Ross <gwr@nexenta.com>
5262 libm needs to be carefully unifdef'd
5268 libm doesn't need to hide symbols which are already local
Reviewed by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
Reviewed by: Igor Kozhukhov <ikozhukhov@gmail.com>
Reviewed by: Gordon Ross <gwr@nexenta.com>
Approved by: Gordon Ross <gwr@nexenta.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libm/common/Q/jnl.c
+++ new/usr/src/lib/libm/common/Q/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 -#pragma weak jnl = __jnl
32 -#pragma weak ynl = __ynl
33 -#endif
30 +#pragma weak __jnl = jnl
31 +#pragma weak __ynl = ynl
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, 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)
86 84 return (j0l(x));
87 85 if (n == 1)
88 86 return (j1l(x));
89 87 if (x != x)
90 88 return (x+x);
91 89 if ((n&1) == 0)
92 90 sgn = 0; /* even n */
93 91 else
94 92 sgn = signbitl(x); /* old n */
95 93 x = fabsl(x);
96 94 if (x == zero || !finitel(x)) b = zero;
97 95 else if ((GENERIC)n <= x) {
98 96 /*
99 97 * Safe to use
100 98 * J(n+1,x)=2n/x *J(n,x)-J(n-1,x)
101 99 */
102 100 if (x > 1.0e91L) {
103 101 /*
104 102 * x >> n**2
105 103 * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
106 104 * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
107 105 * Let s=sin(x), c=cos(x),
108 106 * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
109 107 *
110 108 * n sin(xn)*sqt2 cos(xn)*sqt2
111 109 * ----------------------------------
112 110 * 0 s-c c+s
113 111 * 1 -s-c -c+s
114 112 * 2 -s+c -c-s
115 113 * 3 s+c c-s
116 114 */
117 115 switch (n&3) {
118 116 case 0: temp = cosl(x)+sinl(x); break;
119 117 case 1: temp = -cosl(x)+sinl(x); break;
120 118 case 2: temp = -cosl(x)-sinl(x); break;
121 119 case 3: temp = cosl(x)-sinl(x); break;
122 120 }
123 121 b = invsqrtpi*temp/sqrtl(x);
124 122 } else {
125 123 a = j0l(x);
126 124 b = j1l(x);
127 125 for (i = 1; i < n; i++) {
128 126 temp = b;
129 127 b = b*((GENERIC)(i+i)/x) - a; /* avoid underflow */
130 128 a = temp;
131 129 }
132 130 }
133 131 } else {
134 132 if (x < 1e-17L) { /* use J(n,x) = 1/n!*(x/2)^n */
135 133 b = powl(0.5L*x, (GENERIC)n);
136 134 if (b != zero) {
137 135 for (a = one, i = 1; i <= n; i++) a *= (GENERIC)i;
138 136 b = b/a;
139 137 }
140 138 } else {
141 139 /* use backward recurrence */
142 140 /*
143 141 * x x^2 x^2
144 142 * J(n,x)/J(n-1,x) = ---- ------ ------ .....
145 143 * 2n - 2(n+1) - 2(n+2)
146 144 *
147 145 * 1 1 1
148 146 * (for large x) = ---- ------ ------ .....
149 147 * 2n 2(n+1) 2(n+2)
150 148 * -- - ------ - ------ -
151 149 * x x x
152 150 *
153 151 * Let w = 2n/x and h=2/x, then the above quotient
154 152 * is equal to the continued fraction:
155 153 * 1
156 154 * = -----------------------
157 155 * 1
158 156 * w - -----------------
159 157 * 1
160 158 * w+h - ---------
161 159 * w+2h - ...
162 160 *
163 161 * To determine how many terms needed, let
164 162 * Q(0) = w, Q(1) = w(w+h) - 1,
165 163 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
166 164 * When Q(k) > 1e4 good for single
167 165 * When Q(k) > 1e9 good for double
168 166 * When Q(k) > 1e17 good for quaduple
169 167 */
170 168 /* determin k */
171 169 GENERIC t, v;
172 170 double q0, q1, h, tmp; int k, m;
173 171 w = (n+n)/(double)x; h = 2.0/(double)x;
174 172 q0 = w; z = w+h; q1 = w*z - 1.0; k = 1;
175 173 while (q1 < 1.0e17) {
176 174 k += 1; z += h;
177 175 tmp = z*q1 - q0;
178 176 q0 = q1;
179 177 q1 = tmp;
180 178 }
181 179 m = n+n;
182 180 for (t = zero, i = 2*(n+k); i >= m; i -= 2) t = one/(i/x-t);
183 181 a = t;
184 182 b = one;
185 183 /*
186 184 * estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
187 185 * hence, if n*(log(2n/x)) > ...
188 186 * single 8.8722839355e+01
189 187 * double 7.09782712893383973096e+02
190 188 * long double 1.1356523406294143949491931077970765006170e+04
191 189 * then recurrent value may overflow and the result is
192 190 * likely underflow to zero
193 191 */
194 192 tmp = n;
195 193 v = two/x;
196 194 tmp = tmp*logl(fabsl(v*tmp));
197 195 if (tmp < 1.1356523406294143949491931077970765e+04L) {
198 196 for (i = n-1; i > 0; i--) {
199 197 temp = b;
200 198 b = ((i+i)/x)*b - a;
201 199 a = temp;
202 200 }
203 201 } else {
204 202 for (i = n-1; i > 0; i--) {
205 203 temp = b;
206 204 b = ((i+i)/x)*b - a;
207 205 a = temp;
208 206 if (b > 1e1000L) {
209 207 a /= b;
210 208 t /= b;
211 209 b = 1.0;
212 210 }
213 211 }
214 212 }
215 213 b = (t*j0l(x)/b);
216 214 }
217 215 }
218 216 if (sgn == 1)
219 217 return (-b);
220 218 else
221 219 return (b);
222 220 }
223 221
224 222 GENERIC ynl(n, x)
225 223 int n; GENERIC x; {
226 224 int i;
227 225 int sign;
228 226 GENERIC a, b, temp;
229 227
230 228 if (x != x)
231 229 return (x+x);
232 230 if (x <= zero) {
233 231 if (x == zero)
234 232 return (-one/zero);
235 233 else
236 234 return (zero/zero);
237 235 }
238 236 sign = 1;
239 237 if (n < 0) {
240 238 n = -n;
241 239 if ((n&1) == 1) sign = -1;
242 240 }
243 241 if (n == 0)
244 242 return (y0l(x));
245 243 if (n == 1)
246 244 return (sign*y1l(x));
247 245 if (!finitel(x))
248 246 return (zero);
249 247
250 248 if (x > 1.0e91L) { /* x >> n**2
251 249 Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
252 250 Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
253 251 Let s = sin(x), c = cos(x),
254 252 xn = x-(2n+1)*pi/4, sqt2 = sqrt(2), then
255 253
256 254 n sin(xn)*sqt2 cos(xn)*sqt2
257 255 ----------------------------------
258 256 0 s-c c+s
259 257 1 -s-c -c+s
260 258 2 -s+c -c-s
261 259 3 s+c c-s
262 260 */
263 261 switch (n&3) {
264 262 case 0: temp = sinl(x)-cosl(x); break;
265 263 case 1: temp = -sinl(x)-cosl(x); break;
266 264 case 2: temp = -sinl(x)+cosl(x); break;
267 265 case 3: temp = sinl(x)+cosl(x); break;
268 266 }
269 267 b = invsqrtpi*temp/sqrtl(x);
270 268 } else {
271 269 a = y0l(x);
272 270 b = y1l(x);
273 271 /*
274 272 * fix 1262058 and take care of non-default rounding
275 273 */
276 274 for (i = 1; i < n; i++) {
277 275 temp = b;
278 276 b *= (GENERIC) (i + i) / x;
279 277 if (b <= -LDBL_MAX)
280 278 break;
281 279 b -= a;
282 280 a = temp;
283 281 }
284 282 }
285 283 if (sign > 0)
286 284 return (b);
287 285 else
288 286 return (-b);
289 287 }
↓ open down ↓ |
246 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX