Skip to content

Commit b7fea94

Browse files
committed
Generically implement ToBitMaskArray
1 parent 939914e commit b7fea94

File tree

5 files changed

+141
-3
lines changed

5 files changed

+141
-3
lines changed

crates/core_simd/src/masks.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
mod mask_impl;
1414

1515
mod to_bitmask;
16-
pub use to_bitmask::ToBitMask;
16+
pub use to_bitmask::{ToBitMask, ToBitMaskArray};
17+
18+
#[cfg(feature = "generic_const_exprs")]
19+
pub use to_bitmask::bitmask_len;
1720

1821
use crate::simd::{intrinsics, LaneCount, Simd, SimdElement, SimdPartialEq, SupportedLaneCount};
1922
use core::cmp::Ordering;

crates/core_simd/src/masks/bitmask.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(unused_imports)]
22
use super::MaskElement;
33
use crate::simd::intrinsics;
4-
use crate::simd::{LaneCount, Simd, SupportedLaneCount, ToBitMask};
4+
use crate::simd::{LaneCount, Simd, SupportedLaneCount, ToBitMask, ToBitMaskArray};
55
use core::marker::PhantomData;
66

77
/// A mask where each lane is represented by a single bit.
@@ -115,6 +115,24 @@ where
115115
unsafe { Self(intrinsics::simd_bitmask(value), PhantomData) }
116116
}
117117

118+
#[inline]
119+
#[must_use = "method returns a new array and does not mutate the original value"]
120+
pub fn to_bitmask_array<const N: usize>(self) -> [u8; N] {
121+
assert!(core::mem::size_of::<Self>() == N);
122+
123+
// Safety: converting an integer to an array of bytes of the same size is safe
124+
unsafe { core::mem::transmute_copy(&self.0) }
125+
}
126+
127+
#[inline]
128+
#[must_use = "method returns a new mask and does not mutate the original value"]
129+
pub fn from_bitmask_array<const N: usize>(bitmask: [u8; N]) -> Self {
130+
assert!(core::mem::size_of::<Self>() == N);
131+
132+
// Safety: converting an array of bytes to an integer of the same size is safe
133+
Self(unsafe { core::mem::transmute_copy(&bitmask) }, PhantomData)
134+
}
135+
118136
#[inline]
119137
pub fn to_bitmask_integer<U>(self) -> U
120138
where

crates/core_simd/src/masks/full_masks.rs

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use super::MaskElement;
44
use crate::simd::intrinsics;
5-
use crate::simd::{LaneCount, Simd, SupportedLaneCount, ToBitMask};
5+
use crate::simd::{LaneCount, Simd, SupportedLaneCount, ToBitMask, ToBitMaskArray};
66

77
#[repr(transparent)]
88
pub struct Mask<T, const LANES: usize>(Simd<T, LANES>)
@@ -139,6 +139,72 @@ where
139139
unsafe { Mask(intrinsics::simd_cast(self.0)) }
140140
}
141141

142+
#[inline]
143+
#[must_use = "method returns a new array and does not mutate the original value"]
144+
pub fn to_bitmask_array<const N: usize>(self) -> [u8; N]
145+
where
146+
super::Mask<T, LANES>: ToBitMaskArray,
147+
[(); <super::Mask<T, LANES> as ToBitMaskArray>::BYTES]: Sized,
148+
{
149+
assert_eq!(<super::Mask<T, LANES> as ToBitMaskArray>::BYTES, N);
150+
151+
// Safety: N is the correct bitmask size
152+
//
153+
// The transmute below allows this function to be marked safe, since it will prevent
154+
// monomorphization errors in the case of an incorrect size.
155+
unsafe {
156+
// Compute the bitmask
157+
let bitmask: [u8; <super::Mask<T, LANES> as ToBitMaskArray>::BYTES] =
158+
intrinsics::simd_bitmask(self.0);
159+
160+
// Transmute to the return type, previously asserted to be the same size
161+
let mut bitmask: [u8; N] = core::mem::transmute_copy(&bitmask);
162+
163+
// LLVM assumes bit order should match endianness
164+
if cfg!(target_endian = "big") {
165+
for x in bitmask.as_mut() {
166+
*x = x.reverse_bits();
167+
}
168+
};
169+
170+
bitmask
171+
}
172+
}
173+
174+
#[inline]
175+
#[must_use = "method returns a new mask and does not mutate the original value"]
176+
pub fn from_bitmask_array<const N: usize>(mut bitmask: [u8; N]) -> Self
177+
where
178+
super::Mask<T, LANES>: ToBitMaskArray,
179+
[(); <super::Mask<T, LANES> as ToBitMaskArray>::BYTES]: Sized,
180+
{
181+
assert_eq!(<super::Mask<T, LANES> as ToBitMaskArray>::BYTES, N);
182+
183+
// Safety: N is the correct bitmask size
184+
//
185+
// The transmute below allows this function to be marked safe, since it will prevent
186+
// monomorphization errors in the case of an incorrect size.
187+
unsafe {
188+
// LLVM assumes bit order should match endianness
189+
if cfg!(target_endian = "big") {
190+
for x in bitmask.as_mut() {
191+
*x = x.reverse_bits();
192+
}
193+
}
194+
195+
// Transmute to the bitmask type, previously asserted to be the same size
196+
let bitmask: [u8; <super::Mask<T, LANES> as ToBitMaskArray>::BYTES] =
197+
core::mem::transmute_copy(&bitmask);
198+
199+
// Compute the regular mask
200+
Self::from_int_unchecked(intrinsics::simd_select_bitmask(
201+
bitmask,
202+
Self::splat(true).to_int(),
203+
Self::splat(false).to_int(),
204+
))
205+
}
206+
}
207+
142208
#[inline]
143209
pub(crate) fn to_bitmask_integer<U: ReverseBits>(self) -> U
144210
where

crates/core_simd/src/masks/to_bitmask.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@ pub unsafe trait ToBitMask: Sealed {
3131
fn from_bitmask(bitmask: Self::BitMask) -> Self;
3232
}
3333

34+
/// Converts masks to and from byte array bitmasks.
35+
///
36+
/// Each bit of the bitmask corresponds to a mask lane, starting with the LSB of the first byte.
37+
///
38+
/// # Safety
39+
/// This trait is `unsafe` and sealed, since the `BYTES` value must match the number of lanes in
40+
/// the mask.
41+
pub unsafe trait ToBitMaskArray: Sealed {
42+
/// The length of the bitmask array.
43+
const BYTES: usize;
44+
45+
/// Converts a mask to a bitmask.
46+
fn to_bitmask_array(self) -> [u8; Self::BYTES];
47+
48+
/// Converts a bitmask to a mask.
49+
fn from_bitmask_array(bitmask: [u8; Self::BYTES]) -> Self;
50+
}
51+
3452
macro_rules! impl_integer_intrinsic {
3553
{ $(unsafe impl ToBitMask<BitMask=$int:ty> for Mask<_, $lanes:literal>)* } => {
3654
$(
@@ -58,3 +76,23 @@ impl_integer_intrinsic! {
5876
unsafe impl ToBitMask<BitMask=u32> for Mask<_, 32>
5977
unsafe impl ToBitMask<BitMask=u64> for Mask<_, 64>
6078
}
79+
80+
/// Returns the minimum numnber of bytes in a bitmask with `lanes` lanes.
81+
pub const fn bitmask_len(lanes: usize) -> usize {
82+
(lanes + 7) / 8
83+
}
84+
85+
unsafe impl<T: MaskElement, const LANES: usize> ToBitMaskArray for Mask<T, LANES>
86+
where
87+
LaneCount<LANES>: SupportedLaneCount,
88+
{
89+
const BYTES: usize = bitmask_len(LANES);
90+
91+
fn to_bitmask_array(self) -> [u8; Self::BYTES] {
92+
self.0.to_bitmask_array()
93+
}
94+
95+
fn from_bitmask_array(bitmask: [u8; Self::BYTES]) -> Self {
96+
Mask(mask_impl::Mask::from_bitmask_array(bitmask))
97+
}
98+
}

crates/core_simd/tests/masks.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,19 @@ macro_rules! test_mask_api {
122122
cast_impl::<i64>();
123123
cast_impl::<isize>();
124124
}
125+
126+
#[test]
127+
fn roundtrip_bitmask_array_conversion() {
128+
use core_simd::ToBitMaskArray;
129+
let values = [
130+
true, false, false, true, false, false, true, false,
131+
true, true, false, false, false, false, false, true,
132+
];
133+
let mask = core_simd::Mask::<$type, 16>::from_array(values);
134+
let bitmask = mask.to_bitmask_array();
135+
assert_eq!(bitmask, [0b01001001, 0b10000011]);
136+
assert_eq!(core_simd::Mask::<$type, 16>::from_bitmask_array(bitmask), mask);
137+
}
125138
}
126139
}
127140
}

0 commit comments

Comments
 (0)