Coverage Report

Created: 2026-09-28 07:13

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