@@ -2272,23 +2272,10 @@ macro_rules! uint_impl {
22722272 without modifying the original"]
22732273 #[ inline]
22742274 #[ track_caller]
2275- pub const fn strict_pow( self , mut exp: u32 ) -> Self {
2276- if exp == 0 {
2277- return 1 ;
2278- }
2279- let mut base = self ;
2280- let mut acc: Self = 1 ;
2281-
2282- loop {
2283- if ( exp & 1 ) == 1 {
2284- acc = acc. strict_mul( base) ;
2285- // since exp!=0, finally the exp must be 1.
2286- if exp == 1 {
2287- return acc;
2288- }
2289- }
2290- exp /= 2 ;
2291- base = base. strict_mul( base) ;
2275+ pub const fn strict_pow( self , exp: u32 ) -> Self {
2276+ match self . checked_pow( exp) {
2277+ None => imp:: overflow_panic:: pow( ) ,
2278+ Some ( a) => a,
22922279 }
22932280 }
22942281
@@ -2778,43 +2765,9 @@ macro_rules! uint_impl {
27782765 #[ must_use = "this returns the result of the operation, \
27792766 without modifying the original"]
27802767 #[ inline]
2781- pub const fn wrapping_pow( self , mut exp: u32 ) -> Self {
2782- if exp == 0 {
2783- return 1 ;
2784- }
2785- let mut base = self ;
2786- let mut acc: Self = 1 ;
2787-
2788- if intrinsics:: is_val_statically_known( exp) {
2789- while exp > 1 {
2790- if ( exp & 1 ) == 1 {
2791- acc = acc. wrapping_mul( base) ;
2792- }
2793- exp /= 2 ;
2794- base = base. wrapping_mul( base) ;
2795- }
2796-
2797- // since exp!=0, finally the exp must be 1.
2798- // Deal with the final bit of the exponent separately, since
2799- // squaring the base afterwards is not necessary.
2800- acc. wrapping_mul( base)
2801- } else {
2802- // This is faster than the above when the exponent is not known
2803- // at compile time. We can't use the same code for the constant
2804- // exponent case because LLVM is currently unable to unroll
2805- // this loop.
2806- loop {
2807- if ( exp & 1 ) == 1 {
2808- acc = acc. wrapping_mul( base) ;
2809- // since exp!=0, finally the exp must be 1.
2810- if exp == 1 {
2811- return acc;
2812- }
2813- }
2814- exp /= 2 ;
2815- base = base. wrapping_mul( base) ;
2816- }
2817- }
2768+ pub const fn wrapping_pow( self , exp: u32 ) -> Self {
2769+ let ( a, _) = self . overflowing_pow( exp) ;
2770+ a
28182771 }
28192772
28202773 /// Calculates `self` + `rhs`.
@@ -3456,30 +3409,26 @@ macro_rules! uint_impl {
34563409 without modifying the original"]
34573410 #[ inline]
34583411 pub const fn overflowing_pow( self , mut exp: u32 ) -> ( Self , bool ) {
3459- if exp == 0 {
3460- return ( 1 , false ) ;
3412+ if exp == 0 {
3413+ return ( 1 , false ) ;
34613414 }
34623415 let mut base = self ;
34633416 let mut acc: Self = 1 ;
3464- let mut overflown = false ;
3465- // Scratch space for storing results of overflowing_mul.
3466- let mut r;
3417+ let mut overflow = false ;
3418+ let mut tmp_overflow;
34673419
34683420 loop {
34693421 if ( exp & 1 ) == 1 {
3470- r = acc. overflowing_mul( base) ;
3422+ ( acc, tmp_overflow) = acc. overflowing_mul( base) ;
3423+ overflow |= tmp_overflow;
34713424 // since exp!=0, finally the exp must be 1.
34723425 if exp == 1 {
3473- r. 1 |= overflown;
3474- return r;
3426+ return ( acc, overflow) ;
34753427 }
3476- acc = r. 0 ;
3477- overflown |= r. 1 ;
34783428 }
34793429 exp /= 2 ;
3480- r = base. overflowing_mul( base) ;
3481- base = r. 0 ;
3482- overflown |= r. 1 ;
3430+ ( base, tmp_overflow) = base. overflowing_mul( base) ;
3431+ overflow |= tmp_overflow;
34833432 }
34843433 }
34853434
@@ -3497,43 +3446,11 @@ macro_rules! uint_impl {
34973446 without modifying the original"]
34983447 #[ inline]
34993448 #[ rustc_inherit_overflow_checks]
3500- pub const fn pow( self , mut exp: u32 ) -> Self {
3501- if exp == 0 {
3502- return 1 ;
3503- }
3504- let mut base = self ;
3505- let mut acc = 1 ;
3506-
3507- if intrinsics:: is_val_statically_known( exp) {
3508- while exp > 1 {
3509- if ( exp & 1 ) == 1 {
3510- acc = acc * base;
3511- }
3512- exp /= 2 ;
3513- base = base * base;
3514- }
3515-
3516- // since exp!=0, finally the exp must be 1.
3517- // Deal with the final bit of the exponent separately, since
3518- // squaring the base afterwards is not necessary and may cause a
3519- // needless overflow.
3520- acc * base
3449+ pub const fn pow( self , exp: u32 ) -> Self {
3450+ if intrinsics:: overflow_checks( ) {
3451+ self . strict_pow( exp)
35213452 } else {
3522- // This is faster than the above when the exponent is not known
3523- // at compile time. We can't use the same code for the constant
3524- // exponent case because LLVM is currently unable to unroll
3525- // this loop.
3526- loop {
3527- if ( exp & 1 ) == 1 {
3528- acc = acc * base;
3529- // since exp!=0, finally the exp must be 1.
3530- if exp == 1 {
3531- return acc;
3532- }
3533- }
3534- exp /= 2 ;
3535- base = base * base;
3536- }
3453+ self . wrapping_pow( exp)
35373454 }
35383455 }
35393456
0 commit comments