Skip to content

only use fma intrinsics if fma is enabled #44805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/libstd/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,9 @@ impl f32 {
/// error. This produces a more accurate result with better performance than
/// a separate multiplication operation followed by an add.
///
/// This will fall back to computing `(self * a) + b` if the target-feature `fma`
/// is not enabled.
///
/// ```
/// use std::f32;
///
Expand All @@ -410,7 +413,11 @@ impl f32 {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn mul_add(self, a: f32, b: f32) -> f32 {
unsafe { intrinsics::fmaf32(self, a, b) }
if cfg!(target_feature="fma") {
unsafe { intrinsics::fmaf32(self, a, b) }
} else {
self * a + b
}
}

/// Takes the reciprocal (inverse) of a number, `1/x`.
Expand Down
9 changes: 8 additions & 1 deletion src/libstd/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ impl f64 {
/// error. This produces a more accurate result with better performance than
/// a separate multiplication operation followed by an add.
///
/// This will fall back to computing `(self * a) + b` if the target-feature `fma`
/// is not enabled.
///
/// ```
/// let m = 10.0_f64;
/// let x = 4.0_f64;
Expand All @@ -356,7 +359,11 @@ impl f64 {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn mul_add(self, a: f64, b: f64) -> f64 {
unsafe { intrinsics::fmaf64(self, a, b) }
if cfg!(target_feature="fma") {
unsafe { intrinsics::fmaf64(self, a, b) }
} else {
self * a + b
}
}

/// Takes the reciprocal (inverse) of a number, `1/x`.
Expand Down