Coverage Report

Created: 2025-08-29 06:18

/rust/registry/src/index.crates.io-6f17d22bba15001f/libm-0.2.11/src/math/hypot.rs
Line
Count
Source (jump to first uncovered line)
1
use core::f64;
2
3
use super::sqrt;
4
5
const SPLIT: f64 = 134217728. + 1.; // 0x1p27 + 1 === (2 ^ 27) + 1
6
7
0
fn sq(x: f64) -> (f64, f64) {
8
0
    let xh: f64;
9
0
    let xl: f64;
10
0
    let xc: f64;
11
0
12
0
    xc = x * SPLIT;
13
0
    xh = x - xc + xc;
14
0
    xl = x - xh;
15
0
    let hi = x * x;
16
0
    let lo = xh * xh - hi + 2. * xh * xl + xl * xl;
17
0
    (hi, lo)
18
0
}
19
20
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
21
0
pub fn hypot(mut x: f64, mut y: f64) -> f64 {
22
0
    let x1p700 = f64::from_bits(0x6bb0000000000000); // 0x1p700 === 2 ^ 700
23
0
    let x1p_700 = f64::from_bits(0x1430000000000000); // 0x1p-700 === 2 ^ -700
24
0
25
0
    let mut uxi = x.to_bits();
26
0
    let mut uyi = y.to_bits();
27
0
    let uti;
28
0
    let ex: i64;
29
0
    let ey: i64;
30
0
    let mut z: f64;
31
0
32
0
    /* arrange |x| >= |y| */
33
0
    uxi &= -1i64 as u64 >> 1;
34
0
    uyi &= -1i64 as u64 >> 1;
35
0
    if uxi < uyi {
36
0
        uti = uxi;
37
0
        uxi = uyi;
38
0
        uyi = uti;
39
0
    }
40
41
    /* special cases */
42
0
    ex = (uxi >> 52) as i64;
43
0
    ey = (uyi >> 52) as i64;
44
0
    x = f64::from_bits(uxi);
45
0
    y = f64::from_bits(uyi);
46
0
    /* note: hypot(inf,nan) == inf */
47
0
    if ey == 0x7ff {
48
0
        return y;
49
0
    }
50
0
    if ex == 0x7ff || uyi == 0 {
51
0
        return x;
52
0
    }
53
0
    /* note: hypot(x,y) ~= x + y*y/x/2 with inexact for small y/x */
54
0
    /* 64 difference is enough for ld80 double_t */
55
0
    if ex - ey > 64 {
56
0
        return x + y;
57
0
    }
58
0
59
0
    /* precise sqrt argument in nearest rounding mode without overflow */
60
0
    /* xh*xh must not overflow and xl*xl must not underflow in sq */
61
0
    z = 1.;
62
0
    if ex > 0x3ff + 510 {
63
0
        z = x1p700;
64
0
        x *= x1p_700;
65
0
        y *= x1p_700;
66
0
    } else if ey < 0x3ff - 450 {
67
0
        z = x1p_700;
68
0
        x *= x1p700;
69
0
        y *= x1p700;
70
0
    }
71
0
    let (hx, lx) = sq(x);
72
0
    let (hy, ly) = sq(y);
73
0
    z * sqrt(ly + lx + hy + hx)
74
0
}