about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorkennytm <kennytm@gmail.com>2018-10-19 11:03:34 +0800
committerkennytm <kennytm@gmail.com>2018-10-19 16:47:51 +0800
commit399314d802366023fc3d2b7ac7b7decfcc3397ea (patch)
treed148fea6c7c51fd2fdb1e6a18c140712d837e5f2 /src/libstd
parent9d2eb9b7529321a6c60bf299b48440e259a7d9d9 (diff)
parentf08db6bf1ee44dd1bc8c4d3ddcea1425fcd8d118 (diff)
downloadrust-399314d802366023fc3d2b7ac7b7decfcc3397ea.tar.gz
rust-399314d802366023fc3d2b7ac7b7decfcc3397ea.zip
Rollup merge of #55169 - raphlinus:copysign, r=joshtriplett
Add a `copysign` function to f32 and f64

This patch adds a `copysign` function to the float primitive types. It is an exceptionally useful function for writing efficient numeric code, as it often avoids branches, is auto-vectorizable, and there are efficient intrinsics for most platforms.

I think this might work as-is, as the relevant `copysign` intrinsic is already used internally for the implementation of `signum`. It's possible that an implementation might be needed in japaric/libm for portability across all platforms, in which case I'll do that also.

Part of the work towards #55107
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/f32.rs29
-rw-r--r--src/libstd/f64.rs29
2 files changed, 58 insertions, 0 deletions
diff --git a/src/libstd/f32.rs b/src/libstd/f32.rs
index 8e8340b3ed9..c3f225d1eb0 100644
--- a/src/libstd/f32.rs
+++ b/src/libstd/f32.rs
@@ -198,6 +198,35 @@ impl f32 {
         }
     }
 
+    /// Returns a number composed of the magnitude of one number and the sign of
+    /// another.
+    ///
+    /// Equal to `self` if the sign of `self` and `y` are the same, otherwise
+    /// equal to `-y`. If `self` is a `NAN`, then a `NAN` with the sign of `y`
+    /// is returned.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(copysign)]
+    /// use std::f32;
+    ///
+    /// let f = 3.5_f32;
+    ///
+    /// assert_eq!(f.copysign(0.42), 3.5_f32);
+    /// assert_eq!(f.copysign(-0.42), -3.5_f32);
+    /// assert_eq!((-f).copysign(0.42), 3.5_f32);
+    /// assert_eq!((-f).copysign(-0.42), -3.5_f32);
+    ///
+    /// assert!(f32::NAN.copysign(1.0).is_nan());
+    /// ```
+    #[inline]
+    #[must_use]
+    #[unstable(feature="copysign", issue="55169")]
+    pub fn copysign(self, y: f32) -> f32 {
+        unsafe { intrinsics::copysignf32(self, y) }
+    }
+
     /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
     /// error, yielding a more accurate result than an unfused multiply-add.
     ///
diff --git a/src/libstd/f64.rs b/src/libstd/f64.rs
index 6880294afca..da062dda77a 100644
--- a/src/libstd/f64.rs
+++ b/src/libstd/f64.rs
@@ -176,6 +176,35 @@ impl f64 {
         }
     }
 
+    /// Returns a number composed of the magnitude of one number and the sign of
+    /// another.
+    ///
+    /// Equal to `self` if the sign of `self` and `y` are the same, otherwise
+    /// equal to `-y`. If `self` is a `NAN`, then a `NAN` with the sign of `y`
+    /// is returned.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(copysign)]
+    /// use std::f64;
+    ///
+    /// let f = 3.5_f64;
+    ///
+    /// assert_eq!(f.copysign(0.42), 3.5_f64);
+    /// assert_eq!(f.copysign(-0.42), -3.5_f64);
+    /// assert_eq!((-f).copysign(0.42), 3.5_f64);
+    /// assert_eq!((-f).copysign(-0.42), -3.5_f64);
+    ///
+    /// assert!(f64::NAN.copysign(1.0).is_nan());
+    /// ```
+    #[inline]
+    #[must_use]
+    #[unstable(feature="copysign", issue="55169")]
+    pub fn copysign(self, y: f64) -> f64 {
+        unsafe { intrinsics::copysignf64(self, y) }
+    }
+
     /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
     /// error, yielding a more accurate result than an unfused multiply-add.
     ///