trig_const/pow.rs
1/* origin: FreeBSD /usr/src/lib/msun/src/e_pow.c */
2/*
3 * ====================================================
4 * Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved.
5 *
6 * Permission to use, copy, modify, and distribute this
7 * software is freely granted, provided that this notice
8 * is preserved.
9 * ====================================================
10 */
11
12// pow(x,y) return x**y
13//
14// n
15// Method: Let x = 2 * (1+f)
16// 1. Compute and return log2(x) in two pieces:
17// log2(x) = w1 + w2,
18// where w1 has 53-24 = 29 bit trailing zeros.
19// 2. Perform y*log2(x) = n+y' by simulating multi-precision
20// arithmetic, where |y'|<=0.5.
21// 3. Return x**y = 2**n*exp(y'*log2)
22//
23// Special cases:
24// 1. (anything) ** 0 is 1
25// 2. 1 ** (anything) is 1
26// 3. (anything except 1) ** NAN is NAN
27// 4. NAN ** (anything except 0) is NAN
28// 5. +-(|x| > 1) ** +INF is +INF
29// 6. +-(|x| > 1) ** -INF is +0
30// 7. +-(|x| < 1) ** +INF is +0
31// 8. +-(|x| < 1) ** -INF is +INF
32// 9. -1 ** +-INF is 1
33// 10. +0 ** (+anything except 0, NAN) is +0
34// 11. -0 ** (+anything except 0, NAN, odd integer) is +0
35// 12. +0 ** (-anything except 0, NAN) is +INF, raise divbyzero
36// 13. -0 ** (-anything except 0, NAN, odd integer) is +INF, raise divbyzero
37// 14. -0 ** (+odd integer) is -0
38// 15. -0 ** (-odd integer) is -INF, raise divbyzero
39// 16. +INF ** (+anything except 0,NAN) is +INF
40// 17. +INF ** (-anything except 0,NAN) is +0
41// 18. -INF ** (+odd integer) is -INF
42// 19. -INF ** (anything) = -0 ** (-anything), (anything except odd integer)
43// 20. (anything) ** 1 is (anything)
44// 21. (anything) ** -1 is 1/(anything)
45// 22. (-anything) ** (integer) is (-1)**(integer)*(+anything**integer)
46// 23. (-anything except 0 and inf) ** (non-integer) is NAN
47//
48// Accuracy:
49// pow(x,y) returns x**y nearly rounded. In particular
50// pow(integer,integer)
51// always returns the correct integer provided it is
52// representable.
53//
54// Constants :
55// The hexadecimal values are the intended ones for the following
56// constants. The decimal values may be used, provided that the
57// compiler will convert from decimal to binary accurately enough
58// to produce the hexadecimal values shown.
59//
60use crate::{fabs, get_high_word, scalbn::scalbn, sqrt, with_set_high_word, with_set_low_word};
61
62const BP: [f64; 2] = [1.0, 1.5];
63const DP_H: [f64; 2] = [0.0, 5.84962487220764160156e-01]; /* 0x3fe2b803_40000000 */
64const DP_L: [f64; 2] = [0.0, 1.35003920212974897128e-08]; /* 0x3E4CFDEB, 0x43CFD006 */
65const TWO53: f64 = 9007199254740992.0; /* 0x43400000_00000000 */
66const HUGE: f64 = 1.0e300;
67const TINY: f64 = 1.0e-300;
68// poly coefs for (3/2)*(log(x)-2s-2/3*s**3:
69const L1: f64 = 5.99999999999994648725e-01; /* 0x3fe33333_33333303 */
70const L2: f64 = 4.28571428578550184252e-01; /* 0x3fdb6db6_db6fabff */
71const L3: f64 = 3.33333329818377432918e-01; /* 0x3fd55555_518f264d */
72const L4: f64 = 2.72728123808534006489e-01; /* 0x3fd17460_a91d4101 */
73const L5: f64 = 2.30660745775561754067e-01; /* 0x3fcd864a_93c9db65 */
74const L6: f64 = 2.06975017800338417784e-01; /* 0x3fca7e28_4a454eef */
75const P1: f64 = 1.66666666666666019037e-01; /* 0x3fc55555_5555553e */
76const P2: f64 = -2.77777777770155933842e-03; /* 0xbf66c16c_16bebd93 */
77const P3: f64 = 6.61375632143793436117e-05; /* 0x3f11566a_af25de2c */
78const P4: f64 = -1.65339022054652515390e-06; /* 0xbebbbd41_c5d26bf1 */
79const P5: f64 = 4.13813679705723846039e-08; /* 0x3e663769_72bea4d0 */
80const LG2: f64 = 6.93147180559945286227e-01; /* 0x3fe62e42_fefa39ef */
81const LG2_H: f64 = 6.93147182464599609375e-01; /* 0x3fe62e43_00000000 */
82const LG2_L: f64 = -1.90465429995776804525e-09; /* 0xbe205c61_0ca86c39 */
83const OVT: f64 = 8.0085662595372944372e-017; /* -(1024-log2(ovfl+.5ulp)) */
84const CP: f64 = 9.61796693925975554329e-01; /* 0x3feec709_dc3a03fd =2/(3ln2) */
85const CP_H: f64 = 9.61796700954437255859e-01; /* 0x3feec709_e0000000 =(float)cp */
86const CP_L: f64 = -7.02846165095275826516e-09; /* 0xbe3e2fe0_145b01f5 =tail of cp_h*/
87const IVLN2: f64 = 1.44269504088896338700e+00; /* 0x3ff71547_652b82fe =1/ln2 */
88const IVLN2_H: f64 = 1.44269502162933349609e+00; /* 0x3ff71547_60000000 =24b 1/ln2*/
89const IVLN2_L: f64 = 1.92596299112661746887e-08; /* 0x3e54ae0b_f85ddf44 =1/ln2 tail*/
90
91/// Power function
92///
93/// ```
94/// # use trig_const::pow;
95/// let _a = pow(10.0, 2.5);
96/// assert_eq!(pow(10.0, 2.0), 100.0);
97/// ```
98pub const fn pow(x: f64, y: f64) -> f64 {
99 let t1: f64;
100 let t2: f64;
101
102 let (hx, lx): (i32, u32) = ((x.to_bits() >> 32) as i32, x.to_bits() as u32);
103 let (hy, ly): (i32, u32) = ((y.to_bits() >> 32) as i32, y.to_bits() as u32);
104
105 let mut ix: i32 = hx & 0x7fffffff_i32;
106 let iy: i32 = hy & 0x7fffffff_i32;
107
108 /* x**0 = 1, even if x is NaN */
109 if ((iy as u32) | ly) == 0 {
110 return 1.0;
111 }
112
113 /* 1**y = 1, even if y is NaN */
114 if hx == 0x3ff00000 && lx == 0 {
115 return 1.0;
116 }
117
118 /* NaN if either arg is NaN */
119 if ix > 0x7ff00000
120 || (ix == 0x7ff00000 && lx != 0)
121 || iy > 0x7ff00000
122 || (iy == 0x7ff00000 && ly != 0)
123 {
124 return x + y;
125 }
126
127 /* determine if y is an odd int when x < 0
128 * yisint = 0 ... y is not an integer
129 * yisint = 1 ... y is an odd int
130 * yisint = 2 ... y is an even int
131 */
132 let mut yisint: i32 = 0;
133 let mut k: i32;
134 let mut j: i32;
135 if hx < 0 {
136 if iy >= 0x43400000 {
137 yisint = 2; /* even integer y */
138 } else if iy >= 0x3ff00000 {
139 k = (iy >> 20) - 0x3ff; /* exponent */
140
141 if k > 20 {
142 j = (ly >> (52 - k)) as i32;
143
144 if (j << (52 - k)) == (ly as i32) {
145 yisint = 2 - (j & 1);
146 }
147 } else if ly == 0 {
148 j = iy >> (20 - k);
149
150 if (j << (20 - k)) == iy {
151 yisint = 2 - (j & 1);
152 }
153 }
154 }
155 }
156
157 if ly == 0 {
158 /* special value of y */
159 if iy == 0x7ff00000 {
160 /* y is +-inf */
161
162 return if ((ix - 0x3ff00000) | (lx as i32)) == 0 {
163 /* (-1)**+-inf is 1 */
164 1.0
165 } else if ix >= 0x3ff00000 {
166 /* (|x|>1)**+-inf = inf,0 */
167 if hy >= 0 {
168 y
169 } else {
170 0.0
171 }
172 } else {
173 /* (|x|<1)**+-inf = 0,inf */
174 if hy >= 0 {
175 0.0
176 } else {
177 -y
178 }
179 };
180 }
181
182 if iy == 0x3ff00000 {
183 /* y is +-1 */
184 return if hy >= 0 { x } else { 1.0 / x };
185 }
186
187 if hy == 0x40000000 {
188 /* y is 2 */
189 return x * x;
190 }
191
192 if hy == 0x3fe00000 {
193 /* y is 0.5 */
194 if hx >= 0 {
195 /* x >= +0 */
196 return sqrt(x);
197 }
198 }
199 }
200
201 let mut ax: f64 = fabs(x);
202 if lx == 0 {
203 /* special value of x */
204 if ix == 0x7ff00000 || ix == 0 || ix == 0x3ff00000 {
205 /* x is +-0,+-inf,+-1 */
206 let mut z: f64 = ax;
207
208 if hy < 0 {
209 /* z = (1/|x|) */
210 z = 1.0 / z;
211 }
212
213 if hx < 0 {
214 if ((ix - 0x3ff00000) | yisint) == 0 {
215 z = f64::NAN; /* (-1)**non-int is NaN */
216 } else if yisint == 1 {
217 z = -z; /* (x<0)**odd = -(|x|**odd) */
218 }
219 }
220
221 return z;
222 }
223 }
224
225 let mut s: f64 = 1.0; /* sign of result */
226 if hx < 0 {
227 if yisint == 0 {
228 /* (x<0)**(non-int) is NaN */
229 return f64::NAN;
230 }
231
232 if yisint == 1 {
233 /* (x<0)**(odd int) */
234 s = -1.0;
235 }
236 }
237
238 /* |y| is HUGE */
239 if iy > 0x41e00000 {
240 /* if |y| > 2**31 */
241 if iy > 0x43f00000 {
242 /* if |y| > 2**64, must o/uflow */
243 if ix <= 0x3fefffff {
244 return if hy < 0 { HUGE * HUGE } else { TINY * TINY };
245 }
246
247 if ix >= 0x3ff00000 {
248 return if hy > 0 { HUGE * HUGE } else { TINY * TINY };
249 }
250 }
251
252 /* over/underflow if x is not close to one */
253 if ix < 0x3fefffff {
254 return if hy < 0 {
255 s * HUGE * HUGE
256 } else {
257 s * TINY * TINY
258 };
259 }
260 if ix > 0x3ff00000 {
261 return if hy > 0 {
262 s * HUGE * HUGE
263 } else {
264 s * TINY * TINY
265 };
266 }
267
268 /* now |1-x| is TINY <= 2**-20, suffice to compute
269 log(x) by x-x^2/2+x^3/3-x^4/4 */
270 let t: f64 = ax - 1.0; /* t has 20 trailing zeros */
271 let w: f64 = (t * t) * (0.5 - t * (0.3333333333333333333333 - t * 0.25));
272 let u: f64 = IVLN2_H * t; /* ivln2_h has 21 sig. bits */
273 let v: f64 = t * IVLN2_L - w * IVLN2;
274 t1 = with_set_low_word(u + v, 0);
275 t2 = v - (t1 - u);
276 } else {
277 // double ss,s2,s_h,s_l,t_h,t_l;
278 let mut n: i32 = 0;
279
280 if ix < 0x00100000 {
281 /* take care subnormal number */
282 ax *= TWO53;
283 n -= 53;
284 ix = get_high_word(ax) as i32;
285 }
286
287 n += (ix >> 20) - 0x3ff;
288 j = ix & 0x000fffff;
289
290 /* determine interval */
291 let k: i32;
292 ix = j | 0x3ff00000; /* normalize ix */
293 if j <= 0x3988E {
294 /* |x|<sqrt(3/2) */
295 k = 0;
296 } else if j < 0xBB67A {
297 /* |x|<sqrt(3) */
298 k = 1;
299 } else {
300 k = 0;
301 n += 1;
302 ix -= 0x00100000;
303 }
304 ax = with_set_high_word(ax, ix as u32);
305
306 /* compute ss = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */
307 let u: f64 = ax - BP[k as usize]; /* bp[0]=1.0, bp[1]=1.5 */
308 let v: f64 = 1.0 / (ax + BP[k as usize]);
309 let ss: f64 = u * v;
310 let s_h = with_set_low_word(ss, 0);
311
312 /* t_h=ax+bp[k] High */
313 let t_h: f64 = with_set_high_word(
314 0.0,
315 ((ix as u32 >> 1) | 0x20000000) + 0x00080000 + ((k as u32) << 18),
316 );
317 let t_l: f64 = ax - (t_h - BP[k as usize]);
318 let s_l: f64 = v * ((u - s_h * t_h) - s_h * t_l);
319
320 /* compute log(ax) */
321 let s2: f64 = ss * ss;
322 let mut r: f64 = s2 * s2 * (L1 + s2 * (L2 + s2 * (L3 + s2 * (L4 + s2 * (L5 + s2 * L6)))));
323 r += s_l * (s_h + ss);
324 let s2: f64 = s_h * s_h;
325 let t_h: f64 = with_set_low_word(3.0 + s2 + r, 0);
326 let t_l: f64 = r - ((t_h - 3.0) - s2);
327
328 /* u+v = ss*(1+...) */
329 let u: f64 = s_h * t_h;
330 let v: f64 = s_l * t_h + t_l * ss;
331
332 /* 2/(3log2)*(ss+...) */
333 let p_h: f64 = with_set_low_word(u + v, 0);
334 let p_l = v - (p_h - u);
335 let z_h: f64 = CP_H * p_h; /* cp_h+cp_l = 2/(3*log2) */
336 let z_l: f64 = CP_L * p_h + p_l * CP + DP_L[k as usize];
337
338 /* log2(ax) = (ss+..)*2/(3*log2) = n + dp_h + z_h + z_l */
339 let t: f64 = n as f64;
340 t1 = with_set_low_word(((z_h + z_l) + DP_H[k as usize]) + t, 0);
341 t2 = z_l - (((t1 - t) - DP_H[k as usize]) - z_h);
342 }
343
344 /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */
345 let y1: f64 = with_set_low_word(y, 0);
346 let p_l: f64 = (y - y1) * t1 + y * t2;
347 let mut p_h: f64 = y1 * t1;
348 let z: f64 = p_l + p_h;
349 let mut j: i32 = (z.to_bits() >> 32) as i32;
350 let i: i32 = z.to_bits() as i32;
351 // let (j, i): (i32, i32) = ((z.to_bits() >> 32) as i32, z.to_bits() as i32);
352
353 if j >= 0x40900000 {
354 /* z >= 1024 */
355 if (j - 0x40900000) | i != 0 {
356 /* if z > 1024 */
357 return s * HUGE * HUGE; /* overflow */
358 }
359
360 if p_l + OVT > z - p_h {
361 return s * HUGE * HUGE; /* overflow */
362 }
363 } else if (j & 0x7fffffff) >= 0x4090cc00 {
364 /* z <= -1075 */
365 // FIXME: instead of abs(j) use unsigned j
366
367 if (((j as u32) - 0xc090cc00) | (i as u32)) != 0 {
368 /* z < -1075 */
369 return s * TINY * TINY; /* underflow */
370 }
371
372 if p_l <= z - p_h {
373 return s * TINY * TINY; /* underflow */
374 }
375 }
376
377 /* compute 2**(p_h+p_l) */
378 let i: i32 = j & 0x7fffffff_i32;
379 k = (i >> 20) - 0x3ff;
380 let mut n: i32 = 0;
381
382 if i > 0x3fe00000 {
383 /* if |z| > 0.5, set n = [z+0.5] */
384 n = j + (0x00100000 >> (k + 1));
385 k = ((n & 0x7fffffff) >> 20) - 0x3ff; /* new k for n */
386 let t: f64 = with_set_high_word(0.0, (n & !(0x000fffff >> k)) as u32);
387 n = ((n & 0x000fffff) | 0x00100000) >> (20 - k);
388 if j < 0 {
389 n = -n;
390 }
391 p_h -= t;
392 }
393
394 let t: f64 = with_set_low_word(p_l + p_h, 0);
395 let u: f64 = t * LG2_H;
396 let v: f64 = (p_l - (t - p_h)) * LG2 + t * LG2_L;
397 let mut z: f64 = u + v;
398 let w: f64 = v - (z - u);
399 let t: f64 = z * z;
400 let t1: f64 = z - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5))));
401 let r: f64 = (z * t1) / (t1 - 2.0) - (w + z * w);
402 z = 1.0 - (r - z);
403 j = get_high_word(z) as i32;
404 j += n << 20;
405
406 if (j >> 20) <= 0 {
407 /* subnormal output */
408 z = scalbn(z, n);
409 } else {
410 z = with_set_high_word(z, j as u32);
411 }
412
413 s * z
414}