Skip to content

Value of f32.is_sign_positive() differs from native code #3139

@dtolnay

Description

@dtolnay

In the most recently nightly, I started getting a failing test with cargo miri test in https://github.com/dtolnay/basic-toml. The crate does not contain unsafe code so I wonder whether this could be a Miri bug.

The failing test is here, specifically this assertion. It deserializes a f32 from TOML +nan and asserts that .is_sign_positive() returns true. Under cargo test, the f32's bit pattern is 0x7FC0_0000 which is +NaN. Under cargo miri test the bit pattern is 0xFFC0_0000 which is -NaN and the test fails.

https://github.com/dtolnay/basic-toml/actions/runs/6634770330/job/18024693503

I made some attempts at a minimal repro along the lines of the following, but was not able to reproduce a -NaN this way, yet.

use serde::Deserialize as _;

fn main() {
    println!("{}", f64::NAN.is_sign_positive());
    println!("{}", f32::NAN.is_sign_positive());
    println!("{}", (f64::NAN as f32).is_sign_positive());
    println!("{}", (unsafe { std::ptr::read_volatile(&f64::NAN) } as f32).is_sign_positive());
    println!("{}", f32::deserialize(serde::de::value::F64Deserializer::<serde::de::value::Error>::new(f64::NAN)).unwrap().is_sign_positive());
}

Next I tried bisecting Miri commits by doing ./miri toolchain && ./miri install in the miri repo followed by cargo +miri miri test --test float against that commit of the basic-toml repo linked above. The bisect landed on #3136 which has no relevant code change in miri. Based on the rust-version change in that PR, I checked git diff 9e3f784eb2c..62fae2305e5 (rust-lang/rust@9e3f784...62fae23#files_bucket) which also had nothing that looked relevant.

I'll keep working on a minimal repro but filing this now in case you have a ready way to debug this kind of thing, or spot something obvious in #3136 or the rustc diff that I overlooked.

rustc 1.75.0-nightly (df871fbf0 2023-10-24)
binary: rustc
commit-hash: df871fbf053de3a855398964cd05fadbe91cf4fd
commit-date: 2023-10-24
host: x86_64-unknown-linux-gnu
release: 1.75.0-nightly
LLVM version: 17.0.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions