| 1 | /* SPDX-License-Identifier: MIT OR Apache-2.0 */ |
| 2 | //! IEEE 754-2011 `maxNum`. This has been superseded by IEEE 754-2019 `maximumNumber`. |
| 3 | //! |
| 4 | //! Per the spec, returns the canonicalized result of: |
| 5 | //! - `x` if `x > y` |
| 6 | //! - `y` if `y > x` |
| 7 | //! - The other number if one is NaN |
| 8 | //! - Otherwise, either `x` or `y`, canonicalized |
| 9 | //! - -0.0 and +0.0 may be disregarded (unlike newer operations) |
| 10 | //! |
| 11 | //! Excluded from our implementation is sNaN handling. |
| 12 | //! |
| 13 | //! More on the differences: [link]. |
| 14 | //! |
| 15 | //! [link]: https://grouper.ieee.org/groups/msc/ANSI_IEEE-Std-754-2019/background/minNum_maxNum_Removal_Demotion_v3.pdf |
| 16 | |
| 17 | use super::super::Float; |
| 18 | |
| 19 | #[cfg_attr (all(test, assert_no_panic), no_panic::no_panic)] |
| 20 | pub fn fmax<F: Float>(x: F, y: F) -> F { |
| 21 | let res: F = if x.is_nan() || x < y { y } else { x }; |
| 22 | // Canonicalize |
| 23 | res * F::ONE |
| 24 | } |
| 25 | |
| 26 | #[cfg (test)] |
| 27 | mod tests { |
| 28 | use super::*; |
| 29 | use crate::support::{Hexf, Int}; |
| 30 | |
| 31 | fn spec_test<F: Float>() { |
| 32 | let cases = [ |
| 33 | (F::ZERO, F::ZERO, F::ZERO), |
| 34 | (F::ONE, F::ONE, F::ONE), |
| 35 | (F::ZERO, F::ONE, F::ONE), |
| 36 | (F::ONE, F::ZERO, F::ONE), |
| 37 | (F::ZERO, F::NEG_ONE, F::ZERO), |
| 38 | (F::NEG_ONE, F::ZERO, F::ZERO), |
| 39 | (F::INFINITY, F::ZERO, F::INFINITY), |
| 40 | (F::NEG_INFINITY, F::ZERO, F::ZERO), |
| 41 | (F::NAN, F::ZERO, F::ZERO), |
| 42 | (F::ZERO, F::NAN, F::ZERO), |
| 43 | (F::NAN, F::NAN, F::NAN), |
| 44 | ]; |
| 45 | |
| 46 | for (x, y, res) in cases { |
| 47 | let val = fmax(x, y); |
| 48 | assert_biteq!(val, res, "fmax({}, {})" , Hexf(x), Hexf(y)); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | #[test ] |
| 53 | #[cfg (f16_enabled)] |
| 54 | fn spec_tests_f16() { |
| 55 | spec_test::<f16>(); |
| 56 | } |
| 57 | |
| 58 | #[test ] |
| 59 | fn spec_tests_f32() { |
| 60 | spec_test::<f32>(); |
| 61 | } |
| 62 | |
| 63 | #[test ] |
| 64 | fn spec_tests_f64() { |
| 65 | spec_test::<f64>(); |
| 66 | } |
| 67 | |
| 68 | #[test ] |
| 69 | #[cfg (f128_enabled)] |
| 70 | fn spec_tests_f128() { |
| 71 | spec_test::<f128>(); |
| 72 | } |
| 73 | } |
| 74 | |