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

MIR is double-rounding f32 literals #32805

Closed
arielb1 opened this issue Apr 7, 2016 · 3 comments
Closed

MIR is double-rounding f32 literals #32805

arielb1 opened this issue Apr 7, 2016 · 3 comments
Labels
A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html

Comments

@arielb1
Copy link
Contributor

arielb1 commented Apr 7, 2016

STR

#![feature(rustc_attrs)]

#[rustc_mir]
fn const_mir() -> f32 { 9007199791611905.0 }

fn const_old() -> f32 { 9007199791611905.0 }

fn main() {
    let s = "9007199791611905.0"; // (1<<53)+(1<<29)+1
    println!("old: {}", const_old());
    println!("MIR: {}", const_mir());
    println!("single rounding: {}", s.parse::<f32>().unwrap());
    println!("double rounding: {}", s.parse::<f64>().unwrap() as f32);
}

Expected Result

Both MIR and old trans should use a single rounding, that's it

old: 9007200000000000
MIR: 9007200000000000
single rounding: 9007200000000000
double rounding: 9007199000000000

Actual Result

old trans is single-rounding as it should, but MIR is doing a double rounding:

old: 9007200000000000
MIR: 9007199000000000
single rounding: 9007200000000000
double rounding: 9007199000000000
@arielb1 arielb1 added A-amusing A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html labels Apr 7, 2016
@nagisa
Copy link
Member

nagisa commented Apr 7, 2016

Amusing indeed!

@eddyb
Copy link
Member

eddyb commented Apr 27, 2016

This is because of ConstVal::Float(f64), isn't it?

@hanna-kruppe
Copy link
Contributor

hanna-kruppe commented Apr 27, 2016

Yes, fixing this requires adding a f32 variant and performing f32 arithmetic operations in constant folding.

bors added a commit that referenced this issue Jun 6, 2016
 [MIR] Fix double-rounding of float constants and ignore NaN sign in tests.

Fixes #32805 by handling f32 and f64 separately in rustc_const_eval.

Also removes `#[rustc_no_mir]` from a couple libstd tests by ignoring NaN sign.
Turns out that runtime evaluation of `0.0 / 0.0` produces a NaN with the sign bit set,
whereas LLVM constant folds it to a NaN with the sign bit unset, which we were testing for.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html
Projects
None yet
Development

No branches or pull requests

4 participants