Skip to content

Commit e0e9a45

Browse files
Merge pull request rust-lang#377 from rust-lang/bitmask-followup
Follow-up fixes for to_bitmask
2 parents 64ea088 + 5739caa commit e0e9a45

File tree

2 files changed

+47
-50
lines changed

2 files changed

+47
-50
lines changed

crates/core_simd/src/masks.rs

+23
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,16 @@ where
295295
///
296296
/// Each bit is set if the corresponding element in the mask is `true`.
297297
/// The remaining bits are unset.
298+
///
299+
/// The bits are packed into the first N bits of the vector:
300+
/// ```
301+
/// # #![feature(portable_simd)]
302+
/// # #[cfg(feature = "as_crate")] use core_simd::simd;
303+
/// # #[cfg(not(feature = "as_crate"))] use core::simd;
304+
/// # use simd::mask32x8;
305+
/// let mask = mask32x8::from_array([true, false, true, false, false, false, true, false]);
306+
/// assert_eq!(mask.to_bitmask_vector()[0], 0b01000101);
307+
/// ```
298308
#[inline]
299309
#[must_use = "method returns a new integer and does not mutate the original value"]
300310
pub fn to_bitmask_vector(self) -> Simd<u8, N> {
@@ -304,6 +314,19 @@ where
304314
/// Create a mask from a bitmask vector.
305315
///
306316
/// For each bit, if it is set, the corresponding element in the mask is set to `true`.
317+
///
318+
/// The bits are packed into the first N bits of the vector:
319+
/// ```
320+
/// # #![feature(portable_simd)]
321+
/// # #[cfg(feature = "as_crate")] use core_simd::simd;
322+
/// # #[cfg(not(feature = "as_crate"))] use core::simd;
323+
/// # use simd::{mask32x8, u8x8};
324+
/// let bitmask = u8x8::from_array([0b01000101, 0, 0, 0, 0, 0, 0, 0]);
325+
/// assert_eq!(
326+
/// mask32x8::from_bitmask_vector(bitmask),
327+
/// mask32x8::from_array([true, false, true, false, false, false, true, false]),
328+
/// );
329+
/// ```
307330
#[inline]
308331
#[must_use = "method returns a new mask and does not mutate the original value"]
309332
pub fn from_bitmask_vector(bitmask: Simd<u8, N>) -> Self {

crates/core_simd/src/masks/full_masks.rs

+24-50
Original file line numberDiff line numberDiff line change
@@ -237,62 +237,36 @@ where
237237
#[inline]
238238
pub(crate) fn to_bitmask_integer(self) -> u64 {
239239
// TODO modify simd_bitmask to zero-extend output, making this unnecessary
240-
macro_rules! bitmask {
241-
{ $($ty:ty: $($len:literal),*;)* } => {
242-
match N {
243-
$($(
244-
// Safety: bitmask matches length
245-
$len => unsafe { self.to_bitmask_impl::<$ty, $len>() as u64 },
246-
)*)*
247-
// Safety: bitmask matches length
248-
_ => unsafe { self.to_bitmask_impl::<u64, 64>() },
249-
}
250-
}
251-
}
252-
#[cfg(all_lane_counts)]
253-
bitmask! {
254-
u8: 1, 2, 3, 4, 5, 6, 7, 8;
255-
u16: 9, 10, 11, 12, 13, 14, 15, 16;
256-
u32: 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32;
257-
u64: 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64;
258-
}
259-
#[cfg(not(all_lane_counts))]
260-
bitmask! {
261-
u8: 1, 2, 4, 8;
262-
u16: 16;
263-
u32: 32;
264-
u64: 64;
240+
if N <= 8 {
241+
// Safety: bitmask matches length
242+
unsafe { self.to_bitmask_impl::<u8, 8>() as u64 }
243+
} else if N <= 16 {
244+
// Safety: bitmask matches length
245+
unsafe { self.to_bitmask_impl::<u16, 16>() as u64 }
246+
} else if N <= 32 {
247+
// Safety: bitmask matches length
248+
unsafe { self.to_bitmask_impl::<u32, 32>() as u64 }
249+
} else {
250+
// Safety: bitmask matches length
251+
unsafe { self.to_bitmask_impl::<u64, 64>() }
265252
}
266253
}
267254

268255
#[inline]
269256
pub(crate) fn from_bitmask_integer(bitmask: u64) -> Self {
270257
// TODO modify simd_bitmask_select to truncate input, making this unnecessary
271-
macro_rules! bitmask {
272-
{ $($ty:ty: $($len:literal),*;)* } => {
273-
match N {
274-
$($(
275-
// Safety: bitmask matches length
276-
$len => unsafe { Self::from_bitmask_impl::<$ty, $len>(bitmask as $ty) },
277-
)*)*
278-
// Safety: bitmask matches length
279-
_ => unsafe { Self::from_bitmask_impl::<u64, 64>(bitmask) },
280-
}
281-
}
282-
}
283-
#[cfg(all_lane_counts)]
284-
bitmask! {
285-
u8: 1, 2, 3, 4, 5, 6, 7, 8;
286-
u16: 9, 10, 11, 12, 13, 14, 15, 16;
287-
u32: 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32;
288-
u64: 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64;
289-
}
290-
#[cfg(not(all_lane_counts))]
291-
bitmask! {
292-
u8: 1, 2, 4, 8;
293-
u16: 16;
294-
u32: 32;
295-
u64: 64;
258+
if N <= 8 {
259+
// Safety: bitmask matches length
260+
unsafe { Self::from_bitmask_impl::<u8, 8>(bitmask as u8) }
261+
} else if N <= 16 {
262+
// Safety: bitmask matches length
263+
unsafe { Self::from_bitmask_impl::<u16, 16>(bitmask as u16) }
264+
} else if N <= 32 {
265+
// Safety: bitmask matches length
266+
unsafe { Self::from_bitmask_impl::<u32, 32>(bitmask as u32) }
267+
} else {
268+
// Safety: bitmask matches length
269+
unsafe { Self::from_bitmask_impl::<u64, 64>(bitmask) }
296270
}
297271
}
298272

0 commit comments

Comments
 (0)