1 | /* origin: FreeBSD /usr/src/lib/msun/src/e_powf.c */ |
2 | /* |
3 | * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. |
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 | use core::cmp::Ordering; |
17 | |
18 | use super::{fabsf, scalbnf, sqrtf}; |
19 | |
20 | const BP: [f32; 2] = [1.0, 1.5]; |
21 | const DP_H: [f32; 2] = [0.0, 5.84960938e-01]; /* 0x3f15c000 */ |
22 | const DP_L: [f32; 2] = [0.0, 1.56322085e-06]; /* 0x35d1cfdc */ |
23 | const TWO24: f32 = 16777216.0; /* 0x4b800000 */ |
24 | const HUGE: f32 = 1.0e30; |
25 | const TINY: f32 = 1.0e-30; |
26 | const L1: f32 = 6.0000002384e-01; /* 0x3f19999a */ |
27 | const L2: f32 = 4.2857143283e-01; /* 0x3edb6db7 */ |
28 | const L3: f32 = 3.3333334327e-01; /* 0x3eaaaaab */ |
29 | const L4: f32 = 2.7272811532e-01; /* 0x3e8ba305 */ |
30 | const L5: f32 = 2.3066075146e-01; /* 0x3e6c3255 */ |
31 | const L6: f32 = 2.0697501302e-01; /* 0x3e53f142 */ |
32 | const P1: f32 = 1.6666667163e-01; /* 0x3e2aaaab */ |
33 | const P2: f32 = -2.7777778450e-03; /* 0xbb360b61 */ |
34 | const P3: f32 = 6.6137559770e-05; /* 0x388ab355 */ |
35 | const P4: f32 = -1.6533901999e-06; /* 0xb5ddea0e */ |
36 | const P5: f32 = 4.1381369442e-08; /* 0x3331bb4c */ |
37 | const LG2: f32 = 6.9314718246e-01; /* 0x3f317218 */ |
38 | const LG2_H: f32 = 6.93145752e-01; /* 0x3f317200 */ |
39 | const LG2_L: f32 = 1.42860654e-06; /* 0x35bfbe8c */ |
40 | const OVT: f32 = 4.2995665694e-08; /* -(128-log2(ovfl+.5ulp)) */ |
41 | const CP: f32 = 9.6179670095e-01; /* 0x3f76384f =2/(3ln2) */ |
42 | const CP_H: f32 = 9.6191406250e-01; /* 0x3f764000 =12b cp */ |
43 | const CP_L: f32 = -1.1736857402e-04; /* 0xb8f623c6 =tail of cp_h */ |
44 | const IVLN2: f32 = 1.4426950216e+00; |
45 | const IVLN2_H: f32 = 1.4426879883e+00; |
46 | const IVLN2_L: f32 = 7.0526075433e-06; |
47 | |
48 | /// Returns `x` to the power of `y` (f32). |
49 | #[cfg_attr (all(test, assert_no_panic), no_panic::no_panic)] |
50 | pub fn powf(x: f32, y: f32) -> f32 { |
51 | let mut z: f32; |
52 | let mut ax: f32; |
53 | let z_h: f32; |
54 | let z_l: f32; |
55 | let mut p_h: f32; |
56 | let mut p_l: f32; |
57 | let y1: f32; |
58 | let mut t1: f32; |
59 | let t2: f32; |
60 | let mut r: f32; |
61 | let s: f32; |
62 | let mut sn: f32; |
63 | let mut t: f32; |
64 | let mut u: f32; |
65 | let mut v: f32; |
66 | let mut w: f32; |
67 | let i: i32; |
68 | let mut j: i32; |
69 | let mut k: i32; |
70 | let mut yisint: i32; |
71 | let mut n: i32; |
72 | let hx: i32; |
73 | let hy: i32; |
74 | let mut ix: i32; |
75 | let iy: i32; |
76 | let mut is: i32; |
77 | |
78 | hx = x.to_bits() as i32; |
79 | hy = y.to_bits() as i32; |
80 | |
81 | ix = hx & 0x7fffffff; |
82 | iy = hy & 0x7fffffff; |
83 | |
84 | /* x**0 = 1, even if x is NaN */ |
85 | if iy == 0 { |
86 | return 1.0; |
87 | } |
88 | |
89 | /* 1**y = 1, even if y is NaN */ |
90 | if hx == 0x3f800000 { |
91 | return 1.0; |
92 | } |
93 | |
94 | /* NaN if either arg is NaN */ |
95 | if ix > 0x7f800000 || iy > 0x7f800000 { |
96 | return x + y; |
97 | } |
98 | |
99 | /* determine if y is an odd int when x < 0 |
100 | * yisint = 0 ... y is not an integer |
101 | * yisint = 1 ... y is an odd int |
102 | * yisint = 2 ... y is an even int |
103 | */ |
104 | yisint = 0; |
105 | if hx < 0 { |
106 | if iy >= 0x4b800000 { |
107 | yisint = 2; /* even integer y */ |
108 | } else if iy >= 0x3f800000 { |
109 | k = (iy >> 23) - 0x7f; /* exponent */ |
110 | j = iy >> (23 - k); |
111 | if (j << (23 - k)) == iy { |
112 | yisint = 2 - (j & 1); |
113 | } |
114 | } |
115 | } |
116 | |
117 | /* special value of y */ |
118 | if iy == 0x7f800000 { |
119 | /* y is +-inf */ |
120 | match ix.cmp(&0x3f800000) { |
121 | /* (-1)**+-inf is 1 */ |
122 | Ordering::Equal => return 1.0, |
123 | /* (|x|>1)**+-inf = inf,0 */ |
124 | Ordering::Greater => return if hy >= 0 { y } else { 0.0 }, |
125 | /* (|x|<1)**+-inf = 0,inf */ |
126 | Ordering::Less => return if hy >= 0 { 0.0 } else { -y }, |
127 | } |
128 | } |
129 | if iy == 0x3f800000 { |
130 | /* y is +-1 */ |
131 | return if hy >= 0 { x } else { 1.0 / x }; |
132 | } |
133 | |
134 | if hy == 0x40000000 { |
135 | /* y is 2 */ |
136 | return x * x; |
137 | } |
138 | |
139 | if hy == 0x3f000000 |
140 | /* y is 0.5 */ |
141 | && hx >= 0 |
142 | { |
143 | /* x >= +0 */ |
144 | return sqrtf(x); |
145 | } |
146 | |
147 | ax = fabsf(x); |
148 | /* special value of x */ |
149 | if ix == 0x7f800000 || ix == 0 || ix == 0x3f800000 { |
150 | /* x is +-0,+-inf,+-1 */ |
151 | z = ax; |
152 | if hy < 0 { |
153 | /* z = (1/|x|) */ |
154 | z = 1.0 / z; |
155 | } |
156 | |
157 | if hx < 0 { |
158 | if ((ix - 0x3f800000) | yisint) == 0 { |
159 | z = (z - z) / (z - z); /* (-1)**non-int is NaN */ |
160 | } else if yisint == 1 { |
161 | z = -z; /* (x<0)**odd = -(|x|**odd) */ |
162 | } |
163 | } |
164 | return z; |
165 | } |
166 | |
167 | sn = 1.0; /* sign of result */ |
168 | if hx < 0 { |
169 | if yisint == 0 { |
170 | /* (x<0)**(non-int) is NaN */ |
171 | return (x - x) / (x - x); |
172 | } |
173 | |
174 | if yisint == 1 { |
175 | /* (x<0)**(odd int) */ |
176 | sn = -1.0; |
177 | } |
178 | } |
179 | |
180 | /* |y| is HUGE */ |
181 | if iy > 0x4d000000 { |
182 | /* if |y| > 2**27 */ |
183 | /* over/underflow if x is not close to one */ |
184 | if ix < 0x3f7ffff8 { |
185 | return if hy < 0 { |
186 | sn * HUGE * HUGE |
187 | } else { |
188 | sn * TINY * TINY |
189 | }; |
190 | } |
191 | |
192 | if ix > 0x3f800007 { |
193 | return if hy > 0 { |
194 | sn * HUGE * HUGE |
195 | } else { |
196 | sn * TINY * TINY |
197 | }; |
198 | } |
199 | |
200 | /* now |1-x| is TINY <= 2**-20, suffice to compute |
201 | log(x) by x-x^2/2+x^3/3-x^4/4 */ |
202 | t = ax - 1.; /* t has 20 trailing zeros */ |
203 | w = (t * t) * (0.5 - t * (0.333333333333 - t * 0.25)); |
204 | u = IVLN2_H * t; /* IVLN2_H has 16 sig. bits */ |
205 | v = t * IVLN2_L - w * IVLN2; |
206 | t1 = u + v; |
207 | is = t1.to_bits() as i32; |
208 | t1 = f32::from_bits(is as u32 & 0xfffff000); |
209 | t2 = v - (t1 - u); |
210 | } else { |
211 | let mut s2: f32; |
212 | let mut s_h: f32; |
213 | let s_l: f32; |
214 | let mut t_h: f32; |
215 | let mut t_l: f32; |
216 | |
217 | n = 0; |
218 | /* take care subnormal number */ |
219 | if ix < 0x00800000 { |
220 | ax *= TWO24; |
221 | n -= 24; |
222 | ix = ax.to_bits() as i32; |
223 | } |
224 | n += ((ix) >> 23) - 0x7f; |
225 | j = ix & 0x007fffff; |
226 | /* determine interval */ |
227 | ix = j | 0x3f800000; /* normalize ix */ |
228 | if j <= 0x1cc471 { |
229 | /* |x|<sqrt(3/2) */ |
230 | k = 0; |
231 | } else if j < 0x5db3d7 { |
232 | /* |x|<sqrt(3) */ |
233 | k = 1; |
234 | } else { |
235 | k = 0; |
236 | n += 1; |
237 | ix -= 0x00800000; |
238 | } |
239 | ax = f32::from_bits(ix as u32); |
240 | |
241 | /* compute s = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */ |
242 | u = ax - i!(BP, k as usize); /* bp[0]=1.0, bp[1]=1.5 */ |
243 | v = 1.0 / (ax + i!(BP, k as usize)); |
244 | s = u * v; |
245 | s_h = s; |
246 | is = s_h.to_bits() as i32; |
247 | s_h = f32::from_bits(is as u32 & 0xfffff000); |
248 | /* t_h=ax+bp[k] High */ |
249 | is = (((ix as u32 >> 1) & 0xfffff000) | 0x20000000) as i32; |
250 | t_h = f32::from_bits(is as u32 + 0x00400000 + ((k as u32) << 21)); |
251 | t_l = ax - (t_h - i!(BP, k as usize)); |
252 | s_l = v * ((u - s_h * t_h) - s_h * t_l); |
253 | /* compute log(ax) */ |
254 | s2 = s * s; |
255 | r = s2 * s2 * (L1 + s2 * (L2 + s2 * (L3 + s2 * (L4 + s2 * (L5 + s2 * L6))))); |
256 | r += s_l * (s_h + s); |
257 | s2 = s_h * s_h; |
258 | t_h = 3.0 + s2 + r; |
259 | is = t_h.to_bits() as i32; |
260 | t_h = f32::from_bits(is as u32 & 0xfffff000); |
261 | t_l = r - ((t_h - 3.0) - s2); |
262 | /* u+v = s*(1+...) */ |
263 | u = s_h * t_h; |
264 | v = s_l * t_h + t_l * s; |
265 | /* 2/(3log2)*(s+...) */ |
266 | p_h = u + v; |
267 | is = p_h.to_bits() as i32; |
268 | p_h = f32::from_bits(is as u32 & 0xfffff000); |
269 | p_l = v - (p_h - u); |
270 | z_h = CP_H * p_h; /* cp_h+cp_l = 2/(3*log2) */ |
271 | z_l = CP_L * p_h + p_l * CP + i!(DP_L, k as usize); |
272 | /* log2(ax) = (s+..)*2/(3*log2) = n + dp_h + z_h + z_l */ |
273 | t = n as f32; |
274 | t1 = ((z_h + z_l) + i!(DP_H, k as usize)) + t; |
275 | is = t1.to_bits() as i32; |
276 | t1 = f32::from_bits(is as u32 & 0xfffff000); |
277 | t2 = z_l - (((t1 - t) - i!(DP_H, k as usize)) - z_h); |
278 | }; |
279 | |
280 | /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */ |
281 | is = y.to_bits() as i32; |
282 | y1 = f32::from_bits(is as u32 & 0xfffff000); |
283 | p_l = (y - y1) * t1 + y * t2; |
284 | p_h = y1 * t1; |
285 | z = p_l + p_h; |
286 | j = z.to_bits() as i32; |
287 | if j > 0x43000000 { |
288 | /* if z > 128 */ |
289 | return sn * HUGE * HUGE; /* overflow */ |
290 | } else if j == 0x43000000 { |
291 | /* if z == 128 */ |
292 | if p_l + OVT > z - p_h { |
293 | return sn * HUGE * HUGE; /* overflow */ |
294 | } |
295 | } else if (j & 0x7fffffff) > 0x43160000 { |
296 | /* z < -150 */ |
297 | // FIXME: check should be (uint32_t)j > 0xc3160000 |
298 | return sn * TINY * TINY; /* underflow */ |
299 | } else if j as u32 == 0xc3160000 |
300 | /* z == -150 */ |
301 | && p_l <= z - p_h |
302 | { |
303 | return sn * TINY * TINY; /* underflow */ |
304 | } |
305 | |
306 | /* |
307 | * compute 2**(p_h+p_l) |
308 | */ |
309 | i = j & 0x7fffffff; |
310 | k = (i >> 23) - 0x7f; |
311 | n = 0; |
312 | if i > 0x3f000000 { |
313 | /* if |z| > 0.5, set n = [z+0.5] */ |
314 | n = j + (0x00800000 >> (k + 1)); |
315 | k = ((n & 0x7fffffff) >> 23) - 0x7f; /* new k for n */ |
316 | t = f32::from_bits(n as u32 & !(0x007fffff >> k)); |
317 | n = ((n & 0x007fffff) | 0x00800000) >> (23 - k); |
318 | if j < 0 { |
319 | n = -n; |
320 | } |
321 | p_h -= t; |
322 | } |
323 | t = p_l + p_h; |
324 | is = t.to_bits() as i32; |
325 | t = f32::from_bits(is as u32 & 0xffff8000); |
326 | u = t * LG2_H; |
327 | v = (p_l - (t - p_h)) * LG2 + t * LG2_L; |
328 | z = u + v; |
329 | w = v - (z - u); |
330 | t = z * z; |
331 | t1 = z - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5)))); |
332 | r = (z * t1) / (t1 - 2.0) - (w + z * w); |
333 | z = 1.0 - (r - z); |
334 | j = z.to_bits() as i32; |
335 | j += n << 23; |
336 | if (j >> 23) <= 0 { |
337 | /* subnormal output */ |
338 | z = scalbnf(z, n); |
339 | } else { |
340 | z = f32::from_bits(j as u32); |
341 | } |
342 | sn * z |
343 | } |
344 | |