Skip to content

use sign variable in abs and wrapping_abs methods - #64386

Merged
bors merged 1 commit into
rust-lang:masterfrom
tspiteri:const-abs2
Sep 25, 2019
Merged

use sign variable in abs and wrapping_abs methods#64386
bors merged 1 commit into
rust-lang:masterfrom
tspiteri:const-abs2

Conversation

@tspiteri

Copy link
Copy Markdown
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-highfiverust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 11, 2019
Comment threadsrc/libcore/num/mod.rs Outdated
Comment threadsrc/libcore/num/mod.rs Outdated
@eddyb

Copy link
Copy Markdown
Contributor

cc @RalfJung@oli-obk

Comment threadsrc/libcore/num/mod.rs Outdated

Copy link
Copy Markdown
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.)

@tspiteritspiteriSep 13, 2019

Copy link
Copy Markdown
ContributorAuthor

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
Copy Markdown

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

@oli-obk

Copy link
Copy Markdown
Contributor

@bors r+

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

@bors

bors commented Sep 25, 2019

Copy link
Copy Markdown
Collaborator

📌 Commit 562903a has been approved by oli-obk

@borsbors 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
Copy Markdown
Contributor

r? @oli-obk

@rust-highfiverust-highfive assigned oli-obk and unassigned eddybSep 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
@CentrilCentril mentioned this pull request Sep 25, 2019
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:masterSep 25, 2019
@tspiteri
tspiteri deleted the const-abs2 branch October 1, 2019 10:39
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-borsStatus: 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

@tspiteri@eddyb@wirelessringo@oli-obk@bors@RalfJung@rust-highfive