Merge pull request #251 from rust-lang/mask-cast

Add Mask::cast
This commit is contained in:
Caleb Zulawski 2022-05-21 17:49:35 -04:00 committed by GitHub
commit 939914e39d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 1 deletions

View File

@ -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
@ -571,7 +578,7 @@ macro_rules! impl_from {
LaneCount<LANES>: SupportedLaneCount,
{
fn from(value: Mask<$from, LANES>) -> Self {
Self(value.0.convert())
value.cast()
}
}
)*

View File

@ -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>();
}
}
}
}