@@ -2232,6 +2232,22 @@ macro_rules! uint_impl {
22322232 let mut base = self ;
22332233 let mut acc: Self = 1 ;
22342234
2235+ if intrinsics:: is_val_statically_known( exp) {
2236+ while exp > 1 {
2237+ if ( exp & 1 ) == 1 {
2238+ acc = try_opt!( acc. checked_mul( base) ) ;
2239+ }
2240+ exp /= 2 ;
2241+ base = try_opt!( base. checked_mul( base) ) ;
2242+ }
2243+
2244+ // since exp!=0, finally the exp must be 1.
2245+ // Deal with the final bit of the exponent separately, since
2246+ // squaring the base afterwards is not necessary and may cause a
2247+ // needless overflow.
2248+ return acc. checked_mul( base) ;
2249+ }
2250+
22352251 loop {
22362252 if ( exp & 1 ) == 1 {
22372253 acc = try_opt!( acc. checked_mul( base) ) ;
@@ -3417,6 +3433,26 @@ macro_rules! uint_impl {
34173433 let mut overflow = false ;
34183434 let mut tmp_overflow;
34193435
3436+ if intrinsics:: is_val_statically_known( exp) {
3437+ while exp > 1 {
3438+ if ( exp & 1 ) == 1 {
3439+ ( acc, tmp_overflow) = acc. overflowing_mul( base) ;
3440+ overflow |= tmp_overflow;
3441+ }
3442+ exp /= 2 ;
3443+ ( base, tmp_overflow) = base. overflowing_mul( base) ;
3444+ overflow |= tmp_overflow;
3445+ }
3446+
3447+ // since exp!=0, finally the exp must be 1.
3448+ // Deal with the final bit of the exponent separately, since
3449+ // squaring the base afterwards is not necessary and may cause a
3450+ // needless overflow.
3451+ ( acc, tmp_overflow) = acc. overflowing_mul( base) ;
3452+ overflow |= tmp_overflow;
3453+ return ( acc, overflow) ;
3454+ }
3455+
34203456 loop {
34213457 if ( exp & 1 ) == 1 {
34223458 ( acc, tmp_overflow) = acc. overflowing_mul( base) ;
0 commit comments