Skip to content

Commit 399314d

Browse files
committed
Rollup merge of rust-lang#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 rust-lang#55107
2 parents 9d2eb9b + f08db6b commit 399314d

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/libstd/f32.rs

+29
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,35 @@ impl f32 {
198198
}
199199
}
200200

201+
/// Returns a number composed of the magnitude of one number and the sign of
202+
/// another.
203+
///
204+
/// Equal to `self` if the sign of `self` and `y` are the same, otherwise
205+
/// equal to `-y`. If `self` is a `NAN`, then a `NAN` with the sign of `y`
206+
/// is returned.
207+
///
208+
/// # Examples
209+
///
210+
/// ```
211+
/// #![feature(copysign)]
212+
/// use std::f32;
213+
///
214+
/// let f = 3.5_f32;
215+
///
216+
/// assert_eq!(f.copysign(0.42), 3.5_f32);
217+
/// assert_eq!(f.copysign(-0.42), -3.5_f32);
218+
/// assert_eq!((-f).copysign(0.42), 3.5_f32);
219+
/// assert_eq!((-f).copysign(-0.42), -3.5_f32);
220+
///
221+
/// assert!(f32::NAN.copysign(1.0).is_nan());
222+
/// ```
223+
#[inline]
224+
#[must_use]
225+
#[unstable(feature="copysign", issue="55169")]
226+
pub fn copysign(self, y: f32) -> f32 {
227+
unsafe { intrinsics::copysignf32(self, y) }
228+
}
229+
201230
/// Fused multiply-add. Computes `(self * a) + b` with only one rounding
202231
/// error, yielding a more accurate result than an unfused multiply-add.
203232
///

src/libstd/f64.rs

+29
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,35 @@ impl f64 {
176176
}
177177
}
178178

179+
/// Returns a number composed of the magnitude of one number and the sign of
180+
/// another.
181+
///
182+
/// Equal to `self` if the sign of `self` and `y` are the same, otherwise
183+
/// equal to `-y`. If `self` is a `NAN`, then a `NAN` with the sign of `y`
184+
/// is returned.
185+
///
186+
/// # Examples
187+
///
188+
/// ```
189+
/// #![feature(copysign)]
190+
/// use std::f64;
191+
///
192+
/// let f = 3.5_f64;
193+
///
194+
/// assert_eq!(f.copysign(0.42), 3.5_f64);
195+
/// assert_eq!(f.copysign(-0.42), -3.5_f64);
196+
/// assert_eq!((-f).copysign(0.42), 3.5_f64);
197+
/// assert_eq!((-f).copysign(-0.42), -3.5_f64);
198+
///
199+
/// assert!(f64::NAN.copysign(1.0).is_nan());
200+
/// ```
201+
#[inline]
202+
#[must_use]
203+
#[unstable(feature="copysign", issue="55169")]
204+
pub fn copysign(self, y: f64) -> f64 {
205+
unsafe { intrinsics::copysignf64(self, y) }
206+
}
207+
179208
/// Fused multiply-add. Computes `(self * a) + b` with only one rounding
180209
/// error, yielding a more accurate result than an unfused multiply-add.
181210
///

0 commit comments

Comments
 (0)