]> git.proxmox.com Git - rustc.git/blame - vendor/compiler_builtins/libm/src/math/fabsf.rs
New upstream version 1.41.1+dfsg1
[rustc.git] / vendor / compiler_builtins / libm / src / math / fabsf.rs
CommitLineData
dc9dc135
XL
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`.
48663c56 4#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
8faf50e0 5pub fn fabsf(x: f32) -> f32 {
a1dfa0c6
XL
6 // On wasm32 we know that LLVM's intrinsic will compile to an optimized
7 // `f32.abs` native instruction, so we can leverage this for both code size
8 // and speed.
9 llvm_intrinsically_optimized! {
10 #[cfg(target_arch = "wasm32")] {
11 return unsafe { ::core::intrinsics::fabsf32(x) }
12 }
13 }
8faf50e0
XL
14 f32::from_bits(x.to_bits() & 0x7fffffff)
15}
60c5eb7d
XL
16
17#[cfg(test)]
18mod tests {
19 use super::*;
20 use core::f32::*;
21
22 #[test]
23 fn sanity_check() {
24 assert_eq!(fabsf(-1.0), 1.0);
25 assert_eq!(fabsf(2.8), 2.8);
26 }
27
28 /// The spec: https://en.cppreference.com/w/cpp/numeric/math/fabs
29 #[test]
30 fn spec_tests() {
31 assert!(fabsf(NAN).is_nan());
32 for f in [0.0, -0.0].iter().copied() {
33 assert_eq!(fabsf(f), 0.0);
34 }
35 for f in [INFINITY, NEG_INFINITY].iter().copied() {
36 assert_eq!(fabsf(f), INFINITY);
37 }
38 }
39}