Skip to content

Commit

Permalink
Make i*::signum a const fn.
Browse files Browse the repository at this point in the history
This uses a well-known branchless implementation of `signum`.
Its `const`-ness is unstable and requires `#![feature(const_int_sign)]`.
  • Loading branch information
ecstatic-morse committed Jun 7, 2019
1 parent c8865d8 commit bd899d0
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1993,13 +1993,10 @@ assert_eq!((-10", stringify!($SelfT), ").signum(), -1);",
$EndFeature, "
```"),
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_int_sign")]
#[inline]
pub fn signum(self) -> Self {
match self {
n if n > 0 => 1,
0 => 0,
_ => -1,
}
pub const fn signum(self) -> Self {
(self > 0) as Self - (self < 0) as Self
}
}

Expand Down

0 comments on commit bd899d0

Please sign in to comment.