Skip to content

Commit cca9102

Browse files
committed
Change bitmasks to use less opaque type
1 parent 9ab0507 commit cca9102

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

crates/core_simd/src/lane_count.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@ use sealed::Sealed;
66
/// A type representing a vector lane count.
77
pub struct LaneCount<const LANES: usize>;
88

9+
impl<const LANES: usize> LaneCount<LANES> {
10+
/// The number of bytes in a bitmask with this many lanes.
11+
pub const BITMASK_LEN: usize = (LANES + 7) / 8;
12+
}
13+
914
/// Helper trait for vector lane counts.
1015
pub trait SupportedLaneCount: Sealed {
11-
/// The bitmask representation of a mask.
16+
#[doc(hidden)]
1217
type BitMask: Copy + Default + AsRef<[u8]> + AsMut<[u8]>;
1318

1419
#[doc(hidden)]

crates/core_simd/src/masks.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,12 @@ macro_rules! define_opaque_mask {
160160
}
161161

162162
/// Convert this mask to a bitmask, with one bit set per lane.
163-
pub fn to_bitmask(self) -> <crate::LaneCount<LANES> as crate::SupportedLaneCount>::BitMask {
163+
pub fn to_bitmask(self) -> [u8; crate::LaneCount::<LANES>::BITMASK_LEN] {
164164
self.0.to_bitmask()
165165
}
166166

167167
/// Convert a bitmask to a mask.
168-
pub fn from_bitmask(bitmask: <crate::LaneCount<LANES> as crate::SupportedLaneCount>::BitMask) -> Self {
168+
pub fn from_bitmask(bitmask: [u8; crate::LaneCount::<LANES>::BITMASK_LEN]) -> Self {
169169
Self(<$inner_ty>::from_bitmask(bitmask))
170170
}
171171
}

crates/core_simd/src/masks/bitmask.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,15 @@ where
128128
}
129129

130130
#[inline]
131-
pub fn to_bitmask(self) -> <LaneCount<LANES> as SupportedLaneCount>::BitMask {
132-
self.0
131+
pub fn to_bitmask(self) -> [u8; LaneCount::<LANES>::BITMASK_LEN] {
132+
// Safety: these are the same type and we are laundering the generic
133+
unsafe { core::mem::transmute_copy(&self.0) }
133134
}
134135

135136
#[inline]
136-
pub fn from_bitmask(bitmask: <LaneCount<LANES> as SupportedLaneCount>::BitMask) -> Self {
137-
Self(bitmask)
137+
pub fn from_bitmask(bitmask: [u8; LaneCount::<LANES>::BITMASK_LEN]) -> Self {
138+
// Safety: these are the same type and we are laundering the generic
139+
Self(unsafe { core::mem::transmute_copy(&bitmask) })
138140
}
139141

140142
#[inline]

crates/core_simd/src/masks/full_masks.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,15 @@ macro_rules! define_mask {
103103
}
104104

105105
#[inline]
106-
pub fn to_bitmask(self) -> <crate::LaneCount::<LANES> as crate::SupportedLaneCount>::BitMask {
106+
pub fn to_bitmask(self) -> [u8; crate::LaneCount::<LANES>::BITMASK_LEN] {
107107
unsafe {
108108
// TODO remove the transmute when rustc can use arrays of u8 as bitmasks
109109
assert_eq!(
110-
core::mem::size_of::<<crate::LaneCount::<LANES> as crate::SupportedLaneCount>::BitMask>(),
111110
core::mem::size_of::<<crate::LaneCount::<LANES> as crate::SupportedLaneCount>::IntBitMask>(),
111+
crate::LaneCount::<LANES>::BITMASK_LEN,
112112
);
113113
let bitmask: <crate::LaneCount::<LANES> as crate::SupportedLaneCount>::IntBitMask = crate::intrinsics::simd_bitmask(self.0);
114-
let mut bitmask: <crate::LaneCount::<LANES> as crate::SupportedLaneCount>::BitMask = core::mem::transmute_copy(&bitmask);
114+
let mut bitmask: [u8; crate::LaneCount::<LANES>::BITMASK_LEN] = core::mem::transmute_copy(&bitmask);
115115

116116
// There is a bug where LLVM appears to implement this operation with the wrong
117117
// bit order.
@@ -127,7 +127,7 @@ macro_rules! define_mask {
127127
}
128128

129129
#[inline]
130-
pub fn from_bitmask(mut bitmask: <crate::LaneCount::<LANES> as crate::SupportedLaneCount>::BitMask) -> Self {
130+
pub fn from_bitmask(mut bitmask: [u8; crate::LaneCount::<LANES>::BITMASK_LEN]) -> Self {
131131
unsafe {
132132
// There is a bug where LLVM appears to implement this operation with the wrong
133133
// bit order.
@@ -140,8 +140,8 @@ macro_rules! define_mask {
140140

141141
// TODO remove the transmute when rustc can use arrays of u8 as bitmasks
142142
assert_eq!(
143-
core::mem::size_of::<<crate::LaneCount::<LANES> as crate::SupportedLaneCount>::BitMask>(),
144143
core::mem::size_of::<<crate::LaneCount::<LANES> as crate::SupportedLaneCount>::IntBitMask>(),
144+
crate::LaneCount::<LANES>::BITMASK_LEN,
145145
);
146146
let bitmask: <crate::LaneCount::<LANES> as crate::SupportedLaneCount>::IntBitMask = core::mem::transmute_copy(&bitmask);
147147

0 commit comments

Comments
 (0)