Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 049e8ca

Browse files
Refactor float arith with #[must_use]
1 parent 8aef340 commit 049e8ca

File tree

1 file changed

+64
-14
lines changed

1 file changed

+64
-14
lines changed

crates/core_simd/src/ops.rs

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,70 @@ bitops! {
220220
}
221221
}
222222

223+
macro_rules! float_arith {
224+
($(impl<const LANES: usize> FloatArith for Simd<$float:ty, LANES> {
225+
fn add(self, rhs: Self) -> Self::Output;
226+
fn mul(self, rhs: Self) -> Self::Output;
227+
fn sub(self, rhs: Self) -> Self::Output;
228+
fn div(self, rhs: Self) -> Self::Output;
229+
fn rem(self, rhs: Self) -> Self::Output;
230+
})*) => {
231+
$(
232+
unsafe_base_op!{
233+
impl<const LANES: usize> Add for Simd<$float, LANES> {
234+
fn add(self, rhs: Self) -> Self::Output {
235+
unsafe { simd_add }
236+
}
237+
}
238+
239+
impl<const LANES: usize> Mul for Simd<$float, LANES> {
240+
fn mul(self, rhs: Self) -> Self::Output {
241+
unsafe { simd_mul }
242+
}
243+
}
244+
245+
impl<const LANES: usize> Sub for Simd<$float, LANES> {
246+
fn sub(self, rhs: Self) -> Self::Output {
247+
unsafe { simd_sub }
248+
}
249+
}
250+
251+
impl<const LANES: usize> Div for Simd<$float, LANES> {
252+
fn div(self, rhs: Self) -> Self::Output {
253+
unsafe { simd_div }
254+
}
255+
}
256+
257+
impl<const LANES: usize> Rem for Simd<$float, LANES> {
258+
fn rem(self, rhs: Self) -> Self::Output {
259+
unsafe { simd_rem }
260+
}
261+
}
262+
}
263+
)*
264+
};
265+
}
266+
267+
// We don't need any special precautions here:
268+
// Floats always accept arithmetic ops, but may become NaN.
269+
float_arith! {
270+
impl<const LANES: usize> FloatArith for Simd<f32, LANES> {
271+
fn add(self, rhs: Self) -> Self::Output;
272+
fn mul(self, rhs: Self) -> Self::Output;
273+
fn sub(self, rhs: Self) -> Self::Output;
274+
fn div(self, rhs: Self) -> Self::Output;
275+
fn rem(self, rhs: Self) -> Self::Output;
276+
}
277+
278+
impl<const LANES: usize> FloatArith for Simd<f64, LANES> {
279+
fn add(self, rhs: Self) -> Self::Output;
280+
fn mul(self, rhs: Self) -> Self::Output;
281+
fn sub(self, rhs: Self) -> Self::Output;
282+
fn div(self, rhs: Self) -> Self::Output;
283+
fn rem(self, rhs: Self) -> Self::Output;
284+
}
285+
}
286+
223287
/// Automatically implements operators over references in addition to the provided operator.
224288
macro_rules! impl_ref_ops {
225289
// binary op
@@ -284,19 +348,6 @@ macro_rules! impl_op {
284348
};
285349
}
286350

287-
/// Implements floating-point operators for the provided types.
288-
macro_rules! impl_float_ops {
289-
{ $($scalar:ty),* } => {
290-
$(
291-
impl_op! { impl Add for $scalar }
292-
impl_op! { impl Sub for $scalar }
293-
impl_op! { impl Mul for $scalar }
294-
impl_op! { impl Div for $scalar }
295-
impl_op! { impl Rem for $scalar }
296-
)*
297-
};
298-
}
299-
300351
/// Implements unsigned integer operators for the provided types.
301352
macro_rules! impl_unsigned_int_ops {
302353
{ $($scalar:ty),* } => {
@@ -375,4 +426,3 @@ macro_rules! impl_signed_int_ops {
375426

376427
impl_unsigned_int_ops! { u8, u16, u32, u64, usize }
377428
impl_signed_int_ops! { i8, i16, i32, i64, isize }
378-
impl_float_ops! { f32, f64 }

0 commit comments

Comments
 (0)