Skip to content

Commit 7f3dc04

Browse files
committed
Auto merge of #87150 - rusticstuff:simplify_wrapping_neg, r=m-ou-se
Make wrapping_neg() use wrapping_sub(), #[inline(always)] This is a follow-up change to the fix for #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).
2 parents 87d713f + a3fb1d6 commit 7f3dc04

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

library/core/src/num/int_macros.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1132,9 +1132,9 @@ macro_rules! int_impl {
11321132
/// ```
11331133
#[stable(feature = "num_wrapping", since = "1.2.0")]
11341134
#[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1135-
#[inline]
1135+
#[inline(always)]
11361136
pub const fn wrapping_neg(self) -> Self {
1137-
self.overflowing_neg().0
1137+
(0 as $SelfT).wrapping_sub(self)
11381138
}
11391139

11401140
/// Panic-free bitwise shift-left; yields `self << mask(rhs)`, where `mask` removes

library/core/src/num/uint_macros.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1246,9 +1246,9 @@ macro_rules! uint_impl {
12461246
/// ```
12471247
#[stable(feature = "num_wrapping", since = "1.2.0")]
12481248
#[rustc_const_stable(feature = "const_wrapping_math", since = "1.32.0")]
1249-
#[inline]
1249+
#[inline(always)]
12501250
pub const fn wrapping_neg(self) -> Self {
1251-
self.overflowing_neg().0
1251+
(0 as $SelfT).wrapping_sub(self)
12521252
}
12531253

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

0 commit comments

Comments
 (0)