Skip to content

Commit

Permalink
Merge pull request rust-lang#251 from rust-lang/mask-cast
Browse files Browse the repository at this point in the history
Add Mask::cast
  • Loading branch information
calebzulawski authored May 21, 2022
2 parents af53b5d + c9f4e0e commit 939914e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
9 changes: 8 additions & 1 deletion crates/core_simd/src/masks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ where
self.0.to_int()
}

/// Converts the mask to a mask of any other lane size.
#[inline]
#[must_use = "method returns a new mask and does not mutate the original value"]
pub fn cast<U: MaskElement>(self) -> Mask<U, LANES> {
Mask(self.0.convert())
}

/// Tests the value of the specified lane.
///
/// # Safety
Expand Down Expand Up @@ -571,7 +578,7 @@ macro_rules! impl_from {
LaneCount<LANES>: SupportedLaneCount,
{
fn from(value: Mask<$from, LANES>) -> Self {
Self(value.0.convert())
value.cast()
}
}
)*
Expand Down
23 changes: 23 additions & 0 deletions crates/core_simd/tests/masks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,29 @@ macro_rules! test_mask_api {
assert_eq!(bitmask, 0b01);
assert_eq!(core_simd::Mask::<$type, 2>::from_bitmask(bitmask), mask);
}

#[test]
fn cast() {
fn cast_impl<T: core_simd::MaskElement>()
where
core_simd::Mask<$type, 8>: Into<core_simd::Mask<T, 8>>,
{
let values = [true, false, false, true, false, false, true, false];
let mask = core_simd::Mask::<$type, 8>::from_array(values);

let cast_mask = mask.cast::<T>();
assert_eq!(values, cast_mask.to_array());

let into_mask: core_simd::Mask<T, 8> = mask.into();
assert_eq!(values, into_mask.to_array());
}

cast_impl::<i8>();
cast_impl::<i16>();
cast_impl::<i32>();
cast_impl::<i64>();
cast_impl::<isize>();
}
}
}
}
Expand Down

0 comments on commit 939914e

Please sign in to comment.