Skip to content

Commit 269bf89

Browse files
Make integer power methods const
1 parent 2afa993 commit 269bf89

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

src/libcore/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,14 @@
7272
#![feature(concat_idents)]
7373
#![feature(const_alloc_layout)]
7474
#![feature(const_if_match)]
75+
#![feature(const_loop)]
7576
#![feature(const_checked_int_methods)]
7677
#![feature(const_euclidean_int_methods)]
7778
#![feature(const_overflowing_int_methods)]
7879
#![feature(const_saturating_int_methods)]
7980
#![feature(const_int_unchecked_arith)]
81+
#![feature(const_int_pow)]
82+
#![feature(constctlz)]
8083
#![feature(const_panic)]
8184
#![feature(const_fn_union)]
8285
#![feature(const_generics)]

src/libcore/num/mod.rs

+28-14
Original file line numberDiff line numberDiff line change
@@ -1001,10 +1001,11 @@ $EndFeature, "
10011001
```"),
10021002

10031003
#[stable(feature = "no_panic_pow", since = "1.34.0")]
1004+
#[rustc_const_unstable(feature = "const_int_pow", issue = "53718")]
10041005
#[must_use = "this returns the result of the operation, \
10051006
without modifying the original"]
10061007
#[inline]
1007-
pub fn checked_pow(self, mut exp: u32) -> Option<Self> {
1008+
pub const fn checked_pow(self, mut exp: u32) -> Option<Self> {
10081009
let mut base = self;
10091010
let mut acc: Self = 1;
10101011

@@ -1188,10 +1189,11 @@ assert_eq!(", stringify!($SelfT), "::MIN.saturating_pow(3), ", stringify!($SelfT
11881189
$EndFeature, "
11891190
```"),
11901191
#[stable(feature = "no_panic_pow", since = "1.34.0")]
1192+
#[rustc_const_unstable(feature = "const_int_pow", issue = "53718")]
11911193
#[must_use = "this returns the result of the operation, \
11921194
without modifying the original"]
11931195
#[inline]
1194-
pub fn saturating_pow(self, exp: u32) -> Self {
1196+
pub const fn saturating_pow(self, exp: u32) -> Self {
11951197
match self.checked_pow(exp) {
11961198
Some(x) => x,
11971199
None if self < 0 && exp % 2 == 1 => Self::min_value(),
@@ -1531,10 +1533,11 @@ assert_eq!(3i8.wrapping_pow(6), -39);",
15311533
$EndFeature, "
15321534
```"),
15331535
#[stable(feature = "no_panic_pow", since = "1.34.0")]
1536+
#[rustc_const_unstable(feature = "const_int_pow", issue = "53718")]
15341537
#[must_use = "this returns the result of the operation, \
15351538
without modifying the original"]
15361539
#[inline]
1537-
pub fn wrapping_pow(self, mut exp: u32) -> Self {
1540+
pub const fn wrapping_pow(self, mut exp: u32) -> Self {
15381541
let mut base = self;
15391542
let mut acc: Self = 1;
15401543

@@ -1908,10 +1911,11 @@ assert_eq!(3i8.overflowing_pow(5), (-13, true));",
19081911
$EndFeature, "
19091912
```"),
19101913
#[stable(feature = "no_panic_pow", since = "1.34.0")]
1914+
#[rustc_const_unstable(feature = "const_int_pow", issue = "53718")]
19111915
#[must_use = "this returns the result of the operation, \
19121916
without modifying the original"]
19131917
#[inline]
1914-
pub fn overflowing_pow(self, mut exp: u32) -> (Self, bool) {
1918+
pub const fn overflowing_pow(self, mut exp: u32) -> (Self, bool) {
19151919
let mut base = self;
19161920
let mut acc: Self = 1;
19171921
let mut overflown = false;
@@ -1957,11 +1961,12 @@ assert_eq!(x.pow(5), 32);",
19571961
$EndFeature, "
19581962
```"),
19591963
#[stable(feature = "rust1", since = "1.0.0")]
1964+
#[rustc_const_unstable(feature = "const_int_pow", issue = "53718")]
19601965
#[must_use = "this returns the result of the operation, \
19611966
without modifying the original"]
19621967
#[inline]
19631968
#[rustc_inherit_overflow_checks]
1964-
pub fn pow(self, mut exp: u32) -> Self {
1969+
pub const fn pow(self, mut exp: u32) -> Self {
19651970
let mut base = self;
19661971
let mut acc = 1;
19671972

@@ -3127,10 +3132,11 @@ Basic usage:
31273132
assert_eq!(", stringify!($SelfT), "::max_value().checked_pow(2), None);", $EndFeature, "
31283133
```"),
31293134
#[stable(feature = "no_panic_pow", since = "1.34.0")]
3135+
#[rustc_const_unstable(feature = "const_int_pow", issue = "53718")]
31303136
#[must_use = "this returns the result of the operation, \
31313137
without modifying the original"]
31323138
#[inline]
3133-
pub fn checked_pow(self, mut exp: u32) -> Option<Self> {
3139+
pub const fn checked_pow(self, mut exp: u32) -> Option<Self> {
31343140
let mut base = self;
31353141
let mut acc: Self = 1;
31363142

@@ -3242,10 +3248,11 @@ assert_eq!(", stringify!($SelfT), "::MAX.saturating_pow(2), ", stringify!($SelfT
32423248
$EndFeature, "
32433249
```"),
32443250
#[stable(feature = "no_panic_pow", since = "1.34.0")]
3251+
#[rustc_const_unstable(feature = "const_int_pow", issue = "53718")]
32453252
#[must_use = "this returns the result of the operation, \
32463253
without modifying the original"]
32473254
#[inline]
3248-
pub fn saturating_pow(self, exp: u32) -> Self {
3255+
pub const fn saturating_pow(self, exp: u32) -> Self {
32493256
match self.checked_pow(exp) {
32503257
Some(x) => x,
32513258
None => Self::max_value(),
@@ -3535,10 +3542,11 @@ Basic usage:
35353542
assert_eq!(3u8.wrapping_pow(6), 217);", $EndFeature, "
35363543
```"),
35373544
#[stable(feature = "no_panic_pow", since = "1.34.0")]
3545+
#[rustc_const_unstable(feature = "const_int_pow", issue = "53718")]
35383546
#[must_use = "this returns the result of the operation, \
35393547
without modifying the original"]
35403548
#[inline]
3541-
pub fn wrapping_pow(self, mut exp: u32) -> Self {
3549+
pub const fn wrapping_pow(self, mut exp: u32) -> Self {
35423550
let mut base = self;
35433551
let mut acc: Self = 1;
35443552

@@ -3861,10 +3869,11 @@ Basic usage:
38613869
assert_eq!(3u8.overflowing_pow(6), (217, true));", $EndFeature, "
38623870
```"),
38633871
#[stable(feature = "no_panic_pow", since = "1.34.0")]
3872+
#[rustc_const_unstable(feature = "const_int_pow", issue = "53718")]
38643873
#[must_use = "this returns the result of the operation, \
38653874
without modifying the original"]
38663875
#[inline]
3867-
pub fn overflowing_pow(self, mut exp: u32) -> (Self, bool) {
3876+
pub const fn overflowing_pow(self, mut exp: u32) -> (Self, bool) {
38683877
let mut base = self;
38693878
let mut acc: Self = 1;
38703879
let mut overflown = false;
@@ -3907,11 +3916,12 @@ Basic usage:
39073916
", $Feature, "assert_eq!(2", stringify!($SelfT), ".pow(5), 32);", $EndFeature, "
39083917
```"),
39093918
#[stable(feature = "rust1", since = "1.0.0")]
3919+
#[rustc_const_unstable(feature = "const_int_pow", issue = "53718")]
39103920
#[must_use = "this returns the result of the operation, \
39113921
without modifying the original"]
39123922
#[inline]
39133923
#[rustc_inherit_overflow_checks]
3914-
pub fn pow(self, mut exp: u32) -> Self {
3924+
pub const fn pow(self, mut exp: u32) -> Self {
39153925
let mut base = self;
39163926
let mut acc = 1;
39173927

@@ -4022,7 +4032,8 @@ assert!(!10", stringify!($SelfT), ".is_power_of_two());", $EndFeature, "
40224032
// overflow cases it instead ends up returning the maximum value
40234033
// of the type, and can return 0 for 0.
40244034
#[inline]
4025-
fn one_less_than_next_power_of_two(self) -> Self {
4035+
#[rustc_const_unstable(feature = "const_int_pow", issue = "53718")]
4036+
const fn one_less_than_next_power_of_two(self) -> Self {
40264037
if self <= 1 { return 0; }
40274038

40284039
let p = self - 1;
@@ -4050,9 +4061,10 @@ Basic usage:
40504061
assert_eq!(3", stringify!($SelfT), ".next_power_of_two(), 4);", $EndFeature, "
40514062
```"),
40524063
#[stable(feature = "rust1", since = "1.0.0")]
4064+
#[rustc_const_unstable(feature = "const_int_pow", issue = "53718")]
40534065
#[inline]
40544066
#[rustc_inherit_overflow_checks]
4055-
pub fn next_power_of_two(self) -> Self {
4067+
pub const fn next_power_of_two(self) -> Self {
40564068
self.one_less_than_next_power_of_two() + 1
40574069
}
40584070
}
@@ -4075,7 +4087,8 @@ $EndFeature, "
40754087
```"),
40764088
#[inline]
40774089
#[stable(feature = "rust1", since = "1.0.0")]
4078-
pub fn checked_next_power_of_two(self) -> Option<Self> {
4090+
#[rustc_const_unstable(feature = "const_int_pow", issue = "53718")]
4091+
pub const fn checked_next_power_of_two(self) -> Option<Self> {
40794092
self.one_less_than_next_power_of_two().checked_add(1)
40804093
}
40814094
}
@@ -4099,7 +4112,8 @@ $EndFeature, "
40994112
```"),
41004113
#[unstable(feature = "wrapping_next_power_of_two", issue = "32463",
41014114
reason = "needs decision on wrapping behaviour")]
4102-
pub fn wrapping_next_power_of_two(self) -> Self {
4115+
#[rustc_const_unstable(feature = "const_int_pow", issue = "53718")]
4116+
pub const fn wrapping_next_power_of_two(self) -> Self {
41034117
self.one_less_than_next_power_of_two().wrapping_add(1)
41044118
}
41054119
}

0 commit comments

Comments
 (0)