xref: /freebsd/lib/msun/src/e_jnf.c (revision 61e21613)
1 /* e_jnf.c -- float version of e_jn.c.
2  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3  */
4 
5 /*
6  * ====================================================
7  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8  *
9  * Developed at SunPro, a Sun Microsystems, Inc. business.
10  * Permission to use, copy, modify, and distribute this
11  * software is freely granted, provided that this notice
12  * is preserved.
13  * ====================================================
14  */
15 
16 #include <sys/cdefs.h>
17 /*
18  * See e_jn.c for complete comments.
19  */
20 
21 #include "math.h"
22 #include "math_private.h"
23 
24 static const volatile float vone = 1, vzero = 0;
25 
26 static const float
27 two   =  2.0000000000e+00, /* 0x40000000 */
28 one   =  1.0000000000e+00; /* 0x3F800000 */
29 
30 static const float zero  =  0.0000000000e+00;
31 
32 float
33 jnf(int n, float x)
34 {
35 	int32_t i,hx,ix, sgn;
36 	float a, b, temp, di;
37 	float z, w;
38 
39     /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
40      * Thus, J(-n,x) = J(n,-x)
41      */
42 	GET_FLOAT_WORD(hx,x);
43 	ix = 0x7fffffff&hx;
44     /* if J(n,NaN) is NaN */
45 	if(ix>0x7f800000) return x+x;
46 	if(n<0){
47 		n = -n;
48 		x = -x;
49 		hx ^= 0x80000000;
50 	}
51 	if(n==0) return(j0f(x));
52 	if(n==1) return(j1f(x));
53 	sgn = (n&1)&(hx>>31);	/* even n -- 0, odd n -- sign(x) */
54 	x = fabsf(x);
55 	if(ix==0||ix>=0x7f800000) 	/* if x is 0 or inf */
56 	    b = zero;
57 	else if((float)n<=x) {
58 		/* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
59 	    a = j0f(x);
60 	    b = j1f(x);
61 	    for(i=1;i<n;i++){
62 		temp = b;
63 		b = b*((float)(i+i)/x) - a; /* avoid underflow */
64 		a = temp;
65 	    }
66 	} else {
67 	    if(ix<0x30800000) {	/* x < 2**-29 */
68     /* x is tiny, return the first Taylor expansion of J(n,x)
69      * J(n,x) = 1/n!*(x/2)^n  - ...
70      */
71 		if(n>33)	/* underflow */
72 		    b = zero;
73 		else {
74 		    temp = x*(float)0.5; b = temp;
75 		    for (a=one,i=2;i<=n;i++) {
76 			a *= (float)i;		/* a = n! */
77 			b *= temp;		/* b = (x/2)^n */
78 		    }
79 		    b = b/a;
80 		}
81 	    } else {
82 		/* use backward recurrence */
83 		/* 			x      x^2      x^2
84 		 *  J(n,x)/J(n-1,x) =  ----   ------   ------   .....
85 		 *			2n  - 2(n+1) - 2(n+2)
86 		 *
87 		 * 			1      1        1
88 		 *  (for large x)   =  ----  ------   ------   .....
89 		 *			2n   2(n+1)   2(n+2)
90 		 *			-- - ------ - ------ -
91 		 *			 x     x         x
92 		 *
93 		 * Let w = 2n/x and h=2/x, then the above quotient
94 		 * is equal to the continued fraction:
95 		 *		    1
96 		 *	= -----------------------
97 		 *		       1
98 		 *	   w - -----------------
99 		 *			  1
100 		 * 	        w+h - ---------
101 		 *		       w+2h - ...
102 		 *
103 		 * To determine how many terms needed, let
104 		 * Q(0) = w, Q(1) = w(w+h) - 1,
105 		 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
106 		 * When Q(k) > 1e4	good for single
107 		 * When Q(k) > 1e9	good for double
108 		 * When Q(k) > 1e17	good for quadruple
109 		 */
110 	    /* determine k */
111 		float t,v;
112 		float q0,q1,h,tmp; int32_t k,m;
113 		w  = (n+n)/(float)x; h = (float)2.0/(float)x;
114 		q0 = w;  z = w+h; q1 = w*z - (float)1.0; k=1;
115 		while(q1<(float)1.0e9) {
116 			k += 1; z += h;
117 			tmp = z*q1 - q0;
118 			q0 = q1;
119 			q1 = tmp;
120 		}
121 		m = n+n;
122 		for(t=zero, i = 2*(n+k); i>=m; i -= 2) t = one/(i/x-t);
123 		a = t;
124 		b = one;
125 		/*  estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
126 		 *  Hence, if n*(log(2n/x)) > ...
127 		 *  single 8.8722839355e+01
128 		 *  double 7.09782712893383973096e+02
129 		 *  long double 1.1356523406294143949491931077970765006170e+04
130 		 *  then recurrent value may overflow and the result is
131 		 *  likely underflow to zero
132 		 */
133 		tmp = n;
134 		v = two/x;
135 		tmp = tmp*logf(fabsf(v*tmp));
136 		if(tmp<(float)8.8721679688e+01) {
137 	    	    for(i=n-1,di=(float)(i+i);i>0;i--){
138 		        temp = b;
139 			b *= di;
140 			b  = b/x - a;
141 		        a = temp;
142 			di -= two;
143 	     	    }
144 		} else {
145 	    	    for(i=n-1,di=(float)(i+i);i>0;i--){
146 		        temp = b;
147 			b *= di;
148 			b  = b/x - a;
149 		        a = temp;
150 			di -= two;
151 		    /* scale b to avoid spurious overflow */
152 			if(b>(float)1e10) {
153 			    a /= b;
154 			    t /= b;
155 			    b  = one;
156 			}
157 	     	    }
158 		}
159 		z = j0f(x);
160 		w = j1f(x);
161 		if (fabsf(z) >= fabsf(w))
162 		    b = (t*z/b);
163 		else
164 		    b = (t*w/a);
165 	    }
166 	}
167 	if(sgn==1) return -b; else return b;
168 }
169 
170 float
171 ynf(int n, float x)
172 {
173 	int32_t i,hx,ix,ib;
174 	int32_t sign;
175 	float a, b, temp;
176 
177 	GET_FLOAT_WORD(hx,x);
178 	ix = 0x7fffffff&hx;
179 	if(ix>0x7f800000) return x+x;
180 	if(ix==0) return -one/vzero;
181 	if(hx<0) return vzero/vzero;
182 	sign = 1;
183 	if(n<0){
184 		n = -n;
185 		sign = 1 - ((n&1)<<1);
186 	}
187 	if(n==0) return(y0f(x));
188 	if(n==1) return(sign*y1f(x));
189 	if(ix==0x7f800000) return zero;
190 
191 	a = y0f(x);
192 	b = y1f(x);
193 	/* quit if b is -inf */
194 	GET_FLOAT_WORD(ib,b);
195 	for(i=1;i<n&&ib!=0xff800000;i++){
196 	    temp = b;
197 	    b = ((float)(i+i)/x)*b - a;
198 	    GET_FLOAT_WORD(ib,b);
199 	    a = temp;
200 	}
201 	if(sign>0) return b; else return -b;
202 }
203