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

Make `i*::signum` a `const fn`. #61635

Merged
merged 2 commits into from Jun 8, 2019

Conversation

Projects
None yet
5 participants
@ecstatic-morse
Copy link
Contributor

commented Jun 7, 2019

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 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.

@rust-highfive

This comment has been minimized.

Copy link
Collaborator

commented Jun 7, 2019

r? @bluss

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

@oli-obk
Copy link
Contributor

left a comment

Show resolved Hide resolved src/libcore/num/mod.rs

@oli-obk oli-obk assigned oli-obk and unassigned bluss Jun 7, 2019

Make `i*::signum` a `const fn`.
This uses a well-known branchless implementation of `signum`.
Its `const`-ness is unstable and requires `#![feature(const_int_sign)]`.

@ecstatic-morse ecstatic-morse force-pushed the ecstatic-morse:const-signum branch from e080fac to bd899d0 Jun 7, 2019

@ecstatic-morse ecstatic-morse marked this pull request as ready for review Jun 7, 2019

@oli-obk

This comment has been minimized.

Copy link
Contributor

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.

@ecstatic-morse ecstatic-morse force-pushed the ecstatic-morse:const-signum branch from d79e75d to 1bf37ae Jun 7, 2019

@ecstatic-morse ecstatic-morse force-pushed the ecstatic-morse:const-signum branch from 1bf37ae to 7ec54f9 Jun 7, 2019

@rust-highfive

This comment has been minimized.

Copy link
Collaborator

commented Jun 8, 2019

The job x86_64-gnu-llvm-6.0 of your PR failed on Travis (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
travis_time:end:17e9407e:start=1559951213612132101,finish=1559951215669921732,duration=2057789631
$ git checkout -qf FETCH_HEAD
travis_fold:end:git.checkout

Encrypted environment variables have been removed for security reasons.
See https://docs.travis-ci.com/user/pull-requests/#pull-requests-and-security-restrictions
$ export SCCACHE_BUCKET=rust-lang-ci-sccache2
$ export SCCACHE_REGION=us-west-1
$ export GCP_CACHE_BUCKET=rust-lang-ci-cache
$ export AWS_ACCESS_KEY_ID=AKIA46X5W6CZEJZ6XT55
---
[00:53:41] .................................................................................................... 500/5656
[00:53:44] ........................i........................................................................... 600/5656
[00:53:48] .................................................................................................... 700/5656
[00:53:53] .................................................................................................... 800/5656
[00:53:57] ..........................F......................................................................... 900/5656
[00:54:04] ...................................................iiiii............................................ 1100/5656
[00:54:08] .................................................................................................... 1200/5656
[00:54:10] .................................................................................................... 1300/5656
[00:54:13] .................................................................................................... 1400/5656
---
[00:56:51] 
[00:56:51] 23 error[E0716]: temporary value dropped while borrowed
[00:56:51] 24   --> $DIR/const-int-sign.rs:7:30
[00:56:51] 25    |
[00:56:51] - LL |     let sgn: &'static bool = &(5_i32.signum());
[00:56:51] -    |              -------------    ^^^^^^^^^^^^^^^^ creates a temporary which is freed while still in use
[00:56:51] + LL |     let sgn: &'static i32 = &(5_i32.signum());
[00:56:51] +    |              ------------    ^^^^^^^^^^^^^^^^ creates a temporary which is freed while still in use
[00:56:51] 29    |              type annotation requires that borrow lasts for `'static`
[00:56:51] 30 LL |
[00:56:51] 
[00:56:51] 
[00:56:51] 
[00:56:51] The actual stderr differed from the expected stderr.
[00:56:51] Actual stderr saved to /checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/consts/const-int-sign/const-int-sign.stderr
[00:56:51] To update references, rerun the tests and pass the `--bless` flag
[00:56:51] To only update this specific test, also pass `--test-args consts/const-int-sign.rs`
[00:56:51] error: 1 errors occurred comparing output.
[00:56:51] status: exit code: 1
[00:56:51] status: exit code: 1
[00:56:51] command: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/src/test/ui/consts/const-int-sign.rs" "-Zthreads=1" "--target=x86_64-unknown-linux-gnu" "--error-format" "json" "-Zui-testing" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/consts/const-int-sign" "-Crpath" "-O" "-Cdebuginfo=0" "-Zunstable-options" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/consts/const-int-sign/auxiliary" "-A" "unused"
[00:56:51] ------------------------------------------
[00:56:51] 
[00:56:51] ------------------------------------------
[00:56:51] stderr:
[00:56:51] stderr:
[00:56:51] ------------------------------------------
[00:56:51] error[E0716]: temporary value dropped while borrowed
[00:56:51]   --> /checkout/src/test/ui/consts/const-int-sign.rs:2:29
[00:56:51]    |
[00:56:51] LL |     let x: &'static bool = &(5_i32.is_negative());
[00:56:51]    |            -------------    ^^^^^^^^^^^^^^^^^^^^^ creates a temporary which is freed while still in use
[00:56:51]    |            type annotation requires that borrow lasts for `'static`
[00:56:51] ...
[00:56:51] LL | }
[00:56:51]    | - temporary value is freed at the end of this statement
[00:56:51]    | - temporary value is freed at the end of this statement
[00:56:51] 
[00:56:51] error[E0716]: temporary value dropped while borrowed
[00:56:51]   --> /checkout/src/test/ui/consts/const-int-sign.rs:4:29
[00:56:51]    |
[00:56:51] LL |     let y: &'static bool = &(5_i32.is_positive());
[00:56:51]    |            -------------    ^^^^^^^^^^^^^^^^^^^^^ creates a temporary which is freed while still in use
[00:56:51]    |            type annotation requires that borrow lasts for `'static`
[00:56:51] ...
[00:56:51] LL | }
[00:56:51]    | - temporary value is freed at the end of this statement
[00:56:51]    | - temporary value is freed at the end of this statement
[00:56:51] 
[00:56:51] error[E0716]: temporary value dropped while borrowed
[00:56:51]   --> /checkout/src/test/ui/consts/const-int-sign.rs:7:30
[00:56:51]    |
[00:56:51] LL |     let sgn: &'static i32 = &(5_i32.signum());
[00:56:51]    |              ------------    ^^^^^^^^^^^^^^^^ creates a temporary which is freed while still in use
[00:56:51]    |              type annotation requires that borrow lasts for `'static`
[00:56:51] LL |     //~^ ERROR temporary value dropped while borrowed
[00:56:51] LL | }
[00:56:51]    | - temporary value is freed at the end of this statement
---
[00:56:51] thread 'main' panicked at 'Some tests failed', src/tools/compiletest/src/main.rs:521:22
[00:56:51] note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
[00:56:51] 
[00:56:51] 
[00:56:51] command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-tools-bin/compiletest" "--compile-lib-path" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/lib" "--run-lib-path" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/x86_64-unknown-linux-gnu/lib" "--rustc-path" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "--src-base" "/checkout/src/test/ui" "--build-base" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui" "--stage-id" "stage2-x86_64-unknown-linux-gnu" "--mode" "ui" "--target" "x86_64-unknown-linux-gnu" "--host" "x86_64-unknown-linux-gnu" "--llvm-filecheck" "/usr/lib/llvm-6.0/bin/FileCheck" "--host-rustcflags" "-Crpath -O -Cdebuginfo=0 -Zunstable-options  -Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "--target-rustcflags" "-Crpath -O -Cdebuginfo=0 -Zunstable-options  -Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "--docck-python" "/usr/bin/python2.7" "--lldb-python" "/usr/bin/python2.7" "--gdb" "/usr/bin/gdb" "--quiet" "--llvm-version" "6.0.0\n" "--system-llvm" "--cc" "" "--cxx" "" "--cflags" "" "--llvm-components" "" "--llvm-cxxflags" "" "--adb-path" "adb" "--adb-test-dir" "/data/tmp/work" "--android-cross-path" "" "--color" "always"
[00:56:51] 
[00:56:51] 
[00:56:51] failed to run: /checkout/obj/build/bootstrap/debug/bootstrap test
[00:56:51] Build completed unsuccessfully in 0:52:44

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 @TimNN. (Feature Requests)

@ecstatic-morse ecstatic-morse force-pushed the ecstatic-morse:const-signum branch from 7ec54f9 to c2d8841 Jun 8, 2019

@ecstatic-morse ecstatic-morse force-pushed the ecstatic-morse:const-signum branch from c2d8841 to f6611db Jun 8, 2019

@ecstatic-morse

This comment has been minimized.

Copy link
Contributor Author

commented Jun 8, 2019

@oli-obk I think this is ready to go now.

@oli-obk

This comment has been minimized.

Copy link
Contributor

commented Jun 8, 2019

@bors r+

@bors

This comment has been minimized.

Copy link
Contributor

commented Jun 8, 2019

📌 Commit f6611db has been approved by oli-obk

@bors

This comment has been minimized.

Copy link
Contributor

commented Jun 8, 2019

⌛️ Testing commit f6611db with merge 7f90abe...

bors added a commit that referenced this pull request Jun 8, 2019

Auto merge of #61635 - ecstatic-morse:const-signum, r=oli-obk
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

This comment has been minimized.

Copy link
Contributor

commented Jun 8, 2019

☀️ Test successful - checks-travis, status-appveyor
Approved by: oli-obk
Pushing 7f90abe to master...

@bors bors added the merged-by-bors label Jun 8, 2019

@bors bors merged commit f6611db into rust-lang:master Jun 8, 2019

2 checks passed

Travis CI - Pull Request Build Passed
Details
homu Test successful
Details
@rust-highfive

This comment has been minimized.

Copy link
Collaborator

commented Jun 8, 2019

📣 Toolstate changed by #61635!

Tested on commit 7f90abe.
Direct link to PR: #61635

🎉 rls on windows: test-fail → test-pass (cc @Xanewok, @rust-lang/infra).

rust-highfive added a commit to rust-lang-nursery/rust-toolstate that referenced this pull request Jun 8, 2019

📣 Toolstate changed by rust-lang/rust#61635!
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).

@ecstatic-morse ecstatic-morse deleted the ecstatic-morse:const-signum branch Jun 8, 2019

@ecstatic-morse ecstatic-morse referenced this pull request Jun 8, 2019

Open

Tracking Issue for more const int functions #53718

21 of 57 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.