@@ -1103,6 +1103,9 @@ $EndFeature, "
1103
1103
without modifying the original"]
1104
1104
#[ inline]
1105
1105
pub const fn checked_pow( self , mut exp: u32 ) -> Option <Self > {
1106
+ if exp == 0 {
1107
+ return Some ( 1 ) ;
1108
+ }
1106
1109
let mut base = self ;
1107
1110
let mut acc: Self = 1 ;
1108
1111
@@ -1113,15 +1116,11 @@ $EndFeature, "
1113
1116
exp /= 2 ;
1114
1117
base = try_opt!( base. checked_mul( base) ) ;
1115
1118
}
1116
-
1119
+ // since exp!=0, finally the exp must be 1.
1117
1120
// Deal with the final bit of the exponent separately, since
1118
1121
// squaring the base afterwards is not necessary and may cause a
1119
1122
// needless overflow.
1120
- if exp == 1 {
1121
- acc = try_opt!( acc. checked_mul( base) ) ;
1122
- }
1123
-
1124
- Some ( acc)
1123
+ Some ( try_opt!( acc. checked_mul( base) ) )
1125
1124
}
1126
1125
}
1127
1126
@@ -1631,6 +1630,9 @@ $EndFeature, "
1631
1630
without modifying the original"]
1632
1631
#[ inline]
1633
1632
pub const fn wrapping_pow( self , mut exp: u32 ) -> Self {
1633
+ if exp == 0 {
1634
+ return 1 ;
1635
+ }
1634
1636
let mut base = self ;
1635
1637
let mut acc: Self = 1 ;
1636
1638
@@ -1641,15 +1643,12 @@ $EndFeature, "
1641
1643
exp /= 2 ;
1642
1644
base = base. wrapping_mul( base) ;
1643
1645
}
1644
-
1646
+
1647
+ // since exp!=0, finally the exp must be 1.
1645
1648
// Deal with the final bit of the exponent separately, since
1646
1649
// squaring the base afterwards is not necessary and may cause a
1647
1650
// needless overflow.
1648
- if exp == 1 {
1649
- acc = acc. wrapping_mul( base) ;
1650
- }
1651
-
1652
- acc
1651
+ acc. wrapping_mul( base) ;
1653
1652
}
1654
1653
}
1655
1654
@@ -1999,6 +1998,9 @@ $EndFeature, "
1999
1998
without modifying the original"]
2000
1999
#[ inline]
2001
2000
pub const fn overflowing_pow( self , mut exp: u32 ) -> ( Self , bool ) {
2001
+ if exp == 0 {
2002
+ return ( 1 , false ) ;
2003
+ }
2002
2004
let mut base = self ;
2003
2005
let mut acc: Self = 1 ;
2004
2006
let mut overflown = false ;
@@ -2016,17 +2018,14 @@ $EndFeature, "
2016
2018
base = r. 0 ;
2017
2019
overflown |= r. 1 ;
2018
2020
}
2019
-
2021
+
2022
+ // since exp!=0, finally the exp must be 1.
2020
2023
// Deal with the final bit of the exponent separately, since
2021
2024
// squaring the base afterwards is not necessary and may cause a
2022
2025
// needless overflow.
2023
- if exp == 1 {
2024
- r = acc. overflowing_mul( base) ;
2025
- acc = r. 0 ;
2026
- overflown |= r. 1 ;
2027
- }
2028
-
2029
- ( acc, overflown)
2026
+ r = acc. overflowing_mul( base) ;
2027
+ r. 1 |= overflown;
2028
+ r
2030
2029
}
2031
2030
}
2032
2031
@@ -2050,6 +2049,9 @@ $EndFeature, "
2050
2049
#[ inline]
2051
2050
#[ rustc_inherit_overflow_checks]
2052
2051
pub const fn pow( self , mut exp: u32 ) -> Self {
2052
+ if exp == 0 {
2053
+ return 1 ;
2054
+ }
2053
2055
let mut base = self ;
2054
2056
let mut acc = 1 ;
2055
2057
@@ -2061,14 +2063,11 @@ $EndFeature, "
2061
2063
base = base * base;
2062
2064
}
2063
2065
2066
+ // since exp!=0, finally the exp must be 1.
2064
2067
// Deal with the final bit of the exponent separately, since
2065
2068
// squaring the base afterwards is not necessary and may cause a
2066
2069
// needless overflow.
2067
- if exp == 1 {
2068
- acc = acc * base;
2069
- }
2070
-
2071
- acc
2070
+ acc * base
2072
2071
}
2073
2072
}
2074
2073
@@ -3306,6 +3305,9 @@ assert_eq!(", stringify!($SelfT), "::MAX.checked_pow(2), None);", $EndFeature, "
3306
3305
without modifying the original"]
3307
3306
#[ inline]
3308
3307
pub const fn checked_pow( self , mut exp: u32 ) -> Option <Self > {
3308
+ if exp == 0 {
3309
+ return Some ( 1 ) ;
3310
+ }
3309
3311
let mut base = self ;
3310
3312
let mut acc: Self = 1 ;
3311
3313
@@ -3317,14 +3319,12 @@ assert_eq!(", stringify!($SelfT), "::MAX.checked_pow(2), None);", $EndFeature, "
3317
3319
base = try_opt!( base. checked_mul( base) ) ;
3318
3320
}
3319
3321
3322
+ // since exp!=0, finally the exp must be 1.
3320
3323
// Deal with the final bit of the exponent separately, since
3321
3324
// squaring the base afterwards is not necessary and may cause a
3322
3325
// needless overflow.
3323
- if exp == 1 {
3324
- acc = try_opt!( acc. checked_mul( base) ) ;
3325
- }
3326
3326
3327
- Some ( acc)
3327
+ Some ( try_opt! ( acc. checked_mul ( base ) ) )
3328
3328
}
3329
3329
}
3330
3330
@@ -3715,6 +3715,9 @@ assert_eq!(3u8.wrapping_pow(6), 217);", $EndFeature, "
3715
3715
without modifying the original"]
3716
3716
#[ inline]
3717
3717
pub const fn wrapping_pow( self , mut exp: u32 ) -> Self {
3718
+ if exp == 0 {
3719
+ return 1 ;
3720
+ }
3718
3721
let mut base = self ;
3719
3722
let mut acc: Self = 1 ;
3720
3723
@@ -3726,14 +3729,11 @@ assert_eq!(3u8.wrapping_pow(6), 217);", $EndFeature, "
3726
3729
base = base. wrapping_mul( base) ;
3727
3730
}
3728
3731
3732
+ // since exp!=0, finally the exp must be 1.
3729
3733
// Deal with the final bit of the exponent separately, since
3730
3734
// squaring the base afterwards is not necessary and may cause a
3731
3735
// needless overflow.
3732
- if exp == 1 {
3733
- acc = acc. wrapping_mul( base) ;
3734
- }
3735
-
3736
- acc
3736
+ acc. wrapping_mul( base)
3737
3737
}
3738
3738
}
3739
3739
@@ -4058,16 +4058,14 @@ assert_eq!(3u8.overflowing_pow(6), (217, true));", $EndFeature, "
4058
4058
overflown |= r. 1 ;
4059
4059
}
4060
4060
4061
+ // since exp!=0, finally the exp must be 1.
4061
4062
// Deal with the final bit of the exponent separately, since
4062
4063
// squaring the base afterwards is not necessary and may cause a
4063
4064
// needless overflow.
4064
- if exp == 1 {
4065
- r = acc. overflowing_mul( base) ;
4066
- acc = r. 0 ;
4067
- overflown |= r. 1 ;
4068
- }
4065
+ r = acc. overflowing_mul( base) ;
4066
+ r. 1 |= overflown;
4069
4067
4070
- ( acc , overflown )
4068
+ r
4071
4069
}
4072
4070
}
4073
4071
@@ -4088,6 +4086,9 @@ Basic usage:
4088
4086
#[ inline]
4089
4087
#[ rustc_inherit_overflow_checks]
4090
4088
pub const fn pow( self , mut exp: u32 ) -> Self {
4089
+ if exp == 0 {
4090
+ return 1 ;
4091
+ }
4091
4092
let mut base = self ;
4092
4093
let mut acc = 1 ;
4093
4094
@@ -4099,14 +4100,11 @@ Basic usage:
4099
4100
base = base * base;
4100
4101
}
4101
4102
4103
+ // since exp!=0, finally the exp must be 1.
4102
4104
// Deal with the final bit of the exponent separately, since
4103
4105
// squaring the base afterwards is not necessary and may cause a
4104
4106
// needless overflow.
4105
- if exp == 1 {
4106
- acc = acc * base;
4107
- }
4108
-
4109
- acc
4107
+ acc * base
4110
4108
}
4111
4109
}
4112
4110
0 commit comments