1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
  24  */
  25 
  26 /*
  27  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  28  * Use is subject to license terms.
  29  */
  30 
  31 #pragma weak __jn = jn
  32 #pragma weak __yn = yn
  33 
  34 /*
  35  * floating point Bessel's function of the 1st and 2nd kind
  36  * of order n: jn(n,x),yn(n,x);
  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 "libm.h"
  60 #include <float.h>                        /* DBL_MIN */
  61 #include <values.h>                       /* X_TLOSS */
  62 #include "xpg6.h"                       /* __xpg6 */
  63 
  64 #define GENERIC double
  65 
  66 static const GENERIC
  67         invsqrtpi = 5.641895835477562869480794515607725858441e-0001,
  68         two = 2.0,
  69         zero = 0.0,
  70         one = 1.0;
  71 
  72 GENERIC
  73 jn(int n, GENERIC x)
  74 {
  75         int i, sgn;
  76         GENERIC a, b, temp = 0;
  77         GENERIC z, w, ox, on;
  78 
  79         /*
  80          * J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
  81          * Thus, J(-n,x) = J(n,-x)
  82          */
  83         ox = x;
  84         on = (GENERIC)n;
  85 
  86         if (n < 0) {
  87                 n = -n;
  88                 x = -x;
  89         }
  90 
  91         if (isnan(x))
  92                 return (x * x);         /* + -> * for Cheetah */
  93 
  94         if (!((int)_lib_version == libm_ieee || (__xpg6 &
  95             _C99SUSv3_math_errexcept) != 0)) {
  96                 if (fabs(x) > X_TLOSS)
  97                         return (_SVID_libm_err(on, ox, 38));
  98         }
  99 
 100         if (n == 0)
 101                 return (j0(x));
 102 
 103         if (n == 1)
 104                 return (j1(x));
 105 
 106         if ((n & 1) == 0)
 107                 sgn = 0;                /* even n */
 108         else
 109                 sgn = signbit(x);       /* old n  */
 110 
 111         x = fabs(x);
 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                  */
 120                 if (x > 1.0e91) {
 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) {
 136                         case 0:
 137                                 temp = cos(x) + sin(x);
 138                                 break;
 139                         case 1:
 140                                 temp = -cos(x) + sin(x);
 141                                 break;
 142                         case 2:
 143                                 temp = -cos(x) - sin(x);
 144                                 break;
 145                         case 3:
 146                                 temp = cos(x) - sin(x);
 147                                 break;
 148                         }
 149 
 150                         b = invsqrtpi * temp / sqrt(x);
 151                 } else {
 152                         a = j0(x);
 153                         b = j1(x);
 154 
 155                         for (i = 1; i < n; i++) {
 156                                 temp = b;
 157                                 /* avoid underflow */
 158                                 b = b * ((GENERIC)(i + i) / x) - a;
 159                                 a = temp;
 160                         }
 161                 }
 162         } else {
 163                 if (x < 1e-9) {              /* use J(n,x) = 1/n!*(x/2)^n */
 164                         b = pow(0.5 * x, (GENERIC)n);
 165 
 166                         if (b != zero) {
 167                                 for (a = one, i = 1; i <= n; i++)
 168                                         a *= (GENERIC)i;
 169 
 170                                 b = b / a;
 171                         }
 172                 } else {
 173                         /*
 174                          * use backward recurrence
 175                          *                      x         x^2     x^2
 176                          *  J(n,x)/J(n-1,x) =  ----   ------   ------   .....
 177                          *                      2n  - 2(n+1) - 2(n+2)
 178                          *
 179                          *                      1         1         1
 180                          *  (for large x)   =  ----  ------   ------   .....
 181                          *                      2n   2(n+1)   2(n+2)
 182                          *                      -- - ------ - ------ -
 183                          *                       x       x               x
 184                          *
 185                          * Let w = 2n/x and h = 2/x, then the above quotient
 186                          * is equal to the continued fraction:
 187                          *                  1
 188                          *      = -----------------------
 189                          *                         1
 190                          *         w - -----------------
 191                          *                        1
 192                          *                      w+h - ---------
 193                          *                         w+2h - ...
 194                          *
 195                          * To determine how many terms needed, let
 196                          * Q(0) = w, Q(1) = w(w+h) - 1,
 197                          * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
 198                          * When Q(k) > 1e4   good for single
 199                          * When Q(k) > 1e9   good for double
 200                          * When Q(k) > 1e17  good for quaduple
 201                          */
 202                         /* determine k */
 203                         GENERIC t, v;
 204                         double q0, q1, h, tmp;
 205                         int k, m;
 206 
 207                         w = (n + n) / (double)x;
 208                         h = 2.0 / (double)x;
 209                         q0 = w;
 210                         z = w + h;
 211                         q1 = w * z - 1.0;
 212                         k = 1;
 213 
 214                         while (q1 < 1.0e9) {
 215                                 k += 1;
 216                                 z += h;
 217                                 tmp = z * q1 - q0;
 218                                 q0 = q1;
 219                                 q1 = tmp;
 220                         }
 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 
 227                         a = t;
 228                         b = one;
 229 
 230                         /* BEGIN CSTYLED */
 231                         /*
 232                          * estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
 233                          * hence, if n*(log(2n/x)) > ...
 234                          *  single:
 235                          *    8.8722839355e+01
 236                          *  double:
 237                          *    7.09782712893383973096e+02
 238                          *  long double:
 239                          *    1.1356523406294143949491931077970765006170e+04
 240                          * then recurrent value may overflow and the result is
 241                          * likely underflow to zero
 242                          */
 243                         /* END CSTYLED */
 244                         tmp = n;
 245                         v = two / x;
 246                         tmp = tmp * log(fabs(v * tmp));
 247 
 248                         if (tmp < 7.09782712893383973096e+02) {
 249                                 for (i = n - 1; i > 0; i--) {
 250                                         temp = b;
 251                                         b = ((i + i) / x) * b - a;
 252                                         a = temp;
 253                                 }
 254                         } else {
 255                                 for (i = n - 1; i > 0; i--) {
 256                                         temp = b;
 257                                         b = ((i + i) / x) * b - a;
 258                                         a = temp;
 259 
 260                                         if (b > 1e100) {
 261                                                 a /= b;
 262                                                 t /= b;
 263                                                 b = 1.0;
 264                                         }
 265                                 }
 266                         }
 267 
 268                         b = (t * j0(x) / b);
 269                 }
 270         }
 271 
 272         if (sgn != 0)
 273                 return (-b);
 274         else
 275                 return (b);
 276 }
 277 
 278 GENERIC
 279 yn(int n, GENERIC x)
 280 {
 281         int i;
 282         int sign;
 283         GENERIC a, b, temp = 0, ox, on;
 284 
 285         ox = x;
 286         on = (GENERIC)n;
 287 
 288         if (isnan(x))
 289                 return (x * x);         /* + -> * for Cheetah */
 290 
 291         if (x <= zero) {
 292                 if (x == zero) {
 293                         /* return -one/zero; */
 294                         return (_SVID_libm_err((GENERIC)n, x, 12));
 295                 } else {
 296                         /* return zero/zero; */
 297                         return (_SVID_libm_err((GENERIC)n, x, 13));
 298                 }
 299         }
 300 
 301         if (!((int)_lib_version == libm_ieee || (__xpg6 &
 302             _C99SUSv3_math_errexcept) != 0)) {
 303                 if (x > X_TLOSS)
 304                         return (_SVID_libm_err(on, ox, 39));
 305         }
 306 
 307         sign = 1;
 308 
 309         if (n < 0) {
 310                 n = -n;
 311 
 312                 if ((n & 1) == 1)
 313                         sign = -1;
 314         }
 315 
 316         if (n == 0)
 317                 return (y0(x));
 318 
 319         if (n == 1)
 320                 return (sign * y1(x));
 321 
 322         if (!finite(x))
 323                 return (zero);
 324 
 325         if (x > 1.0e91) {
 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) {
 341                 case 0:
 342                         temp = sin(x) - cos(x);
 343                         break;
 344                 case 1:
 345                         temp = -sin(x) - cos(x);
 346                         break;
 347                 case 2:
 348                         temp = -sin(x) + cos(x);
 349                         break;
 350                 case 3:
 351                         temp = sin(x) + cos(x);
 352                         break;
 353                 }
 354 
 355                 b = invsqrtpi * temp / sqrt(x);
 356         } else {
 357                 a = y0(x);
 358                 b = y1(x);
 359 
 360                 /*
 361                  * fix 1262058 and take care of non-default rounding
 362                  */
 363                 for (i = 1; i < n; i++) {
 364                         temp = b;
 365                         b *= (GENERIC)(i + i) / x;
 366 
 367                         if (b <= -DBL_MAX)
 368                                 break;
 369 
 370                         b -= a;
 371                         a = temp;
 372                 }
 373         }
 374 
 375         if (sign > 0)
 376                 return (b);
 377         else
 378                 return (-b);
 379 }