rust-lang/rust

Some num tests fail for {i586, i686, x86_64}-unknown-linux-gnu

Closed this issue · 5 comments

Consider the following tests extract from libstd:

#[cfg(test)]
mod tests {
    use std::f32;
    use std::f64;

    macro_rules! assert_approx_eq {
        ($a:expr, $b:expr) => ({
            let (a, b) = (&$a, &$b);
            assert!((*a - *b).abs() < 1.0e-6,
                    "{} is not approximately equal to {}", *a, *b);
        })
    }

    #[test]
    fn test_mul_add() {
        let nan = f64::NAN;
        let inf = f64::INFINITY;
        let neg_inf = f64::NEG_INFINITY;
        assert_approx_eq!(12.3f64.mul_add(4.5, 6.7), 62.05);
        assert_approx_eq!((-12.3f64).mul_add(-4.5, -6.7), 48.65);
        assert_approx_eq!(0.0f64.mul_add(8.9, 1.2), 1.2);
        assert_approx_eq!(3.4f64.mul_add(-0.0, 5.6), 5.6);
        assert!(nan.mul_add(7.8, 9.0).is_nan());
        assert_eq!(inf.mul_add(7.8, 9.0), inf);
        assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
        assert_eq!(8.9f64.mul_add(inf, 3.2), inf);
        assert_eq!((-3.2f64).mul_add(2.4, neg_inf), neg_inf);
    }

    #[test]
    fn f32_test_float_bits_conv() {
        assert_eq!((1f32).to_bits(), 0x3f800000);
        assert_eq!((12.5f32).to_bits(), 0x41480000);
        assert_eq!((1337f32).to_bits(), 0x44a72000);
        assert_eq!((-14.25f32).to_bits(), 0xc1640000);
        assert_approx_eq!(f32::from_bits(0x3f800000), 1.0);
        assert_approx_eq!(f32::from_bits(0x41480000), 12.5);
        assert_approx_eq!(f32::from_bits(0x44a72000), 1337.0);
        assert_approx_eq!(f32::from_bits(0xc1640000), -14.25);

        // Check that NaNs roundtrip their bits regardless of signalingness
        // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
        let masked_nan1 = f32::NAN.to_bits() ^ 0x002A_AAAA;
        let masked_nan2 = f32::NAN.to_bits() ^ 0x0055_5555;
        assert!(f32::from_bits(masked_nan1).is_nan());
        assert!(f32::from_bits(masked_nan2).is_nan());

        assert_eq!(f32::from_bits(masked_nan1).to_bits(), masked_nan1);
        assert_eq!(f32::from_bits(masked_nan2).to_bits(), masked_nan2);
    }

    #[test]
    fn f64_test_float_bits_conv() {
        assert_eq!((1f64).to_bits(), 0x3ff0000000000000);
        assert_eq!((12.5f64).to_bits(), 0x4029000000000000);
        assert_eq!((1337f64).to_bits(), 0x4094e40000000000);
        assert_eq!((-14.25f64).to_bits(), 0xc02c800000000000);
        assert_approx_eq!(f64::from_bits(0x3ff0000000000000), 1.0);
        assert_approx_eq!(f64::from_bits(0x4029000000000000), 12.5);
        assert_approx_eq!(f64::from_bits(0x4094e40000000000), 1337.0);
        assert_approx_eq!(f64::from_bits(0xc02c800000000000), -14.25);

        // Check that NaNs roundtrip their bits regardless of signalingness
        // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
        let masked_nan1 = f64::NAN.to_bits() ^ 0x000A_AAAA_AAAA_AAAA;
        let masked_nan2 = f64::NAN.to_bits() ^ 0x0005_5555_5555_5555;
        assert!(f64::from_bits(masked_nan1).is_nan());
        assert!(f64::from_bits(masked_nan2).is_nan());

        assert_eq!(f64::from_bits(masked_nan1).to_bits(), masked_nan1);
        assert_eq!(f64::from_bits(masked_nan2).to_bits(), masked_nan2);
    }
}

Using rustc 1.24.0-nightly (250b49205 2017-12-21) it fails in debug mode for the targets {i586, i686}-unknown-linux-gnu but works in release mode. It works in both release and debug mode for x86_64-unknown-linux-gnu.

Using rustc 1.23.0-beta.2 (c9107ee93 2017-12-08) or rustc 1.22.1 (05e2e1c41 2017-11-22) only tests::test_mul_add works in release mode for {i586, i686, x86_64}-unknown-linux-gnu. For debug mode all tests fails for {i586, i686}-unknown-linux-gnu, tests::test_mul_add works for x86_64-unknown-linux-gnu.

I don't see how this is related to #53081.

Retested with latest nightly:
Debug mode i686:

running 3 tests
test tests::test_mul_add ... ok
test tests::f64_test_float_bits_conv ... FAILED
test tests::f32_test_float_bits_conv ... FAILED

failures:

---- tests::f64_test_float_bits_conv stdout ----
thread 'tests::f64_test_float_bits_conv' panicked at 'assertion failed: `(left == right)`
  left: `9221870836978985642`,
 right: `9219619037165300394`', src\lib.rs:70:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

---- tests::f32_test_float_bits_conv stdout ----
thread 'tests::f32_test_float_bits_conv' panicked at 'assertion failed: `(left == right)`
  left: `2144687445`,
 right: `2140493141`', src\lib.rs:49:9


failures:
    tests::f32_test_float_bits_conv
    tests::f64_test_float_bits_conv

test result: FAILED. 1 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out

Release mode works fine for i686, x86_64 works fine regardless the mode.

Separated the only two cases which are failing:

use std::f32;
use std::f64;

fn f32_bug() {
    let masked_nan2 = f32::NAN.to_bits() ^ 0x0055_5555;
    assert_eq!(f32::from_bits(masked_nan2).to_bits(), masked_nan2);
}

fn f64_bug() {
    let masked_nan1 = f64::NAN.to_bits() ^ 0x000A_AAAA_AAAA_AAAA;
    assert_eq!(f64::from_bits(masked_nan1).to_bits(), masked_nan1);
}

Misread the context while triaging. Sorry for the noise.

#73288 is an exact duplicate of this issue. I'm closing this one in favor of #73288 since it has a bit more discussion on it.

There's also #73328 to document what guarantees we make around NaN payloads/signedness.

The issue title says:

Some num tests fail for {i586, i686, x86_64}-unknown-linux-gnu

However the text seems to say that this always works on x86_64 (and checking it on the playground, it does work right now). I'm also not aware of floating-point trouble on x86_64. So indeed I assume this is a duplicate of #73288, now tracked as #115567.