Skip to content

Add classification functions #80

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
Apr 9, 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
86 changes: 86 additions & 0 deletions crates/core_simd/src/comparisons.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use crate::LanesAtMost32;

macro_rules! implement_mask_ops {
{ $($vector:ident => $mask:ident ($inner_mask_ty:ident, $inner_ty:ident),)* } => {
$(
impl<const LANES: usize> crate::$vector<LANES>
where
crate::$vector<LANES>: LanesAtMost32,
crate::$inner_ty<LANES>: LanesAtMost32,
{
/// 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()
}
}

/// 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()
}
}

/// 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()
}
}

/// 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()
}
}

/// 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()
}
}

/// 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()
}
}
}
)*
}
}

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

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

SimdF32 => Mask32 (SimdMask32, SimdI32),
SimdF64 => Mask64 (SimdMask64, SimdI64),
}
24 changes: 12 additions & 12 deletions crates/core_simd/src/first.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// Implements common traits on the specified vector `$name`, holding multiple `$lanes` of `$type`.
macro_rules! impl_vector {
{ $name:ident, $type:ty } => {
impl<const LANES: usize> $name<LANES> where Self: crate::LanesAtMost64 {
impl<const LANES: usize> $name<LANES> where Self: crate::LanesAtMost32 {
/// Construct a SIMD vector by setting all lanes to the given value.
pub const fn splat(value: $type) -> Self {
Self([value; LANES])
Expand Down Expand Up @@ -44,31 +44,31 @@ macro_rules! impl_vector {
}
}

impl<const LANES: usize> Copy for $name<LANES> where Self: crate::LanesAtMost64 {}
impl<const LANES: usize> Copy for $name<LANES> where Self: crate::LanesAtMost32 {}

impl<const LANES: usize> Clone for $name<LANES> where Self: crate::LanesAtMost64 {
impl<const LANES: usize> Clone for $name<LANES> where Self: crate::LanesAtMost32 {
#[inline]
fn clone(&self) -> Self {
*self
}
}

impl<const LANES: usize> Default for $name<LANES> where Self: crate::LanesAtMost64 {
impl<const LANES: usize> Default for $name<LANES> where Self: crate::LanesAtMost32 {
#[inline]
fn default() -> Self {
Self::splat(<$type>::default())
}
}

impl<const LANES: usize> PartialEq for $name<LANES> where Self: crate::LanesAtMost64 {
impl<const LANES: usize> PartialEq for $name<LANES> where Self: crate::LanesAtMost32 {
#[inline]
fn eq(&self, other: &Self) -> bool {
// TODO use SIMD equality
self.to_array() == other.to_array()
}
}

impl<const LANES: usize> PartialOrd for $name<LANES> where Self: crate::LanesAtMost64 {
impl<const LANES: usize> PartialOrd for $name<LANES> where Self: crate::LanesAtMost32 {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
// TODO use SIMD equalitya
Expand All @@ -77,43 +77,43 @@ macro_rules! impl_vector {
}

// array references
impl<const LANES: usize> AsRef<[$type; LANES]> for $name<LANES> where Self: crate::LanesAtMost64 {
impl<const LANES: usize> AsRef<[$type; LANES]> for $name<LANES> where Self: crate::LanesAtMost32 {
#[inline]
fn as_ref(&self) -> &[$type; LANES] {
&self.0
}
}

impl<const LANES: usize> AsMut<[$type; LANES]> for $name<LANES> where Self: crate::LanesAtMost64 {
impl<const LANES: usize> AsMut<[$type; LANES]> for $name<LANES> where Self: crate::LanesAtMost32 {
#[inline]
fn as_mut(&mut self) -> &mut [$type; LANES] {
&mut self.0
}
}

// slice references
impl<const LANES: usize> AsRef<[$type]> for $name<LANES> where Self: crate::LanesAtMost64 {
impl<const LANES: usize> AsRef<[$type]> for $name<LANES> where Self: crate::LanesAtMost32 {
#[inline]
fn as_ref(&self) -> &[$type] {
&self.0
}
}

impl<const LANES: usize> AsMut<[$type]> for $name<LANES> where Self: crate::LanesAtMost64 {
impl<const LANES: usize> AsMut<[$type]> for $name<LANES> where Self: crate::LanesAtMost32 {
#[inline]
fn as_mut(&mut self) -> &mut [$type] {
&mut self.0
}
}

// vector/array conversion
impl<const LANES: usize> From<[$type; LANES]> for $name<LANES> where Self: crate::LanesAtMost64 {
impl<const LANES: usize> From<[$type; LANES]> for $name<LANES> where Self: crate::LanesAtMost32 {
fn from(array: [$type; LANES]) -> Self {
Self(array)
}
}

impl <const LANES: usize> From<$name<LANES>> for [$type; LANES] where $name<LANES>: crate::LanesAtMost64 {
impl <const LANES: usize> From<$name<LANES>> for [$type; LANES] where $name<LANES>: crate::LanesAtMost32 {
fn from(vector: $name<LANES>) -> Self {
vector.to_array()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/core_simd/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ macro_rules! impl_fmt_trait {
$( // repeat trait
impl<const LANES: usize> core::fmt::$trait for crate::$type<LANES>
where
Self: crate::LanesAtMost64,
Self: crate::LanesAtMost32,
{
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
$format(self.as_ref(), f)
Expand Down
1 change: 0 additions & 1 deletion crates/core_simd/src/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ extern "platform-intrinsic" {
pub(crate) fn simd_shuffle8<T, U>(x: T, y: T, idx: [u32; 8]) -> U;
pub(crate) fn simd_shuffle16<T, U>(x: T, y: T, idx: [u32; 16]) -> U;
pub(crate) fn simd_shuffle32<T, U>(x: T, y: T, idx: [u32; 32]) -> U;
pub(crate) fn simd_shuffle64<T, U>(x: T, y: T, idx: [u32; 64]) -> U;

// {s,u}add.sat
pub(crate) fn simd_saturating_add<T>(x: T, y: T) -> T;
Expand Down
15 changes: 7 additions & 8 deletions crates/core_simd/src/lanes_at_most_64.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
/// Implemented for bitmask sizes that are supported by the implementation.
pub trait LanesAtMost64 {}
pub trait LanesAtMost32 {}

macro_rules! impl_for {
{ $name:ident } => {
impl LanesAtMost64 for $name<1> {}
impl LanesAtMost64 for $name<2> {}
impl LanesAtMost64 for $name<4> {}
impl LanesAtMost64 for $name<8> {}
impl LanesAtMost64 for $name<16> {}
impl LanesAtMost64 for $name<32> {}
impl LanesAtMost64 for $name<64> {}
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> {}
}
}

Expand Down
3 changes: 2 additions & 1 deletion crates/core_simd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod permute;
#[macro_use]
mod transmute;

mod comparisons;
mod fmt;
mod intrinsics;
mod ops;
Expand All @@ -20,7 +21,7 @@ mod round;
mod math;

mod lanes_at_most_64;
pub use lanes_at_most_64::LanesAtMost64;
pub use lanes_at_most_64::LanesAtMost32;

mod masks;
pub use masks::*;
Expand Down
Loading