@@ -2413,23 +2413,10 @@ macro_rules! uint_impl {
24132413 without modifying the original"]
24142414 #[ inline]
24152415 #[ track_caller]
2416- pub const fn strict_pow( self , mut exp: u32 ) -> Self {
2417- if exp == 0 {
2418- return 1 ;
2419- }
2420- let mut base = self ;
2421- let mut acc: Self = 1 ;
2422-
2423- loop {
2424- if ( exp & 1 ) == 1 {
2425- acc = acc. strict_mul( base) ;
2426- // since exp!=0, finally the exp must be 1.
2427- if exp == 1 {
2428- return acc;
2429- }
2430- }
2431- exp /= 2 ;
2432- base = base. strict_mul( base) ;
2416+ pub const fn strict_pow( self , exp: u32 ) -> Self {
2417+ match self . checked_pow( exp) {
2418+ None => imp:: overflow_panic:: pow( ) ,
2419+ Some ( a) => a,
24332420 }
24342421 }
24352422
@@ -2919,43 +2906,9 @@ macro_rules! uint_impl {
29192906 #[ must_use = "this returns the result of the operation, \
29202907 without modifying the original"]
29212908 #[ inline]
2922- pub const fn wrapping_pow( self , mut exp: u32 ) -> Self {
2923- if exp == 0 {
2924- return 1 ;
2925- }
2926- let mut base = self ;
2927- let mut acc: Self = 1 ;
2928-
2929- if intrinsics:: is_val_statically_known( exp) {
2930- while exp > 1 {
2931- if ( exp & 1 ) == 1 {
2932- acc = acc. wrapping_mul( base) ;
2933- }
2934- exp /= 2 ;
2935- base = base. wrapping_mul( base) ;
2936- }
2937-
2938- // since exp!=0, finally the exp must be 1.
2939- // Deal with the final bit of the exponent separately, since
2940- // squaring the base afterwards is not necessary.
2941- acc. wrapping_mul( base)
2942- } else {
2943- // This is faster than the above when the exponent is not known
2944- // at compile time. We can't use the same code for the constant
2945- // exponent case because LLVM is currently unable to unroll
2946- // this loop.
2947- loop {
2948- if ( exp & 1 ) == 1 {
2949- acc = acc. wrapping_mul( base) ;
2950- // since exp!=0, finally the exp must be 1.
2951- if exp == 1 {
2952- return acc;
2953- }
2954- }
2955- exp /= 2 ;
2956- base = base. wrapping_mul( base) ;
2957- }
2958- }
2909+ pub const fn wrapping_pow( self , exp: u32 ) -> Self {
2910+ let ( a, _) = self . overflowing_pow( exp) ;
2911+ a
29592912 }
29602913
29612914 /// Calculates `self` + `rhs`.
@@ -3597,30 +3550,26 @@ macro_rules! uint_impl {
35973550 without modifying the original"]
35983551 #[ inline]
35993552 pub const fn overflowing_pow( self , mut exp: u32 ) -> ( Self , bool ) {
3600- if exp == 0 {
3601- return ( 1 , false ) ;
3553+ if exp == 0 {
3554+ return ( 1 , false ) ;
36023555 }
36033556 let mut base = self ;
36043557 let mut acc: Self = 1 ;
3605- let mut overflown = false ;
3606- // Scratch space for storing results of overflowing_mul.
3607- let mut r;
3558+ let mut overflow = false ;
3559+ let mut tmp_overflow;
36083560
36093561 loop {
36103562 if ( exp & 1 ) == 1 {
3611- r = acc. overflowing_mul( base) ;
3563+ ( acc, tmp_overflow) = acc. overflowing_mul( base) ;
3564+ overflow |= tmp_overflow;
36123565 // since exp!=0, finally the exp must be 1.
36133566 if exp == 1 {
3614- r. 1 |= overflown;
3615- return r;
3567+ return ( acc, overflow) ;
36163568 }
3617- acc = r. 0 ;
3618- overflown |= r. 1 ;
36193569 }
36203570 exp /= 2 ;
3621- r = base. overflowing_mul( base) ;
3622- base = r. 0 ;
3623- overflown |= r. 1 ;
3571+ ( base, tmp_overflow) = base. overflowing_mul( base) ;
3572+ overflow |= tmp_overflow;
36243573 }
36253574 }
36263575
@@ -3638,43 +3587,11 @@ macro_rules! uint_impl {
36383587 without modifying the original"]
36393588 #[ inline]
36403589 #[ rustc_inherit_overflow_checks]
3641- pub const fn pow( self , mut exp: u32 ) -> Self {
3642- if exp == 0 {
3643- return 1 ;
3644- }
3645- let mut base = self ;
3646- let mut acc = 1 ;
3647-
3648- if intrinsics:: is_val_statically_known( exp) {
3649- while exp > 1 {
3650- if ( exp & 1 ) == 1 {
3651- acc = acc * base;
3652- }
3653- exp /= 2 ;
3654- base = base * base;
3655- }
3656-
3657- // since exp!=0, finally the exp must be 1.
3658- // Deal with the final bit of the exponent separately, since
3659- // squaring the base afterwards is not necessary and may cause a
3660- // needless overflow.
3661- acc * base
3590+ pub const fn pow( self , exp: u32 ) -> Self {
3591+ if intrinsics:: overflow_checks( ) {
3592+ self . strict_pow( exp)
36623593 } else {
3663- // This is faster than the above when the exponent is not known
3664- // at compile time. We can't use the same code for the constant
3665- // exponent case because LLVM is currently unable to unroll
3666- // this loop.
3667- loop {
3668- if ( exp & 1 ) == 1 {
3669- acc = acc * base;
3670- // since exp!=0, finally the exp must be 1.
3671- if exp == 1 {
3672- return acc;
3673- }
3674- }
3675- exp /= 2 ;
3676- base = base * base;
3677- }
3594+ self . wrapping_pow( exp)
36783595 }
36793596 }
36803597
0 commit comments