-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
improve performance of signed saturating_mul #65312
Conversation
Reciprocal throughput is improved from 2.3 to 1.7. https://godbolt.org/z/ROMiX6
r? @dtolnay (rust_highfive has picked a reviewer for you, use r? to override) |
@bors r+ |
📌 Commit 57aae75 has been approved by |
The code I showed in the issue is equivalent to also changing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Reasoning through the implementation: the new code is equivalent to:
(self < 0 && rhs < 0) || (self >= 0 && rhs >= 0)
but we know self != 0
and rhs != 0
or else the checked_mul would not have overflowed.
@bors r+ |
💡 This pull request was already approved, no need to approve it again.
|
📌 Commit 57aae75 has been approved by |
r? @nagisa |
@@ -1058,7 +1058,7 @@ $EndFeature, " | |||
#[inline] | |||
pub fn saturating_mul(self, rhs: Self) -> Self { | |||
self.checked_mul(rhs).unwrap_or_else(|| { | |||
if (self < 0 && rhs < 0) || (self > 0 && rhs > 0) { | |||
if (self < 0) == (rhs < 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually also more readable than before IMO, nice :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And in this case I think constness just has to wait :)
improve performance of signed saturating_mul Reciprocal throughput is improved from 2.3 to 1.7. https://godbolt.org/z/ROMiX6 Fixes rust-lang#65309.
improve performance of signed saturating_mul Reciprocal throughput is improved from 2.3 to 1.7. https://godbolt.org/z/ROMiX6 Fixes rust-lang#65309.
Rollup of 10 pull requests Successful merges: - #65214 (Split non-CAS atomic support off into target_has_atomic_load_store) - #65246 (vxWorks: implement get_path() and get_mode() for File fmt::Debug) - #65312 (improve performance of signed saturating_mul) - #65336 (Fix typo in task::Waker) - #65346 (nounwind tests and cleanup) - #65347 (Fix #[unwind(abort)] with Rust ABI) - #65366 (Implement Error::source on IntoStringError + Remove superfluous cause impls) - #65369 (Don't discard value names when using address or memory sanitizer) - #65370 (Add `dyn` to `Any` documentation) - #65373 (Fix typo in docs for `Rc`) Failed merges: r? @ghost
Reciprocal throughput is improved from 2.3 to 1.7. https://godbolt.org/z/ROMiX6
Fixes #65309.