Uh oh!
There was an error while loading. Please reload this page.
Make i*::signum a const fn. - #61635
Conversation
rust-highfive
commented
Jun 7, 2019
r? @bluss (rust_highfive has picked a reviewer for you, use r? to override) |
Uh oh!
There was an error while loading. Please reload this page.
This uses a well-known branchless implementation of `signum`. Its `const`-ness is unstable and requires `#![feature(const_int_sign)]`.
oli-obk
commented
Jun 7, 2019
can you also add some tests using this inside a constant and comparing the result with expected values? There should already be tests for other const fns for integer types, you can add the new test to them. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
rust-highfive
commented
Jun 8, 2019
The job Click to expand the log.I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
ecstatic-morse
commented
Jun 8, 2019
@oli-obk I think this is ready to go now. |
oli-obk
commented
Jun 8, 2019
@bors r+ |
bors
commented
Jun 8, 2019
📌 Commit f6611db has been approved by |
bors
commented
Jun 8, 2019
Make `i*::signum` a `const fn`. Ticks a box in #53718. This uses a well-known branchless implementation of `signum`: `(n > 0) as i32 - (n < 0) as i32`. Here's a [playground](https://play.rust-lang.org/?version=nightly&mode=release&edition=2018&gist=747cf191c4974bf66c9d75e509ae6e6e) comparing the two techniques. On x86 in release mode, the branchless implementation is able to replace a `mov` and `cmov` with a `sar` and `add`, so this should be a bit faster as well. ~~This is marked as a draft since I think I'll need to add `#[rustc_const_unstable]` somewhere. Perhaps the reviewer can point me in the right direction.~~
bors
commented
Jun 8, 2019
☀️ Test successful - checks-travis, status-appveyor |
rust-highfive
commented
Jun 8, 2019
Tested on commit rust-lang/rust@7f90abe. Direct link to PR: <rust-lang/rust#61635> 🎉 rls on windows: test-fail → test-pass (cc @Xanewok, @rust-lang/infra).
For what it's worth, regarding 'branchless' in the commit message, this code was/is branchless and more or less as efficient before and after this commit. Example on ARM64: Note though that the old form also informed the compiler that the result was either -1, 0 or +1. The new form looks to the compiler like the result might be any integer value. That might have side effects on optimization opportunities in inlined contexts. |
ecstatic-morse
commented
Jun 17, 2019
@bjacob, I compared the generated assembly (albeit only on x86) in the PR message, so I'm aware that the optimized version of the old implementation is branchless. I've reproduced the full output below for you. playground::signum: # @playground::signum# %bb.0: xorl %eax, %eax testl %edi, %edi setg %al sarl$31, %edi addl %edi, %eax retq # -- End functionplayground::signum_branch: # @playground::signum_branch# %bb.0: xorl %ecx, %ecx testl %edi, %edi setne %cl movl $-1, %eax cmovnsl %ecx, %eax retq # -- End functionThe main benefit of this PR is that Your last point is an interesting one, although I'm not aware of a set of optimizations that would make use this information. Perhaps you have an idea? |
bjacob
commented
Jun 17, 2019
right, sorry I had not followed the link in the commit message. |
bjacob
commented
Jun 17, 2019
On the other hand, I'm impressed that even in the new form, the compiler was able to deduce that the result could only be -1, 0 or 1 so it still elided the default: case. |
Ah, I didn't consider branching on the result of I don't know how common code like this is, since match n {
n if n > 0 => {}0 => {}
_ => {}}is much more straightforward (I only use |
ecstatic-morse
commented
Sep 17, 2019
@Centril This should get |
Clean up const-hack PRs now that const if / match exist. Closesrust-lang#67627. Cleans up these merged PRs tagged with `const-hack`: - rust-lang#63810 - rust-lang#63786 - rust-lang#61635 - rust-lang#58044 reverting their contents to have the match or if expressions they originally contained. r? @oli-obk There's one more PR in those tagged with `const-hack` that originally wasn't merged (rust-lang#65107). Reading the thread, it looks like it was originally closed because the `const-hack` for the checked arithmetic non-negligibly hurt performance, and because there was no way to manipulate the returned Option at compile time anyway (with neither const if nor const match). Would you like me to add these changes to the changes from this PR here too, now that we have the necessary features?
Ticks a box in #53718.
This uses a well-known branchless implementation of
signum:(n > 0) as i32 - (n < 0) as i32.Here's a playground comparing the two techniques. On x86 in release mode, the branchless implementation is able to replace a
movandcmovwith asarandadd, so this should be a bit faster as well.This is marked as a draft since I think I'll need to add#[rustc_const_unstable]somewhere. Perhaps the reviewer can point me in the right direction.