Skip to content

Commit 81484a3

Browse files
Merge portable-simd#207 - ./feature/simplify-select
Remove Select trait I realized that our `select` implementation predated `Simd` being generic over element type, and we don't really need the `Select` trait at all. The function signature is much simpler now (generic over element type, rather than over the entire vector). This did require changing mask select to be a different function, but I think that's fine considering they're not necessarily vectors.
2 parents a838552 + d9f82f9 commit 81484a3

File tree

2 files changed

+21
-54
lines changed

2 files changed

+21
-54
lines changed

crates/core_simd/src/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ pub mod simd {
2727

2828
pub use crate::core_simd::lane_count::{LaneCount, SupportedLaneCount};
2929
pub use crate::core_simd::masks::*;
30-
pub use crate::core_simd::select::Select;
3130
pub use crate::core_simd::swizzle::*;
3231
pub use crate::core_simd::vector::*;
3332
}

crates/core_simd/src/select.rs

Lines changed: 21 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,6 @@
11
use crate::simd::intrinsics;
22
use crate::simd::{LaneCount, Mask, MaskElement, Simd, SimdElement, SupportedLaneCount};
33

4-
mod sealed {
5-
pub trait Sealed<Mask> {
6-
fn select(mask: Mask, true_values: Self, false_values: Self) -> Self;
7-
}
8-
}
9-
use sealed::Sealed;
10-
11-
/// Supporting trait for vector `select` function
12-
pub trait Select<Mask>: Sealed<Mask> {}
13-
14-
impl<T, const LANES: usize> Sealed<Mask<T::Mask, LANES>> for Simd<T, LANES>
15-
where
16-
T: SimdElement,
17-
LaneCount<LANES>: SupportedLaneCount,
18-
{
19-
#[inline]
20-
#[must_use = "method returns a new vector and does not mutate the original inputs"]
21-
fn select(mask: Mask<T::Mask, LANES>, true_values: Self, false_values: Self) -> Self {
22-
unsafe { intrinsics::simd_select(mask.to_int(), true_values, false_values) }
23-
}
24-
}
25-
26-
impl<T, const LANES: usize> Select<Mask<T::Mask, LANES>> for Simd<T, LANES>
27-
where
28-
T: SimdElement,
29-
LaneCount<LANES>: SupportedLaneCount,
30-
{
31-
}
32-
33-
impl<T, const LANES: usize> Sealed<Self> for Mask<T, LANES>
34-
where
35-
T: MaskElement,
36-
LaneCount<LANES>: SupportedLaneCount,
37-
{
38-
#[inline]
39-
#[must_use = "method returns a new vector and does not mutate the original inputs"]
40-
fn select(mask: Self, true_values: Self, false_values: Self) -> Self {
41-
mask & true_values | !mask & false_values
42-
}
43-
}
44-
45-
impl<T, const LANES: usize> Select<Self> for Mask<T, LANES>
46-
where
47-
T: MaskElement,
48-
LaneCount<LANES>: SupportedLaneCount,
49-
{
50-
}
51-
524
impl<T, const LANES: usize> Mask<T, LANES>
535
where
546
T: MaskElement,
@@ -69,21 +21,37 @@ where
6921
/// let c = mask.select(a, b);
7022
/// assert_eq!(c.to_array(), [0, 5, 6, 3]);
7123
/// ```
24+
#[inline]
25+
#[must_use = "method returns a new vector and does not mutate the original inputs"]
26+
pub fn select<U>(
27+
self,
28+
true_values: Simd<U, LANES>,
29+
false_values: Simd<U, LANES>,
30+
) -> Simd<U, LANES>
31+
where
32+
U: SimdElement<Mask = T>,
33+
{
34+
unsafe { intrinsics::simd_select(self.to_int(), true_values, false_values) }
35+
}
36+
37+
/// Choose lanes from two masks.
38+
///
39+
/// For each lane in the mask, choose the corresponding lane from `true_values` if
40+
/// that lane mask is true, and `false_values` if that lane mask is false.
7241
///
73-
/// `select` can also be used on masks:
7442
/// ```
7543
/// # #![feature(portable_simd)]
7644
/// # #[cfg(feature = "std")] use core_simd::Mask;
7745
/// # #[cfg(not(feature = "std"))] use core::simd::Mask;
7846
/// let a = Mask::<i32, 4>::from_array([true, true, false, false]);
7947
/// let b = Mask::<i32, 4>::from_array([false, false, true, true]);
8048
/// let mask = Mask::<i32, 4>::from_array([true, false, false, true]);
81-
/// let c = mask.select(a, b);
49+
/// let c = mask.select_mask(a, b);
8250
/// assert_eq!(c.to_array(), [true, false, true, false]);
8351
/// ```
8452
#[inline]
85-
#[must_use = "method returns a new vector and does not mutate the original inputs"]
86-
pub fn select<S: Select<Self>>(self, true_values: S, false_values: S) -> S {
87-
S::select(self, true_values, false_values)
53+
#[must_use = "method returns a new mask and does not mutate the original inputs"]
54+
pub fn select_mask(self, true_values: Self, false_values: Self) -> Self {
55+
self & true_values | !self & false_values
8856
}
8957
}

0 commit comments

Comments
 (0)