Skip to content

Commit 010c332

Browse files
committed
Try using is_val_statically_known in pow
Does not compile.
1 parent 1faa101 commit 010c332

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@
170170
#![feature(internal_impls_macro)]
171171
#![feature(ip)]
172172
#![feature(is_ascii_octdigit)]
173+
#![feature(is_val_statically_known)]
173174
#![feature(isqrt)]
174175
#![feature(link_cfg)]
175176
#![feature(offset_of_enum)]

library/core/src/num/int_macros.rs

+30-23
Original file line numberDiff line numberDiff line change
@@ -2746,32 +2746,39 @@ macro_rules! int_impl {
27462746
pub const fn pow(self, mut exp: u32) -> Self {
27472747
let mut base = self;
27482748

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;
2749+
if intrinsics::is_val_statically_known(exp) {
2750+
// Unroll multiplications for small exponent values.
2751+
// This gives the optimizer a way to efficiently inline call sites
2752+
// for the most common use cases with constant exponents.
2753+
// Currently, LLVM is unable to unroll the loop below.
2754+
match exp {
2755+
0 => return 1,
2756+
1 => return base,
2757+
2 => return base * base,
2758+
3 => {
2759+
let squared = base * base;
2760+
return squared * base;
2761+
}
2762+
4 => {
2763+
let squared = base * base;
2764+
return squared * squared;
2765+
}
2766+
5 => {
2767+
let squared = base * base;
2768+
return squared * squared * base;
2769+
}
2770+
6 => {
2771+
let cubed = base * base * base;
2772+
return cubed * cubed;
2773+
}
2774+
_ => {}
27682775
}
2769-
6 => {
2770-
let cubed = base * base * base;
2771-
return cubed * cubed;
2776+
} else {
2777+
if exp == 0 {
2778+
return 1;
27722779
}
2773-
_ => {}
27742780
}
2781+
debug_assert!(exp != 0);
27752782

27762783
let mut acc = 1;
27772784

0 commit comments

Comments
 (0)