Skip to content

Commit 13726cc

Browse files
authored
Rollup merge of rust-lang#63786 - tspiteri:const-abs, r=alexcrichton
Make `abs`, `wrapping_abs`, `overflowing_abs` const functions This makes `abs`, `wrapping_abs` and `overflowing_abs` const functions like rust-lang#58044 makes `wrapping_neg` and `overflowing_neg` const functions. `abs` is made const by returning `(self ^ -1) - -1` = `!self + 1` = `-self` for negative numbers and `(self ^ 0) - 0` = `self` for non-negative numbers. The subexpression `self >> ($BITS - 1)` evaluates to `-1` for negative numbers and `0` otherwise. The subtraction overflows when `self` is `min_value()`, as we would be subtracting `max_value() - -1`; this is when `abs` should overflow. `wrapping_abs` and `overflowing_abs` make use of `wrapping_sub` and `overflowing_sub` instead of the subtraction operator.
2 parents 403c0de + adee559 commit 13726cc

File tree

4 files changed

+31
-21
lines changed

4 files changed

+31
-21
lines changed

src/libcore/num/mod.rs

+9-21
Original file line numberDiff line numberDiff line change
@@ -1401,12 +1401,8 @@ $EndFeature, "
14011401
```"),
14021402
#[stable(feature = "no_panic_abs", since = "1.13.0")]
14031403
#[inline]
1404-
pub fn wrapping_abs(self) -> Self {
1405-
if self.is_negative() {
1406-
self.wrapping_neg()
1407-
} else {
1408-
self
1409-
}
1404+
pub const fn wrapping_abs(self) -> Self {
1405+
(self ^ (self >> ($BITS - 1))).wrapping_sub(self >> ($BITS - 1))
14101406
}
14111407
}
14121408

@@ -1764,12 +1760,8 @@ $EndFeature, "
17641760
```"),
17651761
#[stable(feature = "no_panic_abs", since = "1.13.0")]
17661762
#[inline]
1767-
pub fn overflowing_abs(self) -> (Self, bool) {
1768-
if self.is_negative() {
1769-
self.overflowing_neg()
1770-
} else {
1771-
(self, false)
1772-
}
1763+
pub const fn overflowing_abs(self) -> (Self, bool) {
1764+
(self ^ (self >> ($BITS - 1))).overflowing_sub(self >> ($BITS - 1))
17731765
}
17741766
}
17751767

@@ -1973,15 +1965,11 @@ $EndFeature, "
19731965
#[stable(feature = "rust1", since = "1.0.0")]
19741966
#[inline]
19751967
#[rustc_inherit_overflow_checks]
1976-
pub fn abs(self) -> Self {
1977-
if self.is_negative() {
1978-
// Note that the #[inline] above means that the overflow
1979-
// semantics of this negation depend on the crate we're being
1980-
// inlined into.
1981-
-self
1982-
} else {
1983-
self
1984-
}
1968+
pub const fn abs(self) -> Self {
1969+
// Note that the #[inline] above means that the overflow
1970+
// semantics of the subtraction depend on the crate we're being
1971+
// inlined into.
1972+
(self ^ (self >> ($BITS - 1))) - (self >> ($BITS - 1))
19851973
}
19861974
}
19871975

src/test/ui/consts/const-int-overflowing-rpass.rs

+8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ const SHR_B: (u32, bool) = 0x10u32.overflowing_shr(132);
1818
const NEG_A: (u32, bool) = 0u32.overflowing_neg();
1919
const NEG_B: (u32, bool) = core::u32::MAX.overflowing_neg();
2020

21+
const ABS_POS: (i32, bool) = 10i32.overflowing_abs();
22+
const ABS_NEG: (i32, bool) = (-10i32).overflowing_abs();
23+
const ABS_MIN: (i32, bool) = i32::min_value().overflowing_abs();
24+
2125
fn main() {
2226
assert_eq!(ADD_A, (7, false));
2327
assert_eq!(ADD_B, (0, true));
@@ -36,4 +40,8 @@ fn main() {
3640

3741
assert_eq!(NEG_A, (0, false));
3842
assert_eq!(NEG_B, (1, true));
43+
44+
assert_eq!(ABS_POS, (10, false));
45+
assert_eq!(ABS_NEG, (10, false));
46+
assert_eq!(ABS_MIN, (i32::min_value(), true));
3947
}

src/test/ui/consts/const-int-sign-rpass.rs

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ const SIGNUM_POS: i32 = 10i32.signum();
1111
const SIGNUM_NIL: i32 = 0i32.signum();
1212
const SIGNUM_NEG: i32 = (-42i32).signum();
1313

14+
const ABS_A: i32 = 10i32.abs();
15+
const ABS_B: i32 = (-10i32).abs();
16+
1417
fn main() {
1518
assert!(NEGATIVE_A);
1619
assert!(!NEGATIVE_B);
@@ -20,4 +23,7 @@ fn main() {
2023
assert_eq!(SIGNUM_POS, 1);
2124
assert_eq!(SIGNUM_NIL, 0);
2225
assert_eq!(SIGNUM_NEG, -1);
26+
27+
assert_eq!(ABS_A, 10);
28+
assert_eq!(ABS_B, 10);
2329
}

src/test/ui/consts/const-int-wrapping-rpass.rs

+8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ const SHR_B: u32 = 128u32.wrapping_shr(128);
1818
const NEG_A: u32 = 5u32.wrapping_neg();
1919
const NEG_B: u32 = 1234567890u32.wrapping_neg();
2020

21+
const ABS_POS: i32 = 10i32.wrapping_abs();
22+
const ABS_NEG: i32 = (-10i32).wrapping_abs();
23+
const ABS_MIN: i32 = i32::min_value().wrapping_abs();
24+
2125
fn main() {
2226
assert_eq!(ADD_A, 255);
2327
assert_eq!(ADD_B, 199);
@@ -36,4 +40,8 @@ fn main() {
3640

3741
assert_eq!(NEG_A, 4294967291);
3842
assert_eq!(NEG_B, 3060399406);
43+
44+
assert_eq!(ABS_POS, 10);
45+
assert_eq!(ABS_NEG, 10);
46+
assert_eq!(ABS_MIN, i32::min_value());
3947
}

0 commit comments

Comments
 (0)