Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make abs, wrapping_abs, overflowing_abs const functions #63786

Merged
merged 2 commits into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 9 additions & 21 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1401,12 +1401,8 @@ $EndFeature, "
```"),
#[stable(feature = "no_panic_abs", since = "1.13.0")]
#[inline]
pub fn wrapping_abs(self) -> Self {
if self.is_negative() {
self.wrapping_neg()
} else {
self
}
pub const fn wrapping_abs(self) -> Self {
(self ^ (self >> ($BITS - 1))).wrapping_sub(self >> ($BITS - 1))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not a big fan of making code less readable just to please our const qualification. :/

Cc @oli-obk @eddyb

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've had variables for a while, please use them.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has it been verified that this optimizes to identical LLVM IR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eddyb Since this has already passed final comment period and been merged, shall I open a new PR to use variables?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nikic I can't read LLVM IR; what I did do before submitting the PR was to use llvm-mca, and although the assembly is not identical, the performance measure from llvm-mca was the same, or if I remember well, in some tests the reverse throughput improved by something tiny like 0.1, but not regressed. I only used llvm-mca with i686 or x86-64 instructions, and I don't think I tested all types comprehensively.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nikic Hmm, using variables as @eddyb suggested seems to hit two birds with one stone. If I insert a variable for the sign, I get one implementation and an alias: https://godbolt.org/z/m-seT0

Does that mean that the performance will not regress?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tspiteri Oops, I simply made a typo in my test and that's why the result is different, duh :) It actually does produce the same IR, both with variable and without. So forget everything I said, this change is fine from the optimization perspective.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nikic While abs and wrapping_abs seem to be fine performance-wise, overflowing_abs from this PR actually does suffer from the problem discussed, so I will not forget everything you said! :) It's easily fixed by making it return (self.wrapping_abs(), self == Self::min_value()).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And also, the code with the sign factored out is more readable. ;)

That said, I still think we should rather wait for CTFE to support conditionals than compromise code readability, and even the let sign version is still arcane magic. But that call is up to T-libs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we should definitely make as many of these small ops into const as soon as we can.

}
}

Expand Down Expand Up @@ -1764,12 +1760,8 @@ $EndFeature, "
```"),
#[stable(feature = "no_panic_abs", since = "1.13.0")]
#[inline]
pub fn overflowing_abs(self) -> (Self, bool) {
if self.is_negative() {
self.overflowing_neg()
} else {
(self, false)
}
pub const fn overflowing_abs(self) -> (Self, bool) {
(self ^ (self >> ($BITS - 1))).overflowing_sub(self >> ($BITS - 1))
}
}

Expand Down Expand Up @@ -1973,15 +1965,11 @@ $EndFeature, "
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
#[rustc_inherit_overflow_checks]
pub fn abs(self) -> Self {
if self.is_negative() {
// Note that the #[inline] above means that the overflow
// semantics of this negation depend on the crate we're being
// inlined into.
-self
} else {
self
}
pub const fn abs(self) -> Self {
// Note that the #[inline] above means that the overflow
// semantics of the subtraction depend on the crate we're being
// inlined into.
(self ^ (self >> ($BITS - 1))) - (self >> ($BITS - 1))
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/consts/const-int-overflowing-rpass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const SHR_B: (u32, bool) = 0x10u32.overflowing_shr(132);
const NEG_A: (u32, bool) = 0u32.overflowing_neg();
const NEG_B: (u32, bool) = core::u32::MAX.overflowing_neg();

const ABS_POS: (i32, bool) = 10i32.overflowing_abs();
const ABS_NEG: (i32, bool) = (-10i32).overflowing_abs();
const ABS_MIN: (i32, bool) = i32::min_value().overflowing_abs();

fn main() {
assert_eq!(ADD_A, (7, false));
assert_eq!(ADD_B, (0, true));
Expand All @@ -36,4 +40,8 @@ fn main() {

assert_eq!(NEG_A, (0, false));
assert_eq!(NEG_B, (1, true));

assert_eq!(ABS_POS, (10, false));
assert_eq!(ABS_NEG, (10, false));
assert_eq!(ABS_MIN, (i32::min_value(), true));
}
6 changes: 6 additions & 0 deletions src/test/ui/consts/const-int-sign-rpass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const SIGNUM_POS: i32 = 10i32.signum();
const SIGNUM_NIL: i32 = 0i32.signum();
const SIGNUM_NEG: i32 = (-42i32).signum();

const ABS_A: i32 = 10i32.abs();
const ABS_B: i32 = (-10i32).abs();

fn main() {
assert!(NEGATIVE_A);
assert!(!NEGATIVE_B);
Expand All @@ -20,4 +23,7 @@ fn main() {
assert_eq!(SIGNUM_POS, 1);
assert_eq!(SIGNUM_NIL, 0);
assert_eq!(SIGNUM_NEG, -1);

assert_eq!(ABS_A, 10);
assert_eq!(ABS_B, 10);
}
8 changes: 8 additions & 0 deletions src/test/ui/consts/const-int-wrapping-rpass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const SHR_B: u32 = 128u32.wrapping_shr(128);
const NEG_A: u32 = 5u32.wrapping_neg();
const NEG_B: u32 = 1234567890u32.wrapping_neg();

const ABS_POS: i32 = 10i32.wrapping_abs();
const ABS_NEG: i32 = (-10i32).wrapping_abs();
const ABS_MIN: i32 = i32::min_value().wrapping_abs();

fn main() {
assert_eq!(ADD_A, 255);
assert_eq!(ADD_B, 199);
Expand All @@ -36,4 +40,8 @@ fn main() {

assert_eq!(NEG_A, 4294967291);
assert_eq!(NEG_B, 3060399406);

assert_eq!(ABS_POS, 10);
assert_eq!(ABS_NEG, 10);
assert_eq!(ABS_MIN, i32::min_value());
}