Skip to content

Commit

Permalink
Rollup merge of rust-lang#55169 - raphlinus:copysign, r=joshtriplett
Browse files Browse the repository at this point in the history
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 rust-lang#55107
  • Loading branch information
kennytm committed Oct 19, 2018
2 parents 9d2eb9b + f08db6b commit 399314d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/libstd/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
29 changes: 29 additions & 0 deletions src/libstd/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down

0 comments on commit 399314d

Please sign in to comment.