1141296Sdas/* @(#)e_jn.c 1.4 95/01/18 */
22116Sjkh/*
32116Sjkh * ====================================================
42116Sjkh * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
52116Sjkh *
6141296Sdas * Developed at SunSoft, a Sun Microsystems, Inc. business.
72116Sjkh * Permission to use, copy, modify, and distribute this
8336196Smarkj * software is freely granted, provided that this notice
92116Sjkh * is preserved.
102116Sjkh * ====================================================
112116Sjkh */
122116Sjkh
13176451Sdas#include <sys/cdefs.h>
14176451Sdas__FBSDID("$FreeBSD: stable/11/lib/msun/src/e_jn.c 347068 2019-05-03 22:56:50Z peterj $");
152116Sjkh
162116Sjkh/*
172116Sjkh * __ieee754_jn(n, x), __ieee754_yn(n, x)
182116Sjkh * floating point Bessel's function of the 1st and 2nd kind
192116Sjkh * of order n
20336196Smarkj *
212116Sjkh * Special cases:
222116Sjkh *	y0(0)=y1(0)=yn(n,0) = -inf with division by zero signal;
232116Sjkh *	y0(-ve)=y1(-ve)=yn(n,-ve) are NaN with invalid signal.
242116Sjkh * Note 2. About jn(n,x), yn(n,x)
252116Sjkh *	For n=0, j0(x) is called,
262116Sjkh *	for n=1, j1(x) is called,
272116Sjkh *	for n<x, forward recursion us used starting
282116Sjkh *	from values of j0(x) and j1(x).
292116Sjkh *	for n>x, a continued fraction approximation to
302116Sjkh *	j(n,x)/j(n-1,x) is evaluated and then backward
312116Sjkh *	recursion is used starting from a supposed value
322116Sjkh *	for j(n,x). The resulting value of j(0,x) is
332116Sjkh *	compared with the actual value to correct the
342116Sjkh *	supposed value of j(n,x).
352116Sjkh *
362116Sjkh *	yn(n,x) is similar in all respects, except
372116Sjkh *	that forward recursion is used for all
382116Sjkh *	values of n>1.
392116Sjkh */
402116Sjkh
412116Sjkh#include "math.h"
422116Sjkh#include "math_private.h"
432116Sjkh
44279856Skarglstatic const volatile double vone = 1, vzero = 0;
45279856Skargl
462116Sjkhstatic const double
472116Sjkhinvsqrtpi=  5.64189583547756279280e-01, /* 0x3FE20DD7, 0x50429B6D */
482116Sjkhtwo   =  2.00000000000000000000e+00, /* 0x40000000, 0x00000000 */
492116Sjkhone   =  1.00000000000000000000e+00; /* 0x3FF00000, 0x00000000 */
502116Sjkh
512116Sjkhstatic const double zero  =  0.00000000000000000000e+00;
522116Sjkh
5397413Salfreddouble
5497413Salfred__ieee754_jn(int n, double x)
552116Sjkh{
562116Sjkh	int32_t i,hx,ix,lx, sgn;
57347068Speterj	double a, b, c, s, temp, di;
582116Sjkh	double z, w;
592116Sjkh
602116Sjkh    /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
612116Sjkh     * Thus, J(-n,x) = J(n,-x)
622116Sjkh     */
632116Sjkh	EXTRACT_WORDS(hx,lx,x);
642116Sjkh	ix = 0x7fffffff&hx;
652116Sjkh    /* if J(n,NaN) is NaN */
662116Sjkh	if((ix|((u_int32_t)(lx|-lx))>>31)>0x7ff00000) return x+x;
67336196Smarkj	if(n<0){
682116Sjkh		n = -n;
692116Sjkh		x = -x;
702116Sjkh		hx ^= 0x80000000;
712116Sjkh	}
722116Sjkh	if(n==0) return(__ieee754_j0(x));
732116Sjkh	if(n==1) return(__ieee754_j1(x));
742116Sjkh	sgn = (n&1)&(hx>>31);	/* even n -- 0, odd n -- sign(x) */
752116Sjkh	x = fabs(x);
762116Sjkh	if((ix|lx)==0||ix>=0x7ff00000) 	/* if x is 0 or inf */
772116Sjkh	    b = zero;
78336196Smarkj	else if((double)n<=x) {
792116Sjkh		/* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
802116Sjkh	    if(ix>=0x52D00000) { /* x > 2**302 */
81336196Smarkj    /* (x >> n**2)
822116Sjkh     *	    Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
832116Sjkh     *	    Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
84336196Smarkj     *	    Let s=sin(x), c=cos(x),
85336196Smarkj     *		xn=x-(2n+1)*pi/4, sqt2 = sqrt(2), then
862116Sjkh     *
872116Sjkh     *		   n	sin(xn)*sqt2	cos(xn)*sqt2
882116Sjkh     *		----------------------------------
892116Sjkh     *		   0	 s-c		 c+s
902116Sjkh     *		   1	-s-c 		-c+s
912116Sjkh     *		   2	-s+c		-c-s
922116Sjkh     *		   3	 s+c		 c-s
932116Sjkh     */
94347068Speterj		sincos(x, &s, &c);
952116Sjkh		switch(n&3) {
96347068Speterj		    case 0: temp =  c+s; break;
97347068Speterj		    case 1: temp = -c+s; break;
98347068Speterj		    case 2: temp = -c-s; break;
99347068Speterj		    case 3: temp =  c-s; break;
1002116Sjkh		}
1012116Sjkh		b = invsqrtpi*temp/sqrt(x);
102336196Smarkj	    } else {
1032116Sjkh	        a = __ieee754_j0(x);
1042116Sjkh	        b = __ieee754_j1(x);
1052116Sjkh	        for(i=1;i<n;i++){
1062116Sjkh		    temp = b;
1072116Sjkh		    b = b*((double)(i+i)/x) - a; /* avoid underflow */
1082116Sjkh		    a = temp;
1092116Sjkh	        }
1102116Sjkh	    }
1112116Sjkh	} else {
1122116Sjkh	    if(ix<0x3e100000) {	/* x < 2**-29 */
113336196Smarkj    /* x is tiny, return the first Taylor expansion of J(n,x)
1142116Sjkh     * J(n,x) = 1/n!*(x/2)^n  - ...
1152116Sjkh     */
1162116Sjkh		if(n>33)	/* underflow */
1172116Sjkh		    b = zero;
1182116Sjkh		else {
1192116Sjkh		    temp = x*0.5; b = temp;
1202116Sjkh		    for (a=one,i=2;i<=n;i++) {
1212116Sjkh			a *= (double)i;		/* a = n! */
1222116Sjkh			b *= temp;		/* b = (x/2)^n */
1232116Sjkh		    }
1242116Sjkh		    b = b/a;
1252116Sjkh		}
1262116Sjkh	    } else {
1272116Sjkh		/* use backward recurrence */
128336196Smarkj		/* 			x      x^2      x^2
1292116Sjkh		 *  J(n,x)/J(n-1,x) =  ----   ------   ------   .....
1302116Sjkh		 *			2n  - 2(n+1) - 2(n+2)
1312116Sjkh		 *
132336196Smarkj		 * 			1      1        1
1332116Sjkh		 *  (for large x)   =  ----  ------   ------   .....
1342116Sjkh		 *			2n   2(n+1)   2(n+2)
135336196Smarkj		 *			-- - ------ - ------ -
1362116Sjkh		 *			 x     x         x
1372116Sjkh		 *
1382116Sjkh		 * Let w = 2n/x and h=2/x, then the above quotient
1392116Sjkh		 * is equal to the continued fraction:
1402116Sjkh		 *		    1
1412116Sjkh		 *	= -----------------------
1422116Sjkh		 *		       1
1432116Sjkh		 *	   w - -----------------
1442116Sjkh		 *			  1
1452116Sjkh		 * 	        w+h - ---------
1462116Sjkh		 *		       w+2h - ...
1472116Sjkh		 *
1482116Sjkh		 * To determine how many terms needed, let
1492116Sjkh		 * Q(0) = w, Q(1) = w(w+h) - 1,
1502116Sjkh		 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
151336196Smarkj		 * When Q(k) > 1e4	good for single
152336196Smarkj		 * When Q(k) > 1e9	good for double
153336196Smarkj		 * When Q(k) > 1e17	good for quadruple
1542116Sjkh		 */
1552116Sjkh	    /* determine k */
1562116Sjkh		double t,v;
1572116Sjkh		double q0,q1,h,tmp; int32_t k,m;
1582116Sjkh		w  = (n+n)/(double)x; h = 2.0/(double)x;
1592116Sjkh		q0 = w;  z = w+h; q1 = w*z - 1.0; k=1;
1602116Sjkh		while(q1<1.0e9) {
1612116Sjkh			k += 1; z += h;
1622116Sjkh			tmp = z*q1 - q0;
1632116Sjkh			q0 = q1;
1642116Sjkh			q1 = tmp;
1652116Sjkh		}
1662116Sjkh		m = n+n;
1672116Sjkh		for(t=zero, i = 2*(n+k); i>=m; i -= 2) t = one/(i/x-t);
1682116Sjkh		a = t;
1692116Sjkh		b = one;
1702116Sjkh		/*  estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
1712116Sjkh		 *  Hence, if n*(log(2n/x)) > ...
1722116Sjkh		 *  single 8.8722839355e+01
1732116Sjkh		 *  double 7.09782712893383973096e+02
1742116Sjkh		 *  long double 1.1356523406294143949491931077970765006170e+04
1758870Srgrimes		 *  then recurrent value may overflow and the result is
1762116Sjkh		 *  likely underflow to zero
1772116Sjkh		 */
1782116Sjkh		tmp = n;
1792116Sjkh		v = two/x;
1802116Sjkh		tmp = tmp*__ieee754_log(fabs(v*tmp));
1812116Sjkh		if(tmp<7.09782712893383973096e+02) {
1822116Sjkh	    	    for(i=n-1,di=(double)(i+i);i>0;i--){
1832116Sjkh		        temp = b;
1842116Sjkh			b *= di;
1852116Sjkh			b  = b/x - a;
1862116Sjkh		        a = temp;
1872116Sjkh			di -= two;
1882116Sjkh	     	    }
1892116Sjkh		} else {
1902116Sjkh	    	    for(i=n-1,di=(double)(i+i);i>0;i--){
1912116Sjkh		        temp = b;
1922116Sjkh			b *= di;
1932116Sjkh			b  = b/x - a;
1942116Sjkh		        a = temp;
1952116Sjkh			di -= two;
1962116Sjkh		    /* scale b to avoid spurious overflow */
1972116Sjkh			if(b>1e100) {
1982116Sjkh			    a /= b;
1992116Sjkh			    t /= b;
2002116Sjkh			    b  = one;
2012116Sjkh			}
2022116Sjkh	     	    }
2032116Sjkh		}
204215237Suqs		z = __ieee754_j0(x);
205215237Suqs		w = __ieee754_j1(x);
206215237Suqs		if (fabs(z) >= fabs(w))
207215237Suqs		    b = (t*z/b);
208215237Suqs		else
209215237Suqs		    b = (t*w/a);
2102116Sjkh	    }
2112116Sjkh	}
2122116Sjkh	if(sgn==1) return -b; else return b;
2132116Sjkh}
2142116Sjkh
21597413Salfreddouble
21697413Salfred__ieee754_yn(int n, double x)
2172116Sjkh{
2182116Sjkh	int32_t i,hx,ix,lx;
2192116Sjkh	int32_t sign;
220347068Speterj	double a, b, c, s, temp;
2212116Sjkh
2222116Sjkh	EXTRACT_WORDS(hx,lx,x);
2232116Sjkh	ix = 0x7fffffff&hx;
224279856Skargl	/* yn(n,NaN) = NaN */
2252116Sjkh	if((ix|((u_int32_t)(lx|-lx))>>31)>0x7ff00000) return x+x;
226279856Skargl	/* yn(n,+-0) = -inf and raise divide-by-zero exception. */
227279856Skargl	if((ix|lx)==0) return -one/vzero;
228279856Skargl	/* yn(n,x<0) = NaN and raise invalid exception. */
229279856Skargl	if(hx<0) return vzero/vzero;
2302116Sjkh	sign = 1;
2312116Sjkh	if(n<0){
2322116Sjkh		n = -n;
2337658Sbde		sign = 1 - ((n&1)<<1);
2342116Sjkh	}
2352116Sjkh	if(n==0) return(__ieee754_y0(x));
2362116Sjkh	if(n==1) return(sign*__ieee754_y1(x));
2372116Sjkh	if(ix==0x7ff00000) return zero;
2382116Sjkh	if(ix>=0x52D00000) { /* x > 2**302 */
239336196Smarkj    /* (x >> n**2)
2402116Sjkh     *	    Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
2412116Sjkh     *	    Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
242336196Smarkj     *	    Let s=sin(x), c=cos(x),
243336196Smarkj     *		xn=x-(2n+1)*pi/4, sqt2 = sqrt(2), then
2442116Sjkh     *
2452116Sjkh     *		   n	sin(xn)*sqt2	cos(xn)*sqt2
2462116Sjkh     *		----------------------------------
2472116Sjkh     *		   0	 s-c		 c+s
2482116Sjkh     *		   1	-s-c 		-c+s
2492116Sjkh     *		   2	-s+c		-c-s
2502116Sjkh     *		   3	 s+c		 c-s
2512116Sjkh     */
252347068Speterj		sincos(x, &s, &c);
2532116Sjkh		switch(n&3) {
254347068Speterj		    case 0: temp =  s-c; break;
255347068Speterj		    case 1: temp = -s-c; break;
256347068Speterj		    case 2: temp = -s+c; break;
257347068Speterj		    case 3: temp =  s+c; break;
2582116Sjkh		}
2592116Sjkh		b = invsqrtpi*temp/sqrt(x);
2602116Sjkh	} else {
2612116Sjkh	    u_int32_t high;
2622116Sjkh	    a = __ieee754_y0(x);
2632116Sjkh	    b = __ieee754_y1(x);
2642116Sjkh	/* quit if b is -inf */
2652116Sjkh	    GET_HIGH_WORD(high,b);
2668870Srgrimes	    for(i=1;i<n&&high!=0xfff00000;i++){
2672116Sjkh		temp = b;
2682116Sjkh		b = ((double)(i+i)/x)*b - a;
2692116Sjkh		GET_HIGH_WORD(high,b);
2702116Sjkh		a = temp;
2712116Sjkh	    }
2722116Sjkh	}
2732116Sjkh	if(sign>0) return b; else return -b;
2742116Sjkh}
275