Print this page
11210 libm should be cstyle(1ONBLD) clean


   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  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  27  * Use is subject to license terms.
  28  */
  29 
  30 #pragma weak __jnl = jnl
  31 #pragma weak __ynl = ynl
  32 
  33 /*
  34  * floating point Bessel's function of the 1st and 2nd kind
  35  * of order n: jn(n,x),yn(n,x);
  36  *
  37  * Special cases:
  38  *      y0(0)=y1(0)=yn(n,0) = -inf with division by zero signal;
  39  *      y0(-ve)=y1(-ve)=yn(n,-ve) are NaN with invalid signal.
  40  * Note 2. About jn(n,x), yn(n,x)
  41  *      For n=0, j0(x) is called,
  42  *      for n=1, j1(x) is called,
  43  *      for n<x, forward recursion us used starting
  44  *      from values of j0(x) and j1(x).
  45  *      for n>x, a continued fraction approximation to
  46  *      j(n,x)/j(n-1,x) is evaluated and then backward
  47  *      recursion is used starting from a supposed value
  48  *      for j(n,x). The resulting value of j(0,x) is
  49  *      compared with the actual value to correct the
  50  *      supposed value of j(n,x).
  51  *
  52  *      yn(n,x) is similar in all respects, except
  53  *      that forward recursion is used for all
  54  *      values of n>1.
  55  *
  56  */
  57 
  58 #include "libm.h"
  59 #include "longdouble.h"
  60 #include <float.h>        /* LDBL_MAX */
  61 
  62 #define GENERIC long double
  63 
  64 static const GENERIC
  65 invsqrtpi = 5.641895835477562869480794515607725858441e-0001L,
  66 two  = 2.0L,
  67 zero = 0.0L,
  68 one  = 1.0L;
  69 
  70 GENERIC
  71 jnl(int n, GENERIC x)
  72 {
  73         int i, sgn;
  74         GENERIC a, b, temp, z, w;
  75 
  76         /*
  77          * J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
  78          * Thus, J(-n,x) = J(n,-x)
  79          */
  80         if (n < 0) {
  81                 n = -n;
  82                 x = -x;
  83         }

  84         if (n == 0)
  85                 return (j0l(x));

  86         if (n == 1)
  87                 return (j1l(x));

  88         if (x != x)
  89                 return (x+x);
  90         if ((n&1) == 0)

  91                 sgn = 0;                        /* even n */
  92         else
  93                 sgn = signbitl(x);      /* old n  */

  94         x = fabsl(x);
  95         if (x == zero || !finitel(x)) b = zero;
  96         else if ((GENERIC)n <= x) {


  97                                         /*
  98                                          * Safe to use
  99                                          * J(n+1,x)=2n/x *J(n,x)-J(n-1,x)
 100                                          */
 101                 if (x > 1.0e91L) {
 102                                 /*
 103                                  * x >> n**2
 104                                  *  Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
 105                                  *   Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
 106                                  *   Let s=sin(x), c=cos(x),
 107                                  *      xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
 108                                  *
 109                                  *         n    sin(xn)*sqt2    cos(xn)*sqt2
 110                                  *      ----------------------------------
 111                                  *         0     s-c             c+s
 112                                  *         1    -s-c            -c+s
 113                                  *         2    -s+c            -c-s
 114                                  *         3     s+c             c-s
 115                                  */
 116                         switch (n&3) {
 117                         case 0:
 118                                 temp =  cosl(x)+sinl(x);
 119                                 break;
 120                         case 1:
 121                                 temp = -cosl(x)+sinl(x);
 122                                 break;
 123                         case 2:
 124                                 temp = -cosl(x)-sinl(x);
 125                                 break;
 126                         case 3:
 127                                 temp =  cosl(x)-sinl(x);
 128                                 break;
 129                         }
 130                         b = invsqrtpi*temp/sqrtl(x);

 131                 } else {
 132                         a = j0l(x);
 133                         b = j1l(x);

 134                         for (i = 1; i < n; i++) {
 135                                 temp = b;
 136                                 /* avoid underflow */
 137                                 b = b*((GENERIC)(i+i)/x) - a;
 138                                 a = temp;
 139                         }
 140                 }
 141         } else {
 142                 if (x < 1e-17L) {    /* use J(n,x) = 1/n!*(x/2)^n */
 143                         b = powl(0.5L*x, (GENERIC)n);

 144                         if (b != zero) {
 145                                 for (a = one, i = 1; i <= n; i++)
 146                                         a *= (GENERIC)i;
 147                                 b = b/a;

 148                         }
 149                 } else {
 150                         /* use backward recurrence */
 151                         /* BEGIN CSTYLED */
 152                         /*
 153                          *                      x      x^2      x^2
 154                          *  J(n,x)/J(n-1,x) =  ----   ------   ------   .....
 155                          *                      2n  - 2(n+1) - 2(n+2)
 156                          *
 157                          *                      1      1        1
 158                          *  (for large x)   =  ----  ------   ------   .....
 159                          *                      2n   2(n+1)   2(n+2)
 160                          *                      -- - ------ - ------ -
 161                          *                       x     x         x
 162                          *
 163                          * Let w = 2n/x and h=2/x, then the above quotient
 164                          * is equal to the continued fraction:
 165                          *                  1
 166                          *      = -----------------------
 167                          *                     1
 168                          *         w - -----------------
 169                          *                        1
 170                          *              w+h - ---------
 171                          *                     w+2h - ...
 172                          *
 173                          * To determine how many terms needed, let
 174                          * Q(0) = w, Q(1) = w(w+h) - 1,
 175                          * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
 176                          * When Q(k) > 1e4   good for single
 177                          * When Q(k) > 1e9   good for double
 178                          * When Q(k) > 1e17  good for quaduple
 179                          */
 180                         /* END CSTYLED */
 181                         /* determine k */
 182                         GENERIC t, v;
 183                         double q0, q1, h, tmp;
 184                         int k, m;
 185                         w  = (n+n)/(double)x;
 186                         h = 2.0/(double)x;

 187                         q0 = w;
 188                         z = w+h;
 189                         q1 = w*z - 1.0;
 190                         k = 1;

 191                         while (q1 < 1.0e17) {
 192                                 k += 1;
 193                                 z += h;
 194                                 tmp = z*q1 - q0;
 195                                 q0 = q1;
 196                                 q1 = tmp;
 197                         }
 198                         m = n+n;
 199                         for (t = zero, i = 2*(n+k); i >= m; i -= 2)
 200                                 t = one/(i/x-t);



 201                         a = t;
 202                         b = one;

 203                         /*
 204                          * estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
 205                          * hence, if n*(log(2n/x)) > ...
 206                          *  single:
 207                          *    8.8722839355e+01
 208                          *  double:
 209                          *    7.09782712893383973096e+02
 210                          *  long double:
 211                          *    1.1356523406294143949491931077970765006170e+04
 212                          *  then recurrent value may overflow and the result is
 213                          *  likely underflow to zero
 214                          */
 215                         tmp = n;
 216                         v = two/x;
 217                         tmp = tmp*logl(fabsl(v*tmp));

 218                         if (tmp < 1.1356523406294143949491931077970765e+04L) {
 219                                 for (i = n-1; i > 0; i--) {
 220                                         temp = b;
 221                                         b = ((i+i)/x)*b - a;
 222                                         a = temp;
 223                                 }
 224                         } else {
 225                                 for (i = n-1; i > 0; i--) {
 226                                         temp = b;
 227                                         b = ((i+i)/x)*b - a;
 228                                         a = temp;

 229                                         if (b > 1e1000L) {
 230                                                 a /= b;
 231                                                 t /= b;
 232                                                 b  = 1.0;
 233                                         }
 234                                 }
 235                         }
 236                         b = (t*j0l(x)/b);

 237                 }
 238         }

 239         if (sgn != 0)
 240                 return (-b);
 241         else
 242                 return (b);
 243 }
 244 
 245 GENERIC
 246 ynl(int n, GENERIC x)
 247 {
 248         int i;
 249         int sign;
 250         GENERIC a, b, temp;
 251 
 252         if (x != x)
 253                 return (x+x);

 254         if (x <= zero) {
 255                 if (x == zero)
 256                         return (-one/zero);
 257                 else
 258                         return (zero/zero);
 259         }

 260         sign = 1;

 261         if (n < 0) {
 262                 n = -n;
 263                 if ((n&1) == 1) sign = -1;


 264         }

 265         if (n == 0)
 266                 return (y0l(x));

 267         if (n == 1)
 268                 return (sign*y1l(x));

 269         if (!finitel(x))
 270                 return (zero);
 271 
 272         if (x > 1.0e91L) {
 273                 /*
 274                  * x >> n**2
 275                  *   Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
 276                  *   Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
 277                  *   Let s = sin(x), c = cos(x),
 278                  *      xn = x-(2n+1)*pi/4, sqt2 = sqrt(2), then
 279                  *
 280                  *         n    sin(xn)*sqt2    cos(xn)*sqt2
 281                  *      ----------------------------------
 282                  *         0     s-c             c+s
 283                  *         1    -s-c            -c+s
 284                  *         2    -s+c            -c-s
 285                  *         3     s+c             c-s
 286                  */
 287                 switch (n&3) {
 288                 case 0:
 289                         temp =  sinl(x)-cosl(x);
 290                         break;
 291                 case 1:
 292                         temp = -sinl(x)-cosl(x);
 293                         break;
 294                 case 2:
 295                         temp = -sinl(x)+cosl(x);
 296                         break;
 297                 case 3:
 298                         temp =  sinl(x)+cosl(x);
 299                         break;
 300                 }
 301                 b = invsqrtpi*temp/sqrtl(x);

 302         } else {
 303                 a = y0l(x);
 304                 b = y1l(x);

 305                 /*
 306                  * fix 1262058 and take care of non-default rounding
 307                  */
 308                 for (i = 1; i < n; i++) {
 309                         temp = b;
 310                         b *= (GENERIC) (i + i) / x;

 311                         if (b <= -LDBL_MAX)
 312                                 break;

 313                         b -= a;
 314                         a = temp;
 315                 }
 316         }

 317         if (sign > 0)
 318                 return (b);
 319         else
 320                 return (-b);
 321 }


   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 __jnl = jnl
  32 #pragma weak __ynl = ynl
  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 "longdouble.h"
  61 #include <float.h>                        /* LDBL_MAX */
  62 
  63 #define GENERIC long double
  64 
  65 static const GENERIC
  66         invsqrtpi = 5.641895835477562869480794515607725858441e-0001L,
  67         two = 2.0L,
  68         zero = 0.0L,
  69         one = 1.0L;
  70 
  71 GENERIC
  72 jnl(int n, GENERIC x)
  73 {
  74         int i, sgn;
  75         GENERIC a, b, temp, z, w;
  76 
  77         /*
  78          * J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
  79          * Thus, J(-n,x) = J(n,-x)
  80          */
  81         if (n < 0) {
  82                 n = -n;
  83                 x = -x;
  84         }
  85 
  86         if (n == 0)
  87                 return (j0l(x));
  88 
  89         if (n == 1)
  90                 return (j1l(x));
  91 
  92         if (x != x)
  93                 return (x + x);
  94 
  95         if ((n & 1) == 0)
  96                 sgn = 0;                /* even n */
  97         else
  98                 sgn = signbitl(x);      /* old n  */
  99 
 100         x = fabsl(x);
 101 
 102         if (x == zero || !finitel(x)) {
 103                 b = zero;
 104         } else if ((GENERIC)n <= x) {
 105                 /*
 106                  * Safe to use
 107                  * J(n+1,x)=2n/x *J(n,x)-J(n-1,x)
 108                  */
 109                 if (x > 1.0e91L) {
 110                         /*
 111                          * x >> n**2
 112                          *  Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
 113                          *   Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
 114                          *   Let s=sin(x), c=cos(x),
 115                          *      xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
 116                          *
 117                          *         n    sin(xn)*sqt2    cos(xn)*sqt2
 118                          *      ----------------------------------
 119                          *         0     s-c             c+s
 120                          *         1    -s-c            -c+s
 121                          *         2    -s+c            -c-s
 122                          *         3     s+c             c-s
 123                          */
 124                         switch (n & 3) {
 125                         case 0:
 126                                 temp = cosl(x) + sinl(x);
 127                                 break;
 128                         case 1:
 129                                 temp = -cosl(x) + sinl(x);
 130                                 break;
 131                         case 2:
 132                                 temp = -cosl(x) - sinl(x);
 133                                 break;
 134                         case 3:
 135                                 temp = cosl(x) - sinl(x);
 136                                 break;
 137                         }
 138 
 139                         b = invsqrtpi * temp / sqrtl(x);
 140                 } else {
 141                         a = j0l(x);
 142                         b = j1l(x);
 143 
 144                         for (i = 1; i < n; i++) {
 145                                 temp = b;
 146                                 /* avoid underflow */
 147                                 b = b * ((GENERIC)(i + i) / x) - a;
 148                                 a = temp;
 149                         }
 150                 }
 151         } else {
 152                 if (x < 1e-17L) {    /* use J(n,x) = 1/n!*(x/2)^n */
 153                         b = powl(0.5L * x, (GENERIC)n);
 154 
 155                         if (b != zero) {
 156                                 for (a = one, i = 1; i <= n; i++)
 157                                         a *= (GENERIC)i;
 158 
 159                                 b = b / a;
 160                         }
 161                 } else {
 162                         /* use backward recurrence */
 163                         /* BEGIN CSTYLED */
 164                         /*
 165                          *                      x      x^2      x^2
 166                          *  J(n,x)/J(n-1,x) =  ----   ------   ------   .....
 167                          *                      2n  - 2(n+1) - 2(n+2)
 168                          *
 169                          *                      1      1        1
 170                          *  (for large x)   =  ----  ------   ------   .....
 171                          *                      2n   2(n+1)   2(n+2)
 172                          *                      -- - ------ - ------ -
 173                          *                       x     x         x
 174                          *
 175                          * Let w = 2n/x and h=2/x, then the above quotient
 176                          * is equal to the continued fraction:
 177                          *                  1
 178                          *      = -----------------------
 179                          *                     1
 180                          *         w - -----------------
 181                          *                        1
 182                          *              w+h - ---------
 183                          *                     w+2h - ...
 184                          *
 185                          * To determine how many terms needed, let
 186                          * Q(0) = w, Q(1) = w(w+h) - 1,
 187                          * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
 188                          * When Q(k) > 1e4   good for single
 189                          * When Q(k) > 1e9   good for double
 190                          * When Q(k) > 1e17  good for quaduple
 191                          */
 192                         /* END CSTYLED */
 193                         /* determine k */
 194                         GENERIC t, v;
 195                         double q0, q1, h, tmp;
 196                         int k, m;
 197 
 198                         w = (n + n) / (double)x;
 199                         h = 2.0 / (double)x;
 200                         q0 = w;
 201                         z = w + h;
 202                         q1 = w * z - 1.0;
 203                         k = 1;
 204 
 205                         while (q1 < 1.0e17) {
 206                                 k += 1;
 207                                 z += h;
 208                                 tmp = z * q1 - q0;
 209                                 q0 = q1;
 210                                 q1 = tmp;
 211                         }
 212 
 213                         m = n + n;
 214 
 215                         for (t = zero, i = 2 * (n + k); i >= m; i -= 2)
 216                                 t = one / (i / x - t);
 217 
 218                         a = t;
 219                         b = one;
 220 
 221                         /*
 222                          * estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
 223                          * hence, if n*(log(2n/x)) > ...
 224                          *  single:
 225                          *    8.8722839355e+01
 226                          *  double:
 227                          *    7.09782712893383973096e+02
 228                          *  long double:
 229                          *    1.1356523406294143949491931077970765006170e+04
 230                          *  then recurrent value may overflow and the result is
 231                          *  likely underflow to zero
 232                          */
 233                         tmp = n;
 234                         v = two / x;
 235                         tmp = tmp * logl(fabsl(v * tmp));
 236 
 237                         if (tmp < 1.1356523406294143949491931077970765e+04L) {
 238                                 for (i = n - 1; i > 0; i--) {
 239                                         temp = b;
 240                                         b = ((i + i) / x) * b - a;
 241                                         a = temp;
 242                                 }
 243                         } else {
 244                                 for (i = n - 1; i > 0; i--) {
 245                                         temp = b;
 246                                         b = ((i + i) / x) * b - a;
 247                                         a = temp;
 248 
 249                                         if (b > 1e1000L) {
 250                                                 a /= b;
 251                                                 t /= b;
 252                                                 b = 1.0;
 253                                         }
 254                                 }
 255                         }
 256 
 257                         b = (t * j0l(x) / b);
 258                 }
 259         }
 260 
 261         if (sgn != 0)
 262                 return (-b);
 263         else
 264                 return (b);
 265 }
 266 
 267 GENERIC
 268 ynl(int n, GENERIC x)
 269 {
 270         int i;
 271         int sign;
 272         GENERIC a, b, temp;
 273 
 274         if (x != x)
 275                 return (x + x);
 276 
 277         if (x <= zero) {
 278                 if (x == zero)
 279                         return (-one / zero);
 280                 else
 281                         return (zero / zero);
 282         }
 283 
 284         sign = 1;
 285 
 286         if (n < 0) {
 287                 n = -n;
 288 
 289                 if ((n & 1) == 1)
 290                         sign = -1;
 291         }
 292 
 293         if (n == 0)
 294                 return (y0l(x));
 295 
 296         if (n == 1)
 297                 return (sign * y1l(x));
 298 
 299         if (!finitel(x))
 300                 return (zero);
 301 
 302         if (x > 1.0e91L) {
 303                 /*
 304                  * x >> n**2
 305                  *   Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
 306                  *   Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
 307                  *   Let s = sin(x), c = cos(x),
 308                  *      xn = x-(2n+1)*pi/4, sqt2 = sqrt(2), then
 309                  *
 310                  *         n    sin(xn)*sqt2    cos(xn)*sqt2
 311                  *      ----------------------------------
 312                  *         0     s-c             c+s
 313                  *         1    -s-c            -c+s
 314                  *         2    -s+c            -c-s
 315                  *         3     s+c             c-s
 316                  */
 317                 switch (n & 3) {
 318                 case 0:
 319                         temp = sinl(x) - cosl(x);
 320                         break;
 321                 case 1:
 322                         temp = -sinl(x) - cosl(x);
 323                         break;
 324                 case 2:
 325                         temp = -sinl(x) + cosl(x);
 326                         break;
 327                 case 3:
 328                         temp = sinl(x) + cosl(x);
 329                         break;
 330                 }
 331 
 332                 b = invsqrtpi * temp / sqrtl(x);
 333         } else {
 334                 a = y0l(x);
 335                 b = y1l(x);
 336 
 337                 /*
 338                  * fix 1262058 and take care of non-default rounding
 339                  */
 340                 for (i = 1; i < n; i++) {
 341                         temp = b;
 342                         b *= (GENERIC)(i + i) / x;
 343 
 344                         if (b <= -LDBL_MAX)
 345                                 break;
 346 
 347                         b -= a;
 348                         a = temp;
 349                 }
 350         }
 351 
 352         if (sign > 0)
 353                 return (b);
 354         else
 355                 return (-b);
 356 }