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