Skip to content

Feature/simplify masks #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 20 additions & 25 deletions crates/core_simd/src/comparisons.rs
Original file line number Diff line number Diff line change
@@ -1,64 +1,59 @@
use crate::LanesAtMost32;

macro_rules! implement_mask_ops {
{ $($vector:ident => $mask:ident ($inner_mask_ty:ident, $inner_ty:ident),)* } => {
{ $($vector:ident => $mask:ident ($inner_ty:ident),)* } => {
$(
impl<const LANES: usize> crate::$vector<LANES>
where
crate::$vector<LANES>: LanesAtMost32,
crate::$inner_ty<LANES>: LanesAtMost32,
crate::$mask<LANES>: crate::Mask,
{
/// Test if each lane is equal to the corresponding lane in `other`.
#[inline]
pub fn lanes_eq(self, other: Self) -> crate::$mask<LANES> {
unsafe {
crate::$inner_mask_ty::from_int_unchecked(crate::intrinsics::simd_eq(self, other))
.into()
crate::$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<LANES> {
unsafe {
crate::$inner_mask_ty::from_int_unchecked(crate::intrinsics::simd_ne(self, other))
.into()
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<LANES> {
unsafe {
crate::$inner_mask_ty::from_int_unchecked(crate::intrinsics::simd_lt(self, other))
.into()
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<LANES> {
unsafe {
crate::$inner_mask_ty::from_int_unchecked(crate::intrinsics::simd_gt(self, other))
.into()
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<LANES> {
unsafe {
crate::$inner_mask_ty::from_int_unchecked(crate::intrinsics::simd_le(self, other))
.into()
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<LANES> {
unsafe {
crate::$inner_mask_ty::from_int_unchecked(crate::intrinsics::simd_ge(self, other))
.into()
crate::$mask::from_int_unchecked(crate::intrinsics::simd_ge(self, other))
}
}
}
Expand All @@ -67,18 +62,18 @@ macro_rules! implement_mask_ops {
}

implement_mask_ops! {
SimdI8 => Mask8 (SimdMask8, SimdI8),
SimdI16 => Mask16 (SimdMask16, SimdI16),
SimdI32 => Mask32 (SimdMask32, SimdI32),
SimdI64 => Mask64 (SimdMask64, SimdI64),
SimdIsize => MaskSize (SimdMaskSize, SimdIsize),
SimdI8 => Mask8 (SimdI8),
SimdI16 => Mask16 (SimdI16),
SimdI32 => Mask32 (SimdI32),
SimdI64 => Mask64 (SimdI64),
SimdIsize => MaskSize (SimdIsize),

SimdU8 => Mask8 (SimdMask8, SimdI8),
SimdU16 => Mask16 (SimdMask16, SimdI16),
SimdU32 => Mask32 (SimdMask32, SimdI32),
SimdU64 => Mask64 (SimdMask64, SimdI64),
SimdUsize => MaskSize (SimdMaskSize, SimdIsize),
SimdU8 => Mask8 (SimdI8),
SimdU16 => Mask16 (SimdI16),
SimdU32 => Mask32 (SimdI32),
SimdU64 => Mask64 (SimdI64),
SimdUsize => MaskSize (SimdIsize),

SimdF32 => Mask32 (SimdMask32, SimdI32),
SimdF64 => Mask64 (SimdMask64, SimdI64),
SimdF32 => Mask32 (SimdI32),
SimdF64 => Mask64 (SimdI64),
}
6 changes: 6 additions & 0 deletions crates/core_simd/src/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ extern "platform-intrinsic" {
pub(crate) fn simd_reduce_and<T, U>(x: T) -> U;
pub(crate) fn simd_reduce_or<T, U>(x: T) -> U;
pub(crate) fn simd_reduce_xor<T, U>(x: T) -> U;

// truncate integer vector to bitmask
pub(crate) fn simd_bitmask<T, U>(x: T) -> U;

// select
pub(crate) fn simd_select_bitmask<T, U>(m: T, a: U, b: U) -> U;
}

#[cfg(feature = "std")]
Expand Down
42 changes: 32 additions & 10 deletions crates/core_simd/src/lanes_at_most_32.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
/// Implemented for bitmask sizes that are supported by the implementation.
pub trait LanesAtMost32 {}
/// Implemented for vectors that are supported by the implementation.
pub trait LanesAtMost32: sealed::Sealed {
#[doc(hidden)]
type BitMask: Into<u64>;
}

mod sealed {
pub trait Sealed {}
}

macro_rules! impl_for {
{ $name:ident } => {
impl LanesAtMost32 for $name<1> {}
impl LanesAtMost32 for $name<2> {}
impl LanesAtMost32 for $name<4> {}
impl LanesAtMost32 for $name<8> {}
impl LanesAtMost32 for $name<16> {}
impl LanesAtMost32 for $name<32> {}
impl<const LANES: usize> sealed::Sealed for $name<LANES>
where
$name<LANES>: LanesAtMost32,
{}

impl LanesAtMost32 for $name<1> {
type BitMask = u8;
}
impl LanesAtMost32 for $name<2> {
type BitMask = u8;
}
impl LanesAtMost32 for $name<4> {
type BitMask = u8;
}
impl LanesAtMost32 for $name<8> {
type BitMask = u8;
}
impl LanesAtMost32 for $name<16> {
type BitMask = u16;
}
impl LanesAtMost32 for $name<32> {
type BitMask = u32;
}
}
}

Expand All @@ -28,5 +52,3 @@ impl_for! { SimdIsize }

impl_for! { SimdF32 }
impl_for! { SimdF64 }

impl_for! { BitMask }
Loading