From d9f82f9c4d7f4627721794c3fd9cf4598f60f688 Mon Sep 17 00:00:00 2001 From: Caleb Zulawski Date: Sat, 4 Dec 2021 05:54:15 +0000 Subject: [PATCH 1/2] Remove Select trait --- crates/core_simd/src/mod.rs | 1 - crates/core_simd/src/select.rs | 74 ++++++++++------------------------ 2 files changed, 21 insertions(+), 54 deletions(-) diff --git a/crates/core_simd/src/mod.rs b/crates/core_simd/src/mod.rs index ec874a22389d4..85026265956a2 100644 --- a/crates/core_simd/src/mod.rs +++ b/crates/core_simd/src/mod.rs @@ -27,7 +27,6 @@ pub mod simd { pub use crate::core_simd::lane_count::{LaneCount, SupportedLaneCount}; pub use crate::core_simd::masks::*; - pub use crate::core_simd::select::Select; pub use crate::core_simd::swizzle::*; pub use crate::core_simd::vector::*; } diff --git a/crates/core_simd/src/select.rs b/crates/core_simd/src/select.rs index 5d696ebf76ec2..8d521057fbd3e 100644 --- a/crates/core_simd/src/select.rs +++ b/crates/core_simd/src/select.rs @@ -1,54 +1,6 @@ use crate::simd::intrinsics; use crate::simd::{LaneCount, Mask, MaskElement, Simd, SimdElement, SupportedLaneCount}; -mod sealed { - pub trait Sealed { - fn select(mask: Mask, true_values: Self, false_values: Self) -> Self; - } -} -use sealed::Sealed; - -/// Supporting trait for vector `select` function -pub trait Select: Sealed {} - -impl Sealed> for Simd -where - T: SimdElement, - LaneCount: SupportedLaneCount, -{ - #[inline] - #[must_use = "method returns a new vector and does not mutate the original inputs"] - fn select(mask: Mask, true_values: Self, false_values: Self) -> Self { - unsafe { intrinsics::simd_select(mask.to_int(), true_values, false_values) } - } -} - -impl Select> for Simd -where - T: SimdElement, - LaneCount: SupportedLaneCount, -{ -} - -impl Sealed for Mask -where - T: MaskElement, - LaneCount: SupportedLaneCount, -{ - #[inline] - #[must_use = "method returns a new vector and does not mutate the original inputs"] - fn select(mask: Self, true_values: Self, false_values: Self) -> Self { - mask & true_values | !mask & false_values - } -} - -impl Select for Mask -where - T: MaskElement, - LaneCount: SupportedLaneCount, -{ -} - impl Mask where T: MaskElement, @@ -69,8 +21,24 @@ where /// let c = mask.select(a, b); /// assert_eq!(c.to_array(), [0, 5, 6, 3]); /// ``` + #[inline] + #[must_use = "method returns a new vector and does not mutate the original inputs"] + pub fn select( + self, + true_values: Simd, + false_values: Simd, + ) -> Simd + where + U: SimdElement, + { + unsafe { intrinsics::simd_select(self.to_int(), true_values, false_values) } + } + + /// Choose lanes from two masks. + /// + /// For each lane in the mask, choose the corresponding lane from `true_values` if + /// that lane mask is true, and `false_values` if that lane mask is false. /// - /// `select` can also be used on masks: /// ``` /// # #![feature(portable_simd)] /// # #[cfg(feature = "std")] use core_simd::Mask; @@ -78,12 +46,12 @@ where /// let a = Mask::::from_array([true, true, false, false]); /// let b = Mask::::from_array([false, false, true, true]); /// let mask = Mask::::from_array([true, false, false, true]); - /// let c = mask.select(a, b); + /// let c = mask.select_mask(a, b); /// assert_eq!(c.to_array(), [true, false, true, false]); /// ``` #[inline] - #[must_use = "method returns a new vector and does not mutate the original inputs"] - pub fn select>(self, true_values: S, false_values: S) -> S { - S::select(self, true_values, false_values) + #[must_use = "method returns a new mask and does not mutate the original inputs"] + pub fn select_mask(self, true_values: Self, false_values: Self) -> Self { + self & true_values | !self & false_values } } From 533f0fc81ab9ba097779fcd27c8f9ea12261fef5 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Fri, 10 Dec 2021 20:11:54 -0800 Subject: [PATCH 2/2] Use relative intrinsics paths for bitmasks --- crates/core_simd/src/masks/bitmask.rs | 8 ++------ crates/core_simd/src/masks/full_masks.rs | 4 ++-- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/crates/core_simd/src/masks/bitmask.rs b/crates/core_simd/src/masks/bitmask.rs index 4c964cb52e196..b4217dc87ba9c 100644 --- a/crates/core_simd/src/masks/bitmask.rs +++ b/crates/core_simd/src/masks/bitmask.rs @@ -105,18 +105,14 @@ where #[must_use = "method returns a new vector and does not mutate the original value"] pub fn to_int(self) -> Simd { unsafe { - crate::intrinsics::simd_select_bitmask( - self.0, - Simd::splat(T::TRUE), - Simd::splat(T::FALSE), - ) + intrinsics::simd_select_bitmask(self.0, Simd::splat(T::TRUE), Simd::splat(T::FALSE)) } } #[inline] #[must_use = "method returns a new mask and does not mutate the original value"] pub unsafe fn from_int_unchecked(value: Simd) -> Self { - unsafe { Self(crate::intrinsics::simd_bitmask(value), PhantomData) } + unsafe { Self(intrinsics::simd_bitmask(value), PhantomData) } } #[cfg(feature = "generic_const_exprs")] diff --git a/crates/core_simd/src/masks/full_masks.rs b/crates/core_simd/src/masks/full_masks.rs index 5421ccbe3d8f5..e5bb784bb910f 100644 --- a/crates/core_simd/src/masks/full_masks.rs +++ b/crates/core_simd/src/masks/full_masks.rs @@ -115,7 +115,7 @@ where pub fn to_bitmask(self) -> [u8; LaneCount::::BITMASK_LEN] { unsafe { let mut bitmask: [u8; LaneCount::::BITMASK_LEN] = - crate::intrinsics::simd_bitmask(self.0); + intrinsics::simd_bitmask(self.0); // There is a bug where LLVM appears to implement this operation with the wrong // bit order. @@ -144,7 +144,7 @@ where } } - Self::from_int_unchecked(crate::intrinsics::simd_select_bitmask( + Self::from_int_unchecked(intrinsics::simd_select_bitmask( bitmask, Self::splat(true).to_int(), Self::splat(false).to_int(),