Skip to content

Commit a3fb1d6

Browse files
committed
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.
1 parent b919797 commit a3fb1d6

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

Diff for: library/core/src/num/int_macros.rs

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

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

Diff for: library/core/src/num/uint_macros.rs

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

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

0 commit comments

Comments
 (0)