Skip to content

Commit c4e0622

Browse files
committed
Ensure NaN references values go through function boundary for next_up/down.
1 parent 6f543ee commit c4e0622

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

library/std/src/f32/tests.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,18 @@ fn test_next_up() {
296296
let smallest_normal = f32::from_bits(0x0080_0000);
297297

298298
// Check that NaNs roundtrip.
299+
// Because x87 can lose NaN bits when passed through a function, ensure the reference value
300+
// also passes through a function boundary.
301+
#[inline(never)]
302+
fn identity(x: f32) -> f32 {
303+
crate::hint::black_box(x)
304+
}
299305
let nan0 = f32::NAN;
300306
let nan1 = f32::from_bits(f32::NAN.to_bits() ^ 0x002a_aaaa);
301307
let nan2 = f32::from_bits(f32::NAN.to_bits() ^ 0x0055_5555);
302-
assert_eq!(nan0.next_up().to_bits(), nan0.to_bits());
303-
assert_eq!(nan1.next_up().to_bits(), nan1.to_bits());
304-
assert_eq!(nan2.next_up().to_bits(), nan2.to_bits());
308+
assert_eq!(nan0.next_up().to_bits(), identity(nan0).to_bits());
309+
assert_eq!(nan1.next_up().to_bits(), identity(nan1).to_bits());
310+
assert_eq!(nan2.next_up().to_bits(), identity(nan2).to_bits());
305311

306312
assert_eq!(f32::NEG_INFINITY.next_up(), f32::MIN);
307313
assert_eq!(f32::MIN.next_up(), -max_down);
@@ -327,12 +333,18 @@ fn test_next_down() {
327333
let smallest_normal = f32::from_bits(0x0080_0000);
328334

329335
// Check that NaNs roundtrip.
336+
// Because x87 can lose NaN bits when passed through a function, ensure the reference value
337+
// also passes through a function boundary.
338+
#[inline(never)]
339+
fn identity(x: f32) -> f32 {
340+
crate::hint::black_box(x)
341+
}
330342
let nan0 = f32::NAN;
331343
let nan1 = f32::from_bits(f32::NAN.to_bits() ^ 0x002a_aaaa);
332344
let nan2 = f32::from_bits(f32::NAN.to_bits() ^ 0x0055_5555);
333-
assert_eq!(nan0.next_down().to_bits(), nan0.to_bits());
334-
assert_eq!(nan1.next_down().to_bits(), nan1.to_bits());
335-
assert_eq!(nan2.next_down().to_bits(), nan2.to_bits());
345+
assert_eq!(nan0.next_down().to_bits(), identity(nan0).to_bits());
346+
assert_eq!(nan1.next_down().to_bits(), identity(nan1).to_bits());
347+
assert_eq!(nan2.next_down().to_bits(), identity(nan2).to_bits());
336348

337349
assert_eq!(f32::NEG_INFINITY.next_down(), f32::NEG_INFINITY);
338350
assert_eq!(f32::MIN.next_down(), f32::NEG_INFINITY);

library/std/src/f64/tests.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,18 @@ fn test_next_up() {
298298
let smallest_normal = f64::from_bits(0x0010_0000_0000_0000);
299299

300300
// Check that NaNs roundtrip.
301+
// Because x87 can lose NaN bits when passed through a function, ensure the reference value
302+
// also passes through a function boundary.
303+
#[inline(never)]
304+
fn identity(x: f64) -> f64 {
305+
crate::hint::black_box(x)
306+
}
301307
let nan0 = f64::NAN;
302308
let nan1 = f64::from_bits(f64::NAN.to_bits() ^ 0x000a_aaaa_aaaa_aaaa);
303309
let nan2 = f64::from_bits(f64::NAN.to_bits() ^ 0x0005_5555_5555_5555);
304-
assert_eq!(nan0.next_up().to_bits(), nan0.to_bits());
305-
assert_eq!(nan1.next_up().to_bits(), nan1.to_bits());
306-
assert_eq!(nan2.next_up().to_bits(), nan2.to_bits());
310+
assert_eq!(nan0.next_up().to_bits(), identity(nan0).to_bits());
311+
assert_eq!(nan1.next_up().to_bits(), identity(nan1).to_bits());
312+
assert_eq!(nan2.next_up().to_bits(), identity(nan2).to_bits());
307313

308314
assert_eq!(f64::NEG_INFINITY.next_up(), f64::MIN);
309315
assert_eq!(f64::MIN.next_up(), -max_down);
@@ -329,12 +335,18 @@ fn test_next_down() {
329335
let smallest_normal = f64::from_bits(0x0010_0000_0000_0000);
330336

331337
// Check that NaNs roundtrip.
338+
// Because x87 can lose NaN bits when passed through a function, ensure the reference value
339+
// also passes through a function boundary.
340+
#[inline(never)]
341+
fn identity(x: f64) -> f64 {
342+
crate::hint::black_box(x)
343+
}
332344
let nan0 = f64::NAN;
333345
let nan1 = f64::from_bits(f64::NAN.to_bits() ^ 0x000a_aaaa_aaaa_aaaa);
334346
let nan2 = f64::from_bits(f64::NAN.to_bits() ^ 0x0005_5555_5555_5555);
335-
assert_eq!(nan0.next_down().to_bits(), nan0.to_bits());
336-
assert_eq!(nan1.next_down().to_bits(), nan1.to_bits());
337-
assert_eq!(nan2.next_down().to_bits(), nan2.to_bits());
347+
assert_eq!(nan0.next_down().to_bits(), identity(nan0).to_bits());
348+
assert_eq!(nan1.next_down().to_bits(), identity(nan1).to_bits());
349+
assert_eq!(nan2.next_down().to_bits(), identity(nan2).to_bits());
338350

339351
assert_eq!(f64::NEG_INFINITY.next_down(), f64::NEG_INFINITY);
340352
assert_eq!(f64::MIN.next_down(), f64::NEG_INFINITY);

0 commit comments

Comments
 (0)