@@ -2048,35 +2048,43 @@ macro_rules! uint_impl {
2048
2048
#[ must_use = "this returns the result of the operation, \
2049
2049
without modifying the original"]
2050
2050
#[ inline]
2051
+ #[ rustc_allow_const_fn_unstable( is_val_statically_known) ]
2051
2052
pub const fn wrapping_pow( self , mut exp: u32 ) -> Self {
2052
2053
let mut base = self ;
2053
2054
2054
- // Unroll multiplications for small exponent values.
2055
- // This gives the optimizer a way to efficiently inline call sites
2056
- // for the most common use cases with constant exponents.
2057
- // Currently, LLVM is unable to unroll the loop below.
2058
- match exp {
2059
- 0 => return 1 ,
2060
- 1 => return base,
2061
- 2 => return base. wrapping_mul( base) ,
2062
- 3 => {
2063
- let squared = base. wrapping_mul( base) ;
2064
- return squared. wrapping_mul( base) ;
2065
- }
2066
- 4 => {
2067
- let squared = base. wrapping_mul( base) ;
2068
- return squared. wrapping_mul( squared) ;
2069
- }
2070
- 5 => {
2071
- let squared = base. wrapping_mul( base) ;
2072
- return squared. wrapping_mul( squared) . wrapping_mul( base) ;
2055
+ if intrinsics:: is_val_statically_known( exp) {
2056
+ // Unroll multiplications for small exponent values.
2057
+ // This gives the optimizer a way to efficiently inline call sites
2058
+ // for the most common use cases with constant exponents.
2059
+ // Currently, LLVM is unable to unroll the loop below.
2060
+ match exp {
2061
+ 0 => return 1 ,
2062
+ 1 => return base,
2063
+ 2 => return base. wrapping_mul( base) ,
2064
+ 3 => {
2065
+ let squared = base. wrapping_mul( base) ;
2066
+ return squared. wrapping_mul( base) ;
2067
+ }
2068
+ 4 => {
2069
+ let squared = base. wrapping_mul( base) ;
2070
+ return squared. wrapping_mul( squared) ;
2071
+ }
2072
+ 5 => {
2073
+ let squared = base. wrapping_mul( base) ;
2074
+ return squared. wrapping_mul( squared) . wrapping_mul( base) ;
2075
+ }
2076
+ 6 => {
2077
+ let cubed = base. wrapping_mul( base) . wrapping_mul( base) ;
2078
+ return cubed. wrapping_mul( cubed) ;
2079
+ }
2080
+ _ => { }
2073
2081
}
2074
- 6 => {
2075
- let cubed = base . wrapping_mul ( base ) . wrapping_mul ( base ) ;
2076
- return cubed . wrapping_mul ( cubed ) ;
2082
+ } else {
2083
+ if exp == 0 {
2084
+ return 1 ;
2077
2085
}
2078
- _ => { }
2079
2086
}
2087
+ debug_assert!( exp != 0 ) ;
2080
2088
2081
2089
let mut acc: Self = 1 ;
2082
2090
@@ -2568,35 +2576,43 @@ macro_rules! uint_impl {
2568
2576
without modifying the original"]
2569
2577
#[ inline]
2570
2578
#[ rustc_inherit_overflow_checks]
2579
+ #[ rustc_allow_const_fn_unstable( is_val_statically_known) ]
2571
2580
pub const fn pow( self , mut exp: u32 ) -> Self {
2572
2581
let mut base = self ;
2573
2582
2574
- // Unroll multiplications for small exponent values.
2575
- // This gives the optimizer a way to efficiently inline call sites
2576
- // for the most common use cases with constant exponents.
2577
- // Currently, LLVM is unable to unroll the loop below.
2578
- match exp {
2579
- 0 => return 1 ,
2580
- 1 => return base,
2581
- 2 => return base * base,
2582
- 3 => {
2583
- let squared = base * base;
2584
- return squared * base;
2585
- }
2586
- 4 => {
2587
- let squared = base * base;
2588
- return squared * squared;
2589
- }
2590
- 5 => {
2591
- let squared = base * base;
2592
- return squared * squared * base;
2583
+ if intrinsics:: is_val_statically_known( exp) {
2584
+ // Unroll multiplications for small exponent values.
2585
+ // This gives the optimizer a way to efficiently inline call sites
2586
+ // for the most common use cases with constant exponents.
2587
+ // Currently, LLVM is unable to unroll the loop below.
2588
+ match exp {
2589
+ 0 => return 1 ,
2590
+ 1 => return base,
2591
+ 2 => return base * base,
2592
+ 3 => {
2593
+ let squared = base * base;
2594
+ return squared * base;
2595
+ }
2596
+ 4 => {
2597
+ let squared = base * base;
2598
+ return squared * squared;
2599
+ }
2600
+ 5 => {
2601
+ let squared = base * base;
2602
+ return squared * squared * base;
2603
+ }
2604
+ 6 => {
2605
+ let cubed = base * base * base;
2606
+ return cubed * cubed;
2607
+ }
2608
+ _ => { }
2593
2609
}
2594
- 6 => {
2595
- let cubed = base * base * base ;
2596
- return cubed * cubed ;
2610
+ } else {
2611
+ if exp == 0 {
2612
+ return 1 ;
2597
2613
}
2598
- _ => { }
2599
2614
}
2615
+ debug_assert!( exp != 0 ) ;
2600
2616
2601
2617
let mut acc = 1 ;
2602
2618
0 commit comments