From e6d95e47983e69e63abfb080efafc2c7fa1f6c67 Mon Sep 17 00:00:00 2001 From: Caleb Zulawski Date: Sat, 7 Aug 2021 05:47:54 +0000 Subject: [PATCH] Implement comparisons generically --- crates/core_simd/src/comparisons.rs | 110 +++++++++++----------------- 1 file changed, 41 insertions(+), 69 deletions(-) diff --git a/crates/core_simd/src/comparisons.rs b/crates/core_simd/src/comparisons.rs index c5e9be9015fe..c094f680a59d 100644 --- a/crates/core_simd/src/comparisons.rs +++ b/crates/core_simd/src/comparisons.rs @@ -1,77 +1,49 @@ -use crate::{LaneCount, SupportedLaneCount}; +use crate::{LaneCount, Mask, Simd, SimdElement, SupportedLaneCount}; -macro_rules! implement_mask_ops { - { $($vector:ident => $mask:ident ($inner_ty:ident),)* } => { - $( - impl crate::$vector - where - LaneCount: SupportedLaneCount, - { - /// Test if each lane is equal to the corresponding lane in `other`. - #[inline] - pub fn lanes_eq(self, other: Self) -> crate::$mask { - unsafe { - crate::$mask::from_int_unchecked(crate::intrinsics::simd_eq(self, other)) - } - } +impl Simd +where + Element: SimdElement + PartialEq, + LaneCount: SupportedLaneCount, +{ + /// Test if each lane is equal to the corresponding lane in `other`. + #[inline] + pub fn lanes_eq(self, other: Self) -> Mask { + unsafe { Mask::from_int_unchecked(crate::intrinsics::simd_eq(self, other)) } + } - /// Test if each lane is not equal to the corresponding lane in `other`. - #[inline] - pub fn lanes_ne(self, other: Self) -> crate::$mask { - unsafe { - crate::$mask::from_int_unchecked(crate::intrinsics::simd_ne(self, other)) - } - } - - /// Test if each lane is less than the corresponding lane in `other`. - #[inline] - pub fn lanes_lt(self, other: Self) -> crate::$mask { - unsafe { - crate::$mask::from_int_unchecked(crate::intrinsics::simd_lt(self, other)) - } - } - - /// Test if each lane is greater than the corresponding lane in `other`. - #[inline] - pub fn lanes_gt(self, other: Self) -> crate::$mask { - unsafe { - crate::$mask::from_int_unchecked(crate::intrinsics::simd_gt(self, other)) - } - } - - /// Test if each lane is less than or equal to the corresponding lane in `other`. - #[inline] - pub fn lanes_le(self, other: Self) -> crate::$mask { - unsafe { - crate::$mask::from_int_unchecked(crate::intrinsics::simd_le(self, other)) - } - } - - /// Test if each lane is greater than or equal to the corresponding lane in `other`. - #[inline] - pub fn lanes_ge(self, other: Self) -> crate::$mask { - unsafe { - crate::$mask::from_int_unchecked(crate::intrinsics::simd_ge(self, other)) - } - } - } - )* + /// Test if each lane is not equal to the corresponding lane in `other`. + #[inline] + pub fn lanes_ne(self, other: Self) -> Mask { + unsafe { Mask::from_int_unchecked(crate::intrinsics::simd_ne(self, other)) } } } -implement_mask_ops! { - SimdI8 => Mask8 (SimdI8), - SimdI16 => Mask16 (SimdI16), - SimdI32 => Mask32 (SimdI32), - SimdI64 => Mask64 (SimdI64), - SimdIsize => MaskSize (SimdIsize), +impl Simd +where + Element: SimdElement + PartialOrd, + LaneCount: SupportedLaneCount, +{ + /// Test if each lane is less than the corresponding lane in `other`. + #[inline] + pub fn lanes_lt(self, other: Self) -> Mask { + unsafe { Mask::from_int_unchecked(crate::intrinsics::simd_lt(self, other)) } + } - SimdU8 => Mask8 (SimdI8), - SimdU16 => Mask16 (SimdI16), - SimdU32 => Mask32 (SimdI32), - SimdU64 => Mask64 (SimdI64), - SimdUsize => MaskSize (SimdIsize), + /// Test if each lane is greater than the corresponding lane in `other`. + #[inline] + pub fn lanes_gt(self, other: Self) -> Mask { + unsafe { Mask::from_int_unchecked(crate::intrinsics::simd_gt(self, other)) } + } - SimdF32 => Mask32 (SimdI32), - SimdF64 => Mask64 (SimdI64), + /// Test if each lane is less than or equal to the corresponding lane in `other`. + #[inline] + pub fn lanes_le(self, other: Self) -> Mask { + unsafe { Mask::from_int_unchecked(crate::intrinsics::simd_le(self, other)) } + } + + /// Test if each lane is greater than or equal to the corresponding lane in `other`. + #[inline] + pub fn lanes_ge(self, other: Self) -> Mask { + unsafe { Mask::from_int_unchecked(crate::intrinsics::simd_ge(self, other)) } + } }