Skip to content

Commit e749327

Browse files
committed
Make saturating int fns const
With rust-lang#49146 merged, these can be const; see rust-lang#53718.
1 parent 17ab2c4 commit e749327

File tree

2 files changed

+126
-33
lines changed

2 files changed

+126
-33
lines changed

src/libcore/num/mod.rs

+44-5
Original file line numberDiff line numberDiff line change
@@ -1172,8 +1172,11 @@ $EndFeature, "
11721172
```"),
11731173

11741174
#[unstable(feature = "saturating_neg", issue = "59983")]
1175+
#[rustc_const_unstable(feature = "const_saturating_int_methods")]
1176+
#[must_use = "this returns the result of the operation, \
1177+
without modifying the original"]
11751178
#[inline]
1176-
pub fn saturating_abs(self) -> Self {
1179+
pub const fn saturating_abs(self) -> Self {
11771180
if self.is_negative() {
11781181
self.saturating_neg()
11791182
} else {
@@ -1202,14 +1205,38 @@ $EndFeature, "
12021205
#[must_use = "this returns the result of the operation, \
12031206
without modifying the original"]
12041207
#[inline]
1205-
pub fn saturating_mul(self, rhs: Self) -> Self {
1206-
self.checked_mul(rhs).unwrap_or_else(|| {
1208+
#[rustc_const_unstable(feature = "const_int_saturating")]
1209+
#[cfg(not(bootstrap))]
1210+
pub const fn saturating_mul(self, rhs: Self) -> Self {
1211+
match self.checked_mul(rhs) {
1212+
Some(r) => r,
1213+
None => {
1214+
if (self < 0) == (rhs < 0) {
1215+
Self::max_value()
1216+
} else {
1217+
Self::min_value()
1218+
}
1219+
},
1220+
}
1221+
}
1222+
}
1223+
1224+
/// No docs for bootstrap.
1225+
#[stable(feature = "wrapping", since = "1.7.0")]
1226+
#[must_use = "this returns the result of the operation, \
1227+
without modifying the original"]
1228+
#[inline]
1229+
#[cfg(bootstrap)]
1230+
pub fn saturating_mul(self, rhs: Self) -> Self {
1231+
match self.checked_mul(rhs) {
1232+
Some(r) => r,
1233+
None => {
12071234
if (self < 0) == (rhs < 0) {
12081235
Self::max_value()
12091236
} else {
12101237
Self::min_value()
12111238
}
1212-
})
1239+
},
12131240
}
12141241
}
12151242

@@ -3351,11 +3378,23 @@ assert_eq!((", stringify!($SelfT), "::MAX).saturating_mul(10), ", stringify!($Se
33513378
#[must_use = "this returns the result of the operation, \
33523379
without modifying the original"]
33533380
#[inline]
3354-
pub fn saturating_mul(self, rhs: Self) -> Self {
3381+
#[rustc_const_unstable(feature = "const_int_saturating")]
3382+
#[cfg(not(bootstrap))]
3383+
pub const fn saturating_mul(self, rhs: Self) -> Self {
33553384
self.checked_mul(rhs).unwrap_or(Self::max_value())
33563385
}
33573386
}
33583387

3388+
/// No docs for bootstrap.
3389+
#[stable(feature = "wrapping", since = "1.7.0")]
3390+
#[must_use = "this returns the result of the operation, \
3391+
without modifying the original"]
3392+
#[inline]
3393+
#[cfg(bootstrap)]
3394+
pub fn saturating_mul(self, rhs: Self) -> Self {
3395+
self.checked_mul(rhs).unwrap_or(Self::max_value())
3396+
}
3397+
33593398
doc_comment! {
33603399
concat!("Saturating integer exponentiation. Computes `self.pow(exp)`,
33613400
saturating at the numeric bounds instead of overflowing.
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,88 @@
11
// run-pass
22
#![feature(const_saturating_int_methods)]
3+
#![feature(const_int_saturating)]
4+
#![feature(saturating_neg)]
35

4-
const INT_U32_NO: u32 = (42 as u32).saturating_add(2);
5-
const INT_U32: u32 = u32::max_value().saturating_add(1);
6-
const INT_U128: u128 = u128::max_value().saturating_add(1);
7-
const INT_I128: i128 = i128::max_value().saturating_add(1);
8-
const INT_I128_NEG: i128 = i128::min_value().saturating_add(-1);
9-
10-
const INT_U32_NO_SUB: u32 = (42 as u32).saturating_sub(2);
11-
const INT_U32_SUB: u32 = (1 as u32).saturating_sub(2);
12-
const INT_I32_NO_SUB: i32 = (-42 as i32).saturating_sub(2);
13-
const INT_I32_NEG_SUB: i32 = i32::min_value().saturating_sub(1);
14-
const INT_I32_POS_SUB: i32 = i32::max_value().saturating_sub(-1);
15-
const INT_U128_SUB: u128 = (0 as u128).saturating_sub(1);
16-
const INT_I128_NEG_SUB: i128 = i128::min_value().saturating_sub(1);
17-
const INT_I128_POS_SUB: i128 = i128::max_value().saturating_sub(-1);
6+
const ADD_INT_U32_NO: u32 = (42 as u32).saturating_add(2);
7+
const ADD_INT_U32: u32 = u32::max_value().saturating_add(1);
8+
const ADD_INT_U128: u128 = u128::max_value().saturating_add(1);
9+
const ADD_INT_I128: i128 = i128::max_value().saturating_add(1);
10+
const ADD_INT_I128_NEG: i128 = i128::min_value().saturating_add(-1);
11+
12+
const SUB_INT_U32_NO: u32 = (42 as u32).saturating_sub(2);
13+
const SUB_INT_U32: u32 = (1 as u32).saturating_sub(2);
14+
const SUB_INT_I32_NO: i32 = (-42 as i32).saturating_sub(2);
15+
const SUB_INT_I32_NEG: i32 = i32::min_value().saturating_sub(1);
16+
const SUB_INT_I32_POS: i32 = i32::max_value().saturating_sub(-1);
17+
const SUB_INT_U128: u128 = (0 as u128).saturating_sub(1);
18+
const SUB_INT_I128_NEG: i128 = i128::min_value().saturating_sub(1);
19+
const SUB_INT_I128_POS: i128 = i128::max_value().saturating_sub(-1);
20+
21+
const MUL_INT_U32_NO: u32 = (42 as u32).saturating_mul(2);
22+
const MUL_INT_U32: u32 = (1 as u32).saturating_mul(2);
23+
const MUL_INT_I32_NO: i32 = (-42 as i32).saturating_mul(2);
24+
const MUL_INT_I32_NEG: i32 = i32::min_value().saturating_mul(1);
25+
const MUL_INT_I32_POS: i32 = i32::max_value().saturating_mul(2);
26+
const MUL_INT_U128: u128 = (0 as u128).saturating_mul(1);
27+
const MUL_INT_I128_NEG: i128 = i128::min_value().saturating_mul(2);
28+
const MUL_INT_I128_POS: i128 = i128::max_value().saturating_mul(2);
29+
30+
const NEG_INT_I8: i8 = (-42i8).saturating_neg();
31+
const NEG_INT_I8_B: i8 = i8::min_value().saturating_neg();
32+
const NEG_INT_I32: i32 = i32::min_value().saturating_neg();
33+
const NEG_INT_I32_B: i32 = i32::max_value().saturating_neg();
34+
const NEG_INT_I128: i128 = i128::min_value().saturating_neg();
35+
const NEG_INT_I128_B: i128 = i128::max_value().saturating_neg();
36+
37+
const ABS_INT_I8_A: i8 = 4i8.saturating_abs();
38+
const ABS_INT_I8_B: i8 = -4i8.saturating_abs();
39+
const ABS_INT_I8_C: i8 = i8::min_value().saturating_abs();
40+
const ABS_INT_I32_A: i32 = 4i32.saturating_abs();
41+
const ABS_INT_I32_B: i32 = -4i32.saturating_abs();
42+
const ABS_INT_I32_C: i32 = i32::min_value().saturating_abs();
43+
const ABS_INT_I128_A: i128 = 4i128.saturating_abs();
44+
const ABS_INT_I128_B: i128 = -4i128.saturating_abs();
45+
const ABS_INT_I128_C: i128 = i128::min_value().saturating_abs();
1846

1947
fn main() {
20-
assert_eq!(INT_U32_NO, 44);
21-
assert_eq!(INT_U32, u32::max_value());
22-
assert_eq!(INT_U128, u128::max_value());
23-
assert_eq!(INT_I128, i128::max_value());
24-
assert_eq!(INT_I128_NEG, i128::min_value());
25-
26-
assert_eq!(INT_U32_NO_SUB, 40);
27-
assert_eq!(INT_U32_SUB, 0);
28-
assert_eq!(INT_I32_NO_SUB, -44);
29-
assert_eq!(INT_I32_NEG_SUB, i32::min_value());
30-
assert_eq!(INT_I32_POS_SUB, i32::max_value());
31-
assert_eq!(INT_U128_SUB, 0);
32-
assert_eq!(INT_I128_NEG_SUB, i128::min_value());
33-
assert_eq!(INT_I128_POS_SUB, i128::max_value());
48+
assert_eq!(ADD_INT_U32_NO, 44);
49+
assert_eq!(ADD_INT_U32, u32::max_value());
50+
assert_eq!(ADD_INT_U128, u128::max_value());
51+
assert_eq!(ADD_INT_I128, i128::max_value());
52+
assert_eq!(ADD_INT_I128_NEG, i128::min_value());
53+
54+
assert_eq!(SUB_INT_U32_NO, 40);
55+
assert_eq!(SUB_INT_U32, 0);
56+
assert_eq!(SUB_INT_I32_NO, -44);
57+
assert_eq!(SUB_INT_I32_NEG, i32::min_value());
58+
assert_eq!(SUB_INT_I32_POS, i32::max_value());
59+
assert_eq!(SUB_INT_U128, 0);
60+
assert_eq!(SUB_INT_I128_NEG, i128::min_value());
61+
assert_eq!(SUB_INT_I128_POS, i128::max_value());
62+
63+
assert_eq!(MUL_INT_U32_NO, 84);
64+
assert_eq!(MUL_INT_U32, 2);
65+
assert_eq!(MUL_INT_I32_NO, -84);
66+
assert_eq!(MUL_INT_I32_NEG, i32::min_value());
67+
assert_eq!(MUL_INT_I32_POS, i32::max_value());
68+
assert_eq!(MUL_INT_U128, 0);
69+
assert_eq!(MUL_INT_I128_NEG, i128::min_value());
70+
assert_eq!(MUL_INT_I128_POS, i128::max_value());
71+
72+
assert_eq!(NEG_INT_I8, 42);
73+
assert_eq!(NEG_INT_I8_B, i8::max_value());
74+
assert_eq!(NEG_INT_I32, i32::max_value());
75+
assert_eq!(NEG_INT_I32_B, i32::min_value() + 1);
76+
assert_eq!(NEG_INT_I128, i128::max_value());
77+
assert_eq!(NEG_INT_I128_B, i128::min_value() + 1);
78+
79+
assert_eq!(ABS_INT_I8_A, 4i8);
80+
assert_eq!(ABS_INT_I8_B, -4i8);
81+
assert_eq!(ABS_INT_I8_C, i8::max_value());
82+
assert_eq!(ABS_INT_I32_A, 4i32);
83+
assert_eq!(ABS_INT_I32_B, -4i32);
84+
assert_eq!(ABS_INT_I32_C, i32::max_value());
85+
assert_eq!(ABS_INT_I128_A, 4i128);
86+
assert_eq!(ABS_INT_I128_B, -4i128);
87+
assert_eq!(ABS_INT_I128_C, i128::max_value());
3488
}

0 commit comments

Comments
 (0)