Skip to main content

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    #[cfg(feature = "nightly")]
100    {
101        #[cfg(feature = "std")]
102        {
103            std::intrinsics::const_eval_select((x, y), pow_inner, f64::powf)
104        }
105        #[cfg(not(feature = "std"))]
106        {
107            core::intrinsics::const_eval_select((x, y), pow_inner, libm::pow)
108        }
109    }
110    #[cfg(not(feature = "nightly"))]
111    {
112        pow_inner(x, y)
113    }
114}
115
116const fn pow_inner(x: f64, y: f64) -> f64 {
117    let t1: f64;
118    let t2: f64;
119
120    let (hx, lx): (i32, u32) = ((x.to_bits() >> 32) as i32, x.to_bits() as u32);
121    let (hy, ly): (i32, u32) = ((y.to_bits() >> 32) as i32, y.to_bits() as u32);
122
123    let mut ix: i32 = hx & 0x7fffffff_i32;
124    let iy: i32 = hy & 0x7fffffff_i32;
125
126    /* x**0 = 1, even if x is NaN */
127    if ((iy as u32) | ly) == 0 {
128        return 1.0;
129    }
130
131    /* 1**y = 1, even if y is NaN */
132    if hx == 0x3ff00000 && lx == 0 {
133        return 1.0;
134    }
135
136    /* NaN if either arg is NaN */
137    if ix > 0x7ff00000
138        || (ix == 0x7ff00000 && lx != 0)
139        || iy > 0x7ff00000
140        || (iy == 0x7ff00000 && ly != 0)
141    {
142        return x + y;
143    }
144
145    /* determine if y is an odd int when x < 0
146     * yisint = 0       ... y is not an integer
147     * yisint = 1       ... y is an odd int
148     * yisint = 2       ... y is an even int
149     */
150    let mut yisint: i32 = 0;
151    let mut k: i32;
152    let mut j: i32;
153    if hx < 0 {
154        if iy >= 0x43400000 {
155            yisint = 2; /* even integer y */
156        } else if iy >= 0x3ff00000 {
157            k = (iy >> 20) - 0x3ff; /* exponent */
158
159            if k > 20 {
160                j = (ly >> (52 - k)) as i32;
161
162                if (j << (52 - k)) == (ly as i32) {
163                    yisint = 2 - (j & 1);
164                }
165            } else if ly == 0 {
166                j = iy >> (20 - k);
167
168                if (j << (20 - k)) == iy {
169                    yisint = 2 - (j & 1);
170                }
171            }
172        }
173    }
174
175    if ly == 0 {
176        /* special value of y */
177        if iy == 0x7ff00000 {
178            /* y is +-inf */
179
180            return if ((ix - 0x3ff00000) | (lx as i32)) == 0 {
181                /* (-1)**+-inf is 1 */
182                1.0
183            } else if ix >= 0x3ff00000 {
184                /* (|x|>1)**+-inf = inf,0 */
185                if hy >= 0 {
186                    y
187                } else {
188                    0.0
189                }
190            } else {
191                /* (|x|<1)**+-inf = 0,inf */
192                if hy >= 0 {
193                    0.0
194                } else {
195                    -y
196                }
197            };
198        }
199
200        if iy == 0x3ff00000 {
201            /* y is +-1 */
202            return if hy >= 0 { x } else { 1.0 / x };
203        }
204
205        if hy == 0x40000000 {
206            /* y is 2 */
207            return x * x;
208        }
209
210        if hy == 0x3fe00000 {
211            /* y is 0.5 */
212            if hx >= 0 {
213                /* x >= +0 */
214                return sqrt(x);
215            }
216        }
217    }
218
219    let mut ax: f64 = fabs(x);
220    if lx == 0 {
221        /* special value of x */
222        if ix == 0x7ff00000 || ix == 0 || ix == 0x3ff00000 {
223            /* x is +-0,+-inf,+-1 */
224            let mut z: f64 = ax;
225
226            if hy < 0 {
227                /* z = (1/|x|) */
228                z = 1.0 / z;
229            }
230
231            if hx < 0 {
232                if ((ix - 0x3ff00000) | yisint) == 0 {
233                    z = f64::NAN; /* (-1)**non-int is NaN */
234                } else if yisint == 1 {
235                    z = -z; /* (x<0)**odd = -(|x|**odd) */
236                }
237            }
238
239            return z;
240        }
241    }
242
243    let mut s: f64 = 1.0; /* sign of result */
244    if hx < 0 {
245        if yisint == 0 {
246            /* (x<0)**(non-int) is NaN */
247            return f64::NAN;
248        }
249
250        if yisint == 1 {
251            /* (x<0)**(odd int) */
252            s = -1.0;
253        }
254    }
255
256    /* |y| is HUGE */
257    if iy > 0x41e00000 {
258        /* if |y| > 2**31 */
259        if iy > 0x43f00000 {
260            /* if |y| > 2**64, must o/uflow */
261            if ix <= 0x3fefffff {
262                return if hy < 0 { HUGE * HUGE } else { TINY * TINY };
263            }
264
265            if ix >= 0x3ff00000 {
266                return if hy > 0 { HUGE * HUGE } else { TINY * TINY };
267            }
268        }
269
270        /* over/underflow if x is not close to one */
271        if ix < 0x3fefffff {
272            return if hy < 0 {
273                s * HUGE * HUGE
274            } else {
275                s * TINY * TINY
276            };
277        }
278        if ix > 0x3ff00000 {
279            return if hy > 0 {
280                s * HUGE * HUGE
281            } else {
282                s * TINY * TINY
283            };
284        }
285
286        /* now |1-x| is TINY <= 2**-20, suffice to compute
287        log(x) by x-x^2/2+x^3/3-x^4/4 */
288        let t: f64 = ax - 1.0; /* t has 20 trailing zeros */
289        let w: f64 = (t * t) * (0.5 - t * (0.3333333333333333333333 - t * 0.25));
290        let u: f64 = IVLN2_H * t; /* ivln2_h has 21 sig. bits */
291        let v: f64 = t * IVLN2_L - w * IVLN2;
292        t1 = with_set_low_word(u + v, 0);
293        t2 = v - (t1 - u);
294    } else {
295        // double ss,s2,s_h,s_l,t_h,t_l;
296        let mut n: i32 = 0;
297
298        if ix < 0x00100000 {
299            /* take care subnormal number */
300            ax *= TWO53;
301            n -= 53;
302            ix = get_high_word(ax) as i32;
303        }
304
305        n += (ix >> 20) - 0x3ff;
306        j = ix & 0x000fffff;
307
308        /* determine interval */
309        let k: i32;
310        ix = j | 0x3ff00000; /* normalize ix */
311        if j <= 0x3988E {
312            /* |x|<sqrt(3/2) */
313            k = 0;
314        } else if j < 0xBB67A {
315            /* |x|<sqrt(3)   */
316            k = 1;
317        } else {
318            k = 0;
319            n += 1;
320            ix -= 0x00100000;
321        }
322        ax = with_set_high_word(ax, ix as u32);
323
324        /* compute ss = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */
325        let u: f64 = ax - BP[k as usize]; /* bp[0]=1.0, bp[1]=1.5 */
326        let v: f64 = 1.0 / (ax + BP[k as usize]);
327        let ss: f64 = u * v;
328        let s_h = with_set_low_word(ss, 0);
329
330        /* t_h=ax+bp[k] High */
331        let t_h: f64 = with_set_high_word(
332            0.0,
333            ((ix as u32 >> 1) | 0x20000000) + 0x00080000 + ((k as u32) << 18),
334        );
335        let t_l: f64 = ax - (t_h - BP[k as usize]);
336        let s_l: f64 = v * ((u - s_h * t_h) - s_h * t_l);
337
338        /* compute log(ax) */
339        let s2: f64 = ss * ss;
340        let mut r: f64 = s2 * s2 * (L1 + s2 * (L2 + s2 * (L3 + s2 * (L4 + s2 * (L5 + s2 * L6)))));
341        r += s_l * (s_h + ss);
342        let s2: f64 = s_h * s_h;
343        let t_h: f64 = with_set_low_word(3.0 + s2 + r, 0);
344        let t_l: f64 = r - ((t_h - 3.0) - s2);
345
346        /* u+v = ss*(1+...) */
347        let u: f64 = s_h * t_h;
348        let v: f64 = s_l * t_h + t_l * ss;
349
350        /* 2/(3log2)*(ss+...) */
351        let p_h: f64 = with_set_low_word(u + v, 0);
352        let p_l = v - (p_h - u);
353        let z_h: f64 = CP_H * p_h; /* cp_h+cp_l = 2/(3*log2) */
354        let z_l: f64 = CP_L * p_h + p_l * CP + DP_L[k as usize];
355
356        /* log2(ax) = (ss+..)*2/(3*log2) = n + dp_h + z_h + z_l */
357        let t: f64 = n as f64;
358        t1 = with_set_low_word(((z_h + z_l) + DP_H[k as usize]) + t, 0);
359        t2 = z_l - (((t1 - t) - DP_H[k as usize]) - z_h);
360    }
361
362    /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */
363    let y1: f64 = with_set_low_word(y, 0);
364    let p_l: f64 = (y - y1) * t1 + y * t2;
365    let mut p_h: f64 = y1 * t1;
366    let z: f64 = p_l + p_h;
367    let mut j: i32 = (z.to_bits() >> 32) as i32;
368    let i: i32 = z.to_bits() as i32;
369    // let (j, i): (i32, i32) = ((z.to_bits() >> 32) as i32, z.to_bits() as i32);
370
371    if j >= 0x40900000 {
372        /* z >= 1024 */
373        if (j - 0x40900000) | i != 0 {
374            /* if z > 1024 */
375            return s * HUGE * HUGE; /* overflow */
376        }
377
378        if p_l + OVT > z - p_h {
379            return s * HUGE * HUGE; /* overflow */
380        }
381    } else if (j & 0x7fffffff) >= 0x4090cc00 {
382        /* z <= -1075 */
383        // FIXME: instead of abs(j) use unsigned j
384
385        if (((j as u32) - 0xc090cc00) | (i as u32)) != 0 {
386            /* z < -1075 */
387            return s * TINY * TINY; /* underflow */
388        }
389
390        if p_l <= z - p_h {
391            return s * TINY * TINY; /* underflow */
392        }
393    }
394
395    /* compute 2**(p_h+p_l) */
396    let i: i32 = j & 0x7fffffff_i32;
397    k = (i >> 20) - 0x3ff;
398    let mut n: i32 = 0;
399
400    if i > 0x3fe00000 {
401        /* if |z| > 0.5, set n = [z+0.5] */
402        n = j + (0x00100000 >> (k + 1));
403        k = ((n & 0x7fffffff) >> 20) - 0x3ff; /* new k for n */
404        let t: f64 = with_set_high_word(0.0, (n & !(0x000fffff >> k)) as u32);
405        n = ((n & 0x000fffff) | 0x00100000) >> (20 - k);
406        if j < 0 {
407            n = -n;
408        }
409        p_h -= t;
410    }
411
412    let t: f64 = with_set_low_word(p_l + p_h, 0);
413    let u: f64 = t * LG2_H;
414    let v: f64 = (p_l - (t - p_h)) * LG2 + t * LG2_L;
415    let mut z: f64 = u + v;
416    let w: f64 = v - (z - u);
417    let t: f64 = z * z;
418    let t1: f64 = z - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5))));
419    let r: f64 = (z * t1) / (t1 - 2.0) - (w + z * w);
420    z = 1.0 - (r - z);
421    j = get_high_word(z) as i32;
422    j += n << 20;
423
424    if (j >> 20) <= 0 {
425        /* subnormal output */
426        z = scalbn(z, n);
427    } else {
428        z = with_set_high_word(z, j as u32);
429    }
430
431    s * z
432}