Coverage Report

Created: 2025-07-18 06:22

/rust/registry/src/index.crates.io-6f17d22bba15001f/libm-0.2.11/src/math/fabsf.rs
Line
Count
Source (jump to first uncovered line)
1
/// Absolute value (magnitude) (f32)
2
/// Calculates the absolute value (magnitude) of the argument `x`,
3
/// by direct manipulation of the bit representation of `x`.
4
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
5
0
pub fn fabsf(x: f32) -> f32 {
6
0
    // On wasm32 we know that LLVM's intrinsic will compile to an optimized
7
0
    // `f32.abs` native instruction, so we can leverage this for both code size
8
0
    // and speed.
9
0
    llvm_intrinsically_optimized! {
10
0
        #[cfg(target_arch = "wasm32")] {
11
0
            return unsafe { ::core::intrinsics::fabsf32(x) }
12
0
        }
13
0
    }
14
0
    f32::from_bits(x.to_bits() & 0x7fffffff)
15
0
}
16
17
// PowerPC tests are failing on LLVM 13: https://github.com/rust-lang/rust/issues/88520
18
#[cfg(not(target_arch = "powerpc64"))]
19
#[cfg(test)]
20
mod tests {
21
    use core::f32::*;
22
23
    use super::*;
24
25
    #[test]
26
    fn sanity_check() {
27
        assert_eq!(fabsf(-1.0), 1.0);
28
        assert_eq!(fabsf(2.8), 2.8);
29
    }
30
31
    /// The spec: https://en.cppreference.com/w/cpp/numeric/math/fabs
32
    #[test]
33
    fn spec_tests() {
34
        assert!(fabsf(NAN).is_nan());
35
        for f in [0.0, -0.0].iter().copied() {
36
            assert_eq!(fabsf(f), 0.0);
37
        }
38
        for f in [INFINITY, NEG_INFINITY].iter().copied() {
39
            assert_eq!(fabsf(f), INFINITY);
40
        }
41
    }
42
}