Skip to content

Commit

Permalink
Add lane count marker type
Browse files Browse the repository at this point in the history
  • Loading branch information
calebzulawski committed Jul 24, 2021
1 parent f93bef3 commit 97c25dd
Show file tree
Hide file tree
Showing 21 changed files with 367 additions and 411 deletions.
6 changes: 2 additions & 4 deletions crates/core_simd/src/comparisons.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use crate::Vector;
use crate::{LaneCount, SupportedLaneCount};

macro_rules! implement_mask_ops {
{ $($vector:ident => $mask:ident ($inner_ty:ident),)* } => {
$(
impl<const LANES: usize> crate::$vector<LANES>
where
crate::$vector<LANES>: Vector,
crate::$inner_ty<LANES>: Vector,
crate::$mask<LANES>: crate::Mask,
LaneCount<LANES>: SupportedLaneCount,
{
/// Test if each lane is equal to the corresponding lane in `other`.
#[inline]
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::Vector,
crate::LaneCount<LANES>: crate::SupportedLaneCount,
{
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
$format(self.as_ref(), f)
Expand Down
10 changes: 6 additions & 4 deletions crates/core_simd/src/iter.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::{LaneCount, SupportedLaneCount};

macro_rules! impl_traits {
{ $type:ident } => {
impl<const LANES: usize> core::iter::Sum<Self> for crate::$type<LANES>
where
Self: crate::Vector,
LaneCount<LANES>: SupportedLaneCount,
{
fn sum<I: core::iter::Iterator<Item = Self>>(iter: I) -> Self {
iter.fold(Default::default(), core::ops::Add::add)
Expand All @@ -11,7 +13,7 @@ macro_rules! impl_traits {

impl<const LANES: usize> core::iter::Product<Self> for crate::$type<LANES>
where
Self: crate::Vector,
LaneCount<LANES>: SupportedLaneCount,
{
fn product<I: core::iter::Iterator<Item = Self>>(iter: I) -> Self {
iter.fold(Default::default(), core::ops::Mul::mul)
Expand All @@ -20,7 +22,7 @@ macro_rules! impl_traits {

impl<'a, const LANES: usize> core::iter::Sum<&'a Self> for crate::$type<LANES>
where
Self: crate::Vector,
LaneCount<LANES>: SupportedLaneCount,
{
fn sum<I: core::iter::Iterator<Item = &'a Self>>(iter: I) -> Self {
iter.fold(Default::default(), core::ops::Add::add)
Expand All @@ -29,7 +31,7 @@ macro_rules! impl_traits {

impl<'a, const LANES: usize> core::iter::Product<&'a Self> for crate::$type<LANES>
where
Self: crate::Vector,
LaneCount<LANES>: SupportedLaneCount,
{
fn product<I: core::iter::Iterator<Item = &'a Self>>(iter: I) -> Self {
iter.fold(Default::default(), core::ops::Mul::mul)
Expand Down
43 changes: 43 additions & 0 deletions crates/core_simd/src/lane_count.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
mod sealed {
pub trait Sealed {}
}
use sealed::Sealed;

/// A type representing a vector lane count.
pub struct LaneCount<const LANES: usize>;

/// Helper trait for vector lane counts.
pub trait SupportedLaneCount: Sealed {
/// The bitmask representation of a mask.
type BitMask: Copy + Default + AsRef<[u8]> + AsMut<[u8]>;

#[doc(hidden)]
type IntBitMask;
}

impl<const LANES: usize> Sealed for LaneCount<LANES> {}

impl SupportedLaneCount for LaneCount<1> {
type BitMask = [u8; 1];
type IntBitMask = u8;
}
impl SupportedLaneCount for LaneCount<2> {
type BitMask = [u8; 1];
type IntBitMask = u8;
}
impl SupportedLaneCount for LaneCount<4> {
type BitMask = [u8; 1];
type IntBitMask = u8;
}
impl SupportedLaneCount for LaneCount<8> {
type BitMask = [u8; 1];
type IntBitMask = u8;
}
impl SupportedLaneCount for LaneCount<16> {
type BitMask = [u8; 2];
type IntBitMask = u16;
}
impl SupportedLaneCount for LaneCount<32> {
type BitMask = [u8; 4];
type IntBitMask = u32;
}
4 changes: 3 additions & 1 deletion crates/core_simd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ mod comparisons;
mod fmt;
mod intrinsics;
mod iter;
mod math;
mod ops;
mod round;
mod vendor;

mod math;
mod lane_count;
pub use lane_count::*;

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

0 comments on commit 97c25dd

Please sign in to comment.