Skip to content

Commit

Permalink
Auto merge of rust-lang#87150 - rusticstuff:simplify_wrapping_neg, r=…
Browse files Browse the repository at this point in the history
…m-ou-se

Make wrapping_neg() use wrapping_sub(), #[inline(always)]

This is a follow-up change to the fix for rust-lang#75598. It simplifies the implementation of wrapping_neg() for all integer types by just calling 0.wrapping_sub(self) and always inlines it. This leads to much less assembly code being emitted for opt-level≤1 and thus much better performance for debug-compiled code.

Background is [this discussion on the internals forum](https://internals.rust-lang.org/t/why-does-rust-generate-10x-as-much-unoptimized-assembly-as-gcc/14930).
  • Loading branch information
bors committed Aug 4, 2021
2 parents 87d713f + a3fb1d6 commit 7f3dc04
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions library/core/src/num/int_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1132,9 +1132,9 @@ macro_rules! int_impl {
/// ```
#[stable(feature = "num_wrapping", since = "1.2.0")]
#[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
#[inline]
#[inline(always)]
pub const fn wrapping_neg(self) -> Self {
self.overflowing_neg().0
(0 as $SelfT).wrapping_sub(self)
}

/// Panic-free bitwise shift-left; yields `self << mask(rhs)`, where `mask` removes
Expand Down
4 changes: 2 additions & 2 deletions library/core/src/num/uint_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1246,9 +1246,9 @@ macro_rules! uint_impl {
/// ```
#[stable(feature = "num_wrapping", since = "1.2.0")]
#[rustc_const_stable(feature = "const_wrapping_math", since = "1.32.0")]
#[inline]
#[inline(always)]
pub const fn wrapping_neg(self) -> Self {
self.overflowing_neg().0
(0 as $SelfT).wrapping_sub(self)
}

/// Panic-free bitwise shift-left; yields `self << mask(rhs)`,
Expand Down

0 comments on commit 7f3dc04

Please sign in to comment.