From 8003b043233213c6f984837d7618f92a6181a875 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Wed, 1 Dec 2021 15:02:03 -0800 Subject: [PATCH] impl Op<&'_ RHS> for &'_ LHS --- crates/core_simd/src/ops/deref.rs | 114 ++++++++++++++++++++-------- crates/core_simd/tests/autoderef.rs | 22 ++++++ 2 files changed, 106 insertions(+), 30 deletions(-) create mode 100644 crates/core_simd/tests/autoderef.rs diff --git a/crates/core_simd/src/ops/deref.rs b/crates/core_simd/src/ops/deref.rs index 1138b9494f6..9883a74c92d 100644 --- a/crates/core_simd/src/ops/deref.rs +++ b/crates/core_simd/src/ops/deref.rs @@ -1,70 +1,124 @@ //! This module hacks in "implicit deref" for Simd's operators. //! Ideally, Rust would take care of this itself, //! and method calls usually handle the LHS implicitly. -//! So, we'll manually deref the RHS. +//! But this is not the case with arithmetic ops. use super::*; -macro_rules! deref_ops { - ($(impl $trait:ident<&Self> for Simd { - fn $call:ident(rhs: &Self) - })*) => { - $(impl $trait<&Self> for Simd +macro_rules! deref_lhs { + (impl $trait:ident for $simd:ty { + fn $call:ident + }) => { + impl $trait<$simd> for &$simd + where + T: SimdElement, + $simd: $trait<$simd, Output = $simd>, + LaneCount: SupportedLaneCount, + { + type Output = Simd; + + #[inline] + #[must_use = "operator returns a new vector without mutating the inputs"] + fn $call(self, rhs: $simd) -> Self::Output { + (*self).$call(rhs) + } + } + }; +} + +macro_rules! deref_rhs { + (impl $trait:ident for $simd:ty { + fn $call:ident + }) => { + impl $trait<&$simd> for $simd where - Self: $trait, T: SimdElement, + $simd: $trait<$simd, Output = $simd>, LaneCount: SupportedLaneCount, { - type Output = Self; + type Output = Simd; #[inline] #[must_use = "operator returns a new vector without mutating the inputs"] - fn $call(self, rhs: &Self) -> Self::Output { + fn $call(self, rhs: &$simd) -> Self::Output { self.$call(*rhs) } - })* + } + }; +} + +macro_rules! deref_ops { + ($(impl $trait:ident for $simd:ty { + fn $call:ident + })*) => { + $( + deref_rhs! { + impl $trait for $simd { + fn $call + } + } + deref_lhs! { + impl $trait for $simd { + fn $call + } + } + impl<'lhs, 'rhs, T, const LANES: usize> $trait<&'rhs $simd> for &'lhs $simd + where + T: SimdElement, + $simd: $trait<$simd, Output = $simd>, + LaneCount: SupportedLaneCount, + { + type Output = $simd; + + #[inline] + #[must_use = "operator returns a new vector without mutating the inputs"] + fn $call(self, rhs: &$simd) -> Self::Output { + (*self).$call(*rhs) + } + } + )* } } deref_ops! { // Arithmetic - impl Add<&Self> for Simd { - fn add(rhs: &Self) + impl Add for Simd { + fn add } - impl Mul<&Self> for Simd { - fn mul(rhs: &Self) + impl Mul for Simd { + fn mul } - impl Sub<&Self> for Simd { - fn sub(rhs: &Self) + impl Sub for Simd { + fn sub } - impl Div<&Self> for Simd { - fn div(rhs: &Self) + impl Div for Simd { + fn div } - impl Rem<&Self> for Simd { - fn rem(rhs: &Self) + impl Rem for Simd { + fn rem } // Bitops - impl BitAnd<&Self> for Simd { - fn bitand(rhs: &Self) + impl BitAnd for Simd { + fn bitand } - impl BitOr<&Self> for Simd { - fn bitor(rhs: &Self) + impl BitOr for Simd { + fn bitor } - impl BitXor<&Self> for Simd { - fn bitxor(rhs: &Self) + impl BitXor for Simd { + fn bitxor } - impl Shl<&Self> for Simd { - fn shl(rhs: &Self) + impl Shl for Simd { + fn shl } - impl Shr<&Self> for Simd { - fn shr(rhs: &Self) + impl Shr for Simd { + fn shr } } diff --git a/crates/core_simd/tests/autoderef.rs b/crates/core_simd/tests/autoderef.rs new file mode 100644 index 00000000000..9359da16ee5 --- /dev/null +++ b/crates/core_simd/tests/autoderef.rs @@ -0,0 +1,22 @@ +// Test that we handle all our "auto-deref" cases correctly. +#![feature(portable_simd)] +use core_simd::f32x4; + +#[cfg(target_arch = "wasm32")] +use wasm_bindgen_test::*; + +#[cfg(target_arch = "wasm32")] +wasm_bindgen_test_configure!(run_in_browser); + +#[test] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] +fn deref() { + let x = f32x4::splat(1.0); + let y = f32x4::splat(2.0); + let a = &x; + let b = &y; + assert_eq!(f32x4::splat(3.0), x + y); + assert_eq!(f32x4::splat(3.0), x + b); + assert_eq!(f32x4::splat(3.0), a + y); + assert_eq!(f32x4::splat(3.0), a + b); +} -- GitLab