@@ -2373,6 +2373,22 @@ macro_rules! uint_impl {
23732373 let mut base = self ;
23742374 let mut acc: Self = 1 ;
23752375
2376+ if intrinsics:: is_val_statically_known( exp) {
2377+ while exp > 1 {
2378+ if ( exp & 1 ) == 1 {
2379+ acc = try_opt!( acc. checked_mul( base) ) ;
2380+ }
2381+ exp /= 2 ;
2382+ base = try_opt!( base. checked_mul( base) ) ;
2383+ }
2384+
2385+ // since exp!=0, finally the exp must be 1.
2386+ // Deal with the final bit of the exponent separately, since
2387+ // squaring the base afterwards is not necessary and may cause a
2388+ // needless overflow.
2389+ return acc. checked_mul( base) ;
2390+ }
2391+
23762392 loop {
23772393 if ( exp & 1 ) == 1 {
23782394 acc = try_opt!( acc. checked_mul( base) ) ;
@@ -3558,6 +3574,26 @@ macro_rules! uint_impl {
35583574 let mut overflow = false ;
35593575 let mut tmp_overflow;
35603576
3577+ if intrinsics:: is_val_statically_known( exp) {
3578+ while exp > 1 {
3579+ if ( exp & 1 ) == 1 {
3580+ ( acc, tmp_overflow) = acc. overflowing_mul( base) ;
3581+ overflow |= tmp_overflow;
3582+ }
3583+ exp /= 2 ;
3584+ ( base, tmp_overflow) = base. overflowing_mul( base) ;
3585+ overflow |= tmp_overflow;
3586+ }
3587+
3588+ // since exp!=0, finally the exp must be 1.
3589+ // Deal with the final bit of the exponent separately, since
3590+ // squaring the base afterwards is not necessary and may cause a
3591+ // needless overflow.
3592+ ( acc, tmp_overflow) = acc. overflowing_mul( base) ;
3593+ overflow |= tmp_overflow;
3594+ return ( acc, overflow) ;
3595+ }
3596+
35613597 loop {
35623598 if ( exp & 1 ) == 1 {
35633599 ( acc, tmp_overflow) = acc. overflowing_mul( base) ;
0 commit comments