about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBrendan Zabarauskas <bjzaba@yahoo.com.au>2014-11-10 11:39:55 +1100
committerBrendan Zabarauskas <bjzaba@yahoo.com.au>2014-11-13 02:04:31 +1100
commit878bebfb63db6ea81f1fb738f4eeaa3afb8e55e4 (patch)
tree390606a20987e06e94e7e9f7cc0ee2a82041a345
parent26196715e8e8fb3a578cc89a54c13773dc1a3772 (diff)
downloadrust-878bebfb63db6ea81f1fb738f4eeaa3afb8e55e4.tar.gz
rust-878bebfb63db6ea81f1fb738f4eeaa3afb8e55e4.zip
Deprecate signum wrapper and clean up signed impls
-rw-r--r--src/libcore/num/mod.rs80
1 files changed, 42 insertions, 38 deletions
diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs
index ef218c1ce5a..bcc2f9fdd44 100644
--- a/src/libcore/num/mod.rs
+++ b/src/libcore/num/mod.rs
@@ -64,8 +64,8 @@ pub trait Signed: Neg<Self> {
     fn is_negative(self) -> bool;
 }
 
-macro_rules! signed_impl(
-    ($($T:ty)*) => ($(
+macro_rules! signed_int_impl {
+    ($T:ty) => {
         impl Signed for $T {
             #[inline]
             fn abs(self) -> $T {
@@ -75,9 +75,9 @@ macro_rules! signed_impl(
             #[inline]
             fn signum(self) -> $T {
                 match self {
-                    n if n > 0 => 1,
-                    0 => 0,
-                    _ => -1,
+                    n if n > 0 =>  1,
+                    0          =>  0,
+                    _          => -1,
                 }
             }
 
@@ -87,13 +87,17 @@ macro_rules! signed_impl(
             #[inline]
             fn is_negative(self) -> bool { self < 0 }
         }
-    )*)
-)
+    }
+}
 
-signed_impl!(int i8 i16 i32 i64)
+signed_int_impl!(i8)
+signed_int_impl!(i16)
+signed_int_impl!(i32)
+signed_int_impl!(i64)
+signed_int_impl!(int)
 
-macro_rules! signed_float_impl(
-    ($T:ty, $nan:expr, $inf:expr, $neg_inf:expr, $fabs:path, $fcopysign:path, $fdim:ident) => {
+macro_rules! signed_float_impl {
+    ($T:ty, $fabs:path, $fcopysign:path) => {
         impl Signed for $T {
             /// Computes the absolute value. Returns `NAN` if the number is `NAN`.
             #[inline]
@@ -103,46 +107,42 @@ macro_rules! signed_float_impl(
 
             /// # Returns
             ///
-            /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
-            /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
-            /// - `NAN` if the number is NaN
+            /// - `1.0` if the number is positive, `+0.0` or `Float::infinity()`
+            /// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()`
+            /// - `Float::nan()` if the number is `Float::nan()`
             #[inline]
             fn signum(self) -> $T {
-                if self != self { $nan } else {
+                if self.is_nan() {
+                    Float::nan()
+                } else {
                     unsafe { $fcopysign(1.0, self) }
                 }
             }
 
-            /// Returns `true` if the number is positive, including `+0.0` and `INFINITY`
+            /// Returns `true` if the number is positive, including `+0.0` and
+            /// `Float::infinity()`.
             #[inline]
-            fn is_positive(self) -> bool { self > 0.0 || (1.0 / self) == $inf }
+            fn is_positive(self) -> bool {
+                self > 0.0 || (1.0 / self) == Float::infinity()
+            }
 
-            /// Returns `true` if the number is negative, including `-0.0` and `NEG_INFINITY`
+            /// Returns `true` if the number is negative, including `-0.0` and
+            /// `Float::neg_infinity()`.
             #[inline]
-            fn is_negative(self) -> bool { self < 0.0 || (1.0 / self) == $neg_inf }
+            fn is_negative(self) -> bool {
+                self < 0.0 || (1.0 / self) == Float::neg_infinity()
+            }
         }
-    }
-)
+    };
+}
 
-signed_float_impl!(f32, f32::NAN, f32::INFINITY, f32::NEG_INFINITY,
-                   intrinsics::fabsf32, intrinsics::copysignf32, fdimf)
-signed_float_impl!(f64, f64::NAN, f64::INFINITY, f64::NEG_INFINITY,
-                   intrinsics::fabsf64, intrinsics::copysignf64, fdim)
+signed_float_impl!(f32,
+    intrinsics::fabsf32,
+    intrinsics::copysignf32)
 
-/// Returns the sign of the number.
-///
-/// For `f32` and `f64`:
-///
-/// * `1.0` if the number is positive, `+0.0` or `INFINITY`
-/// * `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
-/// * `NaN` if the number is `NaN`
-///
-/// For signed integers:
-///
-/// * `0` if the number is zero
-/// * `1` if the number is positive
-/// * `-1` if the number is negative
-#[inline(always)] pub fn signum<T: Signed>(value: T) -> T { value.signum() }
+signed_float_impl!(f64,
+    intrinsics::fabsf64,
+    intrinsics::copysignf64)
 
 /// Raises a value to the power of exp, using exponentiation by squaring.
 ///
@@ -1481,6 +1481,10 @@ one_impl!(f64, 1.0f64)
 pub fn abs<T: Signed>(value: T) -> T {
     value.abs()
 }
+#[deprecated = "Use `Signed::signum`"]
+pub fn signum<T: Signed>(value: T) -> T {
+    value.signum()
+}
 #[deprecated = "Use `UnsignedInt::next_power_of_two`"]
 pub fn next_power_of_two<T: UnsignedInt>(n: T) -> T {
     n.next_power_of_two()