Skip to content

Commit

Permalink
Generically implement ToBitMaskArray
Browse files Browse the repository at this point in the history
  • Loading branch information
calebzulawski committed Feb 11, 2022
1 parent 29087c6 commit 30e85c8
Showing 1 changed file with 10 additions and 25 deletions.
35 changes: 10 additions & 25 deletions crates/core_simd/src/masks/to_bitmask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,32 +79,17 @@ pub const fn bitmask_len(lanes: usize) -> usize {
(lanes + 7) / 8
}

macro_rules! impl_array_bitmask {
{ $(impl ToBitMaskArray<[u8; _]> for Mask<_, $lanes:literal>)* } => {
$(
unsafe impl<T: MaskElement> ToBitMaskArray for Mask<T, $lanes>
{
const BYTES: usize = bitmask_len($lanes);

fn to_bitmask_array(self) -> [u8; Self::BYTES] {
self.0.to_bitmask_array()
}
unsafe impl<T: MaskElement, const LANES: usize> ToBitMaskArray for Mask<T, LANES>
where
LaneCount<LANES>: SupportedLaneCount,
{
const BYTES: usize = bitmask_len(LANES);

fn from_bitmask_array(bitmask: [u8; Self::BYTES]) -> Self {
Mask(mask_impl::Mask::from_bitmask_array(bitmask))
}
}
)*
fn to_bitmask_array(self) -> [u8; Self::BYTES] {
self.0.to_bitmask_array()
}
}

// FIXME this should be specified generically, but it doesn't seem to work with rustc, yet
impl_array_bitmask! {
impl ToBitMaskArray<[u8; _]> for Mask<_, 1>
impl ToBitMaskArray<[u8; _]> for Mask<_, 2>
impl ToBitMaskArray<[u8; _]> for Mask<_, 4>
impl ToBitMaskArray<[u8; _]> for Mask<_, 8>
impl ToBitMaskArray<[u8; _]> for Mask<_, 16>
impl ToBitMaskArray<[u8; _]> for Mask<_, 32>
impl ToBitMaskArray<[u8; _]> for Mask<_, 64>
fn from_bitmask_array(bitmask: [u8; Self::BYTES]) -> Self {
Mask(mask_impl::Mask::from_bitmask_array(bitmask))
}
}

0 comments on commit 30e85c8

Please sign in to comment.