Skip to main content

libm/math/
hypotf.rs

1use super::sqrtf;
2
3#[cfg_attr(assert_no_panic, no_panic::no_panic)]
4pub fn hypotf(mut x: f32, mut y: f32) -> f32 {
5    let x1p90 = f32::from_bits(0x6c800000); // 0x1p90f === 2 ^ 90
6    let x1p_90 = f32::from_bits(0x12800000); // 0x1p-90f === 2 ^ -90
7
8    let mut uxi = x.to_bits();
9    let mut uyi = y.to_bits();
10    let uti;
11    let mut z: f32;
12
13    uxi &= -1i32 as u32 >> 1;
14    uyi &= -1i32 as u32 >> 1;
15    if uxi < uyi {
16        uti = uxi;
17        uxi = uyi;
18        uyi = uti;
19    }
20
21    x = f32::from_bits(uxi);
22    y = f32::from_bits(uyi);
23    if uyi == 0xff << 23 {
24        return y;
25    }
26    if uxi >= 0xff << 23 || uyi == 0 || uxi - uyi >= 25 << 23 {
27        return x + y;
28    }
29
30    z = 1.;
31    if uxi >= (0x7f + 60) << 23 {
32        z = x1p90;
33        x *= x1p_90;
34        y *= x1p_90;
35    } else if uyi < (0x7f - 60) << 23 {
36        z = x1p_90;
37        x *= x1p90;
38        y *= x1p90;
39    }
40    z * sqrtf((x as f64 * x as f64 + y as f64 * y as f64) as f32)
41}