diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2020-01-04 02:19:55 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-01-04 02:19:55 +0100 |
| commit | 745f771484e4564acf3f77fc56836db4f0b83264 (patch) | |
| tree | d736ed8757b6dc4704a8ae35214e56f928710be1 | |
| parent | 689e29f0f58641d4c4532463b13af7c656ae84b8 (diff) | |
| parent | ae002c1d84a2a8e8a272a3c0db28b7402064f072 (diff) | |
| download | rust-745f771484e4564acf3f77fc56836db4f0b83264.tar.gz rust-745f771484e4564acf3f77fc56836db4f0b83264.zip | |
Rollup merge of #67845 - jumbatm:also-unconst-hack-abs, r=oli-obk
Also remove const-hack for abs Closes #67842. r? @oli-obk
| -rw-r--r-- | src/libcore/num/mod.rs | 24 |
1 files changed, 6 insertions, 18 deletions
diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 14540394cab..605ab98219f 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -1997,27 +1997,15 @@ $EndFeature, " ```"), #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")] + #[allow_internal_unstable(const_if_match)] #[inline] #[rustc_inherit_overflow_checks] 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. - - // sign is -1 (all ones) for negative numbers, 0 otherwise. - let sign = self >> ($BITS - 1); - // For positive self, sign == 0 so the expression is simply - // (self ^ 0) - 0 == self == abs(self). - // - // For negative self, self ^ sign == self ^ all_ones. - // But all_ones ^ self == all_ones - self == -1 - self. - // So for negative numbers, (self ^ sign) - sign is - // (-1 - self) - -1 == -self == abs(self). - // - // The subtraction overflows when self is min_value(), because - // (-1 - min_value()) - -1 is max_value() - -1 which overflows. - // This is exactly when we want self.abs() to overflow. - (self ^ sign) - sign + if self.is_negative() { + -self + } else { + self + } } } |
