]> git.lizzy.rs Git - rust.git/commit
Auto merge of #61635 - ecstatic-morse:const-signum, r=oli-obk
authorbors <bors@rust-lang.org>
Sat, 8 Jun 2019 07:20:57 +0000 (07:20 +0000)
committerbors <bors@rust-lang.org>
Sat, 8 Jun 2019 07:20:57 +0000 (07:20 +0000)
commit7f90abe3aa1864e40e3d516b936c4a1a84e72aee
treec7f2291af4dbdb983fa7f5fdd6941b90dde084c4
parent6312b89fdabce0aedf613391266e08c0deef2324
parentf6611db1d59bbf5fb7d5cfbcccc629b6916d06a3
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.~~