Skip to content

Commit 50d79e3

Browse files
committed
Move mul_sub to StdFloat trait
Both mul_add and mul_sub now live in StdFloat for consistency.
1 parent 1ad5819 commit 50d79e3

File tree

2 files changed

+13
-26
lines changed

2 files changed

+13
-26
lines changed

crates/core_simd/src/simd/num/float.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -235,25 +235,6 @@ pub trait SimdFloat: Copy + Sealed {
235235
/// assert!(v.reduce_min().is_nan());
236236
/// ```
237237
fn reduce_min(self) -> Self::Scalar;
238-
/// Fused multiply-subtract: computes `(self * a) - b` with only one rounding error.
239-
///
240-
/// This produces more accurate results than separate multiply and subtract operations,
241-
/// and can be faster on platforms with dedicated FMS instructions.
242-
///
243-
/// # Examples
244-
///
245-
/// ```
246-
/// # #![feature(portable_simd)]
247-
/// # #[cfg(feature = "as_crate")] use core_simd::simd;
248-
/// # #[cfg(not(feature = "as_crate"))] use core::simd;
249-
/// # use simd::prelude::*;
250-
/// let a = f32x4::splat(2.0);
251-
/// let b = f32x4::splat(3.0);
252-
/// let c = f32x4::splat(4.0);
253-
/// assert_eq!(a.mul_sub(b, c), f32x4::splat(2.0)); // 2*3 - 4 = 2
254-
/// ```
255-
#[must_use = "method returns a new vector and does not mutate the original value"]
256-
fn mul_sub(self, a: Self, b: Self) -> Self;
257238
}
258239

259240
macro_rules! impl_trait {
@@ -458,13 +439,6 @@ macro_rules! impl_trait {
458439
// Safety: `self` is a float vector
459440
unsafe { core::intrinsics::simd::simd_reduce_min(self) }
460441
}
461-
462-
#[inline]
463-
fn mul_sub(self, a: Self, b: Self) -> Self {
464-
// self * a - b = self * a + (-b)
465-
// Safety: `self`, `a`, and `b` are float vectors
466-
unsafe { core::intrinsics::simd::simd_fma(self, a, -b) }
467-
}
468442
}
469443
)*
470444
}

crates/std_float/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,19 @@ pub trait StdFloat: Sealed + Sized {
5656
unsafe { intrinsics::simd_fma(self, a, b) }
5757
}
5858

59+
/// Elementwise fused multiply-subtract. Computes `(self * a) - b` with only one rounding error,
60+
/// yielding a more accurate result than an unfused multiply-subtract.
61+
///
62+
/// Using `mul_sub` *may* be more performant than an unfused multiply-subtract if the target
63+
/// architecture has a dedicated `fma` CPU instruction. However, this is not always
64+
/// true, and will be heavily dependent on designing algorithms with specific target
65+
/// hardware in mind.
66+
#[inline]
67+
#[must_use = "method returns a new vector and does not mutate the original value"]
68+
fn mul_sub(self, a: Self, b: Self) -> Self {
69+
unsafe { intrinsics::simd_fma(self, a, -b) }
70+
}
71+
5972
/// Produces a vector where every element has the square root value
6073
/// of the equivalently-indexed element in `self`
6174
#[inline]

0 commit comments

Comments
 (0)