(root)/
glibc-2.38/
sysdeps/
ieee754/
dbl-64/
e_jn.c
       1  /* @(#)e_jn.c 5.1 93/09/24 */
       2  /*
       3   * ====================================================
       4   * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
       5   *
       6   * Developed at SunPro, 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  /*
      14   * __ieee754_jn(n, x), __ieee754_yn(n, x)
      15   * floating point Bessel's function of the 1st and 2nd kind
      16   * of order n
      17   *
      18   * Special cases:
      19   *	y0(0)=y1(0)=yn(n,0) = -inf with overflow signal;
      20   *	y0(-ve)=y1(-ve)=yn(n,-ve) are NaN with invalid signal.
      21   * Note 2. About jn(n,x), yn(n,x)
      22   *	For n=0, j0(x) is called,
      23   *	for n=1, j1(x) is called,
      24   *	for n<x, forward recursion us used starting
      25   *	from values of j0(x) and j1(x).
      26   *	for n>x, a continued fraction approximation to
      27   *	j(n,x)/j(n-1,x) is evaluated and then backward
      28   *	recursion is used starting from a supposed value
      29   *	for j(n,x). The resulting value of j(0,x) is
      30   *	compared with the actual value to correct the
      31   *	supposed value of j(n,x).
      32   *
      33   *	yn(n,x) is similar in all respects, except
      34   *	that forward recursion is used for all
      35   *	values of n>1.
      36   *
      37   */
      38  
      39  #include <errno.h>
      40  #include <float.h>
      41  #include <math.h>
      42  #include <math-narrow-eval.h>
      43  #include <math_private.h>
      44  #include <fenv_private.h>
      45  #include <math-underflow.h>
      46  #include <libm-alias-finite.h>
      47  
      48  static const double
      49    invsqrtpi = 5.64189583547756279280e-01, /* 0x3FE20DD7, 0x50429B6D */
      50    two = 2.00000000000000000000e+00,  /* 0x40000000, 0x00000000 */
      51    one = 1.00000000000000000000e+00;  /* 0x3FF00000, 0x00000000 */
      52  
      53  static const double zero = 0.00000000000000000000e+00;
      54  
      55  double
      56  __ieee754_jn (int n, double x)
      57  {
      58    int32_t i, hx, ix, lx, sgn;
      59    double a, b, temp, di, ret;
      60    double z, w;
      61  
      62    /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
      63     * Thus, J(-n,x) = J(n,-x)
      64     */
      65    EXTRACT_WORDS (hx, lx, x);
      66    ix = 0x7fffffff & hx;
      67    /* if J(n,NaN) is NaN */
      68    if (__glibc_unlikely ((ix | ((uint32_t) (lx | -lx)) >> 31) > 0x7ff00000))
      69      return x + x;
      70    if (n < 0)
      71      {
      72        n = -n;
      73        x = -x;
      74        hx ^= 0x80000000;
      75      }
      76    if (n == 0)
      77      return (__ieee754_j0 (x));
      78    if (n == 1)
      79      return (__ieee754_j1 (x));
      80    sgn = (n & 1) & (hx >> 31);   /* even n -- 0, odd n -- sign(x) */
      81    x = fabs (x);
      82    {
      83      SET_RESTORE_ROUND (FE_TONEAREST);
      84      if (__glibc_unlikely ((ix | lx) == 0 || ix >= 0x7ff00000))
      85        /* if x is 0 or inf */
      86        return sgn == 1 ? -zero : zero;
      87      else if ((double) n <= x)
      88        {
      89  	/* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
      90  	if (ix >= 0x52D00000)      /* x > 2**302 */
      91  	  { /* (x >> n**2)
      92  			 *	    Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
      93  			 *	    Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
      94  			 *	    Let s=sin(x), c=cos(x),
      95  			 *		xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
      96  			 *
      97  			 *		   n	sin(xn)*sqt2	cos(xn)*sqt2
      98  			 *		----------------------------------
      99  			 *		   0	 s-c		 c+s
     100  			 *		   1	-s-c		-c+s
     101  			 *		   2	-s+c		-c-s
     102  			 *		   3	 s+c		 c-s
     103  			 */
     104  	    double s;
     105  	    double c;
     106  	    __sincos (x, &s, &c);
     107  	    switch (n & 3)
     108  	      {
     109  	      case 0: temp = c + s; break;
     110  	      case 1: temp = -c + s; break;
     111  	      case 2: temp = -c - s; break;
     112  	      case 3: temp = c - s; break;
     113  	      default: __builtin_unreachable ();
     114  	      }
     115  	    b = invsqrtpi * temp / sqrt (x);
     116  	  }
     117  	else
     118  	  {
     119  	    a = __ieee754_j0 (x);
     120  	    b = __ieee754_j1 (x);
     121  	    for (i = 1; i < n; i++)
     122  	      {
     123  		temp = b;
     124  		b = b * ((double) (i + i) / x) - a; /* avoid underflow */
     125  		a = temp;
     126  	      }
     127  	  }
     128        }
     129      else
     130        {
     131  	if (ix < 0x3e100000)      /* x < 2**-29 */
     132  	  { /* x is tiny, return the first Taylor expansion of J(n,x)
     133  			 * J(n,x) = 1/n!*(x/2)^n  - ...
     134  			 */
     135  	    if (n > 33)           /* underflow */
     136  	      b = zero;
     137  	    else
     138  	      {
     139  		temp = x * 0.5; b = temp;
     140  		for (a = one, i = 2; i <= n; i++)
     141  		  {
     142  		    a *= (double) i;              /* a = n! */
     143  		    b *= temp;                    /* b = (x/2)^n */
     144  		  }
     145  		b = b / a;
     146  	      }
     147  	  }
     148  	else
     149  	  {
     150  	    /* use backward recurrence */
     151  	    /*			x      x^2      x^2
     152  	     *  J(n,x)/J(n-1,x) =  ----   ------   ------   .....
     153  	     *			2n  - 2(n+1) - 2(n+2)
     154  	     *
     155  	     *			1      1        1
     156  	     *  (for large x)   =  ----  ------   ------   .....
     157  	     *			2n   2(n+1)   2(n+2)
     158  	     *			-- - ------ - ------ -
     159  	     *			 x     x         x
     160  	     *
     161  	     * Let w = 2n/x and h=2/x, then the above quotient
     162  	     * is equal to the continued fraction:
     163  	     *		    1
     164  	     *	= -----------------------
     165  	     *		       1
     166  	     *	   w - -----------------
     167  	     *			  1
     168  	     *		w+h - ---------
     169  	     *		       w+2h - ...
     170  	     *
     171  	     * To determine how many terms needed, let
     172  	     * Q(0) = w, Q(1) = w(w+h) - 1,
     173  	     * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
     174  	     * When Q(k) > 1e4	good for single
     175  	     * When Q(k) > 1e9	good for double
     176  	     * When Q(k) > 1e17	good for quadruple
     177  	     */
     178  	    /* determine k */
     179  	    double t, v;
     180  	    double q0, q1, h, tmp; int32_t k, m;
     181  	    w = (n + n) / (double) x; h = 2.0 / (double) x;
     182  	    q0 = w;  z = w + h; q1 = w * z - 1.0; k = 1;
     183  	    while (q1 < 1.0e9)
     184  	      {
     185  		k += 1; z += h;
     186  		tmp = z * q1 - q0;
     187  		q0 = q1;
     188  		q1 = tmp;
     189  	      }
     190  	    m = n + n;
     191  	    for (t = zero, i = 2 * (n + k); i >= m; i -= 2)
     192  	      t = one / (i / x - t);
     193  	    a = t;
     194  	    b = one;
     195  	    /*  estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
     196  	     *  Hence, if n*(log(2n/x)) > ...
     197  	     *  single 8.8722839355e+01
     198  	     *  double 7.09782712893383973096e+02
     199  	     *  long double 1.1356523406294143949491931077970765006170e+04
     200  	     *  then recurrent value may overflow and the result is
     201  	     *  likely underflow to zero
     202  	     */
     203  	    tmp = n;
     204  	    v = two / x;
     205  	    tmp = tmp * __ieee754_log (fabs (v * tmp));
     206  	    if (tmp < 7.09782712893383973096e+02)
     207  	      {
     208  		for (i = n - 1, di = (double) (i + i); i > 0; i--)
     209  		  {
     210  		    temp = b;
     211  		    b *= di;
     212  		    b = b / x - a;
     213  		    a = temp;
     214  		    di -= two;
     215  		  }
     216  	      }
     217  	    else
     218  	      {
     219  		for (i = n - 1, di = (double) (i + i); i > 0; i--)
     220  		  {
     221  		    temp = b;
     222  		    b *= di;
     223  		    b = b / x - a;
     224  		    a = temp;
     225  		    di -= two;
     226  		    /* scale b to avoid spurious overflow */
     227  		    if (b > 1e100)
     228  		      {
     229  			a /= b;
     230  			t /= b;
     231  			b = one;
     232  		      }
     233  		  }
     234  	      }
     235  	    /* j0() and j1() suffer enormous loss of precision at and
     236  	     * near zero; however, we know that their zero points never
     237  	     * coincide, so just choose the one further away from zero.
     238  	     */
     239  	    z = __ieee754_j0 (x);
     240  	    w = __ieee754_j1 (x);
     241  	    if (fabs (z) >= fabs (w))
     242  	      b = (t * z / b);
     243  	    else
     244  	      b = (t * w / a);
     245  	  }
     246        }
     247      if (sgn == 1)
     248        ret = -b;
     249      else
     250        ret = b;
     251      ret = math_narrow_eval (ret);
     252    }
     253    if (ret == 0)
     254      {
     255        ret = math_narrow_eval (copysign (DBL_MIN, ret) * DBL_MIN);
     256        __set_errno (ERANGE);
     257      }
     258    else
     259      math_check_force_underflow (ret);
     260    return ret;
     261  }
     262  libm_alias_finite (__ieee754_jn, __jn)
     263  
     264  double
     265  __ieee754_yn (int n, double x)
     266  {
     267    int32_t i, hx, ix, lx;
     268    int32_t sign;
     269    double a, b, temp, ret;
     270  
     271    EXTRACT_WORDS (hx, lx, x);
     272    ix = 0x7fffffff & hx;
     273    /* if Y(n,NaN) is NaN */
     274    if (__glibc_unlikely ((ix | ((uint32_t) (lx | -lx)) >> 31) > 0x7ff00000))
     275      return x + x;
     276    sign = 1;
     277    if (n < 0)
     278      {
     279        n = -n;
     280        sign = 1 - ((n & 1) << 1);
     281      }
     282    if (n == 0)
     283      return (__ieee754_y0 (x));
     284    if (__glibc_unlikely ((ix | lx) == 0))
     285      return -sign / zero;
     286    /* -inf and overflow exception.  */;
     287    if (__glibc_unlikely (hx < 0))
     288      return zero / (zero * x);
     289    {
     290      SET_RESTORE_ROUND (FE_TONEAREST);
     291      if (n == 1)
     292        {
     293  	ret = sign * __ieee754_y1 (x);
     294  	goto out;
     295        }
     296      if (__glibc_unlikely (ix == 0x7ff00000))
     297        return zero;
     298      if (ix >= 0x52D00000)      /* x > 2**302 */
     299        { /* (x >> n**2)
     300  	 *	    Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
     301  	 *	    Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
     302  	 *	    Let s=sin(x), c=cos(x),
     303  	 *		xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
     304  	 *
     305  	 *		   n	sin(xn)*sqt2	cos(xn)*sqt2
     306  	 *		----------------------------------
     307  	 *		   0	 s-c		 c+s
     308  	 *		   1	-s-c		-c+s
     309  	 *		   2	-s+c		-c-s
     310  	 *		   3	 s+c		 c-s
     311  	 */
     312  	double c;
     313  	double s;
     314  	__sincos (x, &s, &c);
     315  	switch (n & 3)
     316  	  {
     317  	  case 0: temp = s - c; break;
     318  	  case 1: temp = -s - c; break;
     319  	  case 2: temp = -s + c; break;
     320  	  case 3: temp = s + c; break;
     321  	  default: __builtin_unreachable ();
     322  	  }
     323  	b = invsqrtpi * temp / sqrt (x);
     324        }
     325      else
     326        {
     327  	uint32_t high;
     328  	a = __ieee754_y0 (x);
     329  	b = __ieee754_y1 (x);
     330  	/* quit if b is -inf */
     331  	GET_HIGH_WORD (high, b);
     332  	for (i = 1; i < n && high != 0xfff00000; i++)
     333  	  {
     334  	    temp = b;
     335  	    b = ((double) (i + i) / x) * b - a;
     336  	    GET_HIGH_WORD (high, b);
     337  	    a = temp;
     338  	  }
     339  	/* If B is +-Inf, set up errno accordingly.  */
     340  	if (!isfinite (b))
     341  	  __set_errno (ERANGE);
     342        }
     343      if (sign > 0)
     344        ret = b;
     345      else
     346        ret = -b;
     347    }
     348   out:
     349    if (isinf (ret))
     350      ret = copysign (DBL_MAX, ret) * DBL_MAX;
     351    return ret;
     352  }
     353  libm_alias_finite (__ieee754_yn, __yn)