Skip to content

Commit

Permalink
Manually implement for supported lanes
Browse files Browse the repository at this point in the history
  • Loading branch information
calebzulawski committed Feb 6, 2022
1 parent ff1d45a commit 6f9258d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
3 changes: 1 addition & 2 deletions crates/core_simd/src/masks/full_masks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ where
#[must_use = "method returns a new array and does not mutate the original value"]
pub fn to_bitmask(self) -> [u8; super::bitmask_len(LANES)] {
unsafe {
let mut bitmask: [u8; super::bitmask_len(LANES)] =
intrinsics::simd_bitmask(self.0);
let mut bitmask: [u8; super::bitmask_len(LANES)] = intrinsics::simd_bitmask(self.0);

// There is a bug where LLVM appears to implement this operation with the wrong
// bit order.
Expand Down
34 changes: 25 additions & 9 deletions crates/core_simd/src/masks/to_bitmask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,31 @@ pub const fn bitmask_len(lanes: usize) -> usize {
}

#[cfg(feature = "generic_const_exprs")]
impl<T: MaskElement, const LANES: usize> ToBitMask<[u8; bitmask_len(LANES)]> for Mask<T, LANES>
where
LaneCount<LANES>: SupportedLaneCount,
{
fn to_bitmask(self) -> [u8; bitmask_len(LANES)] {
self.0.to_bitmask()
}
macro_rules! impl_array_bitmask {
{ $(impl ToBitMask<[u8; _]> for Mask<_, $lanes:literal>)* } => {
$(
impl<T: MaskElement> ToBitMask<[u8; bitmask_len($lanes)]> for Mask<T, $lanes>
{
fn to_bitmask(self) -> [u8; bitmask_len($lanes)] {
self.0.to_bitmask()
}

fn from_bitmask(bitmask: [u8; bitmask_len(LANES)]) -> Self {
Mask(mask_impl::Mask::from_bitmask(bitmask))
fn from_bitmask(bitmask: [u8; bitmask_len($lanes)]) -> Self {
Mask(mask_impl::Mask::from_bitmask(bitmask))
}
}
)*
}
}

// FIXME this should be specified generically, but it doesn't seem to work with rustc, yet
#[cfg(feature = "generic_const_exprs")]
impl_array_bitmask! {
impl ToBitMask<[u8; _]> for Mask<_, 1>
impl ToBitMask<[u8; _]> for Mask<_, 2>
impl ToBitMask<[u8; _]> for Mask<_, 4>
impl ToBitMask<[u8; _]> for Mask<_, 8>
impl ToBitMask<[u8; _]> for Mask<_, 16>
impl ToBitMask<[u8; _]> for Mask<_, 32>
impl ToBitMask<[u8; _]> for Mask<_, 64>
}
2 changes: 0 additions & 2 deletions crates/core_simd/tests/masks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ macro_rules! test_mask_api {
assert_eq!(core_simd::Mask::<$type, 16>::from_bitmask(bitmask), mask);
}

/*
#[cfg(feature = "generic_const_exprs")]
#[test]
fn roundtrip_bitmask_array_conversion() {
Expand All @@ -95,7 +94,6 @@ macro_rules! test_mask_api {
assert_eq!(bitmask, [0b01001001, 0b10000011]);
assert_eq!(core_simd::Mask::<$type, 16>::from_bitmask(bitmask), mask);
}
*/
}
}
}
Expand Down

0 comments on commit 6f9258d

Please sign in to comment.