Skip to content

Commit 83ca4b7

Browse files
committed
Auto merge of #84061 - AngelicosPhosphoros:issue-75598-add-inline-always-arithmetic, r=nagisa
Add some #[inline(always)] to arithmetic methods of integers I tried to add it only to methods which return results of intrinsics and don't have any branching. Branching could made performance of debug builds (`-Copt-level=0`) worse. Main goal of changes is allowing wider optimizations in `-Copt-level=1`. Closes: #75598 r? `@nagisa`
2 parents 392ba2b + f8a12c6 commit 83ca4b7

File tree

4 files changed

+76
-76
lines changed

4 files changed

+76
-76
lines changed

library/core/src/num/int_macros.rs

+27-27
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ macro_rules! int_impl {
7979
#[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
8080
#[doc(alias = "popcount")]
8181
#[doc(alias = "popcnt")]
82-
#[inline]
82+
#[inline(always)]
8383
pub const fn count_ones(self) -> u32 { (self as $UnsignedT).count_ones() }
8484

8585
/// Returns the number of zeros in the binary representation of `self`.
@@ -93,7 +93,7 @@ macro_rules! int_impl {
9393
/// ```
9494
#[stable(feature = "rust1", since = "1.0.0")]
9595
#[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
96-
#[inline]
96+
#[inline(always)]
9797
pub const fn count_zeros(self) -> u32 {
9898
(!self).count_ones()
9999
}
@@ -111,7 +111,7 @@ macro_rules! int_impl {
111111
/// ```
112112
#[stable(feature = "rust1", since = "1.0.0")]
113113
#[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
114-
#[inline]
114+
#[inline(always)]
115115
pub const fn leading_zeros(self) -> u32 {
116116
(self as $UnsignedT).leading_zeros()
117117
}
@@ -129,7 +129,7 @@ macro_rules! int_impl {
129129
/// ```
130130
#[stable(feature = "rust1", since = "1.0.0")]
131131
#[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
132-
#[inline]
132+
#[inline(always)]
133133
pub const fn trailing_zeros(self) -> u32 {
134134
(self as $UnsignedT).trailing_zeros()
135135
}
@@ -147,7 +147,7 @@ macro_rules! int_impl {
147147
/// ```
148148
#[stable(feature = "leading_trailing_ones", since = "1.46.0")]
149149
#[rustc_const_stable(feature = "leading_trailing_ones", since = "1.46.0")]
150-
#[inline]
150+
#[inline(always)]
151151
pub const fn leading_ones(self) -> u32 {
152152
(self as $UnsignedT).leading_ones()
153153
}
@@ -165,7 +165,7 @@ macro_rules! int_impl {
165165
/// ```
166166
#[stable(feature = "leading_trailing_ones", since = "1.46.0")]
167167
#[rustc_const_stable(feature = "leading_trailing_ones", since = "1.46.0")]
168-
#[inline]
168+
#[inline(always)]
169169
pub const fn trailing_ones(self) -> u32 {
170170
(self as $UnsignedT).trailing_ones()
171171
}
@@ -189,7 +189,7 @@ macro_rules! int_impl {
189189
#[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
190190
#[must_use = "this returns the result of the operation, \
191191
without modifying the original"]
192-
#[inline]
192+
#[inline(always)]
193193
pub const fn rotate_left(self, n: u32) -> Self {
194194
(self as $UnsignedT).rotate_left(n) as Self
195195
}
@@ -214,7 +214,7 @@ macro_rules! int_impl {
214214
#[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
215215
#[must_use = "this returns the result of the operation, \
216216
without modifying the original"]
217-
#[inline]
217+
#[inline(always)]
218218
pub const fn rotate_right(self, n: u32) -> Self {
219219
(self as $UnsignedT).rotate_right(n) as Self
220220
}
@@ -234,7 +234,7 @@ macro_rules! int_impl {
234234
/// ```
235235
#[stable(feature = "rust1", since = "1.0.0")]
236236
#[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
237-
#[inline]
237+
#[inline(always)]
238238
pub const fn swap_bytes(self) -> Self {
239239
(self as $UnsignedT).swap_bytes() as Self
240240
}
@@ -255,7 +255,7 @@ macro_rules! int_impl {
255255
/// ```
256256
#[stable(feature = "reverse_bits", since = "1.37.0")]
257257
#[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
258-
#[inline]
258+
#[inline(always)]
259259
#[must_use]
260260
pub const fn reverse_bits(self) -> Self {
261261
(self as $UnsignedT).reverse_bits() as Self
@@ -416,7 +416,7 @@ macro_rules! int_impl {
416416
)]
417417
#[must_use = "this returns the result of the operation, \
418418
without modifying the original"]
419-
#[inline]
419+
#[inline(always)]
420420
pub unsafe fn unchecked_add(self, rhs: Self) -> Self {
421421
// SAFETY: the caller must uphold the safety contract for
422422
// `unchecked_add`.
@@ -454,7 +454,7 @@ macro_rules! int_impl {
454454
)]
455455
#[must_use = "this returns the result of the operation, \
456456
without modifying the original"]
457-
#[inline]
457+
#[inline(always)]
458458
pub unsafe fn unchecked_sub(self, rhs: Self) -> Self {
459459
// SAFETY: the caller must uphold the safety contract for
460460
// `unchecked_sub`.
@@ -492,7 +492,7 @@ macro_rules! int_impl {
492492
)]
493493
#[must_use = "this returns the result of the operation, \
494494
without modifying the original"]
495-
#[inline]
495+
#[inline(always)]
496496
pub unsafe fn unchecked_mul(self, rhs: Self) -> Self {
497497
// SAFETY: the caller must uphold the safety contract for
498498
// `unchecked_mul`.
@@ -741,7 +741,7 @@ macro_rules! int_impl {
741741
#[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
742742
#[must_use = "this returns the result of the operation, \
743743
without modifying the original"]
744-
#[inline]
744+
#[inline(always)]
745745
pub const fn saturating_add(self, rhs: Self) -> Self {
746746
intrinsics::saturating_add(self, rhs)
747747
}
@@ -762,7 +762,7 @@ macro_rules! int_impl {
762762
#[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
763763
#[must_use = "this returns the result of the operation, \
764764
without modifying the original"]
765-
#[inline]
765+
#[inline(always)]
766766
pub const fn saturating_sub(self, rhs: Self) -> Self {
767767
intrinsics::saturating_sub(self, rhs)
768768
}
@@ -783,7 +783,7 @@ macro_rules! int_impl {
783783
784784
#[stable(feature = "saturating_neg", since = "1.45.0")]
785785
#[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
786-
#[inline]
786+
#[inline(always)]
787787
pub const fn saturating_neg(self) -> Self {
788788
intrinsics::saturating_sub(0, self)
789789
}
@@ -883,7 +883,7 @@ macro_rules! int_impl {
883883
#[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
884884
#[must_use = "this returns the result of the operation, \
885885
without modifying the original"]
886-
#[inline]
886+
#[inline(always)]
887887
pub const fn wrapping_add(self, rhs: Self) -> Self {
888888
intrinsics::wrapping_add(self, rhs)
889889
}
@@ -903,7 +903,7 @@ macro_rules! int_impl {
903903
#[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
904904
#[must_use = "this returns the result of the operation, \
905905
without modifying the original"]
906-
#[inline]
906+
#[inline(always)]
907907
pub const fn wrapping_sub(self, rhs: Self) -> Self {
908908
intrinsics::wrapping_sub(self, rhs)
909909
}
@@ -923,7 +923,7 @@ macro_rules! int_impl {
923923
#[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
924924
#[must_use = "this returns the result of the operation, \
925925
without modifying the original"]
926-
#[inline]
926+
#[inline(always)]
927927
pub const fn wrapping_mul(self, rhs: Self) -> Self {
928928
intrinsics::wrapping_mul(self, rhs)
929929
}
@@ -1081,7 +1081,7 @@ macro_rules! int_impl {
10811081
#[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
10821082
#[must_use = "this returns the result of the operation, \
10831083
without modifying the original"]
1084-
#[inline]
1084+
#[inline(always)]
10851085
pub const fn wrapping_shl(self, rhs: u32) -> Self {
10861086
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
10871087
// out of bounds
@@ -1110,7 +1110,7 @@ macro_rules! int_impl {
11101110
#[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
11111111
#[must_use = "this returns the result of the operation, \
11121112
without modifying the original"]
1113-
#[inline]
1113+
#[inline(always)]
11141114
pub const fn wrapping_shr(self, rhs: u32) -> Self {
11151115
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
11161116
// out of bounds
@@ -1225,7 +1225,7 @@ macro_rules! int_impl {
12251225
#[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
12261226
#[must_use = "this returns the result of the operation, \
12271227
without modifying the original"]
1228-
#[inline]
1228+
#[inline(always)]
12291229
pub const fn overflowing_add(self, rhs: Self) -> (Self, bool) {
12301230
let (a, b) = intrinsics::add_with_overflow(self as $ActualT, rhs as $ActualT);
12311231
(a as Self, b)
@@ -1249,7 +1249,7 @@ macro_rules! int_impl {
12491249
#[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
12501250
#[must_use = "this returns the result of the operation, \
12511251
without modifying the original"]
1252-
#[inline]
1252+
#[inline(always)]
12531253
pub const fn overflowing_sub(self, rhs: Self) -> (Self, bool) {
12541254
let (a, b) = intrinsics::sub_with_overflow(self as $ActualT, rhs as $ActualT);
12551255
(a as Self, b)
@@ -1272,7 +1272,7 @@ macro_rules! int_impl {
12721272
#[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
12731273
#[must_use = "this returns the result of the operation, \
12741274
without modifying the original"]
1275-
#[inline]
1275+
#[inline(always)]
12761276
pub const fn overflowing_mul(self, rhs: Self) -> (Self, bool) {
12771277
let (a, b) = intrinsics::mul_with_overflow(self as $ActualT, rhs as $ActualT);
12781278
(a as Self, b)
@@ -1725,7 +1725,7 @@ macro_rules! int_impl {
17251725
/// ```
17261726
#[stable(feature = "rust1", since = "1.0.0")]
17271727
#[rustc_const_stable(feature = "const_int_sign", since = "1.47.0")]
1728-
#[inline]
1728+
#[inline(always)]
17291729
pub const fn signum(self) -> Self {
17301730
match self {
17311731
n if n > 0 => 1,
@@ -1747,7 +1747,7 @@ macro_rules! int_impl {
17471747
/// ```
17481748
#[stable(feature = "rust1", since = "1.0.0")]
17491749
#[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1750-
#[inline]
1750+
#[inline(always)]
17511751
pub const fn is_positive(self) -> bool { self > 0 }
17521752

17531753
/// Returns `true` if `self` is negative and `false` if the number is zero or
@@ -1763,7 +1763,7 @@ macro_rules! int_impl {
17631763
/// ```
17641764
#[stable(feature = "rust1", since = "1.0.0")]
17651765
#[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1766-
#[inline]
1766+
#[inline(always)]
17671767
pub const fn is_negative(self) -> bool { self < 0 }
17681768

17691769
/// Return the memory representation of this integer as a byte array in

0 commit comments

Comments
 (0)