@@ -1095,6 +1095,9 @@ $EndFeature, "
10951095 without modifying the original"]
10961096 #[ inline]
10971097 pub const fn checked_pow( self , mut exp: u32 ) -> Option <Self > {
1098+ if exp == 0 {
1099+ return Some ( 1 ) ;
1100+ }
10981101 let mut base = self ;
10991102 let mut acc: Self = 1 ;
11001103
@@ -1105,15 +1108,11 @@ $EndFeature, "
11051108 exp /= 2 ;
11061109 base = try_opt!( base. checked_mul( base) ) ;
11071110 }
1108-
1111+ // since exp!=0, finally the exp must be 1.
11091112 // Deal with the final bit of the exponent separately, since
11101113 // squaring the base afterwards is not necessary and may cause a
11111114 // needless overflow.
1112- if exp == 1 {
1113- acc = try_opt!( acc. checked_mul( base) ) ;
1114- }
1115-
1116- Some ( acc)
1115+ Some ( try_opt!( acc. checked_mul( base) ) )
11171116 }
11181117 }
11191118
@@ -1622,6 +1621,9 @@ $EndFeature, "
16221621 without modifying the original"]
16231622 #[ inline]
16241623 pub const fn wrapping_pow( self , mut exp: u32 ) -> Self {
1624+ if exp == 0 {
1625+ return 1 ;
1626+ }
16251627 let mut base = self ;
16261628 let mut acc: Self = 1 ;
16271629
@@ -1633,14 +1635,11 @@ $EndFeature, "
16331635 base = base. wrapping_mul( base) ;
16341636 }
16351637
1638+ // since exp!=0, finally the exp must be 1.
16361639 // Deal with the final bit of the exponent separately, since
16371640 // squaring the base afterwards is not necessary and may cause a
16381641 // needless overflow.
1639- if exp == 1 {
1640- acc = acc. wrapping_mul( base) ;
1641- }
1642-
1643- acc
1642+ acc. wrapping_mul( base)
16441643 }
16451644 }
16461645
@@ -1989,6 +1988,9 @@ $EndFeature, "
19891988 without modifying the original"]
19901989 #[ inline]
19911990 pub const fn overflowing_pow( self , mut exp: u32 ) -> ( Self , bool ) {
1991+ if exp == 0 {
1992+ return ( 1 , false ) ;
1993+ }
19921994 let mut base = self ;
19931995 let mut acc: Self = 1 ;
19941996 let mut overflown = false ;
@@ -2007,16 +2009,13 @@ $EndFeature, "
20072009 overflown |= r. 1 ;
20082010 }
20092011
2012+ // since exp!=0, finally the exp must be 1.
20102013 // Deal with the final bit of the exponent separately, since
20112014 // squaring the base afterwards is not necessary and may cause a
20122015 // needless overflow.
2013- if exp == 1 {
2014- r = acc. overflowing_mul( base) ;
2015- acc = r. 0 ;
2016- overflown |= r. 1 ;
2017- }
2018-
2019- ( acc, overflown)
2016+ r = acc. overflowing_mul( base) ;
2017+ r. 1 |= overflown;
2018+ r
20202019 }
20212020 }
20222021
@@ -2040,6 +2039,9 @@ $EndFeature, "
20402039 #[ inline]
20412040 #[ rustc_inherit_overflow_checks]
20422041 pub const fn pow( self , mut exp: u32 ) -> Self {
2042+ if exp == 0 {
2043+ return 1 ;
2044+ }
20432045 let mut base = self ;
20442046 let mut acc = 1 ;
20452047
@@ -2051,14 +2053,11 @@ $EndFeature, "
20512053 base = base * base;
20522054 }
20532055
2056+ // since exp!=0, finally the exp must be 1.
20542057 // Deal with the final bit of the exponent separately, since
20552058 // squaring the base afterwards is not necessary and may cause a
20562059 // needless overflow.
2057- if exp == 1 {
2058- acc = acc * base;
2059- }
2060-
2061- acc
2060+ acc * base
20622061 }
20632062 }
20642063
@@ -3295,6 +3294,9 @@ assert_eq!(", stringify!($SelfT), "::MAX.checked_pow(2), None);", $EndFeature, "
32953294 without modifying the original"]
32963295 #[ inline]
32973296 pub const fn checked_pow( self , mut exp: u32 ) -> Option <Self > {
3297+ if exp == 0 {
3298+ return Some ( 1 ) ;
3299+ }
32983300 let mut base = self ;
32993301 let mut acc: Self = 1 ;
33003302
@@ -3306,14 +3308,12 @@ assert_eq!(", stringify!($SelfT), "::MAX.checked_pow(2), None);", $EndFeature, "
33063308 base = try_opt!( base. checked_mul( base) ) ;
33073309 }
33083310
3311+ // since exp!=0, finally the exp must be 1.
33093312 // Deal with the final bit of the exponent separately, since
33103313 // squaring the base afterwards is not necessary and may cause a
33113314 // needless overflow.
3312- if exp == 1 {
3313- acc = try_opt!( acc. checked_mul( base) ) ;
3314- }
33153315
3316- Some ( acc)
3316+ Some ( try_opt! ( acc. checked_mul ( base ) ) )
33173317 }
33183318 }
33193319
@@ -3704,6 +3704,9 @@ assert_eq!(3u8.wrapping_pow(6), 217);", $EndFeature, "
37043704 without modifying the original"]
37053705 #[ inline]
37063706 pub const fn wrapping_pow( self , mut exp: u32 ) -> Self {
3707+ if exp == 0 {
3708+ return 1 ;
3709+ }
37073710 let mut base = self ;
37083711 let mut acc: Self = 1 ;
37093712
@@ -3715,14 +3718,11 @@ assert_eq!(3u8.wrapping_pow(6), 217);", $EndFeature, "
37153718 base = base. wrapping_mul( base) ;
37163719 }
37173720
3721+ // since exp!=0, finally the exp must be 1.
37183722 // Deal with the final bit of the exponent separately, since
37193723 // squaring the base afterwards is not necessary and may cause a
37203724 // needless overflow.
3721- if exp == 1 {
3722- acc = acc. wrapping_mul( base) ;
3723- }
3724-
3725- acc
3725+ acc. wrapping_mul( base)
37263726 }
37273727 }
37283728
@@ -4029,6 +4029,9 @@ assert_eq!(3u8.overflowing_pow(6), (217, true));", $EndFeature, "
40294029 without modifying the original"]
40304030 #[ inline]
40314031 pub const fn overflowing_pow( self , mut exp: u32 ) -> ( Self , bool ) {
4032+ if exp == 0 {
4033+ return ( 1 , false ) ;
4034+ }
40324035 let mut base = self ;
40334036 let mut acc: Self = 1 ;
40344037 let mut overflown = false ;
@@ -4047,16 +4050,14 @@ assert_eq!(3u8.overflowing_pow(6), (217, true));", $EndFeature, "
40474050 overflown |= r. 1 ;
40484051 }
40494052
4053+ // since exp!=0, finally the exp must be 1.
40504054 // Deal with the final bit of the exponent separately, since
40514055 // squaring the base afterwards is not necessary and may cause a
40524056 // needless overflow.
4053- if exp == 1 {
4054- r = acc. overflowing_mul( base) ;
4055- acc = r. 0 ;
4056- overflown |= r. 1 ;
4057- }
4057+ r = acc. overflowing_mul( base) ;
4058+ r. 1 |= overflown;
40584059
4059- ( acc , overflown )
4060+ r
40604061 }
40614062 }
40624063
@@ -4077,6 +4078,9 @@ Basic usage:
40774078 #[ inline]
40784079 #[ rustc_inherit_overflow_checks]
40794080 pub const fn pow( self , mut exp: u32 ) -> Self {
4081+ if exp == 0 {
4082+ return 1 ;
4083+ }
40804084 let mut base = self ;
40814085 let mut acc = 1 ;
40824086
@@ -4088,14 +4092,11 @@ Basic usage:
40884092 base = base * base;
40894093 }
40904094
4095+ // since exp!=0, finally the exp must be 1.
40914096 // Deal with the final bit of the exponent separately, since
40924097 // squaring the base afterwards is not necessary and may cause a
40934098 // needless overflow.
4094- if exp == 1 {
4095- acc = acc * base;
4096- }
4097-
4098- acc
4099+ acc * base
40994100 }
41004101 }
41014102
0 commit comments