1 /* e_jnf.c -- float version of e_jn.c.
2 */
3
4 /*
5 * ====================================================
6 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
7 *
8 * Developed at SunPro, a Sun Microsystems, Inc. business.
9 * Permission to use, copy, modify, and distribute this
10 * software is freely granted, provided that this notice
11 * is preserved.
12 * ====================================================
13 */
14
15 #include <errno.h>
16 #include <float.h>
17 #include <math.h>
18 #include <math-narrow-eval.h>
19 #include <math_private.h>
20 #include <fenv_private.h>
21 #include <math-underflow.h>
22 #include <libm-alias-finite.h>
23
24 static const float
25 two = 2.0000000000e+00, /* 0x40000000 */
26 one = 1.0000000000e+00; /* 0x3F800000 */
27
28 static const float zero = 0.0000000000e+00;
29
30 float
__ieee754_jnf(int n,float x)31 __ieee754_jnf(int n, float x)
32 {
33 float ret;
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(__builtin_expect(ix>0x7f800000, 0)) return x+x;
46 if(n<0){
47 n = -n;
48 x = -x;
49 hx ^= 0x80000000;
50 }
51 if(n==0) return(__ieee754_j0f(x));
52 if(n==1) return(__ieee754_j1f(x));
53 sgn = (n&1)&(hx>>31); /* even n -- 0, odd n -- sign(x) */
54 x = fabsf(x);
55 SET_RESTORE_ROUNDF (FE_TONEAREST);
56 if(__builtin_expect(ix==0||ix>=0x7f800000, 0)) /* if x is 0 or inf */
57 return sgn == 1 ? -zero : zero;
58 else if((float)n<=x) {
59 /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
60 a = __ieee754_j0f(x);
61 b = __ieee754_j1f(x);
62 for(i=1;i<n;i++){
63 temp = b;
64 b = b*((double)(i+i)/x) - a; /* avoid underflow */
65 a = temp;
66 }
67 } else {
68 if(ix<0x30800000) { /* x < 2**-29 */
69 /* x is tiny, return the first Taylor expansion of J(n,x)
70 * J(n,x) = 1/n!*(x/2)^n - ...
71 */
72 if(n>33) /* underflow */
73 b = zero;
74 else {
75 temp = x*(float)0.5; b = temp;
76 for (a=one,i=2;i<=n;i++) {
77 a *= (float)i; /* a = n! */
78 b *= temp; /* b = (x/2)^n */
79 }
80 b = b/a;
81 }
82 } else {
83 /* use backward recurrence */
84 /* x x^2 x^2
85 * J(n,x)/J(n-1,x) = ---- ------ ------ .....
86 * 2n - 2(n+1) - 2(n+2)
87 *
88 * 1 1 1
89 * (for large x) = ---- ------ ------ .....
90 * 2n 2(n+1) 2(n+2)
91 * -- - ------ - ------ -
92 * x x x
93 *
94 * Let w = 2n/x and h=2/x, then the above quotient
95 * is equal to the continued fraction:
96 * 1
97 * = -----------------------
98 * 1
99 * w - -----------------
100 * 1
101 * w+h - ---------
102 * w+2h - ...
103 *
104 * To determine how many terms needed, let
105 * Q(0) = w, Q(1) = w(w+h) - 1,
106 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
107 * When Q(k) > 1e4 good for single
108 * When Q(k) > 1e9 good for double
109 * When Q(k) > 1e17 good for quadruple
110 */
111 /* determine k */
112 float t,v;
113 float q0,q1,h,tmp; int32_t k,m;
114 w = (n+n)/(float)x; h = (float)2.0/(float)x;
115 q0 = w; z = w+h; q1 = w*z - (float)1.0; k=1;
116 while(q1<(float)1.0e9) {
117 k += 1; z += h;
118 tmp = z*q1 - q0;
119 q0 = q1;
120 q1 = tmp;
121 }
122 m = n+n;
123 for(t=zero, i = 2*(n+k); i>=m; i -= 2) t = one/(i/x-t);
124 a = t;
125 b = one;
126 /* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
127 * Hence, if n*(log(2n/x)) > ...
128 * single 8.8722839355e+01
129 * double 7.09782712893383973096e+02
130 * long double 1.1356523406294143949491931077970765006170e+04
131 * then recurrent value may overflow and the result is
132 * likely underflow to zero
133 */
134 tmp = n;
135 v = two/x;
136 tmp = tmp*__ieee754_logf(fabsf(v*tmp));
137 if(tmp<8.8721679688e+01f) {
138 for(i=n-1,di=(float)(i+i);i>0;i--){
139 temp = b;
140 b *= di;
141 b = b/x - a;
142 a = temp;
143 di -= two;
144 }
145 } else {
146 for(i=n-1,di=(float)(i+i);i>0;i--){
147 temp = b;
148 b *= di;
149 b = b/x - a;
150 a = temp;
151 di -= two;
152 /* scale b to avoid spurious overflow */
153 if(b>(float)1e10) {
154 a /= b;
155 t /= b;
156 b = one;
157 }
158 }
159 }
160 /* j0() and j1() suffer enormous loss of precision at and
161 * near zero; however, we know that their zero points never
162 * coincide, so just choose the one further away from zero.
163 */
164 z = __ieee754_j0f (x);
165 w = __ieee754_j1f (x);
166 if (fabsf (z) >= fabsf (w))
167 b = (t * z / b);
168 else
169 b = (t * w / a);
170 }
171 }
172 if(sgn==1) ret = -b; else ret = b;
173 ret = math_narrow_eval (ret);
174 }
175 if (ret == 0)
176 {
177 ret = math_narrow_eval (copysignf (FLT_MIN, ret) * FLT_MIN);
178 __set_errno (ERANGE);
179 }
180 else
181 math_check_force_underflow (ret);
182 return ret;
183 }
libm_alias_finite(__ieee754_jnf,__jnf)184 libm_alias_finite (__ieee754_jnf, __jnf)
185
186 float
187 __ieee754_ynf(int n, float x)
188 {
189 float ret;
190 {
191 int32_t i,hx,ix;
192 uint32_t ib;
193 int32_t sign;
194 float a, b, temp;
195
196 GET_FLOAT_WORD(hx,x);
197 ix = 0x7fffffff&hx;
198 /* if Y(n,NaN) is NaN */
199 if(__builtin_expect(ix>0x7f800000, 0)) return x+x;
200 sign = 1;
201 if(n<0){
202 n = -n;
203 sign = 1 - ((n&1)<<1);
204 }
205 if(n==0) return(__ieee754_y0f(x));
206 if(__builtin_expect(ix==0, 0))
207 return -sign/zero;
208 if(__builtin_expect(hx<0, 0)) return zero/(zero*x);
209 SET_RESTORE_ROUNDF (FE_TONEAREST);
210 if(n==1) {
211 ret = sign*__ieee754_y1f(x);
212 goto out;
213 }
214 if(__builtin_expect(ix==0x7f800000, 0)) return zero;
215
216 a = __ieee754_y0f(x);
217 b = __ieee754_y1f(x);
218 /* quit if b is -inf */
219 GET_FLOAT_WORD(ib,b);
220 for(i=1;i<n&&ib!=0xff800000;i++){
221 temp = b;
222 b = ((double)(i+i)/x)*b - a;
223 GET_FLOAT_WORD(ib,b);
224 a = temp;
225 }
226 /* If B is +-Inf, set up errno accordingly. */
227 if (! isfinite (b))
228 __set_errno (ERANGE);
229 if(sign>0) ret = b; else ret = -b;
230 }
231 out:
232 if (isinf (ret))
233 ret = copysignf (FLT_MAX, ret) * FLT_MAX;
234 return ret;
235 }
236 libm_alias_finite (__ieee754_ynf, __ynf)
237