1 | /* |
2 | * ==================================================== |
3 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
4 | * |
5 | * Developed at SunPro, a Sun Microsystems, Inc. business. |
6 | * Permission to use, copy, modify, and distribute this |
7 | * software is freely granted, provided that this notice |
8 | * is preserved. |
9 | * ==================================================== |
10 | */ |
11 | |
12 | /* Modifications for 128-bit long double are |
13 | Copyright (C) 2001 Stephen L. Moshier <moshier@na-net.ornl.gov> |
14 | and are incorporated herein by permission of the author. The author |
15 | reserves the right to distribute this material elsewhere under different |
16 | copying permissions. These modifications are distributed here under |
17 | the following terms: |
18 | |
19 | This library is free software; you can redistribute it and/or |
20 | modify it under the terms of the GNU Lesser General Public |
21 | License as published by the Free Software Foundation; either |
22 | version 2.1 of the License, or (at your option) any later version. |
23 | |
24 | This library is distributed in the hope that it will be useful, |
25 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
26 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
27 | Lesser General Public License for more details. |
28 | |
29 | You should have received a copy of the GNU Lesser General Public |
30 | License along with this library; if not, see |
31 | <http://www.gnu.org/licenses/>. */ |
32 | |
33 | /* |
34 | * __ieee754_jn(n, x), __ieee754_yn(n, x) |
35 | * floating point Bessel's function of the 1st and 2nd kind |
36 | * of order n |
37 | * |
38 | * Special cases: |
39 | * y0(0)=y1(0)=yn(n,0) = -inf with division by zero signal; |
40 | * y0(-ve)=y1(-ve)=yn(n,-ve) are NaN with invalid signal. |
41 | * Note 2. About jn(n,x), yn(n,x) |
42 | * For n=0, j0(x) is called, |
43 | * for n=1, j1(x) is called, |
44 | * for n<x, forward recursion us used starting |
45 | * from values of j0(x) and j1(x). |
46 | * for n>x, a continued fraction approximation to |
47 | * j(n,x)/j(n-1,x) is evaluated and then backward |
48 | * recursion is used starting from a supposed value |
49 | * for j(n,x). The resulting value of j(0,x) is |
50 | * compared with the actual value to correct the |
51 | * supposed value of j(n,x). |
52 | * |
53 | * yn(n,x) is similar in all respects, except |
54 | * that forward recursion is used for all |
55 | * values of n>1. |
56 | * |
57 | */ |
58 | |
59 | #include <errno.h> |
60 | #include <float.h> |
61 | #include <math.h> |
62 | #include <math_private.h> |
63 | #include <math-underflow.h> |
64 | |
65 | static const _Float128 |
66 | invsqrtpi = L(5.6418958354775628694807945156077258584405E-1), |
67 | two = 2, |
68 | one = 1, |
69 | zero = 0; |
70 | |
71 | |
72 | _Float128 |
73 | __ieee754_jnl (int n, _Float128 x) |
74 | { |
75 | uint32_t se; |
76 | int32_t i, ix, sgn; |
77 | _Float128 a, b, temp, di, ret; |
78 | _Float128 z, w; |
79 | ieee854_long_double_shape_type u; |
80 | |
81 | |
82 | /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x) |
83 | * Thus, J(-n,x) = J(n,-x) |
84 | */ |
85 | |
86 | u.value = x; |
87 | se = u.parts32.w0; |
88 | ix = se & 0x7fffffff; |
89 | |
90 | /* if J(n,NaN) is NaN */ |
91 | if (ix >= 0x7fff0000) |
92 | { |
93 | if ((u.parts32.w0 & 0xffff) | u.parts32.w1 | u.parts32.w2 | u.parts32.w3) |
94 | return x + x; |
95 | } |
96 | |
97 | if (n < 0) |
98 | { |
99 | n = -n; |
100 | x = -x; |
101 | se ^= 0x80000000; |
102 | } |
103 | if (n == 0) |
104 | return (__ieee754_j0l (x)); |
105 | if (n == 1) |
106 | return (__ieee754_j1l (x)); |
107 | sgn = (n & 1) & (se >> 31); /* even n -- 0, odd n -- sign(x) */ |
108 | x = fabsl (x); |
109 | |
110 | { |
111 | SET_RESTORE_ROUNDL (FE_TONEAREST); |
112 | if (x == 0 || ix >= 0x7fff0000) /* if x is 0 or inf */ |
113 | return sgn == 1 ? -zero : zero; |
114 | else if ((_Float128) n <= x) |
115 | { |
116 | /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */ |
117 | if (ix >= 0x412D0000) |
118 | { /* x > 2**302 */ |
119 | |
120 | /* ??? Could use an expansion for large x here. */ |
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 | _Float128 s; |
136 | _Float128 c; |
137 | __sincosl (x, &s, &c); |
138 | switch (n & 3) |
139 | { |
140 | case 0: |
141 | temp = c + s; |
142 | break; |
143 | case 1: |
144 | temp = -c + s; |
145 | break; |
146 | case 2: |
147 | temp = -c - s; |
148 | break; |
149 | case 3: |
150 | temp = c - s; |
151 | break; |
152 | } |
153 | b = invsqrtpi * temp / sqrtl (x); |
154 | } |
155 | else |
156 | { |
157 | a = __ieee754_j0l (x); |
158 | b = __ieee754_j1l (x); |
159 | for (i = 1; i < n; i++) |
160 | { |
161 | temp = b; |
162 | b = b * ((_Float128) (i + i) / x) - a; /* avoid underflow */ |
163 | a = temp; |
164 | } |
165 | } |
166 | } |
167 | else |
168 | { |
169 | if (ix < 0x3fc60000) |
170 | { /* x < 2**-57 */ |
171 | /* x is tiny, return the first Taylor expansion of J(n,x) |
172 | * J(n,x) = 1/n!*(x/2)^n - ... |
173 | */ |
174 | if (n >= 400) /* underflow, result < 10^-4952 */ |
175 | b = zero; |
176 | else |
177 | { |
178 | temp = x * 0.5; |
179 | b = temp; |
180 | for (a = one, i = 2; i <= n; i++) |
181 | { |
182 | a *= (_Float128) i; /* a = n! */ |
183 | b *= temp; /* b = (x/2)^n */ |
184 | } |
185 | b = b / a; |
186 | } |
187 | } |
188 | else |
189 | { |
190 | /* use backward recurrence */ |
191 | /* x x^2 x^2 |
192 | * J(n,x)/J(n-1,x) = ---- ------ ------ ..... |
193 | * 2n - 2(n+1) - 2(n+2) |
194 | * |
195 | * 1 1 1 |
196 | * (for large x) = ---- ------ ------ ..... |
197 | * 2n 2(n+1) 2(n+2) |
198 | * -- - ------ - ------ - |
199 | * x x x |
200 | * |
201 | * Let w = 2n/x and h=2/x, then the above quotient |
202 | * is equal to the continued fraction: |
203 | * 1 |
204 | * = ----------------------- |
205 | * 1 |
206 | * w - ----------------- |
207 | * 1 |
208 | * w+h - --------- |
209 | * w+2h - ... |
210 | * |
211 | * To determine how many terms needed, let |
212 | * Q(0) = w, Q(1) = w(w+h) - 1, |
213 | * Q(k) = (w+k*h)*Q(k-1) - Q(k-2), |
214 | * When Q(k) > 1e4 good for single |
215 | * When Q(k) > 1e9 good for double |
216 | * When Q(k) > 1e17 good for quadruple |
217 | */ |
218 | /* determine k */ |
219 | _Float128 t, v; |
220 | _Float128 q0, q1, h, tmp; |
221 | int32_t k, m; |
222 | w = (n + n) / (_Float128) x; |
223 | h = 2 / (_Float128) x; |
224 | q0 = w; |
225 | z = w + h; |
226 | q1 = w * z - 1; |
227 | k = 1; |
228 | while (q1 < L(1.0e17)) |
229 | { |
230 | k += 1; |
231 | z += h; |
232 | tmp = z * q1 - q0; |
233 | q0 = q1; |
234 | q1 = tmp; |
235 | } |
236 | m = n + n; |
237 | for (t = zero, i = 2 * (n + k); i >= m; i -= 2) |
238 | t = one / (i / x - t); |
239 | a = t; |
240 | b = one; |
241 | /* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n) |
242 | * Hence, if n*(log(2n/x)) > ... |
243 | * single 8.8722839355e+01 |
244 | * double 7.09782712893383973096e+02 |
245 | * long double 1.1356523406294143949491931077970765006170e+04 |
246 | * then recurrent value may overflow and the result is |
247 | * likely underflow to zero |
248 | */ |
249 | tmp = n; |
250 | v = two / x; |
251 | tmp = tmp * __ieee754_logl (fabsl (v * tmp)); |
252 | |
253 | if (tmp < L(1.1356523406294143949491931077970765006170e+04)) |
254 | { |
255 | for (i = n - 1, di = (_Float128) (i + i); i > 0; i--) |
256 | { |
257 | temp = b; |
258 | b *= di; |
259 | b = b / x - a; |
260 | a = temp; |
261 | di -= two; |
262 | } |
263 | } |
264 | else |
265 | { |
266 | for (i = n - 1, di = (_Float128) (i + i); i > 0; i--) |
267 | { |
268 | temp = b; |
269 | b *= di; |
270 | b = b / x - a; |
271 | a = temp; |
272 | di -= two; |
273 | /* scale b to avoid spurious overflow */ |
274 | if (b > L(1e100)) |
275 | { |
276 | a /= b; |
277 | t /= b; |
278 | b = one; |
279 | } |
280 | } |
281 | } |
282 | /* j0() and j1() suffer enormous loss of precision at and |
283 | * near zero; however, we know that their zero points never |
284 | * coincide, so just choose the one further away from zero. |
285 | */ |
286 | z = __ieee754_j0l (x); |
287 | w = __ieee754_j1l (x); |
288 | if (fabsl (z) >= fabsl (w)) |
289 | b = (t * z / b); |
290 | else |
291 | b = (t * w / a); |
292 | } |
293 | } |
294 | if (sgn == 1) |
295 | ret = -b; |
296 | else |
297 | ret = b; |
298 | } |
299 | if (ret == 0) |
300 | { |
301 | ret = __copysignl (LDBL_MIN, ret) * LDBL_MIN; |
302 | __set_errno (ERANGE); |
303 | } |
304 | else |
305 | math_check_force_underflow (ret); |
306 | return ret; |
307 | } |
308 | strong_alias (__ieee754_jnl, __jnl_finite) |
309 | |
310 | _Float128 |
311 | __ieee754_ynl (int n, _Float128 x) |
312 | { |
313 | uint32_t se; |
314 | int32_t i, ix; |
315 | int32_t sign; |
316 | _Float128 a, b, temp, ret; |
317 | ieee854_long_double_shape_type u; |
318 | |
319 | u.value = x; |
320 | se = u.parts32.w0; |
321 | ix = se & 0x7fffffff; |
322 | |
323 | /* if Y(n,NaN) is NaN */ |
324 | if (ix >= 0x7fff0000) |
325 | { |
326 | if ((u.parts32.w0 & 0xffff) | u.parts32.w1 | u.parts32.w2 | u.parts32.w3) |
327 | return x + x; |
328 | } |
329 | if (x <= 0) |
330 | { |
331 | if (x == 0) |
332 | return ((n < 0 && (n & 1) != 0) ? 1 : -1) / L(0.0); |
333 | if (se & 0x80000000) |
334 | return zero / (zero * x); |
335 | } |
336 | sign = 1; |
337 | if (n < 0) |
338 | { |
339 | n = -n; |
340 | sign = 1 - ((n & 1) << 1); |
341 | } |
342 | if (n == 0) |
343 | return (__ieee754_y0l (x)); |
344 | { |
345 | SET_RESTORE_ROUNDL (FE_TONEAREST); |
346 | if (n == 1) |
347 | { |
348 | ret = sign * __ieee754_y1l (x); |
349 | goto out; |
350 | } |
351 | if (ix >= 0x7fff0000) |
352 | return zero; |
353 | if (ix >= 0x412D0000) |
354 | { /* x > 2**302 */ |
355 | |
356 | /* ??? See comment above on the possible futility of this. */ |
357 | |
358 | /* (x >> n**2) |
359 | * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi) |
360 | * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi) |
361 | * Let s=sin(x), c=cos(x), |
362 | * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then |
363 | * |
364 | * n sin(xn)*sqt2 cos(xn)*sqt2 |
365 | * ---------------------------------- |
366 | * 0 s-c c+s |
367 | * 1 -s-c -c+s |
368 | * 2 -s+c -c-s |
369 | * 3 s+c c-s |
370 | */ |
371 | _Float128 s; |
372 | _Float128 c; |
373 | __sincosl (x, &s, &c); |
374 | switch (n & 3) |
375 | { |
376 | case 0: |
377 | temp = s - c; |
378 | break; |
379 | case 1: |
380 | temp = -s - c; |
381 | break; |
382 | case 2: |
383 | temp = -s + c; |
384 | break; |
385 | case 3: |
386 | temp = s + c; |
387 | break; |
388 | } |
389 | b = invsqrtpi * temp / sqrtl (x); |
390 | } |
391 | else |
392 | { |
393 | a = __ieee754_y0l (x); |
394 | b = __ieee754_y1l (x); |
395 | /* quit if b is -inf */ |
396 | u.value = b; |
397 | se = u.parts32.w0 & 0xffff0000; |
398 | for (i = 1; i < n && se != 0xffff0000; i++) |
399 | { |
400 | temp = b; |
401 | b = ((_Float128) (i + i) / x) * b - a; |
402 | u.value = b; |
403 | se = u.parts32.w0 & 0xffff0000; |
404 | a = temp; |
405 | } |
406 | } |
407 | /* If B is +-Inf, set up errno accordingly. */ |
408 | if (! isfinite (b)) |
409 | __set_errno (ERANGE); |
410 | if (sign > 0) |
411 | ret = b; |
412 | else |
413 | ret = -b; |
414 | } |
415 | out: |
416 | if (isinf (ret)) |
417 | ret = __copysignl (LDBL_MAX, ret) * LDBL_MAX; |
418 | return ret; |
419 | } |
420 | strong_alias (__ieee754_ynl, __ynl_finite) |
421 | |