xref: /freebsd/lib/msun/src/e_jnf.c (revision aa0a1e58)
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 __FBSDID("$FreeBSD$");
18 
19 #include "math.h"
20 #include "math_private.h"
21 
22 static const float
23 two   =  2.0000000000e+00, /* 0x40000000 */
24 one   =  1.0000000000e+00; /* 0x3F800000 */
25 
26 static const float zero  =  0.0000000000e+00;
27 
28 float
29 __ieee754_jnf(int n, float x)
30 {
31 	int32_t i,hx,ix, sgn;
32 	float a, b, temp, di;
33 	float z, w;
34 
35     /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
36      * Thus, J(-n,x) = J(n,-x)
37      */
38 	GET_FLOAT_WORD(hx,x);
39 	ix = 0x7fffffff&hx;
40     /* if J(n,NaN) is NaN */
41 	if(ix>0x7f800000) return x+x;
42 	if(n<0){
43 		n = -n;
44 		x = -x;
45 		hx ^= 0x80000000;
46 	}
47 	if(n==0) return(__ieee754_j0f(x));
48 	if(n==1) return(__ieee754_j1f(x));
49 	sgn = (n&1)&(hx>>31);	/* even n -- 0, odd n -- sign(x) */
50 	x = fabsf(x);
51 	if(ix==0||ix>=0x7f800000) 	/* if x is 0 or inf */
52 	    b = zero;
53 	else if((float)n<=x) {
54 		/* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
55 	    a = __ieee754_j0f(x);
56 	    b = __ieee754_j1f(x);
57 	    for(i=1;i<n;i++){
58 		temp = b;
59 		b = b*((float)(i+i)/x) - a; /* avoid underflow */
60 		a = temp;
61 	    }
62 	} else {
63 	    if(ix<0x30800000) {	/* x < 2**-29 */
64     /* x is tiny, return the first Taylor expansion of J(n,x)
65      * J(n,x) = 1/n!*(x/2)^n  - ...
66      */
67 		if(n>33)	/* underflow */
68 		    b = zero;
69 		else {
70 		    temp = x*(float)0.5; b = temp;
71 		    for (a=one,i=2;i<=n;i++) {
72 			a *= (float)i;		/* a = n! */
73 			b *= temp;		/* b = (x/2)^n */
74 		    }
75 		    b = b/a;
76 		}
77 	    } else {
78 		/* use backward recurrence */
79 		/* 			x      x^2      x^2
80 		 *  J(n,x)/J(n-1,x) =  ----   ------   ------   .....
81 		 *			2n  - 2(n+1) - 2(n+2)
82 		 *
83 		 * 			1      1        1
84 		 *  (for large x)   =  ----  ------   ------   .....
85 		 *			2n   2(n+1)   2(n+2)
86 		 *			-- - ------ - ------ -
87 		 *			 x     x         x
88 		 *
89 		 * Let w = 2n/x and h=2/x, then the above quotient
90 		 * is equal to the continued fraction:
91 		 *		    1
92 		 *	= -----------------------
93 		 *		       1
94 		 *	   w - -----------------
95 		 *			  1
96 		 * 	        w+h - ---------
97 		 *		       w+2h - ...
98 		 *
99 		 * To determine how many terms needed, let
100 		 * Q(0) = w, Q(1) = w(w+h) - 1,
101 		 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
102 		 * When Q(k) > 1e4	good for single
103 		 * When Q(k) > 1e9	good for double
104 		 * When Q(k) > 1e17	good for quadruple
105 		 */
106 	    /* determine k */
107 		float t,v;
108 		float q0,q1,h,tmp; int32_t k,m;
109 		w  = (n+n)/(float)x; h = (float)2.0/(float)x;
110 		q0 = w;  z = w+h; q1 = w*z - (float)1.0; k=1;
111 		while(q1<(float)1.0e9) {
112 			k += 1; z += h;
113 			tmp = z*q1 - q0;
114 			q0 = q1;
115 			q1 = tmp;
116 		}
117 		m = n+n;
118 		for(t=zero, i = 2*(n+k); i>=m; i -= 2) t = one/(i/x-t);
119 		a = t;
120 		b = one;
121 		/*  estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
122 		 *  Hence, if n*(log(2n/x)) > ...
123 		 *  single 8.8722839355e+01
124 		 *  double 7.09782712893383973096e+02
125 		 *  long double 1.1356523406294143949491931077970765006170e+04
126 		 *  then recurrent value may overflow and the result is
127 		 *  likely underflow to zero
128 		 */
129 		tmp = n;
130 		v = two/x;
131 		tmp = tmp*__ieee754_logf(fabsf(v*tmp));
132 		if(tmp<(float)8.8721679688e+01) {
133 	    	    for(i=n-1,di=(float)(i+i);i>0;i--){
134 		        temp = b;
135 			b *= di;
136 			b  = b/x - a;
137 		        a = temp;
138 			di -= two;
139 	     	    }
140 		} else {
141 	    	    for(i=n-1,di=(float)(i+i);i>0;i--){
142 		        temp = b;
143 			b *= di;
144 			b  = b/x - a;
145 		        a = temp;
146 			di -= two;
147 		    /* scale b to avoid spurious overflow */
148 			if(b>(float)1e10) {
149 			    a /= b;
150 			    t /= b;
151 			    b  = one;
152 			}
153 	     	    }
154 		}
155 		z = __ieee754_j0f(x);
156 		w = __ieee754_j1f(x);
157 		if (fabsf(z) >= fabsf(w))
158 		    b = (t*z/b);
159 		else
160 		    b = (t*w/a);
161 	    }
162 	}
163 	if(sgn==1) return -b; else return b;
164 }
165 
166 float
167 __ieee754_ynf(int n, float x)
168 {
169 	int32_t i,hx,ix,ib;
170 	int32_t sign;
171 	float a, b, temp;
172 
173 	GET_FLOAT_WORD(hx,x);
174 	ix = 0x7fffffff&hx;
175     /* if Y(n,NaN) is NaN */
176 	if(ix>0x7f800000) return x+x;
177 	if(ix==0) return -one/zero;
178 	if(hx<0) return zero/zero;
179 	sign = 1;
180 	if(n<0){
181 		n = -n;
182 		sign = 1 - ((n&1)<<1);
183 	}
184 	if(n==0) return(__ieee754_y0f(x));
185 	if(n==1) return(sign*__ieee754_y1f(x));
186 	if(ix==0x7f800000) return zero;
187 
188 	a = __ieee754_y0f(x);
189 	b = __ieee754_y1f(x);
190 	/* quit if b is -inf */
191 	GET_FLOAT_WORD(ib,b);
192 	for(i=1;i<n&&ib!=0xff800000;i++){
193 	    temp = b;
194 	    b = ((float)(i+i)/x)*b - a;
195 	    GET_FLOAT_WORD(ib,b);
196 	    a = temp;
197 	}
198 	if(sign>0) return b; else return -b;
199 }
200