xref: /freebsd/lib/msun/src/e_jnf.c (revision 7d8f797b725e3efc0a4256554654780df83c456c)
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 /*
20  * See e_jn.c for complete comments.
21  */
22 
23 #include "math.h"
24 #include "math_private.h"
25 
26 static const volatile float vone = 1, vzero = 0;
27 
28 static const float
29 two   =  2.0000000000e+00, /* 0x40000000 */
30 one   =  1.0000000000e+00; /* 0x3F800000 */
31 
32 static const float zero  =  0.0000000000e+00;
33 
34 float
35 __ieee754_jnf(int n, float x)
36 {
37 	int32_t i,hx,ix, sgn;
38 	float a, b, temp, di;
39 	float z, w;
40 
41     /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
42      * Thus, J(-n,x) = J(n,-x)
43      */
44 	GET_FLOAT_WORD(hx,x);
45 	ix = 0x7fffffff&hx;
46     /* if J(n,NaN) is NaN */
47 	if(ix>0x7f800000) return x+x;
48 	if(n<0){
49 		n = -n;
50 		x = -x;
51 		hx ^= 0x80000000;
52 	}
53 	if(n==0) return(__ieee754_j0f(x));
54 	if(n==1) return(__ieee754_j1f(x));
55 	sgn = (n&1)&(hx>>31);	/* even n -- 0, odd n -- sign(x) */
56 	x = fabsf(x);
57 	if(ix==0||ix>=0x7f800000) 	/* if x is 0 or inf */
58 	    b = zero;
59 	else if((float)n<=x) {
60 		/* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
61 	    a = __ieee754_j0f(x);
62 	    b = __ieee754_j1f(x);
63 	    for(i=1;i<n;i++){
64 		temp = b;
65 		b = b*((float)(i+i)/x) - a; /* avoid underflow */
66 		a = temp;
67 	    }
68 	} else {
69 	    if(ix<0x30800000) {	/* x < 2**-29 */
70     /* x is tiny, return the first Taylor expansion of J(n,x)
71      * J(n,x) = 1/n!*(x/2)^n  - ...
72      */
73 		if(n>33)	/* underflow */
74 		    b = zero;
75 		else {
76 		    temp = x*(float)0.5; b = temp;
77 		    for (a=one,i=2;i<=n;i++) {
78 			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 
172 float
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