| 1 | #![cfg(feature = "f16")] |
| 2 | |
| 3 | use lexical_util::bf16::bf16; |
| 4 | use lexical_util::num::Float; |
| 5 | |
| 6 | #[test] |
| 7 | fn as_f32_test() { |
| 8 | assert_eq!(bf16::from_bits(1).as_f32(), 9.18355e-41f32); |
| 9 | assert_eq!(bf16::ZERO.as_f32_const(), 0.0f32); |
| 10 | assert_eq!(bf16::ZERO.to_bits(), 0); |
| 11 | assert_eq!(bf16::ONE.as_f32_const(), 1.0f32); |
| 12 | assert_eq!(bf16::ONE.to_bits(), (127 << 7)); |
| 13 | assert_eq!(bf16::TWO.as_f32_const(), 2.0f32); |
| 14 | assert_eq!(bf16::TWO.to_bits(), (128 << 7)); |
| 15 | assert_eq!(bf16::from_bits(126 << 7).as_f32_const(), 0.5f32); |
| 16 | assert!(bf16::NAN.as_f32_const().is_nan()); |
| 17 | assert!(bf16::INFINITY.as_f32_const().is_inf()); |
| 18 | assert!(bf16::NEG_INFINITY.as_f32_const().is_inf()); |
| 19 | } |
| 20 | |
| 21 | #[test] |
| 22 | fn from_f32_test() { |
| 23 | assert_eq!(bf16::from_f32_const(4.5917e-41f32).to_bits(), 0); |
| 24 | assert_eq!(bf16::from_f32_const(4.5918e-41f32).to_bits(), 0); |
| 25 | assert_eq!(bf16::from_f32_const(4.5919e-41f32).to_bits(), 1); |
| 26 | assert_eq!(bf16::from_f32_const(9.18354e-41f32).to_bits(), 1); |
| 27 | assert_eq!(bf16::from_f32_const(9.18355e-41f32).to_bits(), 1); |
| 28 | assert_eq!(bf16::from_f32_const(9.18356e-41f32).to_bits(), 1); |
| 29 | assert_eq!(bf16::from_f32_const(1.37752e-40f32).to_bits(), 1); |
| 30 | assert_eq!(bf16::from_f32_const(1.37753e-40f32).to_bits(), 2); |
| 31 | assert_eq!(bf16::from_f32_const(1.37754e-40f32).to_bits(), 2); |
| 32 | assert!(bf16::from_f32_const(f32::NAN).is_nan()); |
| 33 | assert!(bf16::from_f32_const(f32::INFINITY).is_inf()); |
| 34 | assert!(bf16::from_f32_const(f32::NEG_INFINITY).is_inf()); |
| 35 | } |
| 36 | |
| 37 | #[test] |
| 38 | fn math_tests() { |
| 39 | assert_eq!(bf16::ONE + bf16::ONE, bf16::TWO); |
| 40 | assert_eq!(bf16::ONE * bf16::ONE, bf16::ONE); |
| 41 | assert_eq!(bf16::ONE / bf16::ONE, bf16::ONE); |
| 42 | assert_eq!(bf16::ONE - bf16::ONE, bf16::ZERO); |
| 43 | assert_eq!(bf16::ONE % bf16::ONE, bf16::ZERO); |
| 44 | } |