Ensure NaN references values go through function boundary for next_up/down.

This commit is contained in:
Orson Peters
2021-11-29 12:10:09 +01:00
committed by Urgau
parent 3241bbcbc7
commit de757e8a98
2 changed files with 36 additions and 12 deletions
+18 -6
View File
@@ -308,12 +308,18 @@ fn test_next_up() {
let smallest_normal = f32::from_bits(0x0080_0000);
// Check that NaNs roundtrip.
// Because x87 can lose NaN bits when passed through a function, ensure the reference value
// also passes through a function boundary.
#[inline(never)]
fn identity(x: f32) -> f32 {
crate::hint::black_box(x)
}
let nan0 = f32::NAN;
let nan1 = f32::from_bits(f32::NAN.to_bits() ^ 0x002a_aaaa);
let nan2 = f32::from_bits(f32::NAN.to_bits() ^ 0x0055_5555);
assert_eq!(nan0.next_up().to_bits(), nan0.to_bits());
assert_eq!(nan1.next_up().to_bits(), nan1.to_bits());
assert_eq!(nan2.next_up().to_bits(), nan2.to_bits());
assert_eq!(nan0.next_up().to_bits(), identity(nan0).to_bits());
assert_eq!(nan1.next_up().to_bits(), identity(nan1).to_bits());
assert_eq!(nan2.next_up().to_bits(), identity(nan2).to_bits());
assert_eq!(f32::NEG_INFINITY.next_up(), f32::MIN);
assert_eq!(f32::MIN.next_up(), -max_down);
@@ -339,12 +345,18 @@ fn test_next_down() {
let smallest_normal = f32::from_bits(0x0080_0000);
// Check that NaNs roundtrip.
// Because x87 can lose NaN bits when passed through a function, ensure the reference value
// also passes through a function boundary.
#[inline(never)]
fn identity(x: f32) -> f32 {
crate::hint::black_box(x)
}
let nan0 = f32::NAN;
let nan1 = f32::from_bits(f32::NAN.to_bits() ^ 0x002a_aaaa);
let nan2 = f32::from_bits(f32::NAN.to_bits() ^ 0x0055_5555);
assert_eq!(nan0.next_down().to_bits(), nan0.to_bits());
assert_eq!(nan1.next_down().to_bits(), nan1.to_bits());
assert_eq!(nan2.next_down().to_bits(), nan2.to_bits());
assert_eq!(nan0.next_down().to_bits(), identity(nan0).to_bits());
assert_eq!(nan1.next_down().to_bits(), identity(nan1).to_bits());
assert_eq!(nan2.next_down().to_bits(), identity(nan2).to_bits());
assert_eq!(f32::NEG_INFINITY.next_down(), f32::NEG_INFINITY);
assert_eq!(f32::MIN.next_down(), f32::NEG_INFINITY);
+18 -6
View File
@@ -298,12 +298,18 @@ fn test_next_up() {
let smallest_normal = f64::from_bits(0x0010_0000_0000_0000);
// Check that NaNs roundtrip.
// Because x87 can lose NaN bits when passed through a function, ensure the reference value
// also passes through a function boundary.
#[inline(never)]
fn identity(x: f64) -> f64 {
crate::hint::black_box(x)
}
let nan0 = f64::NAN;
let nan1 = f64::from_bits(f64::NAN.to_bits() ^ 0x000a_aaaa_aaaa_aaaa);
let nan2 = f64::from_bits(f64::NAN.to_bits() ^ 0x0005_5555_5555_5555);
assert_eq!(nan0.next_up().to_bits(), nan0.to_bits());
assert_eq!(nan1.next_up().to_bits(), nan1.to_bits());
assert_eq!(nan2.next_up().to_bits(), nan2.to_bits());
assert_eq!(nan0.next_up().to_bits(), identity(nan0).to_bits());
assert_eq!(nan1.next_up().to_bits(), identity(nan1).to_bits());
assert_eq!(nan2.next_up().to_bits(), identity(nan2).to_bits());
assert_eq!(f64::NEG_INFINITY.next_up(), f64::MIN);
assert_eq!(f64::MIN.next_up(), -max_down);
@@ -329,12 +335,18 @@ fn test_next_down() {
let smallest_normal = f64::from_bits(0x0010_0000_0000_0000);
// Check that NaNs roundtrip.
// Because x87 can lose NaN bits when passed through a function, ensure the reference value
// also passes through a function boundary.
#[inline(never)]
fn identity(x: f64) -> f64 {
crate::hint::black_box(x)
}
let nan0 = f64::NAN;
let nan1 = f64::from_bits(f64::NAN.to_bits() ^ 0x000a_aaaa_aaaa_aaaa);
let nan2 = f64::from_bits(f64::NAN.to_bits() ^ 0x0005_5555_5555_5555);
assert_eq!(nan0.next_down().to_bits(), nan0.to_bits());
assert_eq!(nan1.next_down().to_bits(), nan1.to_bits());
assert_eq!(nan2.next_down().to_bits(), nan2.to_bits());
assert_eq!(nan0.next_down().to_bits(), identity(nan0).to_bits());
assert_eq!(nan1.next_down().to_bits(), identity(nan1).to_bits());
assert_eq!(nan2.next_down().to_bits(), identity(nan2).to_bits());
assert_eq!(f64::NEG_INFINITY.next_down(), f64::NEG_INFINITY);
assert_eq!(f64::MIN.next_down(), f64::NEG_INFINITY);