Skip to content

Make abs, wrapping_abs, overflowing_abs const functions - #63786

Merged
bors merged 2 commits into
rust-lang:masterfrom
tspiteri:const-abs
Sep 10, 2019
Merged

bors merged 2 commits into
rust-lang:masterfrom
tspiteri:const-abs

Conversation

@tspiteri

@tspiteri tspiteri commented Aug 21, 2019 •

Copy link
Copy Markdown
Contributor

This makes abs, wrapping_abs and overflowing_abs const functions like #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.

@rust-highfive

Copy link
Copy Markdown
Contributor

r? @cramertj

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 21, 2019
@Centril Centril added relnotes Marks issues that should be documented in the release notes of the next release. T-libs-api [DEPRECATED; DO NOT USE] labels Aug 21, 2019
@Centril Centril added this to the 1.39 milestone Aug 21, 2019
@Centril Centril added the needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. label Aug 21, 2019
@cramertj

Copy link
Copy Markdown
Member

r? @alexcrichton

@alexcrichton

Copy link
Copy Markdown
Member

@rfcbot fcp merge

@rfcbot

rfcbot commented Aug 22, 2019 •

Copy link
Copy Markdown

Team member @alexcrichton has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Aug 22, 2019
@Centril Centril removed the needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. label Aug 30, 2019
@Centril

Centril commented Aug 30, 2019

Copy link
Copy Markdown
Contributor

Ping @withoutboats @Kimundi @sfackler in case y'all missed this. :)

@rfcbot

rfcbot commented Aug 30, 2019

Copy link
Copy Markdown

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Aug 30, 2019
@JohnCSimon JohnCSimon closed this Sep 7, 2019
@JohnCSimon JohnCSimon reopened this Sep 7, 2019
@rfcbot

rfcbot commented Sep 9, 2019

Copy link
Copy Markdown

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

The RFC will be merged soon.

@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Sep 9, 2019
@alexcrichton

Copy link
Copy Markdown
Member

@bors: r+

@bors

bors commented Sep 10, 2019

Copy link
Copy Markdown
Collaborator

📌 Commit adee559 has been approved by alexcrichton

@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 10, 2019
Centril added a commit to Centril/rust that referenced this pull request Sep 10, 2019
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.
bors added a commit that referenced this pull request Sep 10, 2019
Rollup of 8 pull requests

Successful merges:

 - #63786 (Make `abs`, `wrapping_abs`, `overflowing_abs` const functions)
 - #63989 (Add Yaah to clippy toolstain notification list)
 - #64256 (test/c-variadic: Fix patterns on powerpc64)
 - #64292 (lowering: extend temporary lifetimes around await)
 - #64311 (lldb: avoid mixing "Hit breakpoint" message with other output.)
 - #64330 (Clarify E0507 to note Fn/FnMut relationship to borrowing)
 - #64331 (Changed instant is earlier to instant is later)
 - #64344 (rustc_mir: buffer -Zdump-mir output instead of pestering the kernel constantly.)

Failed merges:

r? @ghost
@bors
bors merged commit adee559 into rust-lang:master Sep 10, 2019
Comment thread src/libcore/num/mod.rs
self
}
pub const fn wrapping_abs(self) -> Self {
(self ^ (self >> ($BITS - 1))).wrapping_sub(self >> ($BITS - 1))

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.

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

Cc @oli-obk @eddyb

Copy link
Copy Markdown
Contributor

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

ghost Sep 11, 2019

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

ghost Sep 11, 2019

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

ghost Sep 11, 2019

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.

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.

ghost Sep 19, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. relnotes Marks issues that should be documented in the release notes of the next release. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api [DEPRECATED; DO NOT USE]

Projects

None yet

Development

Successfully merging this pull request may close these issues.