Skip to content

Commit 2816b23

Browse files
raphlinusischeinkman
authored andcommitted
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
1 parent 35d9901 commit 2816b23

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/libstd/f32.rs

+28
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,34 @@ 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+
#[unstable(feature="copysign", issue="0")]
225+
pub fn copysign(self, y: f32) -> f32 {
226+
unsafe { intrinsics::copysignf32(self, y) }
227+
}
228+
201229
/// Fused multiply-add. Computes `(self * a) + b` with only one rounding
202230
/// error, yielding a more accurate result than an unfused multiply-add.
203231
///

src/libstd/f64.rs

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

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

0 commit comments

Comments
 (0)