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

Fix issue of missing sign in binary_float_op when result of rem is zero #109573

Closed

Conversation

chenyukang
Copy link
Member

Fixes #109567

@rustbot
Copy link
Collaborator

rustbot commented Mar 24, 2023

r? @wesleywiser

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 24, 2023
@rustbot
Copy link
Collaborator

rustbot commented Mar 24, 2023

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

Comment on lines 125 to 130
Rem => {
let v = (l % r).value;
// IEEE754 requires this
let r = if v.is_zero() { v.copy_sign(l) } else { v };
(r.into(), ty)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is the proper place to fix this. l % r should already give you the right result. There is likely something wrong in the rustc_apfloat crate, probably here, though I don't really understand how soft float works

(Category::Normal, Category::Normal) => {
while self.is_finite_non_zero()
&& rhs.is_finite_non_zero()
&& self.cmp_abs_normal(rhs) != Ordering::Less
{
let mut v = rhs.scalbn(self.ilogb() - rhs.ilogb());
if self.cmp_abs_normal(v) == Ordering::Less {
v = v.scalbn(-1);
}
v.sign = self.sign;
let status;
self = unpack!(status=, self - v);
assert_eq!(status, Status::OK);
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, you're right, I didn't notice use rustc_apfloat::Float.
I think we may have a similar fix at here? I updated the code.

// check-run-results

pub fn f() -> f64 {
std::hint::black_box(-1.0) % std::hint::black_box(-1.0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What part of the compiler are you specifically trying to test here?

If you're updating const eval, maybe it would make sense to add a test for this in https://github.com/rust-lang/rust/tree/master/tests/mir-opt/const_prop?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new fix don't change mir part, so I moved the test case to tests/ui/numbers-arithmetic/.

@chenyukang chenyukang force-pushed the yukang/fix-109567-rem-issue branch from 345cdba to 965a05d Compare March 25, 2023 02:23
@rustbot
Copy link
Collaborator

rustbot commented Mar 25, 2023

Changes rustc_apfloat. rustc_apfloat is currently in limbo and you almost certainly don't want to change it (see #55993).

cc @eddyb

@chenyukang chenyukang force-pushed the yukang/fix-109567-rem-issue branch 3 times, most recently from 1336670 to dacbb8d Compare March 25, 2023 02:33
@chenyukang chenyukang force-pushed the yukang/fix-109567-rem-issue branch from dacbb8d to 99f7fb0 Compare March 25, 2023 02:42
std::hint::black_box(-1.0) % std::hint::black_box(-1.0)
}

pub fn g() -> f64 {
const fn g() -> f64 {
-1.0 % -1.0
}
Copy link
Member

@RalfJung RalfJung Mar 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does nothing. Making a const fn just means the function can be called from a const, but you are not doing that, and then it is a total NOP.

What I meant is something like

const G: f64 = -1.0 % -1.0;

Then you also don't need the feature gate.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed it.
According to #109573 (comment), it's not suggested to make changes to this file? 🤔

@chenyukang chenyukang force-pushed the yukang/fix-109567-rem-issue branch from abd8a4d to eb59b9c Compare March 25, 2023 17:42
@RalfJung
Copy link
Member

Yeah, Rust's apfloat port has been in licensing limbo for years, meaning we have no idea how to even go about fixing bugs in it. :(

@cbeuw
Copy link
Contributor

cbeuw commented Mar 26, 2023

If we can't change apfloat, I suggest that @chenyukang revert to the original patch of doing the check in rustc_const_eval and get the fix merged first. It's not pretty but it's better than exposing the wrong result to users

@RalfJung
Copy link
Member

RalfJung commented Mar 26, 2023

Not sure I agree -- littering the surrounding code with checks to patch the results sounds pretty bad. FWIW this is not the only bugfix that is blocked on apfloat.

We really need to figure out the apfloat situations, and solve it properly.

@chenyukang
Copy link
Member Author

seems we are blocking at this PR: #96784

@Dylan-DPC Dylan-DPC added S-blocked Status: Blocked on something else such as an RFC or other implementation work. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 20, 2023
@eddyb
Copy link
Member

eddyb commented Jul 14, 2023

I posted a longer update to #55993 (comment) regarding the progress with the whole rustc_apfloat situation, but of note for this PR is that the WIP repo already has this bug fixed.

@wesleywiser
Copy link
Member

Hi @chenyukang, thanks for the fix! As @eddyb mentioned, this has been fixed in the new version of the rustc_apfloat code that we are using as of #113843. Since that PR has landed, we've closed this one.

Thanks again for your efforts! 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-blocked Status: Blocked on something else such as an RFC or other implementation work. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Codegen and const eval evaluates -1.0 % -1.0 to 0 with different signs
8 participants