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