math: use the rounding idiom consistently
[oweals/musl.git] / src / math / jn.c
1 /* origin: FreeBSD /usr/src/lib/msun/src/e_jn.c */
2 /*
3  * ====================================================
4  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5  *
6  * Developed at SunSoft, a Sun Microsystems, Inc. business.
7  * Permission to use, copy, modify, and distribute this
8  * software is freely granted, provided that this notice
9  * is preserved.
10  * ====================================================
11  */
12 /*
13  * jn(n, x), yn(n, x)
14  * floating point Bessel's function of the 1st and 2nd kind
15  * of order n
16  *
17  * Special cases:
18  *      y0(0)=y1(0)=yn(n,0) = -inf with division by zero signal;
19  *      y0(-ve)=y1(-ve)=yn(n,-ve) are NaN with invalid signal.
20  * Note 2. About jn(n,x), yn(n,x)
21  *      For n=0, j0(x) is called,
22  *      for n=1, j1(x) is called,
23  *      for n<=x, forward recursion is used starting
24  *      from values of j0(x) and j1(x).
25  *      for n>x, a continued fraction approximation to
26  *      j(n,x)/j(n-1,x) is evaluated and then backward
27  *      recursion is used starting from a supposed value
28  *      for j(n,x). The resulting value of j(0,x) is
29  *      compared with the actual value to correct the
30  *      supposed value of j(n,x).
31  *
32  *      yn(n,x) is similar in all respects, except
33  *      that forward recursion is used for all
34  *      values of n>1.
35  */
36
37 #include "libm.h"
38
39 static const double invsqrtpi = 5.64189583547756279280e-01; /* 0x3FE20DD7, 0x50429B6D */
40
41 double jn(int n, double x)
42 {
43         uint32_t ix, lx;
44         int nm1, i, sign;
45         double a, b, temp;
46
47         EXTRACT_WORDS(ix, lx, x);
48         sign = ix>>31;
49         ix &= 0x7fffffff;
50
51         if ((ix | (lx|-lx)>>31) > 0x7ff00000) /* nan */
52                 return x;
53
54         /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
55          * Thus, J(-n,x) = J(n,-x)
56          */
57         /* nm1 = |n|-1 is used instead of |n| to handle n==INT_MIN */
58         if (n == 0)
59                 return j0(x);
60         if (n < 0) {
61                 nm1 = -(n+1);
62                 x = -x;
63                 sign ^= 1;
64         } else
65                 nm1 = n-1;
66         if (nm1 == 0)
67                 return j1(x);
68
69         sign &= n;  /* even n: 0, odd n: signbit(x) */
70         x = fabs(x);
71         if ((ix|lx) == 0 || ix == 0x7ff00000)  /* if x is 0 or inf */
72                 b = 0.0;
73         else if (nm1 < x) {
74                 /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
75                 if (ix >= 0x52d00000) { /* x > 2**302 */
76                         /* (x >> n**2)
77                          *      Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
78                          *      Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
79                          *      Let s=sin(x), c=cos(x),
80                          *          xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
81                          *
82                          *             n    sin(xn)*sqt2    cos(xn)*sqt2
83                          *          ----------------------------------
84                          *             0     s-c             c+s
85                          *             1    -s-c            -c+s
86                          *             2    -s+c            -c-s
87                          *             3     s+c             c-s
88                          */
89                         switch(nm1&3) {
90                         case 0: temp = -cos(x)+sin(x); break;
91                         case 1: temp = -cos(x)-sin(x); break;
92                         case 2: temp =  cos(x)-sin(x); break;
93                         default:
94                         case 3: temp =  cos(x)+sin(x); break;
95                         }
96                         b = invsqrtpi*temp/sqrt(x);
97                 } else {
98                         a = j0(x);
99                         b = j1(x);
100                         for (i=0; i<nm1; ) {
101                                 i++;
102                                 temp = b;
103                                 b = b*(2.0*i/x) - a; /* avoid underflow */
104                                 a = temp;
105                         }
106                 }
107         } else {
108                 if (ix < 0x3e100000) { /* x < 2**-29 */
109                         /* x is tiny, return the first Taylor expansion of J(n,x)
110                          * J(n,x) = 1/n!*(x/2)^n  - ...
111                          */
112                         if (nm1 > 32)  /* underflow */
113                                 b = 0.0;
114                         else {
115                                 temp = x*0.5;
116                                 b = temp;
117                                 a = 1.0;
118                                 for (i=2; i<=nm1+1; i++) {
119                                         a *= (double)i; /* a = n! */
120                                         b *= temp;      /* b = (x/2)^n */
121                                 }
122                                 b = b/a;
123                         }
124                 } else {
125                         /* use backward recurrence */
126                         /*                      x      x^2      x^2
127                          *  J(n,x)/J(n-1,x) =  ----   ------   ------   .....
128                          *                      2n  - 2(n+1) - 2(n+2)
129                          *
130                          *                      1      1        1
131                          *  (for large x)   =  ----  ------   ------   .....
132                          *                      2n   2(n+1)   2(n+2)
133                          *                      -- - ------ - ------ -
134                          *                       x     x         x
135                          *
136                          * Let w = 2n/x and h=2/x, then the above quotient
137                          * is equal to the continued fraction:
138                          *                  1
139                          *      = -----------------------
140                          *                     1
141                          *         w - -----------------
142                          *                        1
143                          *              w+h - ---------
144                          *                     w+2h - ...
145                          *
146                          * To determine how many terms needed, let
147                          * Q(0) = w, Q(1) = w(w+h) - 1,
148                          * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
149                          * When Q(k) > 1e4      good for single
150                          * When Q(k) > 1e9      good for double
151                          * When Q(k) > 1e17     good for quadruple
152                          */
153                         /* determine k */
154                         double t,q0,q1,w,h,z,tmp,nf;
155                         int k;
156
157                         nf = nm1 + 1.0;
158                         w = 2*nf/x;
159                         h = 2/x;
160                         z = w+h;
161                         q0 = w;
162                         q1 = w*z - 1.0;
163                         k = 1;
164                         while (q1 < 1.0e9) {
165                                 k += 1;
166                                 z += h;
167                                 tmp = z*q1 - q0;
168                                 q0 = q1;
169                                 q1 = tmp;
170                         }
171                         for (t=0.0, i=k; i>=0; i--)
172                                 t = 1/(2*(i+nf)/x - t);
173                         a = t;
174                         b = 1.0;
175                         /*  estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
176                          *  Hence, if n*(log(2n/x)) > ...
177                          *  single 8.8722839355e+01
178                          *  double 7.09782712893383973096e+02
179                          *  long double 1.1356523406294143949491931077970765006170e+04
180                          *  then recurrent value may overflow and the result is
181                          *  likely underflow to zero
182                          */
183                         tmp = nf*log(fabs(w));
184                         if (tmp < 7.09782712893383973096e+02) {
185                                 for (i=nm1; i>0; i--) {
186                                         temp = b;
187                                         b = b*(2.0*i)/x - a;
188                                         a = temp;
189                                 }
190                         } else {
191                                 for (i=nm1; i>0; i--) {
192                                         temp = b;
193                                         b = b*(2.0*i)/x - a;
194                                         a = temp;
195                                         /* scale b to avoid spurious overflow */
196                                         if (b > 0x1p500) {
197                                                 a /= b;
198                                                 t /= b;
199                                                 b  = 1.0;
200                                         }
201                                 }
202                         }
203                         z = j0(x);
204                         w = j1(x);
205                         if (fabs(z) >= fabs(w))
206                                 b = t*z/b;
207                         else
208                                 b = t*w/a;
209                 }
210         }
211         return sign ? -b : b;
212 }
213
214
215 double yn(int n, double x)
216 {
217         uint32_t ix, lx, ib;
218         int nm1, sign, i;
219         double a, b, temp;
220
221         EXTRACT_WORDS(ix, lx, x);
222         sign = ix>>31;
223         ix &= 0x7fffffff;
224
225         if ((ix | (lx|-lx)>>31) > 0x7ff00000) /* nan */
226                 return x;
227         if (sign && (ix|lx)!=0) /* x < 0 */
228                 return 0/0.0;
229         if (ix == 0x7ff00000)
230                 return 0.0;
231
232         if (n == 0)
233                 return y0(x);
234         if (n < 0) {
235                 nm1 = -(n+1);
236                 sign = n&1;
237         } else {
238                 nm1 = n-1;
239                 sign = 0;
240         }
241         if (nm1 == 0)
242                 return sign ? -y1(x) : y1(x);
243
244         if (ix >= 0x52d00000) { /* x > 2**302 */
245                 /* (x >> n**2)
246                  *      Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
247                  *      Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
248                  *      Let s=sin(x), c=cos(x),
249                  *          xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
250                  *
251                  *             n    sin(xn)*sqt2    cos(xn)*sqt2
252                  *          ----------------------------------
253                  *             0     s-c             c+s
254                  *             1    -s-c            -c+s
255                  *             2    -s+c            -c-s
256                  *             3     s+c             c-s
257                  */
258                 switch(nm1&3) {
259                 case 0: temp = -sin(x)-cos(x); break;
260                 case 1: temp = -sin(x)+cos(x); break;
261                 case 2: temp =  sin(x)+cos(x); break;
262                 default:
263                 case 3: temp =  sin(x)-cos(x); break;
264                 }
265                 b = invsqrtpi*temp/sqrt(x);
266         } else {
267                 a = y0(x);
268                 b = y1(x);
269                 /* quit if b is -inf */
270                 GET_HIGH_WORD(ib, b);
271                 for (i=0; i<nm1 && ib!=0xfff00000; ){
272                         i++;
273                         temp = b;
274                         b = (2.0*i/x)*b - a;
275                         GET_HIGH_WORD(ib, b);
276                         a = temp;
277                 }
278         }
279         return sign ? -b : b;
280 }