use super::{Computed, Double, Exceptions, Extended, Single, double, extended, rounding_from_bits};
use crate::Flags;
use crate::test_util::ROUNDINGS;
#[test]
fn exception_masks_partition_the_status_word() {
for bits in 0u8..64 {
let e = Exceptions(bits);
assert_eq!(e.pre_computation(), Exceptions(bits & 0b000_111));
assert_eq!(e.post_computation(), Exceptions(bits & 0b111_000));
assert_eq!(e & Exceptions::ALL, e);
assert!(Exceptions::ALL.contains(e));
assert_eq!(e.when(false), Exceptions::NONE);
}
}
#[test]
fn rounding_control_reads_two_bits() {
for bits in 0u16..256 {
assert_eq!(
rounding_from_bits(bits as u8),
ROUNDINGS[usize::from(bits & 3)]
);
}
}
#[test]
fn semantic_flags_map_to_x86_bits_without_a_denormal_operand_flag() {
let mut combined = Flags::NONE;
for (flag, exception, bits) in [
(Flags::INVALID, Exceptions::IE, 1),
(Flags::DIVIDE_BY_ZERO, Exceptions::ZE, 4),
(Flags::OVERFLOW, Exceptions::OE, 8),
(Flags::UNDERFLOW, Exceptions::UE, 16),
(Flags::INEXACT, Exceptions::PE, 32),
] {
assert_eq!(Exceptions::from(flag), exception);
assert_eq!(exception.0, bits);
combined |= flag;
}
assert_eq!(
Exceptions::from(Flags::OVERFLOW | Flags::INEXACT),
Exceptions::OE | Exceptions::PE
);
assert_eq!(Exceptions::from(combined).0, 0x3D);
assert_eq!(Exceptions::from(Flags::NONE), Exceptions::NONE);
}
#[test]
fn equal_extended_nans_propagate_the_positive_one() {
let positive = Extended {
sign_exponent: 0x7FFF,
significand: 0xC000_0000_0000_0001,
};
let negative = Extended {
sign_exponent: 0xFFFF,
..positive
};
for (a, b) in [(positive, negative), (negative, positive)] {
assert_eq!(extended::propagate_nan(a, b), (positive, Exceptions::NONE));
}
}
#[test]
fn a_nan_keeps_its_leading_payload_across_formats() {
assert_eq!(
double::to_single(Double(0xFFFC_0000_2000_0000)),
Computed::Finished(Single(0xFFE0_0001), Exceptions::NONE)
);
assert_eq!(
double::to_single(Double(0x7FF4_0000_0000_0001)),
Computed::Finished(Single(0x7FE0_0000), Exceptions::IE)
);
assert_eq!(
double::from_single(Single(0x7FA0_0001)),
(Double(0x7FFC_0000_2000_0000), Exceptions::IE)
);
}