about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorDylan MacKenzie <ecstaticmorse@gmail.com>2019-06-07 14:12:54 -0700
committerDylan MacKenzie <ecstaticmorse@gmail.com>2019-06-07 15:37:03 -0700
commitbd899d02e9530577e2d12f80b8defe3ef40144cf (patch)
tree860770ca5e814110ff3fa03a411839b7c0754856 /src
parentc8865d8e195813ade6b84434ac9f8850e7112d1a (diff)
downloadrust-bd899d02e9530577e2d12f80b8defe3ef40144cf.tar.gz
rust-bd899d02e9530577e2d12f80b8defe3ef40144cf.zip
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)]`.
Diffstat (limited to 'src')
-rw-r--r--src/libcore/num/mod.rs9
1 files changed, 3 insertions, 6 deletions
diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs
index dd7090623f5..304b2fc9ebb 100644
--- a/src/libcore/num/mod.rs
+++ b/src/libcore/num/mod.rs
@@ -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
             }
         }