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

"square can be computed more efficiently" is wrong #7058

Closed
RReverser opened this issue Apr 9, 2021 · 8 comments · Fixed by #7201
Closed

"square can be computed more efficiently" is wrong #7058

RReverser opened this issue Apr 9, 2021 · 8 comments · Fixed by #7201
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing good-first-issue These issues are a good way to get started with Clippy I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@RReverser
Copy link

RReverser commented Apr 9, 2021

Lint name:
suboptimal_flops

I tried this code:

#![warn(clippy::suboptimal_flops)]

pub fn sample_sqr(x: f64) -> f64 {
    x.powi(2)
}

I expected to see this happen: it should pass.

Instead, this happened: it says "square can be computed more efficiently" and suggests to use x * x instead.

This is incorrect, because, unlike powf, powi produces exactly same native code as x * x, and is more natural to read (IMO). If anything, due to usage of intrinsic, it might be optimised even more than bare expression, although they're unlikely to be different on practice.

Meta

  • cargo clippy -V: clippy 0.1.53 (2e495d2 2021-04-08)
  • rustc -Vv:
        rustc 1.53.0-nightly (2e495d2e8 2021-04-08)
        binary: rustc
        commit-hash: 2e495d2e845cf27740e3665f718acfd3aa17253e
        commit-date: 2021-04-08
        host: x86_64-unknown-linux-gnu
        release: 1.53.0-nightly
        LLVM version: 12.0.0
    
@RReverser RReverser added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Apr 9, 2021
@camsteffen
Copy link
Contributor

@clarfonthey or @thiagoarrais do you have any input?

@camsteffen camsteffen added the good-first-issue These issues are a good way to get started with Clippy label May 3, 2021
@clarfonthey
Copy link

I believe that this may be a new optimisation -- does it really always generate the same code now?

@thiagoarrais
Copy link
Contributor

For reference: this lint was introduced in #5443. It may indeed have been obsoleted by some new optimization since the original issue (#2040).

@RReverser
Copy link
Author

RReverser commented May 4, 2021

I actually checked with Godbolt and x.powi(2) was generating code equivalent to x * x in every version starting from Rust 1.0.

In fact, even powf(2.0) seems to have always generated equivalent code in optimised mode, although it's less readable and I don't have any opinion on that one.

@camsteffen
Copy link
Contributor

Could it be architecture-dependant?

@RReverser
Copy link
Author

Not sure - it's certainly possible, but I haven't found one where this wouldn't hold on any mainstream arch. Plus, as noted above, it uses a compiler intrinsic under the hood, which I'd assume to always be implemented whatever's most efficient on each supported platform.

@camsteffen
Copy link
Contributor

Seems like we have sufficient info to not lint this case.

@mucinoab
Copy link
Contributor

mucinoab commented May 9, 2021

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing good-first-issue These issues are a good way to get started with Clippy I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants