1321936Shselasky/* e_jnf.c -- float version of e_jn.c.
2321936Shselasky * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3321936Shselasky */
4321936Shselasky
5321936Shselasky/*
6321936Shselasky * ====================================================
7321936Shselasky * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8321936Shselasky *
9321936Shselasky * Developed at SunPro, a Sun Microsystems, Inc. business.
10321936Shselasky * Permission to use, copy, modify, and distribute this
11321936Shselasky * software is freely granted, provided that this notice
12321936Shselasky * is preserved.
13321936Shselasky * ====================================================
14321936Shselasky */
15321936Shselasky
16321936Shselasky#include <sys/cdefs.h>
17321936Shselasky__FBSDID("$FreeBSD$");
18321936Shselasky
19321936Shselasky/*
20321936Shselasky * See e_jn.c for complete comments.
21321936Shselasky */
22321936Shselasky
23321936Shselasky#include "math.h"
24321936Shselasky#include "math_private.h"
25321936Shselasky
26321936Shselaskystatic const volatile float vone = 1, vzero = 0;
27321936Shselasky
28321936Shselaskystatic const float
29321936Shselaskytwo   =  2.0000000000e+00, /* 0x40000000 */
30321936Shselaskyone   =  1.0000000000e+00; /* 0x3F800000 */
31321936Shselasky
32321936Shselaskystatic const float zero  =  0.0000000000e+00;
33321936Shselasky
34321936Shselaskyfloat
35321936Shselasky__ieee754_jnf(int n, float x)
36321936Shselasky{
37321936Shselasky	int32_t i,hx,ix, sgn;
38321936Shselasky	float a, b, temp, di;
39321936Shselasky	float z, w;
40321936Shselasky
41321936Shselasky    /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
42321936Shselasky     * Thus, J(-n,x) = J(n,-x)
43321936Shselasky     */
44321936Shselasky	GET_FLOAT_WORD(hx,x);
45321936Shselasky	ix = 0x7fffffff&hx;
46321936Shselasky    /* if J(n,NaN) is NaN */
47321936Shselasky	if(ix>0x7f800000) return x+x;
48321936Shselasky	if(n<0){
49321936Shselasky		n = -n;
50321936Shselasky		x = -x;
51321936Shselasky		hx ^= 0x80000000;
52321936Shselasky	}
53321936Shselasky	if(n==0) return(__ieee754_j0f(x));
54321936Shselasky	if(n==1) return(__ieee754_j1f(x));
55321936Shselasky	sgn = (n&1)&(hx>>31);	/* even n -- 0, odd n -- sign(x) */
56321936Shselasky	x = fabsf(x);
57321936Shselasky	if(ix==0||ix>=0x7f800000) 	/* if x is 0 or inf */
58321936Shselasky	    b = zero;
59321936Shselasky	else if((float)n<=x) {
60321936Shselasky		/* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
61321936Shselasky	    a = __ieee754_j0f(x);
62321936Shselasky	    b = __ieee754_j1f(x);
63321936Shselasky	    for(i=1;i<n;i++){
64321936Shselasky		temp = b;
65321936Shselasky		b = b*((float)(i+i)/x) - a; /* avoid underflow */
66321936Shselasky		a = temp;
67321936Shselasky	    }
68321936Shselasky	} else {
69321936Shselasky	    if(ix<0x30800000) {	/* x < 2**-29 */
70321936Shselasky    /* x is tiny, return the first Taylor expansion of J(n,x)
71321936Shselasky     * J(n,x) = 1/n!*(x/2)^n  - ...
72321936Shselasky     */
73321936Shselasky		if(n>33)	/* underflow */
74321936Shselasky		    b = zero;
75321936Shselasky		else {
76321936Shselasky		    temp = x*(float)0.5; b = temp;
77321936Shselasky		    for (a=one,i=2;i<=n;i++) {
78321936Shselasky			a *= (float)i;		/* a = n! */
79			b *= temp;		/* b = (x/2)^n */
80		    }
81		    b = b/a;
82		}
83	    } else {
84		/* use backward recurrence */
85		/* 			x      x^2      x^2
86		 *  J(n,x)/J(n-1,x) =  ----   ------   ------   .....
87		 *			2n  - 2(n+1) - 2(n+2)
88		 *
89		 * 			1      1        1
90		 *  (for large x)   =  ----  ------   ------   .....
91		 *			2n   2(n+1)   2(n+2)
92		 *			-- - ------ - ------ -
93		 *			 x     x         x
94		 *
95		 * Let w = 2n/x and h=2/x, then the above quotient
96		 * is equal to the continued fraction:
97		 *		    1
98		 *	= -----------------------
99		 *		       1
100		 *	   w - -----------------
101		 *			  1
102		 * 	        w+h - ---------
103		 *		       w+2h - ...
104		 *
105		 * To determine how many terms needed, let
106		 * Q(0) = w, Q(1) = w(w+h) - 1,
107		 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
108		 * When Q(k) > 1e4	good for single
109		 * When Q(k) > 1e9	good for double
110		 * When Q(k) > 1e17	good for quadruple
111		 */
112	    /* determine k */
113		float t,v;
114		float q0,q1,h,tmp; int32_t k,m;
115		w  = (n+n)/(float)x; h = (float)2.0/(float)x;
116		q0 = w;  z = w+h; q1 = w*z - (float)1.0; k=1;
117		while(q1<(float)1.0e9) {
118			k += 1; z += h;
119			tmp = z*q1 - q0;
120			q0 = q1;
121			q1 = tmp;
122		}
123		m = n+n;
124		for(t=zero, i = 2*(n+k); i>=m; i -= 2) t = one/(i/x-t);
125		a = t;
126		b = one;
127		/*  estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
128		 *  Hence, if n*(log(2n/x)) > ...
129		 *  single 8.8722839355e+01
130		 *  double 7.09782712893383973096e+02
131		 *  long double 1.1356523406294143949491931077970765006170e+04
132		 *  then recurrent value may overflow and the result is
133		 *  likely underflow to zero
134		 */
135		tmp = n;
136		v = two/x;
137		tmp = tmp*__ieee754_logf(fabsf(v*tmp));
138		if(tmp<(float)8.8721679688e+01) {
139	    	    for(i=n-1,di=(float)(i+i);i>0;i--){
140		        temp = b;
141			b *= di;
142			b  = b/x - a;
143		        a = temp;
144			di -= two;
145	     	    }
146		} else {
147	    	    for(i=n-1,di=(float)(i+i);i>0;i--){
148		        temp = b;
149			b *= di;
150			b  = b/x - a;
151		        a = temp;
152			di -= two;
153		    /* scale b to avoid spurious overflow */
154			if(b>(float)1e10) {
155			    a /= b;
156			    t /= b;
157			    b  = one;
158			}
159	     	    }
160		}
161		z = __ieee754_j0f(x);
162		w = __ieee754_j1f(x);
163		if (fabsf(z) >= fabsf(w))
164		    b = (t*z/b);
165		else
166		    b = (t*w/a);
167	    }
168	}
169	if(sgn==1) return -b; else return b;
170}
171
172float
173__ieee754_ynf(int n, float x)
174{
175	int32_t i,hx,ix,ib;
176	int32_t sign;
177	float a, b, temp;
178
179	GET_FLOAT_WORD(hx,x);
180	ix = 0x7fffffff&hx;
181	if(ix>0x7f800000) return x+x;
182	if(ix==0) return -one/vzero;
183	if(hx<0) return vzero/vzero;
184	sign = 1;
185	if(n<0){
186		n = -n;
187		sign = 1 - ((n&1)<<1);
188	}
189	if(n==0) return(__ieee754_y0f(x));
190	if(n==1) return(sign*__ieee754_y1f(x));
191	if(ix==0x7f800000) return zero;
192
193	a = __ieee754_y0f(x);
194	b = __ieee754_y1f(x);
195	/* quit if b is -inf */
196	GET_FLOAT_WORD(ib,b);
197	for(i=1;i<n&&ib!=0xff800000;i++){
198	    temp = b;
199	    b = ((float)(i+i)/x)*b - a;
200	    GET_FLOAT_WORD(ib,b);
201	    a = temp;
202	}
203	if(sign>0) return b; else return -b;
204}
205