| 1 | /// Absolute value (magnitude) (f32) |
| 2 | /// |
| 3 | /// Calculates the absolute value (magnitude) of the argument `x`, |
| 4 | /// by direct manipulation of the bit representation of `x`. |
| 5 | #[cfg_attr (all(test, assert_no_panic), no_panic::no_panic)] |
| 6 | pub fn fabsf(x: f32) -> f32 { |
| 7 | select_implementation! { |
| 8 | name: fabsf, |
| 9 | use_arch: all(target_arch = "wasm32" , intrinsics_enabled), |
| 10 | args: x, |
| 11 | } |
| 12 | |
| 13 | super::generic::fabs(x) |
| 14 | } |
| 15 | |
| 16 | // PowerPC tests are failing on LLVM 13: https://github.com/rust-lang/rust/issues/88520 |
| 17 | #[cfg (not(target_arch = "powerpc64" ))] |
| 18 | #[cfg (test)] |
| 19 | mod tests { |
| 20 | use super::*; |
| 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(f32::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 [f32::INFINITY, f32::NEG_INFINITY].iter().copied() { |
| 36 | assert_eq!(fabsf(f), f32::INFINITY); |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |