From bd899d02e9530577e2d12f80b8defe3ef40144cf Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Fri, 7 Jun 2019 14:12:54 -0700 Subject: [PATCH 1/2] 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)]`. --- src/libcore/num/mod.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index dd7090623f5b3..304b2fc9ebb06 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 } } From f6611db1d59bbf5fb7d5cfbcccc629b6916d06a3 Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Fri, 7 Jun 2019 16:23:33 -0700 Subject: [PATCH 2/2] Add `const`-ness tests for `i32::signum` --- src/test/run-pass/const-int-sign.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/test/run-pass/const-int-sign.rs b/src/test/run-pass/const-int-sign.rs index 9d656a0203069..fcd3ef4ea025b 100644 --- a/src/test/run-pass/const-int-sign.rs +++ b/src/test/run-pass/const-int-sign.rs @@ -1,11 +1,21 @@ +#![feature(const_int_sign)] + const NEGATIVE_A: bool = (-10i32).is_negative(); const NEGATIVE_B: bool = 10i32.is_negative(); -const POSITIVE_A: bool= (-10i32).is_positive(); -const POSITIVE_B: bool= 10i32.is_positive(); +const POSITIVE_A: bool = (-10i32).is_positive(); +const POSITIVE_B: bool = 10i32.is_positive(); + +const SIGNUM_POS: i32 = 10i32.signum(); +const SIGNUM_NIL: i32 = 0i32.signum(); +const SIGNUM_NEG: i32 = (-42i32).signum(); fn main() { assert!(NEGATIVE_A); assert!(!NEGATIVE_B); assert!(!POSITIVE_A); assert!(POSITIVE_B); + + assert_eq!(SIGNUM_POS, 1); + assert_eq!(SIGNUM_NIL, 0); + assert_eq!(SIGNUM_NEG, -1); }