1cbd624adSopenharmony_ci//! A small number of math routines for floats and doubles.
2cbd624adSopenharmony_ci//!
3cbd624adSopenharmony_ci//! These are adapted from libm, a port of musl libc's libm to Rust.
4cbd624adSopenharmony_ci//! libm can be found online [here](https://github.com/rust-lang/libm),
5cbd624adSopenharmony_ci//! and is similarly licensed under an Apache2.0/MIT license
6cbd624adSopenharmony_ci
7cbd624adSopenharmony_ci#![cfg(all(not(feature = "std"), feature = "compact"))]
8cbd624adSopenharmony_ci#![doc(hidden)]
9cbd624adSopenharmony_ci
10cbd624adSopenharmony_ci/* origin: FreeBSD /usr/src/lib/msun/src/e_powf.c */
11cbd624adSopenharmony_ci/*
12cbd624adSopenharmony_ci * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
13cbd624adSopenharmony_ci */
14cbd624adSopenharmony_ci/*
15cbd624adSopenharmony_ci * ====================================================
16cbd624adSopenharmony_ci * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
17cbd624adSopenharmony_ci *
18cbd624adSopenharmony_ci * Developed at SunPro, a Sun Microsystems, Inc. business.
19cbd624adSopenharmony_ci * Permission to use, copy, modify, and distribute this
20cbd624adSopenharmony_ci * software is freely granted, provided that this notice
21cbd624adSopenharmony_ci * is preserved.
22cbd624adSopenharmony_ci * ====================================================
23cbd624adSopenharmony_ci */
24cbd624adSopenharmony_ci
25cbd624adSopenharmony_ci/// # Safety
26cbd624adSopenharmony_ci///
27cbd624adSopenharmony_ci/// Safe if `index < array.len()`.
28cbd624adSopenharmony_cimacro_rules! i {
29cbd624adSopenharmony_ci    ($array:ident, $index:expr) => {
30cbd624adSopenharmony_ci        // SAFETY: safe if `index < array.len()`.
31cbd624adSopenharmony_ci        unsafe { *$array.get_unchecked($index) }
32cbd624adSopenharmony_ci    };
33cbd624adSopenharmony_ci}
34cbd624adSopenharmony_ci
35cbd624adSopenharmony_cipub fn powf(x: f32, y: f32) -> f32 {
36cbd624adSopenharmony_ci    const BP: [f32; 2] = [1.0, 1.5];
37cbd624adSopenharmony_ci    const DP_H: [f32; 2] = [0.0, 5.84960938e-01]; /* 0x3f15c000 */
38cbd624adSopenharmony_ci    const DP_L: [f32; 2] = [0.0, 1.56322085e-06]; /* 0x35d1cfdc */
39cbd624adSopenharmony_ci    const TWO24: f32 = 16777216.0; /* 0x4b800000 */
40cbd624adSopenharmony_ci    const HUGE: f32 = 1.0e30;
41cbd624adSopenharmony_ci    const TINY: f32 = 1.0e-30;
42cbd624adSopenharmony_ci    const L1: f32 = 6.0000002384e-01; /* 0x3f19999a */
43cbd624adSopenharmony_ci    const L2: f32 = 4.2857143283e-01; /* 0x3edb6db7 */
44cbd624adSopenharmony_ci    const L3: f32 = 3.3333334327e-01; /* 0x3eaaaaab */
45cbd624adSopenharmony_ci    const L4: f32 = 2.7272811532e-01; /* 0x3e8ba305 */
46cbd624adSopenharmony_ci    const L5: f32 = 2.3066075146e-01; /* 0x3e6c3255 */
47cbd624adSopenharmony_ci    const L6: f32 = 2.0697501302e-01; /* 0x3e53f142 */
48cbd624adSopenharmony_ci    const P1: f32 = 1.6666667163e-01; /* 0x3e2aaaab */
49cbd624adSopenharmony_ci    const P2: f32 = -2.7777778450e-03; /* 0xbb360b61 */
50cbd624adSopenharmony_ci    const P3: f32 = 6.6137559770e-05; /* 0x388ab355 */
51cbd624adSopenharmony_ci    const P4: f32 = -1.6533901999e-06; /* 0xb5ddea0e */
52cbd624adSopenharmony_ci    const P5: f32 = 4.1381369442e-08; /* 0x3331bb4c */
53cbd624adSopenharmony_ci    const LG2: f32 = 6.9314718246e-01; /* 0x3f317218 */
54cbd624adSopenharmony_ci    const LG2_H: f32 = 6.93145752e-01; /* 0x3f317200 */
55cbd624adSopenharmony_ci    const LG2_L: f32 = 1.42860654e-06; /* 0x35bfbe8c */
56cbd624adSopenharmony_ci    const OVT: f32 = 4.2995665694e-08; /* -(128-log2(ovfl+.5ulp)) */
57cbd624adSopenharmony_ci    const CP: f32 = 9.6179670095e-01; /* 0x3f76384f =2/(3ln2) */
58cbd624adSopenharmony_ci    const CP_H: f32 = 9.6191406250e-01; /* 0x3f764000 =12b cp */
59cbd624adSopenharmony_ci    const CP_L: f32 = -1.1736857402e-04; /* 0xb8f623c6 =tail of cp_h */
60cbd624adSopenharmony_ci    const IVLN2: f32 = 1.4426950216e+00;
61cbd624adSopenharmony_ci    const IVLN2_H: f32 = 1.4426879883e+00;
62cbd624adSopenharmony_ci    const IVLN2_L: f32 = 7.0526075433e-06;
63cbd624adSopenharmony_ci
64cbd624adSopenharmony_ci    let mut z: f32;
65cbd624adSopenharmony_ci    let mut ax: f32;
66cbd624adSopenharmony_ci    let z_h: f32;
67cbd624adSopenharmony_ci    let z_l: f32;
68cbd624adSopenharmony_ci    let mut p_h: f32;
69cbd624adSopenharmony_ci    let mut p_l: f32;
70cbd624adSopenharmony_ci    let y1: f32;
71cbd624adSopenharmony_ci    let mut t1: f32;
72cbd624adSopenharmony_ci    let t2: f32;
73cbd624adSopenharmony_ci    let mut r: f32;
74cbd624adSopenharmony_ci    let s: f32;
75cbd624adSopenharmony_ci    let mut sn: f32;
76cbd624adSopenharmony_ci    let mut t: f32;
77cbd624adSopenharmony_ci    let mut u: f32;
78cbd624adSopenharmony_ci    let mut v: f32;
79cbd624adSopenharmony_ci    let mut w: f32;
80cbd624adSopenharmony_ci    let i: i32;
81cbd624adSopenharmony_ci    let mut j: i32;
82cbd624adSopenharmony_ci    let mut k: i32;
83cbd624adSopenharmony_ci    let mut yisint: i32;
84cbd624adSopenharmony_ci    let mut n: i32;
85cbd624adSopenharmony_ci    let hx: i32;
86cbd624adSopenharmony_ci    let hy: i32;
87cbd624adSopenharmony_ci    let mut ix: i32;
88cbd624adSopenharmony_ci    let iy: i32;
89cbd624adSopenharmony_ci    let mut is: i32;
90cbd624adSopenharmony_ci
91cbd624adSopenharmony_ci    hx = x.to_bits() as i32;
92cbd624adSopenharmony_ci    hy = y.to_bits() as i32;
93cbd624adSopenharmony_ci
94cbd624adSopenharmony_ci    ix = hx & 0x7fffffff;
95cbd624adSopenharmony_ci    iy = hy & 0x7fffffff;
96cbd624adSopenharmony_ci
97cbd624adSopenharmony_ci    /* x**0 = 1, even if x is NaN */
98cbd624adSopenharmony_ci    if iy == 0 {
99cbd624adSopenharmony_ci        return 1.0;
100cbd624adSopenharmony_ci    }
101cbd624adSopenharmony_ci
102cbd624adSopenharmony_ci    /* 1**y = 1, even if y is NaN */
103cbd624adSopenharmony_ci    if hx == 0x3f800000 {
104cbd624adSopenharmony_ci        return 1.0;
105cbd624adSopenharmony_ci    }
106cbd624adSopenharmony_ci
107cbd624adSopenharmony_ci    /* NaN if either arg is NaN */
108cbd624adSopenharmony_ci    if ix > 0x7f800000 || iy > 0x7f800000 {
109cbd624adSopenharmony_ci        return x + y;
110cbd624adSopenharmony_ci    }
111cbd624adSopenharmony_ci
112cbd624adSopenharmony_ci    /* determine if y is an odd int when x < 0
113cbd624adSopenharmony_ci     * yisint = 0       ... y is not an integer
114cbd624adSopenharmony_ci     * yisint = 1       ... y is an odd int
115cbd624adSopenharmony_ci     * yisint = 2       ... y is an even int
116cbd624adSopenharmony_ci     */
117cbd624adSopenharmony_ci    yisint = 0;
118cbd624adSopenharmony_ci    if hx < 0 {
119cbd624adSopenharmony_ci        if iy >= 0x4b800000 {
120cbd624adSopenharmony_ci            yisint = 2; /* even integer y */
121cbd624adSopenharmony_ci        } else if iy >= 0x3f800000 {
122cbd624adSopenharmony_ci            k = (iy >> 23) - 0x7f; /* exponent */
123cbd624adSopenharmony_ci            j = iy >> (23 - k);
124cbd624adSopenharmony_ci            if (j << (23 - k)) == iy {
125cbd624adSopenharmony_ci                yisint = 2 - (j & 1);
126cbd624adSopenharmony_ci            }
127cbd624adSopenharmony_ci        }
128cbd624adSopenharmony_ci    }
129cbd624adSopenharmony_ci
130cbd624adSopenharmony_ci    /* special value of y */
131cbd624adSopenharmony_ci    if iy == 0x7f800000 {
132cbd624adSopenharmony_ci        /* y is +-inf */
133cbd624adSopenharmony_ci        if ix == 0x3f800000 {
134cbd624adSopenharmony_ci            /* (-1)**+-inf is 1 */
135cbd624adSopenharmony_ci            return 1.0;
136cbd624adSopenharmony_ci        } else if ix > 0x3f800000 {
137cbd624adSopenharmony_ci            /* (|x|>1)**+-inf = inf,0 */
138cbd624adSopenharmony_ci            return if hy >= 0 {
139cbd624adSopenharmony_ci                y
140cbd624adSopenharmony_ci            } else {
141cbd624adSopenharmony_ci                0.0
142cbd624adSopenharmony_ci            };
143cbd624adSopenharmony_ci        } else {
144cbd624adSopenharmony_ci            /* (|x|<1)**+-inf = 0,inf */
145cbd624adSopenharmony_ci            return if hy >= 0 {
146cbd624adSopenharmony_ci                0.0
147cbd624adSopenharmony_ci            } else {
148cbd624adSopenharmony_ci                -y
149cbd624adSopenharmony_ci            };
150cbd624adSopenharmony_ci        }
151cbd624adSopenharmony_ci    }
152cbd624adSopenharmony_ci    if iy == 0x3f800000 {
153cbd624adSopenharmony_ci        /* y is +-1 */
154cbd624adSopenharmony_ci        return if hy >= 0 {
155cbd624adSopenharmony_ci            x
156cbd624adSopenharmony_ci        } else {
157cbd624adSopenharmony_ci            1.0 / x
158cbd624adSopenharmony_ci        };
159cbd624adSopenharmony_ci    }
160cbd624adSopenharmony_ci
161cbd624adSopenharmony_ci    if hy == 0x40000000 {
162cbd624adSopenharmony_ci        /* y is 2 */
163cbd624adSopenharmony_ci        return x * x;
164cbd624adSopenharmony_ci    }
165cbd624adSopenharmony_ci
166cbd624adSopenharmony_ci    if hy == 0x3f000000
167cbd624adSopenharmony_ci       /* y is  0.5 */
168cbd624adSopenharmony_ci       && hx >= 0
169cbd624adSopenharmony_ci    {
170cbd624adSopenharmony_ci        /* x >= +0 */
171cbd624adSopenharmony_ci        return sqrtf(x);
172cbd624adSopenharmony_ci    }
173cbd624adSopenharmony_ci
174cbd624adSopenharmony_ci    ax = fabsf(x);
175cbd624adSopenharmony_ci    /* special value of x */
176cbd624adSopenharmony_ci    if ix == 0x7f800000 || ix == 0 || ix == 0x3f800000 {
177cbd624adSopenharmony_ci        /* x is +-0,+-inf,+-1 */
178cbd624adSopenharmony_ci        z = ax;
179cbd624adSopenharmony_ci        if hy < 0 {
180cbd624adSopenharmony_ci            /* z = (1/|x|) */
181cbd624adSopenharmony_ci            z = 1.0 / z;
182cbd624adSopenharmony_ci        }
183cbd624adSopenharmony_ci
184cbd624adSopenharmony_ci        if hx < 0 {
185cbd624adSopenharmony_ci            if ((ix - 0x3f800000) | yisint) == 0 {
186cbd624adSopenharmony_ci                z = (z - z) / (z - z); /* (-1)**non-int is NaN */
187cbd624adSopenharmony_ci            } else if yisint == 1 {
188cbd624adSopenharmony_ci                z = -z; /* (x<0)**odd = -(|x|**odd) */
189cbd624adSopenharmony_ci            }
190cbd624adSopenharmony_ci        }
191cbd624adSopenharmony_ci        return z;
192cbd624adSopenharmony_ci    }
193cbd624adSopenharmony_ci
194cbd624adSopenharmony_ci    sn = 1.0; /* sign of result */
195cbd624adSopenharmony_ci    if hx < 0 {
196cbd624adSopenharmony_ci        if yisint == 0 {
197cbd624adSopenharmony_ci            /* (x<0)**(non-int) is NaN */
198cbd624adSopenharmony_ci            return (x - x) / (x - x);
199cbd624adSopenharmony_ci        }
200cbd624adSopenharmony_ci
201cbd624adSopenharmony_ci        if yisint == 1 {
202cbd624adSopenharmony_ci            /* (x<0)**(odd int) */
203cbd624adSopenharmony_ci            sn = -1.0;
204cbd624adSopenharmony_ci        }
205cbd624adSopenharmony_ci    }
206cbd624adSopenharmony_ci
207cbd624adSopenharmony_ci    /* |y| is HUGE */
208cbd624adSopenharmony_ci    if iy > 0x4d000000 {
209cbd624adSopenharmony_ci        /* if |y| > 2**27 */
210cbd624adSopenharmony_ci        /* over/underflow if x is not close to one */
211cbd624adSopenharmony_ci        if ix < 0x3f7ffff8 {
212cbd624adSopenharmony_ci            return if hy < 0 {
213cbd624adSopenharmony_ci                sn * HUGE * HUGE
214cbd624adSopenharmony_ci            } else {
215cbd624adSopenharmony_ci                sn * TINY * TINY
216cbd624adSopenharmony_ci            };
217cbd624adSopenharmony_ci        }
218cbd624adSopenharmony_ci
219cbd624adSopenharmony_ci        if ix > 0x3f800007 {
220cbd624adSopenharmony_ci            return if hy > 0 {
221cbd624adSopenharmony_ci                sn * HUGE * HUGE
222cbd624adSopenharmony_ci            } else {
223cbd624adSopenharmony_ci                sn * TINY * TINY
224cbd624adSopenharmony_ci            };
225cbd624adSopenharmony_ci        }
226cbd624adSopenharmony_ci
227cbd624adSopenharmony_ci        /* now |1-x| is TINY <= 2**-20, suffice to compute
228cbd624adSopenharmony_ci        log(x) by x-x^2/2+x^3/3-x^4/4 */
229cbd624adSopenharmony_ci        t = ax - 1.; /* t has 20 trailing zeros */
230cbd624adSopenharmony_ci        w = (t * t) * (0.5 - t * (0.333333333333 - t * 0.25));
231cbd624adSopenharmony_ci        u = IVLN2_H * t; /* IVLN2_H has 16 sig. bits */
232cbd624adSopenharmony_ci        v = t * IVLN2_L - w * IVLN2;
233cbd624adSopenharmony_ci        t1 = u + v;
234cbd624adSopenharmony_ci        is = t1.to_bits() as i32;
235cbd624adSopenharmony_ci        t1 = f32::from_bits(is as u32 & 0xfffff000);
236cbd624adSopenharmony_ci        t2 = v - (t1 - u);
237cbd624adSopenharmony_ci    } else {
238cbd624adSopenharmony_ci        let mut s2: f32;
239cbd624adSopenharmony_ci        let mut s_h: f32;
240cbd624adSopenharmony_ci        let s_l: f32;
241cbd624adSopenharmony_ci        let mut t_h: f32;
242cbd624adSopenharmony_ci        let mut t_l: f32;
243cbd624adSopenharmony_ci
244cbd624adSopenharmony_ci        n = 0;
245cbd624adSopenharmony_ci        /* take care subnormal number */
246cbd624adSopenharmony_ci        if ix < 0x00800000 {
247cbd624adSopenharmony_ci            ax *= TWO24;
248cbd624adSopenharmony_ci            n -= 24;
249cbd624adSopenharmony_ci            ix = ax.to_bits() as i32;
250cbd624adSopenharmony_ci        }
251cbd624adSopenharmony_ci        n += ((ix) >> 23) - 0x7f;
252cbd624adSopenharmony_ci        j = ix & 0x007fffff;
253cbd624adSopenharmony_ci        /* determine interval */
254cbd624adSopenharmony_ci        ix = j | 0x3f800000; /* normalize ix */
255cbd624adSopenharmony_ci        if j <= 0x1cc471 {
256cbd624adSopenharmony_ci            /* |x|<sqrt(3/2) */
257cbd624adSopenharmony_ci            k = 0;
258cbd624adSopenharmony_ci        } else if j < 0x5db3d7 {
259cbd624adSopenharmony_ci            /* |x|<sqrt(3)   */
260cbd624adSopenharmony_ci            k = 1;
261cbd624adSopenharmony_ci        } else {
262cbd624adSopenharmony_ci            k = 0;
263cbd624adSopenharmony_ci            n += 1;
264cbd624adSopenharmony_ci            ix -= 0x00800000;
265cbd624adSopenharmony_ci        }
266cbd624adSopenharmony_ci        ax = f32::from_bits(ix as u32);
267cbd624adSopenharmony_ci
268cbd624adSopenharmony_ci        /* compute s = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */
269cbd624adSopenharmony_ci        u = ax - i!(BP, k as usize); /* bp[0]=1.0, bp[1]=1.5 */
270cbd624adSopenharmony_ci        v = 1.0 / (ax + i!(BP, k as usize));
271cbd624adSopenharmony_ci        s = u * v;
272cbd624adSopenharmony_ci        s_h = s;
273cbd624adSopenharmony_ci        is = s_h.to_bits() as i32;
274cbd624adSopenharmony_ci        s_h = f32::from_bits(is as u32 & 0xfffff000);
275cbd624adSopenharmony_ci        /* t_h=ax+bp[k] High */
276cbd624adSopenharmony_ci        is = (((ix as u32 >> 1) & 0xfffff000) | 0x20000000) as i32;
277cbd624adSopenharmony_ci        t_h = f32::from_bits(is as u32 + 0x00400000 + ((k as u32) << 21));
278cbd624adSopenharmony_ci        t_l = ax - (t_h - i!(BP, k as usize));
279cbd624adSopenharmony_ci        s_l = v * ((u - s_h * t_h) - s_h * t_l);
280cbd624adSopenharmony_ci        /* compute log(ax) */
281cbd624adSopenharmony_ci        s2 = s * s;
282cbd624adSopenharmony_ci        r = s2 * s2 * (L1 + s2 * (L2 + s2 * (L3 + s2 * (L4 + s2 * (L5 + s2 * L6)))));
283cbd624adSopenharmony_ci        r += s_l * (s_h + s);
284cbd624adSopenharmony_ci        s2 = s_h * s_h;
285cbd624adSopenharmony_ci        t_h = 3.0 + s2 + r;
286cbd624adSopenharmony_ci        is = t_h.to_bits() as i32;
287cbd624adSopenharmony_ci        t_h = f32::from_bits(is as u32 & 0xfffff000);
288cbd624adSopenharmony_ci        t_l = r - ((t_h - 3.0) - s2);
289cbd624adSopenharmony_ci        /* u+v = s*(1+...) */
290cbd624adSopenharmony_ci        u = s_h * t_h;
291cbd624adSopenharmony_ci        v = s_l * t_h + t_l * s;
292cbd624adSopenharmony_ci        /* 2/(3log2)*(s+...) */
293cbd624adSopenharmony_ci        p_h = u + v;
294cbd624adSopenharmony_ci        is = p_h.to_bits() as i32;
295cbd624adSopenharmony_ci        p_h = f32::from_bits(is as u32 & 0xfffff000);
296cbd624adSopenharmony_ci        p_l = v - (p_h - u);
297cbd624adSopenharmony_ci        z_h = CP_H * p_h; /* cp_h+cp_l = 2/(3*log2) */
298cbd624adSopenharmony_ci        z_l = CP_L * p_h + p_l * CP + i!(DP_L, k as usize);
299cbd624adSopenharmony_ci        /* log2(ax) = (s+..)*2/(3*log2) = n + dp_h + z_h + z_l */
300cbd624adSopenharmony_ci        t = n as f32;
301cbd624adSopenharmony_ci        t1 = ((z_h + z_l) + i!(DP_H, k as usize)) + t;
302cbd624adSopenharmony_ci        is = t1.to_bits() as i32;
303cbd624adSopenharmony_ci        t1 = f32::from_bits(is as u32 & 0xfffff000);
304cbd624adSopenharmony_ci        t2 = z_l - (((t1 - t) - i!(DP_H, k as usize)) - z_h);
305cbd624adSopenharmony_ci    };
306cbd624adSopenharmony_ci
307cbd624adSopenharmony_ci    /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */
308cbd624adSopenharmony_ci    is = y.to_bits() as i32;
309cbd624adSopenharmony_ci    y1 = f32::from_bits(is as u32 & 0xfffff000);
310cbd624adSopenharmony_ci    p_l = (y - y1) * t1 + y * t2;
311cbd624adSopenharmony_ci    p_h = y1 * t1;
312cbd624adSopenharmony_ci    z = p_l + p_h;
313cbd624adSopenharmony_ci    j = z.to_bits() as i32;
314cbd624adSopenharmony_ci    if j > 0x43000000 {
315cbd624adSopenharmony_ci        /* if z > 128 */
316cbd624adSopenharmony_ci        return sn * HUGE * HUGE; /* overflow */
317cbd624adSopenharmony_ci    } else if j == 0x43000000 {
318cbd624adSopenharmony_ci        /* if z == 128 */
319cbd624adSopenharmony_ci        if p_l + OVT > z - p_h {
320cbd624adSopenharmony_ci            return sn * HUGE * HUGE; /* overflow */
321cbd624adSopenharmony_ci        }
322cbd624adSopenharmony_ci    } else if (j & 0x7fffffff) > 0x43160000 {
323cbd624adSopenharmony_ci        /* z < -150 */
324cbd624adSopenharmony_ci        // FIXME: check should be  (uint32_t)j > 0xc3160000
325cbd624adSopenharmony_ci        return sn * TINY * TINY; /* underflow */
326cbd624adSopenharmony_ci    } else if j as u32 == 0xc3160000
327cbd624adSopenharmony_ci              /* z == -150 */
328cbd624adSopenharmony_ci              && p_l <= z - p_h
329cbd624adSopenharmony_ci    {
330cbd624adSopenharmony_ci        return sn * TINY * TINY; /* underflow */
331cbd624adSopenharmony_ci    }
332cbd624adSopenharmony_ci
333cbd624adSopenharmony_ci    /*
334cbd624adSopenharmony_ci     * compute 2**(p_h+p_l)
335cbd624adSopenharmony_ci     */
336cbd624adSopenharmony_ci    i = j & 0x7fffffff;
337cbd624adSopenharmony_ci    k = (i >> 23) - 0x7f;
338cbd624adSopenharmony_ci    n = 0;
339cbd624adSopenharmony_ci    if i > 0x3f000000 {
340cbd624adSopenharmony_ci        /* if |z| > 0.5, set n = [z+0.5] */
341cbd624adSopenharmony_ci        n = j + (0x00800000 >> (k + 1));
342cbd624adSopenharmony_ci        k = ((n & 0x7fffffff) >> 23) - 0x7f; /* new k for n */
343cbd624adSopenharmony_ci        t = f32::from_bits(n as u32 & !(0x007fffff >> k));
344cbd624adSopenharmony_ci        n = ((n & 0x007fffff) | 0x00800000) >> (23 - k);
345cbd624adSopenharmony_ci        if j < 0 {
346cbd624adSopenharmony_ci            n = -n;
347cbd624adSopenharmony_ci        }
348cbd624adSopenharmony_ci        p_h -= t;
349cbd624adSopenharmony_ci    }
350cbd624adSopenharmony_ci    t = p_l + p_h;
351cbd624adSopenharmony_ci    is = t.to_bits() as i32;
352cbd624adSopenharmony_ci    t = f32::from_bits(is as u32 & 0xffff8000);
353cbd624adSopenharmony_ci    u = t * LG2_H;
354cbd624adSopenharmony_ci    v = (p_l - (t - p_h)) * LG2 + t * LG2_L;
355cbd624adSopenharmony_ci    z = u + v;
356cbd624adSopenharmony_ci    w = v - (z - u);
357cbd624adSopenharmony_ci    t = z * z;
358cbd624adSopenharmony_ci    t1 = z - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5))));
359cbd624adSopenharmony_ci    r = (z * t1) / (t1 - 2.0) - (w + z * w);
360cbd624adSopenharmony_ci    z = 1.0 - (r - z);
361cbd624adSopenharmony_ci    j = z.to_bits() as i32;
362cbd624adSopenharmony_ci    j += n << 23;
363cbd624adSopenharmony_ci    if (j >> 23) <= 0 {
364cbd624adSopenharmony_ci        /* subnormal output */
365cbd624adSopenharmony_ci        z = scalbnf(z, n);
366cbd624adSopenharmony_ci    } else {
367cbd624adSopenharmony_ci        z = f32::from_bits(j as u32);
368cbd624adSopenharmony_ci    }
369cbd624adSopenharmony_ci    sn * z
370cbd624adSopenharmony_ci}
371cbd624adSopenharmony_ci
372cbd624adSopenharmony_ci/* origin: FreeBSD /usr/src/lib/msun/src/e_sqrtf.c */
373cbd624adSopenharmony_ci/*
374cbd624adSopenharmony_ci * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
375cbd624adSopenharmony_ci */
376cbd624adSopenharmony_ci/*
377cbd624adSopenharmony_ci * ====================================================
378cbd624adSopenharmony_ci * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
379cbd624adSopenharmony_ci *
380cbd624adSopenharmony_ci * Developed at SunPro, a Sun Microsystems, Inc. business.
381cbd624adSopenharmony_ci * Permission to use, copy, modify, and distribute this
382cbd624adSopenharmony_ci * software is freely granted, provided that this notice
383cbd624adSopenharmony_ci * is preserved.
384cbd624adSopenharmony_ci * ====================================================
385cbd624adSopenharmony_ci */
386cbd624adSopenharmony_ci
387cbd624adSopenharmony_cipub fn sqrtf(x: f32) -> f32 {
388cbd624adSopenharmony_ci    #[cfg(target_feature = "sse")]
389cbd624adSopenharmony_ci    {
390cbd624adSopenharmony_ci        // Note: This path is unlikely since LLVM will usually have already
391cbd624adSopenharmony_ci        // optimized sqrt calls into hardware instructions if sse is available,
392cbd624adSopenharmony_ci        // but if someone does end up here they'll apprected the speed increase.
393cbd624adSopenharmony_ci        #[cfg(target_arch = "x86")]
394cbd624adSopenharmony_ci        use core::arch::x86::*;
395cbd624adSopenharmony_ci        #[cfg(target_arch = "x86_64")]
396cbd624adSopenharmony_ci        use core::arch::x86_64::*;
397cbd624adSopenharmony_ci        // SAFETY: safe, since `_mm_set_ss` takes a 32-bit float, and returns
398cbd624adSopenharmony_ci        // a 128-bit type with the lowest 32-bits as `x`, `_mm_sqrt_ss` calculates
399cbd624adSopenharmony_ci        // the sqrt of this 128-bit vector, and `_mm_cvtss_f32` extracts the lower
400cbd624adSopenharmony_ci        // 32-bits as a 32-bit float.
401cbd624adSopenharmony_ci        unsafe {
402cbd624adSopenharmony_ci            let m = _mm_set_ss(x);
403cbd624adSopenharmony_ci            let m_sqrt = _mm_sqrt_ss(m);
404cbd624adSopenharmony_ci            _mm_cvtss_f32(m_sqrt)
405cbd624adSopenharmony_ci        }
406cbd624adSopenharmony_ci    }
407cbd624adSopenharmony_ci    #[cfg(not(target_feature = "sse"))]
408cbd624adSopenharmony_ci    {
409cbd624adSopenharmony_ci        const TINY: f32 = 1.0e-30;
410cbd624adSopenharmony_ci
411cbd624adSopenharmony_ci        let mut z: f32;
412cbd624adSopenharmony_ci        let sign: i32 = 0x80000000u32 as i32;
413cbd624adSopenharmony_ci        let mut ix: i32;
414cbd624adSopenharmony_ci        let mut s: i32;
415cbd624adSopenharmony_ci        let mut q: i32;
416cbd624adSopenharmony_ci        let mut m: i32;
417cbd624adSopenharmony_ci        let mut t: i32;
418cbd624adSopenharmony_ci        let mut i: i32;
419cbd624adSopenharmony_ci        let mut r: u32;
420cbd624adSopenharmony_ci
421cbd624adSopenharmony_ci        ix = x.to_bits() as i32;
422cbd624adSopenharmony_ci
423cbd624adSopenharmony_ci        /* take care of Inf and NaN */
424cbd624adSopenharmony_ci        if (ix as u32 & 0x7f800000) == 0x7f800000 {
425cbd624adSopenharmony_ci            return x * x + x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf, sqrt(-inf)=sNaN */
426cbd624adSopenharmony_ci        }
427cbd624adSopenharmony_ci
428cbd624adSopenharmony_ci        /* take care of zero */
429cbd624adSopenharmony_ci        if ix <= 0 {
430cbd624adSopenharmony_ci            if (ix & !sign) == 0 {
431cbd624adSopenharmony_ci                return x; /* sqrt(+-0) = +-0 */
432cbd624adSopenharmony_ci            }
433cbd624adSopenharmony_ci            if ix < 0 {
434cbd624adSopenharmony_ci                return (x - x) / (x - x); /* sqrt(-ve) = sNaN */
435cbd624adSopenharmony_ci            }
436cbd624adSopenharmony_ci        }
437cbd624adSopenharmony_ci
438cbd624adSopenharmony_ci        /* normalize x */
439cbd624adSopenharmony_ci        m = ix >> 23;
440cbd624adSopenharmony_ci        if m == 0 {
441cbd624adSopenharmony_ci            /* subnormal x */
442cbd624adSopenharmony_ci            i = 0;
443cbd624adSopenharmony_ci            while ix & 0x00800000 == 0 {
444cbd624adSopenharmony_ci                ix <<= 1;
445cbd624adSopenharmony_ci                i = i + 1;
446cbd624adSopenharmony_ci            }
447cbd624adSopenharmony_ci            m -= i - 1;
448cbd624adSopenharmony_ci        }
449cbd624adSopenharmony_ci        m -= 127; /* unbias exponent */
450cbd624adSopenharmony_ci        ix = (ix & 0x007fffff) | 0x00800000;
451cbd624adSopenharmony_ci        if m & 1 == 1 {
452cbd624adSopenharmony_ci            /* odd m, double x to make it even */
453cbd624adSopenharmony_ci            ix += ix;
454cbd624adSopenharmony_ci        }
455cbd624adSopenharmony_ci        m >>= 1; /* m = [m/2] */
456cbd624adSopenharmony_ci
457cbd624adSopenharmony_ci        /* generate sqrt(x) bit by bit */
458cbd624adSopenharmony_ci        ix += ix;
459cbd624adSopenharmony_ci        q = 0;
460cbd624adSopenharmony_ci        s = 0;
461cbd624adSopenharmony_ci        r = 0x01000000; /* r = moving bit from right to left */
462cbd624adSopenharmony_ci
463cbd624adSopenharmony_ci        while r != 0 {
464cbd624adSopenharmony_ci            t = s + r as i32;
465cbd624adSopenharmony_ci            if t <= ix {
466cbd624adSopenharmony_ci                s = t + r as i32;
467cbd624adSopenharmony_ci                ix -= t;
468cbd624adSopenharmony_ci                q += r as i32;
469cbd624adSopenharmony_ci            }
470cbd624adSopenharmony_ci            ix += ix;
471cbd624adSopenharmony_ci            r >>= 1;
472cbd624adSopenharmony_ci        }
473cbd624adSopenharmony_ci
474cbd624adSopenharmony_ci        /* use floating add to find out rounding direction */
475cbd624adSopenharmony_ci        if ix != 0 {
476cbd624adSopenharmony_ci            z = 1.0 - TINY; /* raise inexact flag */
477cbd624adSopenharmony_ci            if z >= 1.0 {
478cbd624adSopenharmony_ci                z = 1.0 + TINY;
479cbd624adSopenharmony_ci                if z > 1.0 {
480cbd624adSopenharmony_ci                    q += 2;
481cbd624adSopenharmony_ci                } else {
482cbd624adSopenharmony_ci                    q += q & 1;
483cbd624adSopenharmony_ci                }
484cbd624adSopenharmony_ci            }
485cbd624adSopenharmony_ci        }
486cbd624adSopenharmony_ci
487cbd624adSopenharmony_ci        ix = (q >> 1) + 0x3f000000;
488cbd624adSopenharmony_ci        ix += m << 23;
489cbd624adSopenharmony_ci        f32::from_bits(ix as u32)
490cbd624adSopenharmony_ci    }
491cbd624adSopenharmony_ci}
492cbd624adSopenharmony_ci
493cbd624adSopenharmony_ci/// Absolute value (magnitude) (f32)
494cbd624adSopenharmony_ci/// Calculates the absolute value (magnitude) of the argument `x`,
495cbd624adSopenharmony_ci/// by direct manipulation of the bit representation of `x`.
496cbd624adSopenharmony_cipub fn fabsf(x: f32) -> f32 {
497cbd624adSopenharmony_ci    f32::from_bits(x.to_bits() & 0x7fffffff)
498cbd624adSopenharmony_ci}
499cbd624adSopenharmony_ci
500cbd624adSopenharmony_cipub fn scalbnf(mut x: f32, mut n: i32) -> f32 {
501cbd624adSopenharmony_ci    let x1p127 = f32::from_bits(0x7f000000); // 0x1p127f === 2 ^ 127
502cbd624adSopenharmony_ci    let x1p_126 = f32::from_bits(0x800000); // 0x1p-126f === 2 ^ -126
503cbd624adSopenharmony_ci    let x1p24 = f32::from_bits(0x4b800000); // 0x1p24f === 2 ^ 24
504cbd624adSopenharmony_ci
505cbd624adSopenharmony_ci    if n > 127 {
506cbd624adSopenharmony_ci        x *= x1p127;
507cbd624adSopenharmony_ci        n -= 127;
508cbd624adSopenharmony_ci        if n > 127 {
509cbd624adSopenharmony_ci            x *= x1p127;
510cbd624adSopenharmony_ci            n -= 127;
511cbd624adSopenharmony_ci            if n > 127 {
512cbd624adSopenharmony_ci                n = 127;
513cbd624adSopenharmony_ci            }
514cbd624adSopenharmony_ci        }
515cbd624adSopenharmony_ci    } else if n < -126 {
516cbd624adSopenharmony_ci        x *= x1p_126 * x1p24;
517cbd624adSopenharmony_ci        n += 126 - 24;
518cbd624adSopenharmony_ci        if n < -126 {
519cbd624adSopenharmony_ci            x *= x1p_126 * x1p24;
520cbd624adSopenharmony_ci            n += 126 - 24;
521cbd624adSopenharmony_ci            if n < -126 {
522cbd624adSopenharmony_ci                n = -126;
523cbd624adSopenharmony_ci            }
524cbd624adSopenharmony_ci        }
525cbd624adSopenharmony_ci    }
526cbd624adSopenharmony_ci    x * f32::from_bits(((0x7f + n) as u32) << 23)
527cbd624adSopenharmony_ci}
528cbd624adSopenharmony_ci
529cbd624adSopenharmony_ci/* origin: FreeBSD /usr/src/lib/msun/src/e_pow.c */
530cbd624adSopenharmony_ci/*
531cbd624adSopenharmony_ci * ====================================================
532cbd624adSopenharmony_ci * Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved.
533cbd624adSopenharmony_ci *
534cbd624adSopenharmony_ci * Permission to use, copy, modify, and distribute this
535cbd624adSopenharmony_ci * software is freely granted, provided that this notice
536cbd624adSopenharmony_ci * is preserved.
537cbd624adSopenharmony_ci * ====================================================
538cbd624adSopenharmony_ci */
539cbd624adSopenharmony_ci
540cbd624adSopenharmony_ci// pow(x,y) return x**y
541cbd624adSopenharmony_ci//
542cbd624adSopenharmony_ci//                    n
543cbd624adSopenharmony_ci// Method:  Let x =  2   * (1+f)
544cbd624adSopenharmony_ci//      1. Compute and return log2(x) in two pieces:
545cbd624adSopenharmony_ci//              log2(x) = w1 + w2,
546cbd624adSopenharmony_ci//         where w1 has 53-24 = 29 bit trailing zeros.
547cbd624adSopenharmony_ci//      2. Perform y*log2(x) = n+y' by simulating muti-precision
548cbd624adSopenharmony_ci//         arithmetic, where |y'|<=0.5.
549cbd624adSopenharmony_ci//      3. Return x**y = 2**n*exp(y'*log2)
550cbd624adSopenharmony_ci//
551cbd624adSopenharmony_ci// Special cases:
552cbd624adSopenharmony_ci//      1.  (anything) ** 0  is 1
553cbd624adSopenharmony_ci//      2.  1 ** (anything)  is 1
554cbd624adSopenharmony_ci//      3.  (anything except 1) ** NAN is NAN
555cbd624adSopenharmony_ci//      4.  NAN ** (anything except 0) is NAN
556cbd624adSopenharmony_ci//      5.  +-(|x| > 1) **  +INF is +INF
557cbd624adSopenharmony_ci//      6.  +-(|x| > 1) **  -INF is +0
558cbd624adSopenharmony_ci//      7.  +-(|x| < 1) **  +INF is +0
559cbd624adSopenharmony_ci//      8.  +-(|x| < 1) **  -INF is +INF
560cbd624adSopenharmony_ci//      9.  -1          ** +-INF is 1
561cbd624adSopenharmony_ci//      10. +0 ** (+anything except 0, NAN)               is +0
562cbd624adSopenharmony_ci//      11. -0 ** (+anything except 0, NAN, odd integer)  is +0
563cbd624adSopenharmony_ci//      12. +0 ** (-anything except 0, NAN)               is +INF, raise divbyzero
564cbd624adSopenharmony_ci//      13. -0 ** (-anything except 0, NAN, odd integer)  is +INF, raise divbyzero
565cbd624adSopenharmony_ci//      14. -0 ** (+odd integer) is -0
566cbd624adSopenharmony_ci//      15. -0 ** (-odd integer) is -INF, raise divbyzero
567cbd624adSopenharmony_ci//      16. +INF ** (+anything except 0,NAN) is +INF
568cbd624adSopenharmony_ci//      17. +INF ** (-anything except 0,NAN) is +0
569cbd624adSopenharmony_ci//      18. -INF ** (+odd integer) is -INF
570cbd624adSopenharmony_ci//      19. -INF ** (anything) = -0 ** (-anything), (anything except odd integer)
571cbd624adSopenharmony_ci//      20. (anything) ** 1 is (anything)
572cbd624adSopenharmony_ci//      21. (anything) ** -1 is 1/(anything)
573cbd624adSopenharmony_ci//      22. (-anything) ** (integer) is (-1)**(integer)*(+anything**integer)
574cbd624adSopenharmony_ci//      23. (-anything except 0 and inf) ** (non-integer) is NAN
575cbd624adSopenharmony_ci//
576cbd624adSopenharmony_ci// Accuracy:
577cbd624adSopenharmony_ci//      pow(x,y) returns x**y nearly rounded. In particular
578cbd624adSopenharmony_ci//                      pow(integer,integer)
579cbd624adSopenharmony_ci//      always returns the correct integer provided it is
580cbd624adSopenharmony_ci//      representable.
581cbd624adSopenharmony_ci//
582cbd624adSopenharmony_ci// Constants :
583cbd624adSopenharmony_ci// The hexadecimal values are the intended ones for the following
584cbd624adSopenharmony_ci// constants. The decimal values may be used, provided that the
585cbd624adSopenharmony_ci// compiler will convert from decimal to binary accurately enough
586cbd624adSopenharmony_ci// to produce the hexadecimal values shown.
587cbd624adSopenharmony_ci
588cbd624adSopenharmony_cipub fn powd(x: f64, y: f64) -> f64 {
589cbd624adSopenharmony_ci    const BP: [f64; 2] = [1.0, 1.5];
590cbd624adSopenharmony_ci    const DP_H: [f64; 2] = [0.0, 5.84962487220764160156e-01]; /* 0x3fe2b803_40000000 */
591cbd624adSopenharmony_ci    const DP_L: [f64; 2] = [0.0, 1.35003920212974897128e-08]; /* 0x3E4CFDEB, 0x43CFD006 */
592cbd624adSopenharmony_ci    const TWO53: f64 = 9007199254740992.0; /* 0x43400000_00000000 */
593cbd624adSopenharmony_ci    const HUGE: f64 = 1.0e300;
594cbd624adSopenharmony_ci    const TINY: f64 = 1.0e-300;
595cbd624adSopenharmony_ci
596cbd624adSopenharmony_ci    // poly coefs for (3/2)*(log(x)-2s-2/3*s**3:
597cbd624adSopenharmony_ci    const L1: f64 = 5.99999999999994648725e-01; /* 0x3fe33333_33333303 */
598cbd624adSopenharmony_ci    const L2: f64 = 4.28571428578550184252e-01; /* 0x3fdb6db6_db6fabff */
599cbd624adSopenharmony_ci    const L3: f64 = 3.33333329818377432918e-01; /* 0x3fd55555_518f264d */
600cbd624adSopenharmony_ci    const L4: f64 = 2.72728123808534006489e-01; /* 0x3fd17460_a91d4101 */
601cbd624adSopenharmony_ci    const L5: f64 = 2.30660745775561754067e-01; /* 0x3fcd864a_93c9db65 */
602cbd624adSopenharmony_ci    const L6: f64 = 2.06975017800338417784e-01; /* 0x3fca7e28_4a454eef */
603cbd624adSopenharmony_ci    const P1: f64 = 1.66666666666666019037e-01; /* 0x3fc55555_5555553e */
604cbd624adSopenharmony_ci    const P2: f64 = -2.77777777770155933842e-03; /* 0xbf66c16c_16bebd93 */
605cbd624adSopenharmony_ci    const P3: f64 = 6.61375632143793436117e-05; /* 0x3f11566a_af25de2c */
606cbd624adSopenharmony_ci    const P4: f64 = -1.65339022054652515390e-06; /* 0xbebbbd41_c5d26bf1 */
607cbd624adSopenharmony_ci    const P5: f64 = 4.13813679705723846039e-08; /* 0x3e663769_72bea4d0 */
608cbd624adSopenharmony_ci    const LG2: f64 = 6.93147180559945286227e-01; /* 0x3fe62e42_fefa39ef */
609cbd624adSopenharmony_ci    const LG2_H: f64 = 6.93147182464599609375e-01; /* 0x3fe62e43_00000000 */
610cbd624adSopenharmony_ci    const LG2_L: f64 = -1.90465429995776804525e-09; /* 0xbe205c61_0ca86c39 */
611cbd624adSopenharmony_ci    const OVT: f64 = 8.0085662595372944372e-017; /* -(1024-log2(ovfl+.5ulp)) */
612cbd624adSopenharmony_ci    const CP: f64 = 9.61796693925975554329e-01; /* 0x3feec709_dc3a03fd =2/(3ln2) */
613cbd624adSopenharmony_ci    const CP_H: f64 = 9.61796700954437255859e-01; /* 0x3feec709_e0000000 =(float)cp */
614cbd624adSopenharmony_ci    const CP_L: f64 = -7.02846165095275826516e-09; /* 0xbe3e2fe0_145b01f5 =tail of cp_h*/
615cbd624adSopenharmony_ci    const IVLN2: f64 = 1.44269504088896338700e+00; /* 0x3ff71547_652b82fe =1/ln2 */
616cbd624adSopenharmony_ci    const IVLN2_H: f64 = 1.44269502162933349609e+00; /* 0x3ff71547_60000000 =24b 1/ln2*/
617cbd624adSopenharmony_ci    const IVLN2_L: f64 = 1.92596299112661746887e-08; /* 0x3e54ae0b_f85ddf44 =1/ln2 tail*/
618cbd624adSopenharmony_ci
619cbd624adSopenharmony_ci    let t1: f64;
620cbd624adSopenharmony_ci    let t2: f64;
621cbd624adSopenharmony_ci
622cbd624adSopenharmony_ci    let (hx, lx): (i32, u32) = ((x.to_bits() >> 32) as i32, x.to_bits() as u32);
623cbd624adSopenharmony_ci    let (hy, ly): (i32, u32) = ((y.to_bits() >> 32) as i32, y.to_bits() as u32);
624cbd624adSopenharmony_ci
625cbd624adSopenharmony_ci    let mut ix: i32 = (hx & 0x7fffffff) as i32;
626cbd624adSopenharmony_ci    let iy: i32 = (hy & 0x7fffffff) as i32;
627cbd624adSopenharmony_ci
628cbd624adSopenharmony_ci    /* x**0 = 1, even if x is NaN */
629cbd624adSopenharmony_ci    if ((iy as u32) | ly) == 0 {
630cbd624adSopenharmony_ci        return 1.0;
631cbd624adSopenharmony_ci    }
632cbd624adSopenharmony_ci
633cbd624adSopenharmony_ci    /* 1**y = 1, even if y is NaN */
634cbd624adSopenharmony_ci    if hx == 0x3ff00000 && lx == 0 {
635cbd624adSopenharmony_ci        return 1.0;
636cbd624adSopenharmony_ci    }
637cbd624adSopenharmony_ci
638cbd624adSopenharmony_ci    /* NaN if either arg is NaN */
639cbd624adSopenharmony_ci    if ix > 0x7ff00000
640cbd624adSopenharmony_ci        || (ix == 0x7ff00000 && lx != 0)
641cbd624adSopenharmony_ci        || iy > 0x7ff00000
642cbd624adSopenharmony_ci        || (iy == 0x7ff00000 && ly != 0)
643cbd624adSopenharmony_ci    {
644cbd624adSopenharmony_ci        return x + y;
645cbd624adSopenharmony_ci    }
646cbd624adSopenharmony_ci
647cbd624adSopenharmony_ci    /* determine if y is an odd int when x < 0
648cbd624adSopenharmony_ci     * yisint = 0       ... y is not an integer
649cbd624adSopenharmony_ci     * yisint = 1       ... y is an odd int
650cbd624adSopenharmony_ci     * yisint = 2       ... y is an even int
651cbd624adSopenharmony_ci     */
652cbd624adSopenharmony_ci    let mut yisint: i32 = 0;
653cbd624adSopenharmony_ci    let mut k: i32;
654cbd624adSopenharmony_ci    let mut j: i32;
655cbd624adSopenharmony_ci    if hx < 0 {
656cbd624adSopenharmony_ci        if iy >= 0x43400000 {
657cbd624adSopenharmony_ci            yisint = 2; /* even integer y */
658cbd624adSopenharmony_ci        } else if iy >= 0x3ff00000 {
659cbd624adSopenharmony_ci            k = (iy >> 20) - 0x3ff; /* exponent */
660cbd624adSopenharmony_ci
661cbd624adSopenharmony_ci            if k > 20 {
662cbd624adSopenharmony_ci                j = (ly >> (52 - k)) as i32;
663cbd624adSopenharmony_ci
664cbd624adSopenharmony_ci                if (j << (52 - k)) == (ly as i32) {
665cbd624adSopenharmony_ci                    yisint = 2 - (j & 1);
666cbd624adSopenharmony_ci                }
667cbd624adSopenharmony_ci            } else if ly == 0 {
668cbd624adSopenharmony_ci                j = iy >> (20 - k);
669cbd624adSopenharmony_ci
670cbd624adSopenharmony_ci                if (j << (20 - k)) == iy {
671cbd624adSopenharmony_ci                    yisint = 2 - (j & 1);
672cbd624adSopenharmony_ci                }
673cbd624adSopenharmony_ci            }
674cbd624adSopenharmony_ci        }
675cbd624adSopenharmony_ci    }
676cbd624adSopenharmony_ci
677cbd624adSopenharmony_ci    if ly == 0 {
678cbd624adSopenharmony_ci        /* special value of y */
679cbd624adSopenharmony_ci        if iy == 0x7ff00000 {
680cbd624adSopenharmony_ci            /* y is +-inf */
681cbd624adSopenharmony_ci
682cbd624adSopenharmony_ci            return if ((ix - 0x3ff00000) | (lx as i32)) == 0 {
683cbd624adSopenharmony_ci                /* (-1)**+-inf is 1 */
684cbd624adSopenharmony_ci                1.0
685cbd624adSopenharmony_ci            } else if ix >= 0x3ff00000 {
686cbd624adSopenharmony_ci                /* (|x|>1)**+-inf = inf,0 */
687cbd624adSopenharmony_ci                if hy >= 0 {
688cbd624adSopenharmony_ci                    y
689cbd624adSopenharmony_ci                } else {
690cbd624adSopenharmony_ci                    0.0
691cbd624adSopenharmony_ci                }
692cbd624adSopenharmony_ci            } else {
693cbd624adSopenharmony_ci                /* (|x|<1)**+-inf = 0,inf */
694cbd624adSopenharmony_ci                if hy >= 0 {
695cbd624adSopenharmony_ci                    0.0
696cbd624adSopenharmony_ci                } else {
697cbd624adSopenharmony_ci                    -y
698cbd624adSopenharmony_ci                }
699cbd624adSopenharmony_ci            };
700cbd624adSopenharmony_ci        }
701cbd624adSopenharmony_ci
702cbd624adSopenharmony_ci        if iy == 0x3ff00000 {
703cbd624adSopenharmony_ci            /* y is +-1 */
704cbd624adSopenharmony_ci            return if hy >= 0 {
705cbd624adSopenharmony_ci                x
706cbd624adSopenharmony_ci            } else {
707cbd624adSopenharmony_ci                1.0 / x
708cbd624adSopenharmony_ci            };
709cbd624adSopenharmony_ci        }
710cbd624adSopenharmony_ci
711cbd624adSopenharmony_ci        if hy == 0x40000000 {
712cbd624adSopenharmony_ci            /* y is 2 */
713cbd624adSopenharmony_ci            return x * x;
714cbd624adSopenharmony_ci        }
715cbd624adSopenharmony_ci
716cbd624adSopenharmony_ci        if hy == 0x3fe00000 {
717cbd624adSopenharmony_ci            /* y is 0.5 */
718cbd624adSopenharmony_ci            if hx >= 0 {
719cbd624adSopenharmony_ci                /* x >= +0 */
720cbd624adSopenharmony_ci                return sqrtd(x);
721cbd624adSopenharmony_ci            }
722cbd624adSopenharmony_ci        }
723cbd624adSopenharmony_ci    }
724cbd624adSopenharmony_ci
725cbd624adSopenharmony_ci    let mut ax: f64 = fabsd(x);
726cbd624adSopenharmony_ci    if lx == 0 {
727cbd624adSopenharmony_ci        /* special value of x */
728cbd624adSopenharmony_ci        if ix == 0x7ff00000 || ix == 0 || ix == 0x3ff00000 {
729cbd624adSopenharmony_ci            /* x is +-0,+-inf,+-1 */
730cbd624adSopenharmony_ci            let mut z: f64 = ax;
731cbd624adSopenharmony_ci
732cbd624adSopenharmony_ci            if hy < 0 {
733cbd624adSopenharmony_ci                /* z = (1/|x|) */
734cbd624adSopenharmony_ci                z = 1.0 / z;
735cbd624adSopenharmony_ci            }
736cbd624adSopenharmony_ci
737cbd624adSopenharmony_ci            if hx < 0 {
738cbd624adSopenharmony_ci                if ((ix - 0x3ff00000) | yisint) == 0 {
739cbd624adSopenharmony_ci                    z = (z - z) / (z - z); /* (-1)**non-int is NaN */
740cbd624adSopenharmony_ci                } else if yisint == 1 {
741cbd624adSopenharmony_ci                    z = -z; /* (x<0)**odd = -(|x|**odd) */
742cbd624adSopenharmony_ci                }
743cbd624adSopenharmony_ci            }
744cbd624adSopenharmony_ci
745cbd624adSopenharmony_ci            return z;
746cbd624adSopenharmony_ci        }
747cbd624adSopenharmony_ci    }
748cbd624adSopenharmony_ci
749cbd624adSopenharmony_ci    let mut s: f64 = 1.0; /* sign of result */
750cbd624adSopenharmony_ci    if hx < 0 {
751cbd624adSopenharmony_ci        if yisint == 0 {
752cbd624adSopenharmony_ci            /* (x<0)**(non-int) is NaN */
753cbd624adSopenharmony_ci            return (x - x) / (x - x);
754cbd624adSopenharmony_ci        }
755cbd624adSopenharmony_ci
756cbd624adSopenharmony_ci        if yisint == 1 {
757cbd624adSopenharmony_ci            /* (x<0)**(odd int) */
758cbd624adSopenharmony_ci            s = -1.0;
759cbd624adSopenharmony_ci        }
760cbd624adSopenharmony_ci    }
761cbd624adSopenharmony_ci
762cbd624adSopenharmony_ci    /* |y| is HUGE */
763cbd624adSopenharmony_ci    if iy > 0x41e00000 {
764cbd624adSopenharmony_ci        /* if |y| > 2**31 */
765cbd624adSopenharmony_ci        if iy > 0x43f00000 {
766cbd624adSopenharmony_ci            /* if |y| > 2**64, must o/uflow */
767cbd624adSopenharmony_ci            if ix <= 0x3fefffff {
768cbd624adSopenharmony_ci                return if hy < 0 {
769cbd624adSopenharmony_ci                    HUGE * HUGE
770cbd624adSopenharmony_ci                } else {
771cbd624adSopenharmony_ci                    TINY * TINY
772cbd624adSopenharmony_ci                };
773cbd624adSopenharmony_ci            }
774cbd624adSopenharmony_ci
775cbd624adSopenharmony_ci            if ix >= 0x3ff00000 {
776cbd624adSopenharmony_ci                return if hy > 0 {
777cbd624adSopenharmony_ci                    HUGE * HUGE
778cbd624adSopenharmony_ci                } else {
779cbd624adSopenharmony_ci                    TINY * TINY
780cbd624adSopenharmony_ci                };
781cbd624adSopenharmony_ci            }
782cbd624adSopenharmony_ci        }
783cbd624adSopenharmony_ci
784cbd624adSopenharmony_ci        /* over/underflow if x is not close to one */
785cbd624adSopenharmony_ci        if ix < 0x3fefffff {
786cbd624adSopenharmony_ci            return if hy < 0 {
787cbd624adSopenharmony_ci                s * HUGE * HUGE
788cbd624adSopenharmony_ci            } else {
789cbd624adSopenharmony_ci                s * TINY * TINY
790cbd624adSopenharmony_ci            };
791cbd624adSopenharmony_ci        }
792cbd624adSopenharmony_ci        if ix > 0x3ff00000 {
793cbd624adSopenharmony_ci            return if hy > 0 {
794cbd624adSopenharmony_ci                s * HUGE * HUGE
795cbd624adSopenharmony_ci            } else {
796cbd624adSopenharmony_ci                s * TINY * TINY
797cbd624adSopenharmony_ci            };
798cbd624adSopenharmony_ci        }
799cbd624adSopenharmony_ci
800cbd624adSopenharmony_ci        /* now |1-x| is TINY <= 2**-20, suffice to compute
801cbd624adSopenharmony_ci        log(x) by x-x^2/2+x^3/3-x^4/4 */
802cbd624adSopenharmony_ci        let t: f64 = ax - 1.0; /* t has 20 trailing zeros */
803cbd624adSopenharmony_ci        let w: f64 = (t * t) * (0.5 - t * (0.3333333333333333333333 - t * 0.25));
804cbd624adSopenharmony_ci        let u: f64 = IVLN2_H * t; /* ivln2_h has 21 sig. bits */
805cbd624adSopenharmony_ci        let v: f64 = t * IVLN2_L - w * IVLN2;
806cbd624adSopenharmony_ci        t1 = with_set_low_word(u + v, 0);
807cbd624adSopenharmony_ci        t2 = v - (t1 - u);
808cbd624adSopenharmony_ci    } else {
809cbd624adSopenharmony_ci        // double ss,s2,s_h,s_l,t_h,t_l;
810cbd624adSopenharmony_ci        let mut n: i32 = 0;
811cbd624adSopenharmony_ci
812cbd624adSopenharmony_ci        if ix < 0x00100000 {
813cbd624adSopenharmony_ci            /* take care subnormal number */
814cbd624adSopenharmony_ci            ax *= TWO53;
815cbd624adSopenharmony_ci            n -= 53;
816cbd624adSopenharmony_ci            ix = get_high_word(ax) as i32;
817cbd624adSopenharmony_ci        }
818cbd624adSopenharmony_ci
819cbd624adSopenharmony_ci        n += (ix >> 20) - 0x3ff;
820cbd624adSopenharmony_ci        j = ix & 0x000fffff;
821cbd624adSopenharmony_ci
822cbd624adSopenharmony_ci        /* determine interval */
823cbd624adSopenharmony_ci        let k: i32;
824cbd624adSopenharmony_ci        ix = j | 0x3ff00000; /* normalize ix */
825cbd624adSopenharmony_ci        if j <= 0x3988E {
826cbd624adSopenharmony_ci            /* |x|<sqrt(3/2) */
827cbd624adSopenharmony_ci            k = 0;
828cbd624adSopenharmony_ci        } else if j < 0xBB67A {
829cbd624adSopenharmony_ci            /* |x|<sqrt(3)   */
830cbd624adSopenharmony_ci            k = 1;
831cbd624adSopenharmony_ci        } else {
832cbd624adSopenharmony_ci            k = 0;
833cbd624adSopenharmony_ci            n += 1;
834cbd624adSopenharmony_ci            ix -= 0x00100000;
835cbd624adSopenharmony_ci        }
836cbd624adSopenharmony_ci        ax = with_set_high_word(ax, ix as u32);
837cbd624adSopenharmony_ci
838cbd624adSopenharmony_ci        /* compute ss = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */
839cbd624adSopenharmony_ci        let u: f64 = ax - i!(BP, k as usize); /* bp[0]=1.0, bp[1]=1.5 */
840cbd624adSopenharmony_ci        let v: f64 = 1.0 / (ax + i!(BP, k as usize));
841cbd624adSopenharmony_ci        let ss: f64 = u * v;
842cbd624adSopenharmony_ci        let s_h = with_set_low_word(ss, 0);
843cbd624adSopenharmony_ci
844cbd624adSopenharmony_ci        /* t_h=ax+bp[k] High */
845cbd624adSopenharmony_ci        let t_h: f64 = with_set_high_word(
846cbd624adSopenharmony_ci            0.0,
847cbd624adSopenharmony_ci            ((ix as u32 >> 1) | 0x20000000) + 0x00080000 + ((k as u32) << 18),
848cbd624adSopenharmony_ci        );
849cbd624adSopenharmony_ci        let t_l: f64 = ax - (t_h - i!(BP, k as usize));
850cbd624adSopenharmony_ci        let s_l: f64 = v * ((u - s_h * t_h) - s_h * t_l);
851cbd624adSopenharmony_ci
852cbd624adSopenharmony_ci        /* compute log(ax) */
853cbd624adSopenharmony_ci        let s2: f64 = ss * ss;
854cbd624adSopenharmony_ci        let mut r: f64 = s2 * s2 * (L1 + s2 * (L2 + s2 * (L3 + s2 * (L4 + s2 * (L5 + s2 * L6)))));
855cbd624adSopenharmony_ci        r += s_l * (s_h + ss);
856cbd624adSopenharmony_ci        let s2: f64 = s_h * s_h;
857cbd624adSopenharmony_ci        let t_h: f64 = with_set_low_word(3.0 + s2 + r, 0);
858cbd624adSopenharmony_ci        let t_l: f64 = r - ((t_h - 3.0) - s2);
859cbd624adSopenharmony_ci
860cbd624adSopenharmony_ci        /* u+v = ss*(1+...) */
861cbd624adSopenharmony_ci        let u: f64 = s_h * t_h;
862cbd624adSopenharmony_ci        let v: f64 = s_l * t_h + t_l * ss;
863cbd624adSopenharmony_ci
864cbd624adSopenharmony_ci        /* 2/(3log2)*(ss+...) */
865cbd624adSopenharmony_ci        let p_h: f64 = with_set_low_word(u + v, 0);
866cbd624adSopenharmony_ci        let p_l = v - (p_h - u);
867cbd624adSopenharmony_ci        let z_h: f64 = CP_H * p_h; /* cp_h+cp_l = 2/(3*log2) */
868cbd624adSopenharmony_ci        let z_l: f64 = CP_L * p_h + p_l * CP + i!(DP_L, k as usize);
869cbd624adSopenharmony_ci
870cbd624adSopenharmony_ci        /* log2(ax) = (ss+..)*2/(3*log2) = n + dp_h + z_h + z_l */
871cbd624adSopenharmony_ci        let t: f64 = n as f64;
872cbd624adSopenharmony_ci        t1 = with_set_low_word(((z_h + z_l) + i!(DP_H, k as usize)) + t, 0);
873cbd624adSopenharmony_ci        t2 = z_l - (((t1 - t) - i!(DP_H, k as usize)) - z_h);
874cbd624adSopenharmony_ci    }
875cbd624adSopenharmony_ci
876cbd624adSopenharmony_ci    /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */
877cbd624adSopenharmony_ci    let y1: f64 = with_set_low_word(y, 0);
878cbd624adSopenharmony_ci    let p_l: f64 = (y - y1) * t1 + y * t2;
879cbd624adSopenharmony_ci    let mut p_h: f64 = y1 * t1;
880cbd624adSopenharmony_ci    let z: f64 = p_l + p_h;
881cbd624adSopenharmony_ci    let mut j: i32 = (z.to_bits() >> 32) as i32;
882cbd624adSopenharmony_ci    let i: i32 = z.to_bits() as i32;
883cbd624adSopenharmony_ci    // let (j, i): (i32, i32) = ((z.to_bits() >> 32) as i32, z.to_bits() as i32);
884cbd624adSopenharmony_ci
885cbd624adSopenharmony_ci    if j >= 0x40900000 {
886cbd624adSopenharmony_ci        /* z >= 1024 */
887cbd624adSopenharmony_ci        if (j - 0x40900000) | i != 0 {
888cbd624adSopenharmony_ci            /* if z > 1024 */
889cbd624adSopenharmony_ci            return s * HUGE * HUGE; /* overflow */
890cbd624adSopenharmony_ci        }
891cbd624adSopenharmony_ci
892cbd624adSopenharmony_ci        if p_l + OVT > z - p_h {
893cbd624adSopenharmony_ci            return s * HUGE * HUGE; /* overflow */
894cbd624adSopenharmony_ci        }
895cbd624adSopenharmony_ci    } else if (j & 0x7fffffff) >= 0x4090cc00 {
896cbd624adSopenharmony_ci        /* z <= -1075 */
897cbd624adSopenharmony_ci        // FIXME: instead of abs(j) use unsigned j
898cbd624adSopenharmony_ci
899cbd624adSopenharmony_ci        if (((j as u32) - 0xc090cc00) | (i as u32)) != 0 {
900cbd624adSopenharmony_ci            /* z < -1075 */
901cbd624adSopenharmony_ci            return s * TINY * TINY; /* underflow */
902cbd624adSopenharmony_ci        }
903cbd624adSopenharmony_ci
904cbd624adSopenharmony_ci        if p_l <= z - p_h {
905cbd624adSopenharmony_ci            return s * TINY * TINY; /* underflow */
906cbd624adSopenharmony_ci        }
907cbd624adSopenharmony_ci    }
908cbd624adSopenharmony_ci
909cbd624adSopenharmony_ci    /* compute 2**(p_h+p_l) */
910cbd624adSopenharmony_ci    let i: i32 = j & (0x7fffffff as i32);
911cbd624adSopenharmony_ci    k = (i >> 20) - 0x3ff;
912cbd624adSopenharmony_ci    let mut n: i32 = 0;
913cbd624adSopenharmony_ci
914cbd624adSopenharmony_ci    if i > 0x3fe00000 {
915cbd624adSopenharmony_ci        /* if |z| > 0.5, set n = [z+0.5] */
916cbd624adSopenharmony_ci        n = j + (0x00100000 >> (k + 1));
917cbd624adSopenharmony_ci        k = ((n & 0x7fffffff) >> 20) - 0x3ff; /* new k for n */
918cbd624adSopenharmony_ci        let t: f64 = with_set_high_word(0.0, (n & !(0x000fffff >> k)) as u32);
919cbd624adSopenharmony_ci        n = ((n & 0x000fffff) | 0x00100000) >> (20 - k);
920cbd624adSopenharmony_ci        if j < 0 {
921cbd624adSopenharmony_ci            n = -n;
922cbd624adSopenharmony_ci        }
923cbd624adSopenharmony_ci        p_h -= t;
924cbd624adSopenharmony_ci    }
925cbd624adSopenharmony_ci
926cbd624adSopenharmony_ci    let t: f64 = with_set_low_word(p_l + p_h, 0);
927cbd624adSopenharmony_ci    let u: f64 = t * LG2_H;
928cbd624adSopenharmony_ci    let v: f64 = (p_l - (t - p_h)) * LG2 + t * LG2_L;
929cbd624adSopenharmony_ci    let mut z: f64 = u + v;
930cbd624adSopenharmony_ci    let w: f64 = v - (z - u);
931cbd624adSopenharmony_ci    let t: f64 = z * z;
932cbd624adSopenharmony_ci    let t1: f64 = z - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5))));
933cbd624adSopenharmony_ci    let r: f64 = (z * t1) / (t1 - 2.0) - (w + z * w);
934cbd624adSopenharmony_ci    z = 1.0 - (r - z);
935cbd624adSopenharmony_ci    j = get_high_word(z) as i32;
936cbd624adSopenharmony_ci    j += n << 20;
937cbd624adSopenharmony_ci
938cbd624adSopenharmony_ci    if (j >> 20) <= 0 {
939cbd624adSopenharmony_ci        /* subnormal output */
940cbd624adSopenharmony_ci        z = scalbnd(z, n);
941cbd624adSopenharmony_ci    } else {
942cbd624adSopenharmony_ci        z = with_set_high_word(z, j as u32);
943cbd624adSopenharmony_ci    }
944cbd624adSopenharmony_ci
945cbd624adSopenharmony_ci    s * z
946cbd624adSopenharmony_ci}
947cbd624adSopenharmony_ci
948cbd624adSopenharmony_ci/// Absolute value (magnitude) (f64)
949cbd624adSopenharmony_ci/// Calculates the absolute value (magnitude) of the argument `x`,
950cbd624adSopenharmony_ci/// by direct manipulation of the bit representation of `x`.
951cbd624adSopenharmony_cipub fn fabsd(x: f64) -> f64 {
952cbd624adSopenharmony_ci    f64::from_bits(x.to_bits() & (u64::MAX / 2))
953cbd624adSopenharmony_ci}
954cbd624adSopenharmony_ci
955cbd624adSopenharmony_cipub fn scalbnd(x: f64, mut n: i32) -> f64 {
956cbd624adSopenharmony_ci    let x1p1023 = f64::from_bits(0x7fe0000000000000); // 0x1p1023 === 2 ^ 1023
957cbd624adSopenharmony_ci    let x1p53 = f64::from_bits(0x4340000000000000); // 0x1p53 === 2 ^ 53
958cbd624adSopenharmony_ci    let x1p_1022 = f64::from_bits(0x0010000000000000); // 0x1p-1022 === 2 ^ (-1022)
959cbd624adSopenharmony_ci
960cbd624adSopenharmony_ci    let mut y = x;
961cbd624adSopenharmony_ci
962cbd624adSopenharmony_ci    if n > 1023 {
963cbd624adSopenharmony_ci        y *= x1p1023;
964cbd624adSopenharmony_ci        n -= 1023;
965cbd624adSopenharmony_ci        if n > 1023 {
966cbd624adSopenharmony_ci            y *= x1p1023;
967cbd624adSopenharmony_ci            n -= 1023;
968cbd624adSopenharmony_ci            if n > 1023 {
969cbd624adSopenharmony_ci                n = 1023;
970cbd624adSopenharmony_ci            }
971cbd624adSopenharmony_ci        }
972cbd624adSopenharmony_ci    } else if n < -1022 {
973cbd624adSopenharmony_ci        /* make sure final n < -53 to avoid double
974cbd624adSopenharmony_ci        rounding in the subnormal range */
975cbd624adSopenharmony_ci        y *= x1p_1022 * x1p53;
976cbd624adSopenharmony_ci        n += 1022 - 53;
977cbd624adSopenharmony_ci        if n < -1022 {
978cbd624adSopenharmony_ci            y *= x1p_1022 * x1p53;
979cbd624adSopenharmony_ci            n += 1022 - 53;
980cbd624adSopenharmony_ci            if n < -1022 {
981cbd624adSopenharmony_ci                n = -1022;
982cbd624adSopenharmony_ci            }
983cbd624adSopenharmony_ci        }
984cbd624adSopenharmony_ci    }
985cbd624adSopenharmony_ci    y * f64::from_bits(((0x3ff + n) as u64) << 52)
986cbd624adSopenharmony_ci}
987cbd624adSopenharmony_ci
988cbd624adSopenharmony_ci/* origin: FreeBSD /usr/src/lib/msun/src/e_sqrt.c */
989cbd624adSopenharmony_ci/*
990cbd624adSopenharmony_ci * ====================================================
991cbd624adSopenharmony_ci * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
992cbd624adSopenharmony_ci *
993cbd624adSopenharmony_ci * Developed at SunSoft, a Sun Microsystems, Inc. business.
994cbd624adSopenharmony_ci * Permission to use, copy, modify, and distribute this
995cbd624adSopenharmony_ci * software is freely granted, provided that this notice
996cbd624adSopenharmony_ci * is preserved.
997cbd624adSopenharmony_ci * ====================================================
998cbd624adSopenharmony_ci */
999cbd624adSopenharmony_ci/* sqrt(x)
1000cbd624adSopenharmony_ci * Return correctly rounded sqrt.
1001cbd624adSopenharmony_ci *           ------------------------------------------
1002cbd624adSopenharmony_ci *           |  Use the hardware sqrt if you have one |
1003cbd624adSopenharmony_ci *           ------------------------------------------
1004cbd624adSopenharmony_ci * Method:
1005cbd624adSopenharmony_ci *   Bit by bit method using integer arithmetic. (Slow, but portable)
1006cbd624adSopenharmony_ci *   1. Normalization
1007cbd624adSopenharmony_ci *      Scale x to y in [1,4) with even powers of 2:
1008cbd624adSopenharmony_ci *      find an integer k such that  1 <= (y=x*2^(2k)) < 4, then
1009cbd624adSopenharmony_ci *              sqrt(x) = 2^k * sqrt(y)
1010cbd624adSopenharmony_ci *   2. Bit by bit computation
1011cbd624adSopenharmony_ci *      Let q  = sqrt(y) truncated to i bit after binary point (q = 1),
1012cbd624adSopenharmony_ci *           i                                                   0
1013cbd624adSopenharmony_ci *                                     i+1         2
1014cbd624adSopenharmony_ci *          s  = 2*q , and      y  =  2   * ( y - q  ).         (1)
1015cbd624adSopenharmony_ci *           i      i            i                 i
1016cbd624adSopenharmony_ci *
1017cbd624adSopenharmony_ci *      To compute q    from q , one checks whether
1018cbd624adSopenharmony_ci *                  i+1       i
1019cbd624adSopenharmony_ci *
1020cbd624adSopenharmony_ci *                            -(i+1) 2
1021cbd624adSopenharmony_ci *                      (q + 2      ) <= y.                     (2)
1022cbd624adSopenharmony_ci *                        i
1023cbd624adSopenharmony_ci *                                                            -(i+1)
1024cbd624adSopenharmony_ci *      If (2) is false, then q   = q ; otherwise q   = q  + 2      .
1025cbd624adSopenharmony_ci *                             i+1   i             i+1   i
1026cbd624adSopenharmony_ci *
1027cbd624adSopenharmony_ci *      With some algebraic manipulation, it is not difficult to see
1028cbd624adSopenharmony_ci *      that (2) is equivalent to
1029cbd624adSopenharmony_ci *                             -(i+1)
1030cbd624adSopenharmony_ci *                      s  +  2       <= y                      (3)
1031cbd624adSopenharmony_ci *                       i                i
1032cbd624adSopenharmony_ci *
1033cbd624adSopenharmony_ci *      The advantage of (3) is that s  and y  can be computed by
1034cbd624adSopenharmony_ci *                                    i      i
1035cbd624adSopenharmony_ci *      the following recurrence formula:
1036cbd624adSopenharmony_ci *          if (3) is false
1037cbd624adSopenharmony_ci *
1038cbd624adSopenharmony_ci *          s     =  s  ,       y    = y   ;                    (4)
1039cbd624adSopenharmony_ci *           i+1      i          i+1    i
1040cbd624adSopenharmony_ci *
1041cbd624adSopenharmony_ci *          otherwise,
1042cbd624adSopenharmony_ci *                         -i                     -(i+1)
1043cbd624adSopenharmony_ci *          s     =  s  + 2  ,  y    = y  -  s  - 2             (5)
1044cbd624adSopenharmony_ci *           i+1      i          i+1    i     i
1045cbd624adSopenharmony_ci *
1046cbd624adSopenharmony_ci *      One may easily use induction to prove (4) and (5).
1047cbd624adSopenharmony_ci *      Note. Since the left hand side of (3) contain only i+2 bits,
1048cbd624adSopenharmony_ci *            it does not necessary to do a full (53-bit) comparison
1049cbd624adSopenharmony_ci *            in (3).
1050cbd624adSopenharmony_ci *   3. Final rounding
1051cbd624adSopenharmony_ci *      After generating the 53 bits result, we compute one more bit.
1052cbd624adSopenharmony_ci *      Together with the remainder, we can decide whether the
1053cbd624adSopenharmony_ci *      result is exact, bigger than 1/2ulp, or less than 1/2ulp
1054cbd624adSopenharmony_ci *      (it will never equal to 1/2ulp).
1055cbd624adSopenharmony_ci *      The rounding mode can be detected by checking whether
1056cbd624adSopenharmony_ci *      huge + tiny is equal to huge, and whether huge - tiny is
1057cbd624adSopenharmony_ci *      equal to huge for some floating point number "huge" and "tiny".
1058cbd624adSopenharmony_ci *
1059cbd624adSopenharmony_ci * Special cases:
1060cbd624adSopenharmony_ci *      sqrt(+-0) = +-0         ... exact
1061cbd624adSopenharmony_ci *      sqrt(inf) = inf
1062cbd624adSopenharmony_ci *      sqrt(-ve) = NaN         ... with invalid signal
1063cbd624adSopenharmony_ci *      sqrt(NaN) = NaN         ... with invalid signal for signaling NaN
1064cbd624adSopenharmony_ci */
1065cbd624adSopenharmony_ci
1066cbd624adSopenharmony_cipub fn sqrtd(x: f64) -> f64 {
1067cbd624adSopenharmony_ci    #[cfg(target_feature = "sse2")]
1068cbd624adSopenharmony_ci    {
1069cbd624adSopenharmony_ci        // Note: This path is unlikely since LLVM will usually have already
1070cbd624adSopenharmony_ci        // optimized sqrt calls into hardware instructions if sse2 is available,
1071cbd624adSopenharmony_ci        // but if someone does end up here they'll apprected the speed increase.
1072cbd624adSopenharmony_ci        #[cfg(target_arch = "x86")]
1073cbd624adSopenharmony_ci        use core::arch::x86::*;
1074cbd624adSopenharmony_ci        #[cfg(target_arch = "x86_64")]
1075cbd624adSopenharmony_ci        use core::arch::x86_64::*;
1076cbd624adSopenharmony_ci        // SAFETY: safe, since `_mm_set_sd` takes a 64-bit float, and returns
1077cbd624adSopenharmony_ci        // a 128-bit type with the lowest 64-bits as `x`, `_mm_sqrt_ss` calculates
1078cbd624adSopenharmony_ci        // the sqrt of this 128-bit vector, and `_mm_cvtss_f64` extracts the lower
1079cbd624adSopenharmony_ci        // 64-bits as a 64-bit float.
1080cbd624adSopenharmony_ci        unsafe {
1081cbd624adSopenharmony_ci            let m = _mm_set_sd(x);
1082cbd624adSopenharmony_ci            let m_sqrt = _mm_sqrt_pd(m);
1083cbd624adSopenharmony_ci            _mm_cvtsd_f64(m_sqrt)
1084cbd624adSopenharmony_ci        }
1085cbd624adSopenharmony_ci    }
1086cbd624adSopenharmony_ci    #[cfg(not(target_feature = "sse2"))]
1087cbd624adSopenharmony_ci    {
1088cbd624adSopenharmony_ci        use core::num::Wrapping;
1089cbd624adSopenharmony_ci
1090cbd624adSopenharmony_ci        const TINY: f64 = 1.0e-300;
1091cbd624adSopenharmony_ci
1092cbd624adSopenharmony_ci        let mut z: f64;
1093cbd624adSopenharmony_ci        let sign: Wrapping<u32> = Wrapping(0x80000000);
1094cbd624adSopenharmony_ci        let mut ix0: i32;
1095cbd624adSopenharmony_ci        let mut s0: i32;
1096cbd624adSopenharmony_ci        let mut q: i32;
1097cbd624adSopenharmony_ci        let mut m: i32;
1098cbd624adSopenharmony_ci        let mut t: i32;
1099cbd624adSopenharmony_ci        let mut i: i32;
1100cbd624adSopenharmony_ci        let mut r: Wrapping<u32>;
1101cbd624adSopenharmony_ci        let mut t1: Wrapping<u32>;
1102cbd624adSopenharmony_ci        let mut s1: Wrapping<u32>;
1103cbd624adSopenharmony_ci        let mut ix1: Wrapping<u32>;
1104cbd624adSopenharmony_ci        let mut q1: Wrapping<u32>;
1105cbd624adSopenharmony_ci
1106cbd624adSopenharmony_ci        ix0 = (x.to_bits() >> 32) as i32;
1107cbd624adSopenharmony_ci        ix1 = Wrapping(x.to_bits() as u32);
1108cbd624adSopenharmony_ci
1109cbd624adSopenharmony_ci        /* take care of Inf and NaN */
1110cbd624adSopenharmony_ci        if (ix0 & 0x7ff00000) == 0x7ff00000 {
1111cbd624adSopenharmony_ci            return x * x + x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf, sqrt(-inf)=sNaN */
1112cbd624adSopenharmony_ci        }
1113cbd624adSopenharmony_ci        /* take care of zero */
1114cbd624adSopenharmony_ci        if ix0 <= 0 {
1115cbd624adSopenharmony_ci            if ((ix0 & !(sign.0 as i32)) | ix1.0 as i32) == 0 {
1116cbd624adSopenharmony_ci                return x; /* sqrt(+-0) = +-0 */
1117cbd624adSopenharmony_ci            }
1118cbd624adSopenharmony_ci            if ix0 < 0 {
1119cbd624adSopenharmony_ci                return (x - x) / (x - x); /* sqrt(-ve) = sNaN */
1120cbd624adSopenharmony_ci            }
1121cbd624adSopenharmony_ci        }
1122cbd624adSopenharmony_ci        /* normalize x */
1123cbd624adSopenharmony_ci        m = ix0 >> 20;
1124cbd624adSopenharmony_ci        if m == 0 {
1125cbd624adSopenharmony_ci            /* subnormal x */
1126cbd624adSopenharmony_ci            while ix0 == 0 {
1127cbd624adSopenharmony_ci                m -= 21;
1128cbd624adSopenharmony_ci                ix0 |= (ix1 >> 11).0 as i32;
1129cbd624adSopenharmony_ci                ix1 <<= 21;
1130cbd624adSopenharmony_ci            }
1131cbd624adSopenharmony_ci            i = 0;
1132cbd624adSopenharmony_ci            while (ix0 & 0x00100000) == 0 {
1133cbd624adSopenharmony_ci                i += 1;
1134cbd624adSopenharmony_ci                ix0 <<= 1;
1135cbd624adSopenharmony_ci            }
1136cbd624adSopenharmony_ci            m -= i - 1;
1137cbd624adSopenharmony_ci            ix0 |= (ix1 >> (32 - i) as usize).0 as i32;
1138cbd624adSopenharmony_ci            ix1 = ix1 << i as usize;
1139cbd624adSopenharmony_ci        }
1140cbd624adSopenharmony_ci        m -= 1023; /* unbias exponent */
1141cbd624adSopenharmony_ci        ix0 = (ix0 & 0x000fffff) | 0x00100000;
1142cbd624adSopenharmony_ci        if (m & 1) == 1 {
1143cbd624adSopenharmony_ci            /* odd m, double x to make it even */
1144cbd624adSopenharmony_ci            ix0 += ix0 + ((ix1 & sign) >> 31).0 as i32;
1145cbd624adSopenharmony_ci            ix1 += ix1;
1146cbd624adSopenharmony_ci        }
1147cbd624adSopenharmony_ci        m >>= 1; /* m = [m/2] */
1148cbd624adSopenharmony_ci
1149cbd624adSopenharmony_ci        /* generate sqrt(x) bit by bit */
1150cbd624adSopenharmony_ci        ix0 += ix0 + ((ix1 & sign) >> 31).0 as i32;
1151cbd624adSopenharmony_ci        ix1 += ix1;
1152cbd624adSopenharmony_ci        q = 0; /* [q,q1] = sqrt(x) */
1153cbd624adSopenharmony_ci        q1 = Wrapping(0);
1154cbd624adSopenharmony_ci        s0 = 0;
1155cbd624adSopenharmony_ci        s1 = Wrapping(0);
1156cbd624adSopenharmony_ci        r = Wrapping(0x00200000); /* r = moving bit from right to left */
1157cbd624adSopenharmony_ci
1158cbd624adSopenharmony_ci        while r != Wrapping(0) {
1159cbd624adSopenharmony_ci            t = s0 + r.0 as i32;
1160cbd624adSopenharmony_ci            if t <= ix0 {
1161cbd624adSopenharmony_ci                s0 = t + r.0 as i32;
1162cbd624adSopenharmony_ci                ix0 -= t;
1163cbd624adSopenharmony_ci                q += r.0 as i32;
1164cbd624adSopenharmony_ci            }
1165cbd624adSopenharmony_ci            ix0 += ix0 + ((ix1 & sign) >> 31).0 as i32;
1166cbd624adSopenharmony_ci            ix1 += ix1;
1167cbd624adSopenharmony_ci            r >>= 1;
1168cbd624adSopenharmony_ci        }
1169cbd624adSopenharmony_ci
1170cbd624adSopenharmony_ci        r = sign;
1171cbd624adSopenharmony_ci        while r != Wrapping(0) {
1172cbd624adSopenharmony_ci            t1 = s1 + r;
1173cbd624adSopenharmony_ci            t = s0;
1174cbd624adSopenharmony_ci            if t < ix0 || (t == ix0 && t1 <= ix1) {
1175cbd624adSopenharmony_ci                s1 = t1 + r;
1176cbd624adSopenharmony_ci                if (t1 & sign) == sign && (s1 & sign) == Wrapping(0) {
1177cbd624adSopenharmony_ci                    s0 += 1;
1178cbd624adSopenharmony_ci                }
1179cbd624adSopenharmony_ci                ix0 -= t;
1180cbd624adSopenharmony_ci                if ix1 < t1 {
1181cbd624adSopenharmony_ci                    ix0 -= 1;
1182cbd624adSopenharmony_ci                }
1183cbd624adSopenharmony_ci                ix1 -= t1;
1184cbd624adSopenharmony_ci                q1 += r;
1185cbd624adSopenharmony_ci            }
1186cbd624adSopenharmony_ci            ix0 += ix0 + ((ix1 & sign) >> 31).0 as i32;
1187cbd624adSopenharmony_ci            ix1 += ix1;
1188cbd624adSopenharmony_ci            r >>= 1;
1189cbd624adSopenharmony_ci        }
1190cbd624adSopenharmony_ci
1191cbd624adSopenharmony_ci        /* use floating add to find out rounding direction */
1192cbd624adSopenharmony_ci        if (ix0 as u32 | ix1.0) != 0 {
1193cbd624adSopenharmony_ci            z = 1.0 - TINY; /* raise inexact flag */
1194cbd624adSopenharmony_ci            if z >= 1.0 {
1195cbd624adSopenharmony_ci                z = 1.0 + TINY;
1196cbd624adSopenharmony_ci                if q1.0 == 0xffffffff {
1197cbd624adSopenharmony_ci                    q1 = Wrapping(0);
1198cbd624adSopenharmony_ci                    q += 1;
1199cbd624adSopenharmony_ci                } else if z > 1.0 {
1200cbd624adSopenharmony_ci                    if q1.0 == 0xfffffffe {
1201cbd624adSopenharmony_ci                        q += 1;
1202cbd624adSopenharmony_ci                    }
1203cbd624adSopenharmony_ci                    q1 += Wrapping(2);
1204cbd624adSopenharmony_ci                } else {
1205cbd624adSopenharmony_ci                    q1 += q1 & Wrapping(1);
1206cbd624adSopenharmony_ci                }
1207cbd624adSopenharmony_ci            }
1208cbd624adSopenharmony_ci        }
1209cbd624adSopenharmony_ci        ix0 = (q >> 1) + 0x3fe00000;
1210cbd624adSopenharmony_ci        ix1 = q1 >> 1;
1211cbd624adSopenharmony_ci        if (q & 1) == 1 {
1212cbd624adSopenharmony_ci            ix1 |= sign;
1213cbd624adSopenharmony_ci        }
1214cbd624adSopenharmony_ci        ix0 += m << 20;
1215cbd624adSopenharmony_ci        f64::from_bits((ix0 as u64) << 32 | ix1.0 as u64)
1216cbd624adSopenharmony_ci    }
1217cbd624adSopenharmony_ci}
1218cbd624adSopenharmony_ci
1219cbd624adSopenharmony_ci#[inline]
1220cbd624adSopenharmony_cifn get_high_word(x: f64) -> u32 {
1221cbd624adSopenharmony_ci    (x.to_bits() >> 32) as u32
1222cbd624adSopenharmony_ci}
1223cbd624adSopenharmony_ci
1224cbd624adSopenharmony_ci#[inline]
1225cbd624adSopenharmony_cifn with_set_high_word(f: f64, hi: u32) -> f64 {
1226cbd624adSopenharmony_ci    let mut tmp = f.to_bits();
1227cbd624adSopenharmony_ci    tmp &= 0x00000000_ffffffff;
1228cbd624adSopenharmony_ci    tmp |= (hi as u64) << 32;
1229cbd624adSopenharmony_ci    f64::from_bits(tmp)
1230cbd624adSopenharmony_ci}
1231cbd624adSopenharmony_ci
1232cbd624adSopenharmony_ci#[inline]
1233cbd624adSopenharmony_cifn with_set_low_word(f: f64, lo: u32) -> f64 {
1234cbd624adSopenharmony_ci    let mut tmp = f.to_bits();
1235cbd624adSopenharmony_ci    tmp &= 0xffffffff_00000000;
1236cbd624adSopenharmony_ci    tmp |= lo as u64;
1237cbd624adSopenharmony_ci    f64::from_bits(tmp)
1238cbd624adSopenharmony_ci}
1239