Explicitly unroll integer pow for small exponents · patricklam/verify-rust-std@05ee322 (original) (raw)
`@@ -2173,10 +2173,35 @@ macro_rules! int_impl {
`
2173
2173
` without modifying the original"]
`
2174
2174
` #[inline]
`
2175
2175
`pub const fn wrapping_pow(self, mut exp: u32) -> Self {
`
2176
``
`-
if exp == 0 {
`
2177
``
`-
return 1;
`
2178
``
`-
}
`
2179
2176
`let mut base = self;
`
``
2177
+
``
2178
`+
// Unroll multiplications for small exponent values.
`
``
2179
`+
// This gives the optimizer a way to efficiently inline call sites
`
``
2180
`+
// for the most common use cases with constant exponents.
`
``
2181
`+
// Currently, LLVM is unable to unroll the loop below.
`
``
2182
`+
match exp {
`
``
2183
`+
0 => return 1,
`
``
2184
`+
1 => return base,
`
``
2185
`+
2 => return base.wrapping_mul(base),
`
``
2186
`+
3 => {
`
``
2187
`+
let squared = base.wrapping_mul(base);
`
``
2188
`+
return squared.wrapping_mul(base);
`
``
2189
`+
}
`
``
2190
`+
4 => {
`
``
2191
`+
let squared = base.wrapping_mul(base);
`
``
2192
`+
return squared.wrapping_mul(squared);
`
``
2193
`+
}
`
``
2194
`+
5 => {
`
``
2195
`+
let squared = base.wrapping_mul(base);
`
``
2196
`+
return squared.wrapping_mul(squared).wrapping_mul(base);
`
``
2197
`+
}
`
``
2198
`+
6 => {
`
``
2199
`+
let cubed = base.wrapping_mul(base).wrapping_mul(base);
`
``
2200
`+
return cubed.wrapping_mul(cubed);
`
``
2201
`+
}
`
``
2202
`+
_ => {}
`
``
2203
`+
}
`
``
2204
+
2180
2205
`let mut acc: Self = 1;
`
2181
2206
``
2182
2207
`loop {
`
`@@ -2719,10 +2744,35 @@ macro_rules! int_impl {
`
2719
2744
` #[inline]
`
2720
2745
` #[rustc_inherit_overflow_checks]
`
2721
2746
`pub const fn pow(self, mut exp: u32) -> Self {
`
2722
``
`-
if exp == 0 {
`
2723
``
`-
return 1;
`
2724
``
`-
}
`
2725
2747
`let mut base = self;
`
``
2748
+
``
2749
`+
// Unroll multiplications for small exponent values.
`
``
2750
`+
// This gives the optimizer a way to efficiently inline call sites
`
``
2751
`+
// for the most common use cases with constant exponents.
`
``
2752
`+
// Currently, LLVM is unable to unroll the loop below.
`
``
2753
`+
match exp {
`
``
2754
`+
0 => return 1,
`
``
2755
`+
1 => return base,
`
``
2756
`+
2 => return base * base,
`
``
2757
`+
3 => {
`
``
2758
`+
let squared = base * base;
`
``
2759
`+
return squared * base;
`
``
2760
`+
}
`
``
2761
`+
4 => {
`
``
2762
`+
let squared = base * base;
`
``
2763
`+
return squared * squared;
`
``
2764
`+
}
`
``
2765
`+
5 => {
`
``
2766
`+
let squared = base * base;
`
``
2767
`+
return squared * squared * base;
`
``
2768
`+
}
`
``
2769
`+
6 => {
`
``
2770
`+
let cubed = base * base * base;
`
``
2771
`+
return cubed * cubed;
`
``
2772
`+
}
`
``
2773
`+
_ => {}
`
``
2774
`+
}
`
``
2775
+
2726
2776
`let mut acc = 1;
`
2727
2777
``
2728
2778
`loop {
`