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

use sign variable in abs and wrapping_abs methods #64386

Merged
merged 1 commit into from
Sep 25, 2019

Conversation

tspiteri
Copy link
Contributor

This also makes the code easier to understand by hinting at the significance of self >> ($BITS - 1).

Also, now overflowing_abs simply uses wrapping_abs, which is clearer and avoids a potential performance regression in the LLVM IR.

This PR follows from the discussion from #63786.

r? @eddyb
cc @nikic

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 11, 2019
@eddyb
Copy link
Member

eddyb commented Sep 12, 2019

cc @RalfJung @oli-obk

@@ -1402,7 +1402,16 @@ $EndFeature, "
#[stable(feature = "no_panic_abs", since = "1.13.0")]
#[inline]
pub const fn wrapping_abs(self) -> Self {
(self ^ (self >> ($BITS - 1))).wrapping_sub(self >> ($BITS - 1))
// sign is -1 (all ones) for -ve numbers, 0 otherwise.
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for adding comments! However, please spell out "negative" and "positive", these abbreviations are fairly hard to decipher.

(That said, this does not really alleviate my concern about sacrificing code readability to please const qualification.)

Copy link
Contributor Author

@tspiteri tspiteri Sep 13, 2019

Choose a reason for hiding this comment

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

I removed the abbreviations.

About readability, I do not share your concern. While, all other things being equal, simpler code is better, I don't think it is enough reason to delay better functionality, specifically I don't think it is enough reason to delay making the abs functions const until the time when const support in the compiler improves. I also think having this functionality in the standard library is helpful as it avoids having such tricks repeated in user code, where it is more probable that typos get in somewhere.

And in this case the normal issues with less obvious code are not so problematic in my opinion: Modifiability is not such an issue, because there is very little scope for modifying the abs functions. And the risk of getting incorrect behaviour is extremely tiny (I want to say there is no risk in this case, but there's always the remote chance that this change triggers some hidden bug in some other part of the compiler, so I'll stay with extremely tiny ☺).

The comments help as they make it possible (though not necessarily trivial) for even someone with a very limited knowledge of two's-complement representation and bitwise manipulation to verify the correctness of the code.

This also makes the code easier to understand by hinting at the
significance of `self >> ($BITS - 1)` and by including an explanation
in the comments.

Also, now overflowing_abs simply uses wrapping_abs, which is clearer
and avoids a potential performance regression in the LLVM IR.
@wirelessringo
Copy link

Ping from triage. @eddyb any updates on this? Thanks.

@oli-obk
Copy link
Contributor

oli-obk commented Sep 25, 2019

@bors r+

we can get readability back once we have if/match in const fn

@bors
Copy link
Contributor

bors commented Sep 25, 2019

📌 Commit 562903a has been approved by oli-obk

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 25, 2019
@oli-obk
Copy link
Contributor

oli-obk commented Sep 25, 2019

r? @oli-obk

@rust-highfive rust-highfive assigned oli-obk and unassigned eddyb Sep 25, 2019
Centril added a commit to Centril/rust that referenced this pull request Sep 25, 2019
use `sign` variable in abs and wrapping_abs methods

This also makes the code easier to understand by hinting at the significance of `self >> ($BITS - 1)`.

Also, now `overflowing_abs` simply uses `wrapping_abs`, which is clearer and avoids a potential performance regression in the LLVM IR.

This PR follows from the discussion from rust-lang#63786.

r? @eddyb
cc @nikic
bors added a commit that referenced this pull request Sep 25, 2019
Rollup of 6 pull requests

Successful merges:

 - #62975 (Almost fully deprecate hir::map::Map.hir_to_node_id)
 - #64386 (use `sign` variable in abs and wrapping_abs methods)
 - #64508 (or-patterns: Push `PatKind/PatternKind::Or` at top level to HIR & HAIR)
 - #64738 (Add const-eval support for SIMD types, insert, and extract)
 - #64759 (Refactor mbe a tiny bit)
 - #64764 (Master is now 1.40 )

Failed merges:

r? @ghost
@bors bors merged commit 562903a into rust-lang:master Sep 25, 2019
@tspiteri tspiteri deleted the const-abs2 branch October 1, 2019 10:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants