Skip to content

Commit 7b697ff

Browse files
committed
Remove bitmask requirement on generic_const_exprs feature
1 parent c5efbf8 commit 7b697ff

File tree

5 files changed

+25
-24
lines changed

5 files changed

+25
-24
lines changed

crates/core_simd/src/masks.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ mod mask_impl;
1515
mod to_bitmask;
1616
pub use to_bitmask::{ToBitMask, ToBitMaskArray};
1717

18-
#[cfg(feature = "generic_const_exprs")]
19-
pub use to_bitmask::bitmask_len;
20-
2118
use crate::simd::{intrinsics, LaneCount, Simd, SimdElement, SupportedLaneCount};
2219
use core::cmp::Ordering;
2320
use core::{fmt, mem};

crates/core_simd/src/masks/bitmask.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,29 +115,31 @@ where
115115
unsafe { Self(intrinsics::simd_bitmask(value), PhantomData) }
116116
}
117117

118-
#[cfg(feature = "generic_const_exprs")]
118+
// Safety: N must be the exact number of bytes required to hold the bitmask for this mask
119119
#[inline]
120120
#[must_use = "method returns a new array and does not mutate the original value"]
121-
pub fn to_bitmask(self) -> [u8; super::bitmask_len(LANES)] {
121+
pub unsafe fn to_bitmask_array<const N: usize>(self) -> [u8; N] {
122122
// Safety: these are the same type and we are laundering the generic
123123
unsafe { core::mem::transmute_copy(&self.0) }
124124
}
125125

126-
#[cfg(feature = "generic_const_exprs")]
126+
// Safety: N must be the exact number of bytes required to hold the bitmask for this mask
127127
#[inline]
128128
#[must_use = "method returns a new mask and does not mutate the original value"]
129-
pub fn from_bitmask(bitmask: [u8; super::bitmask_len(LANES)]) -> Self {
129+
pub unsafe fn from_bitmask_array<const N: usize>(bitmask: [u8; N]) -> Self {
130130
// Safety: these are the same type and we are laundering the generic
131131
Self(unsafe { core::mem::transmute_copy(&bitmask) }, PhantomData)
132132
}
133133

134+
// Safety: U must be the integer with the exact number of bits required to hold the bitmask for
134135
#[inline]
135-
pub unsafe fn to_bitmask_intrinsic<U>(self) -> U {
136+
pub unsafe fn to_bitmask_integer<U>(self) -> U {
136137
unsafe { core::mem::transmute_copy(&self.0) }
137138
}
138139

140+
// Safety: U must be the integer with the exact number of bits required to hold the bitmask for
139141
#[inline]
140-
pub unsafe fn from_bitmask_intrinsic<U>(bitmask: U) -> Self {
142+
pub unsafe fn from_bitmask_integer<U>(bitmask: U) -> Self {
141143
unsafe { Self(core::mem::transmute_copy(&bitmask), PhantomData) }
142144
}
143145

crates/core_simd/src/masks/full_masks.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ where
109109
unsafe { Mask(intrinsics::simd_cast(self.0)) }
110110
}
111111

112-
#[cfg(feature = "generic_const_exprs")]
112+
// Safety: N must be the exact number of bytes required to hold the bitmask for this mask
113113
#[inline]
114114
#[must_use = "method returns a new array and does not mutate the original value"]
115-
pub fn to_bitmask(self) -> [u8; super::bitmask_len(LANES)] {
115+
pub unsafe fn to_bitmask_array<const N: usize>(self) -> [u8; N] {
116116
unsafe {
117-
let mut bitmask: [u8; super::bitmask_len(LANES)] = intrinsics::simd_bitmask(self.0);
117+
let mut bitmask: [u8; N] = intrinsics::simd_bitmask(self.0);
118118

119119
// There is a bug where LLVM appears to implement this operation with the wrong
120120
// bit order.
@@ -129,10 +129,10 @@ where
129129
}
130130
}
131131

132-
#[cfg(feature = "generic_const_exprs")]
132+
// Safety: N must be the exact number of bytes required to hold the bitmask for this mask
133133
#[inline]
134134
#[must_use = "method returns a new mask and does not mutate the original value"]
135-
pub fn from_bitmask(mut bitmask: [u8; super::bitmask_len(LANES)]) -> Self {
135+
pub unsafe fn from_bitmask_array<const N: usize>(mut bitmask: [u8; N]) -> Self {
136136
unsafe {
137137
// There is a bug where LLVM appears to implement this operation with the wrong
138138
// bit order.
@@ -151,14 +151,18 @@ where
151151
}
152152
}
153153

154+
// Safety: U must be the integer with the exact number of bits required to hold the bitmask for
155+
// this mask
154156
#[inline]
155-
pub unsafe fn to_bitmask_intrinsic<U>(self) -> U {
157+
pub unsafe fn to_bitmask_integer<U>(self) -> U {
156158
// Safety: caller must only return bitmask types
157159
unsafe { intrinsics::simd_bitmask(self.0) }
158160
}
159161

162+
// Safety: U must be the integer with the exact number of bits required to hold the bitmask for
163+
// this mask
160164
#[inline]
161-
pub unsafe fn from_bitmask_intrinsic<U>(bitmask: U) -> Self {
165+
pub unsafe fn from_bitmask_integer<U>(bitmask: U) -> Self {
162166
// Safety: caller must only pass bitmask types
163167
unsafe {
164168
Self::from_int_unchecked(intrinsics::simd_select_bitmask(

crates/core_simd/src/masks/to_bitmask.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ macro_rules! impl_integer_intrinsic {
3535
type BitMask = $int;
3636

3737
fn to_bitmask(self) -> $int {
38-
unsafe { self.0.to_bitmask_intrinsic() }
38+
unsafe { self.0.to_bitmask_integer() }
3939
}
4040

4141
fn from_bitmask(bitmask: $int) -> Self {
42-
unsafe { Self(mask_impl::Mask::from_bitmask_intrinsic(bitmask)) }
42+
unsafe { Self(mask_impl::Mask::from_bitmask_integer(bitmask)) }
4343
}
4444
}
4545
)*
@@ -54,12 +54,10 @@ impl_integer_intrinsic! {
5454
}
5555

5656
/// Returns the minimum numnber of bytes in a bitmask with `lanes` lanes.
57-
#[cfg(feature = "generic_const_exprs")]
5857
pub const fn bitmask_len(lanes: usize) -> usize {
5958
(lanes + 7) / 8
6059
}
6160

62-
#[cfg(feature = "generic_const_exprs")]
6361
macro_rules! impl_array_bitmask {
6462
{ $(impl ToBitMask<[u8; _]> for Mask<_, $lanes:literal>)* } => {
6563
$(
@@ -68,19 +66,20 @@ macro_rules! impl_array_bitmask {
6866
const BYTES: usize = bitmask_len($lanes);
6967

7068
fn to_bitmask_array(self) -> [u8; Self::BYTES] {
71-
self.0.to_bitmask()
69+
// Safety: BYTES is the exact size required
70+
unsafe { self.0.to_bitmask_array() }
7271
}
7372

7473
fn from_bitmask_array(bitmask: [u8; Self::BYTES]) -> Self {
75-
Mask(mask_impl::Mask::from_bitmask(bitmask))
74+
// Safety: BYTES is the exact size required
75+
unsafe { Mask(mask_impl::Mask::from_bitmask_array(bitmask)) }
7676
}
7777
}
7878
)*
7979
}
8080
}
8181

8282
// FIXME this should be specified generically, but it doesn't seem to work with rustc, yet
83-
#[cfg(feature = "generic_const_exprs")]
8483
impl_array_bitmask! {
8584
impl ToBitMask<[u8; _]> for Mask<_, 1>
8685
impl ToBitMask<[u8; _]> for Mask<_, 2>

crates/core_simd/tests/masks.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ macro_rules! test_mask_api {
8181
assert_eq!(core_simd::Mask::<$type, 16>::from_bitmask(bitmask), mask);
8282
}
8383

84-
#[cfg(feature = "generic_const_exprs")]
8584
#[test]
8685
fn roundtrip_bitmask_array_conversion() {
8786
use core_simd::ToBitMaskArray;

0 commit comments

Comments
 (0)