Skip to content

Commit 9a063bc

Browse files
Merge pull request #99 from rust-lang/feature/simplify-masks
Feature/simplify masks
2 parents 5751179 + 589fce0 commit 9a063bc

File tree

13 files changed

+443
-591
lines changed

13 files changed

+443
-591
lines changed

crates/core_simd/src/comparisons.rs

+20-25
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,59 @@
11
use crate::LanesAtMost32;
22

33
macro_rules! implement_mask_ops {
4-
{ $($vector:ident => $mask:ident ($inner_mask_ty:ident, $inner_ty:ident),)* } => {
4+
{ $($vector:ident => $mask:ident ($inner_ty:ident),)* } => {
55
$(
66
impl<const LANES: usize> crate::$vector<LANES>
77
where
88
crate::$vector<LANES>: LanesAtMost32,
99
crate::$inner_ty<LANES>: LanesAtMost32,
10+
crate::$mask<LANES>: crate::Mask,
1011
{
1112
/// Test if each lane is equal to the corresponding lane in `other`.
1213
#[inline]
1314
pub fn lanes_eq(self, other: Self) -> crate::$mask<LANES> {
1415
unsafe {
15-
crate::$inner_mask_ty::from_int_unchecked(crate::intrinsics::simd_eq(self, other))
16-
.into()
16+
crate::$mask::from_int_unchecked(crate::intrinsics::simd_eq(self, other))
1717
}
1818
}
1919

2020
/// Test if each lane is not equal to the corresponding lane in `other`.
2121
#[inline]
2222
pub fn lanes_ne(self, other: Self) -> crate::$mask<LANES> {
2323
unsafe {
24-
crate::$inner_mask_ty::from_int_unchecked(crate::intrinsics::simd_ne(self, other))
25-
.into()
24+
crate::$mask::from_int_unchecked(crate::intrinsics::simd_ne(self, other))
2625
}
2726
}
2827

2928
/// Test if each lane is less than the corresponding lane in `other`.
3029
#[inline]
3130
pub fn lanes_lt(self, other: Self) -> crate::$mask<LANES> {
3231
unsafe {
33-
crate::$inner_mask_ty::from_int_unchecked(crate::intrinsics::simd_lt(self, other))
34-
.into()
32+
crate::$mask::from_int_unchecked(crate::intrinsics::simd_lt(self, other))
3533
}
3634
}
3735

3836
/// Test if each lane is greater than the corresponding lane in `other`.
3937
#[inline]
4038
pub fn lanes_gt(self, other: Self) -> crate::$mask<LANES> {
4139
unsafe {
42-
crate::$inner_mask_ty::from_int_unchecked(crate::intrinsics::simd_gt(self, other))
43-
.into()
40+
crate::$mask::from_int_unchecked(crate::intrinsics::simd_gt(self, other))
4441
}
4542
}
4643

4744
/// Test if each lane is less than or equal to the corresponding lane in `other`.
4845
#[inline]
4946
pub fn lanes_le(self, other: Self) -> crate::$mask<LANES> {
5047
unsafe {
51-
crate::$inner_mask_ty::from_int_unchecked(crate::intrinsics::simd_le(self, other))
52-
.into()
48+
crate::$mask::from_int_unchecked(crate::intrinsics::simd_le(self, other))
5349
}
5450
}
5551

5652
/// Test if each lane is greater than or equal to the corresponding lane in `other`.
5753
#[inline]
5854
pub fn lanes_ge(self, other: Self) -> crate::$mask<LANES> {
5955
unsafe {
60-
crate::$inner_mask_ty::from_int_unchecked(crate::intrinsics::simd_ge(self, other))
61-
.into()
56+
crate::$mask::from_int_unchecked(crate::intrinsics::simd_ge(self, other))
6257
}
6358
}
6459
}
@@ -67,18 +62,18 @@ macro_rules! implement_mask_ops {
6762
}
6863

6964
implement_mask_ops! {
70-
SimdI8 => Mask8 (SimdMask8, SimdI8),
71-
SimdI16 => Mask16 (SimdMask16, SimdI16),
72-
SimdI32 => Mask32 (SimdMask32, SimdI32),
73-
SimdI64 => Mask64 (SimdMask64, SimdI64),
74-
SimdIsize => MaskSize (SimdMaskSize, SimdIsize),
65+
SimdI8 => Mask8 (SimdI8),
66+
SimdI16 => Mask16 (SimdI16),
67+
SimdI32 => Mask32 (SimdI32),
68+
SimdI64 => Mask64 (SimdI64),
69+
SimdIsize => MaskSize (SimdIsize),
7570

76-
SimdU8 => Mask8 (SimdMask8, SimdI8),
77-
SimdU16 => Mask16 (SimdMask16, SimdI16),
78-
SimdU32 => Mask32 (SimdMask32, SimdI32),
79-
SimdU64 => Mask64 (SimdMask64, SimdI64),
80-
SimdUsize => MaskSize (SimdMaskSize, SimdIsize),
71+
SimdU8 => Mask8 (SimdI8),
72+
SimdU16 => Mask16 (SimdI16),
73+
SimdU32 => Mask32 (SimdI32),
74+
SimdU64 => Mask64 (SimdI64),
75+
SimdUsize => MaskSize (SimdIsize),
8176

82-
SimdF32 => Mask32 (SimdMask32, SimdI32),
83-
SimdF64 => Mask64 (SimdMask64, SimdI64),
77+
SimdF32 => Mask32 (SimdI32),
78+
SimdF64 => Mask64 (SimdI64),
8479
}

crates/core_simd/src/intrinsics.rs

+6
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ extern "platform-intrinsic" {
7676
pub(crate) fn simd_reduce_and<T, U>(x: T) -> U;
7777
pub(crate) fn simd_reduce_or<T, U>(x: T) -> U;
7878
pub(crate) fn simd_reduce_xor<T, U>(x: T) -> U;
79+
80+
// truncate integer vector to bitmask
81+
pub(crate) fn simd_bitmask<T, U>(x: T) -> U;
82+
83+
// select
84+
pub(crate) fn simd_select_bitmask<T, U>(m: T, a: U, b: U) -> U;
7985
}
8086

8187
#[cfg(feature = "std")]

crates/core_simd/src/lanes_at_most_32.rs

+32-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,38 @@
1-
/// Implemented for bitmask sizes that are supported by the implementation.
2-
pub trait LanesAtMost32 {}
1+
/// Implemented for vectors that are supported by the implementation.
2+
pub trait LanesAtMost32: sealed::Sealed {
3+
#[doc(hidden)]
4+
type BitMask: Into<u64>;
5+
}
6+
7+
mod sealed {
8+
pub trait Sealed {}
9+
}
310

411
macro_rules! impl_for {
512
{ $name:ident } => {
6-
impl LanesAtMost32 for $name<1> {}
7-
impl LanesAtMost32 for $name<2> {}
8-
impl LanesAtMost32 for $name<4> {}
9-
impl LanesAtMost32 for $name<8> {}
10-
impl LanesAtMost32 for $name<16> {}
11-
impl LanesAtMost32 for $name<32> {}
13+
impl<const LANES: usize> sealed::Sealed for $name<LANES>
14+
where
15+
$name<LANES>: LanesAtMost32,
16+
{}
17+
18+
impl LanesAtMost32 for $name<1> {
19+
type BitMask = u8;
20+
}
21+
impl LanesAtMost32 for $name<2> {
22+
type BitMask = u8;
23+
}
24+
impl LanesAtMost32 for $name<4> {
25+
type BitMask = u8;
26+
}
27+
impl LanesAtMost32 for $name<8> {
28+
type BitMask = u8;
29+
}
30+
impl LanesAtMost32 for $name<16> {
31+
type BitMask = u16;
32+
}
33+
impl LanesAtMost32 for $name<32> {
34+
type BitMask = u32;
35+
}
1236
}
1337
}
1438

@@ -28,5 +52,3 @@ impl_for! { SimdIsize }
2852

2953
impl_for! { SimdF32 }
3054
impl_for! { SimdF64 }
31-
32-
impl_for! { BitMask }

0 commit comments

Comments
 (0)