Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subnormal f64 to f32 cast is incorrect in CTFE #113407

Closed
cbeuw opened this issue Jul 6, 2023 · 9 comments · Fixed by rust-lang/rustc_apfloat#1 or #113843
Closed

Subnormal f64 to f32 cast is incorrect in CTFE #113407

cbeuw opened this issue Jul 6, 2023 · 9 comments · Fixed by rust-lang/rustc_apfloat#1 or #113843
Labels
A-const-prop Area: Constant propagation A-floating-point Area: Floating point numbers and arithmetic A-mir-opt Area: MIR optimizations I-slow Issue: Problems and improvements with respect to performance of generated code.

Comments

@cbeuw
Copy link
Contributor

cbeuw commented Jul 6, 2023

pub fn main() {
    let f = f64::from_bits(0x19873cc2) as f32;
    println!("{:x}", f.to_bits());
    println!("{}", f == 0.);
}
$ rustc -Zmir-opt-level=2 -Copt-level=3 repro.rs && ./repro
1
false
$ ../miri/miri run repro.rs 2> /dev/null
1
false
$ rustc -Zmir-opt-level=1 -Copt-level=3 repro.rs && ./repro
0
true

It should print 0 and true, according to the equivalent C program: https://godbolt.org/z/7Gq51a1Yh

This is probably another apfloat bug

$ rustc --version -v 
rustc 1.72.0-nightly (d9c13cd45 2023-07-05)
binary: rustc
commit-hash: d9c13cd4531649c2028a8384cb4d4e54f985380e
commit-date: 2023-07-05
host: aarch64-unknown-linux-gnu
release: 1.72.0-nightly
LLVM version: 16.0.5

(reproducible on x86 as well)

cc @eddyb @wesleywiser @RalfJung

@RalfJung
Copy link
Member

RalfJung commented Jul 6, 2023

This is strange: https://godbolt.org/z/14GxTsfoM

LLVM and Rust are both using apfloat for compile-time float computations, and yet the MIR opts produce 1 and LLVM produces 0. Is this something that was fixed in upstream apfloat but we don't have the fix yet?

@cbeuw
Copy link
Contributor Author

cbeuw commented Jul 6, 2023

Rust's apfloat port is old and unupdated for a long while. For instance #102403 was fixed upstream 6 years ago llvm/llvm-project@f2c2851

@nikic
Copy link
Contributor

nikic commented Jul 6, 2023

The relevant fix is probably https://reviews.llvm.org/D127140?

@RalfJung
Copy link
Member

RalfJung commented Jul 6, 2023

#113416 should work around this.

@jyn514 jyn514 added I-slow Issue: Problems and improvements with respect to performance of generated code. A-mir-opt Area: MIR optimizations A-floating-point Area: Floating point numbers and arithmetic A-const-prop Area: Constant propagation labels Jul 10, 2023
@eddyb
Copy link
Member

eddyb commented Jul 13, 2023

While trying to trigger a different issue involving float<->float conversions, I believe my rustc_apfloat fuzzer (after commit rust-lang/rustc_apfloat@439177a) reproduced this one instead:

image

(which is good confirmation that we can find such things. too bad about the FMA NaNs polluting fuzzer outputs)

EDIT: also see the longer update post at #55993 (comment)

@eddyb
Copy link
Member

eddyb commented Jul 18, 2023

As per #55993 (comment), this is now fixed in rust-lang/rustc_apfloat.

@cbeuw
Copy link
Contributor Author

cbeuw commented Jul 18, 2023

This should be reopened until rustc starts using the stand-alone crate

@eddyb
Copy link
Member

eddyb commented Jul 18, 2023

Uh oh I did not do that, GH must've gotten overzealous cross-repo.

@eddyb eddyb reopened this Jul 18, 2023
@RalfJung
Copy link
Member

You said "fixes #113407" in that PR, GH did the rest. ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-prop Area: Constant propagation A-floating-point Area: Floating point numbers and arithmetic A-mir-opt Area: MIR optimizations I-slow Issue: Problems and improvements with respect to performance of generated code.
Projects
None yet
5 participants