Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 0ad68db

Browse files
committed
Use u8xN for bitmasks
1 parent 082e3c8 commit 0ad68db

File tree

3 files changed

+14
-56
lines changed

3 files changed

+14
-56
lines changed

crates/core_simd/src/masks.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ where
286286
/// The remaining bits are unset.
287287
#[inline]
288288
#[must_use = "method returns a new integer and does not mutate the original value"]
289-
pub fn to_bitmask_vector(self) -> Simd<T, N> {
289+
pub fn to_bitmask_vector(self) -> Simd<u8, N> {
290290
self.0.to_bitmask_vector()
291291
}
292292

@@ -295,7 +295,7 @@ where
295295
/// For each bit, if it is set, the corresponding element in the mask is set to `true`.
296296
#[inline]
297297
#[must_use = "method returns a new mask and does not mutate the original value"]
298-
pub fn from_bitmask_vector(bitmask: Simd<T, N>) -> Self {
298+
pub fn from_bitmask_vector(bitmask: Simd<u8, N>) -> Self {
299299
Self(mask_impl::Mask::from_bitmask_vector(bitmask))
300300
}
301301
}

crates/core_simd/src/masks/bitmask.rs

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -121,45 +121,18 @@ where
121121

122122
#[inline]
123123
#[must_use = "method returns a new vector and does not mutate the original value"]
124-
pub fn to_bitmask_vector(self) -> Simd<T, N> {
125-
let mut bitmask = Self::splat(false).to_int();
126-
127-
assert!(
128-
core::mem::size_of::<Simd<T, N>>()
129-
>= core::mem::size_of::<<LaneCount<N> as SupportedLaneCount>::BitMask>()
130-
);
131-
132-
// Safety: the bitmask vector is big enough to hold the bitmask
133-
unsafe {
134-
core::ptr::copy_nonoverlapping(
135-
self.0.as_ref().as_ptr(),
136-
bitmask.as_mut_array().as_mut_ptr() as _,
137-
self.0.as_ref().len(),
138-
);
139-
}
140-
124+
pub fn to_bitmask_vector(self) -> Simd<u8, N> {
125+
let mut bitmask = Simd::splat(0);
126+
bitmask.as_mut_array()[..self.0.as_ref().len()].copy_from_slice(self.0.as_ref());
141127
bitmask
142128
}
143129

144130
#[inline]
145131
#[must_use = "method returns a new mask and does not mutate the original value"]
146-
pub fn from_bitmask_vector(bitmask: Simd<T, N>) -> Self {
132+
pub fn from_bitmask_vector(bitmask: Simd<u8, N>) -> Self {
147133
let mut bytes = <LaneCount<N> as SupportedLaneCount>::BitMask::default();
148-
149-
assert!(
150-
core::mem::size_of::<Simd<T, N>>()
151-
>= core::mem::size_of::<<LaneCount<N> as SupportedLaneCount>::BitMask>()
152-
);
153-
154-
// Safety: the bitmask vector is big enough to hold the bitmask
155-
unsafe {
156-
core::ptr::copy_nonoverlapping(
157-
bitmask.as_array().as_ptr() as _,
158-
bytes.as_mut().as_mut_ptr(),
159-
bytes.as_ref().len(),
160-
);
161-
}
162-
134+
let len = bytes.as_ref().len();
135+
bytes.as_mut().copy_from_slice(&bitmask.as_array()[..len]);
163136
Self(bytes, PhantomData)
164137
}
165138

crates/core_simd/src/masks/full_masks.rs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ where
143143

144144
#[inline]
145145
#[must_use = "method returns a new vector and does not mutate the original value"]
146-
pub fn to_bitmask_vector(self) -> Simd<T, N> {
147-
let mut bitmask = Self::splat(false).to_int();
146+
pub fn to_bitmask_vector(self) -> Simd<u8, N> {
147+
let mut bitmask = Simd::splat(0);
148148

149149
// Safety: Bytes is the right size array
150150
unsafe {
@@ -159,36 +159,21 @@ where
159159
}
160160
}
161161

162-
assert!(
163-
core::mem::size_of::<Simd<T, N>>()
164-
>= core::mem::size_of::<<LaneCount<N> as SupportedLaneCount>::BitMask>()
165-
);
166-
core::ptr::copy_nonoverlapping(
167-
bytes.as_ref().as_ptr(),
168-
bitmask.as_mut_array().as_mut_ptr() as _,
169-
bytes.as_ref().len(),
170-
);
162+
bitmask.as_mut_array()[..bytes.as_ref().len()].copy_from_slice(bytes.as_ref());
171163
}
172164

173165
bitmask
174166
}
175167

176168
#[inline]
177169
#[must_use = "method returns a new mask and does not mutate the original value"]
178-
pub fn from_bitmask_vector(bitmask: Simd<T, N>) -> Self {
170+
pub fn from_bitmask_vector(bitmask: Simd<u8, N>) -> Self {
179171
let mut bytes = <LaneCount<N> as SupportedLaneCount>::BitMask::default();
180172

181173
// Safety: Bytes is the right size array
182174
unsafe {
183-
assert!(
184-
core::mem::size_of::<Simd<T, N>>()
185-
>= core::mem::size_of::<<LaneCount<N> as SupportedLaneCount>::BitMask>()
186-
);
187-
core::ptr::copy_nonoverlapping(
188-
bitmask.as_array().as_ptr() as _,
189-
bytes.as_mut().as_mut_ptr(),
190-
bytes.as_mut().len(),
191-
);
175+
let len = bytes.as_ref().len();
176+
bytes.as_mut().copy_from_slice(&bitmask.as_array()[..len]);
192177

193178
// LLVM assumes bit order should match endianness
194179
if cfg!(target_endian = "big") {

0 commit comments

Comments
 (0)