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 |
dtolnay
left a comment
There was a problem hiding this comment.
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 |
| 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.
This is actually also more readable than before IMO, nice :)
There was a problem hiding this comment.
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.