]> git.lizzy.rs Git - rust.git/blob - library/portable-simd/crates/core_simd/src/select.rs
Auto merge of #89167 - workingjubilee:use-simd, r=MarkSimulacrum
[rust.git] / library / portable-simd / crates / core_simd / src / select.rs
1 use crate::simd::intrinsics;
2 use crate::simd::{LaneCount, Mask, MaskElement, Simd, SimdElement, SupportedLaneCount};
3
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     fn select(mask: Mask<T::Mask, LANES>, true_values: Self, false_values: Self) -> Self {
21         unsafe { intrinsics::simd_select(mask.to_int(), true_values, false_values) }
22     }
23 }
24
25 impl<T, const LANES: usize> Select<Mask<T::Mask, LANES>> for Simd<T, LANES>
26 where
27     T: SimdElement,
28     LaneCount<LANES>: SupportedLaneCount,
29 {
30 }
31
32 impl<T, const LANES: usize> Sealed<Self> for Mask<T, LANES>
33 where
34     T: MaskElement,
35     LaneCount<LANES>: SupportedLaneCount,
36 {
37     #[inline]
38     fn select(mask: Self, true_values: Self, false_values: Self) -> Self {
39         mask & true_values | !mask & false_values
40     }
41 }
42
43 impl<T, const LANES: usize> Select<Self> for Mask<T, LANES>
44 where
45     T: MaskElement,
46     LaneCount<LANES>: SupportedLaneCount,
47 {
48 }
49
50 impl<T, const LANES: usize> Mask<T, LANES>
51 where
52     T: MaskElement,
53     LaneCount<LANES>: SupportedLaneCount,
54 {
55     /// Choose lanes from two vectors.
56     ///
57     /// For each lane in the mask, choose the corresponding lane from `true_values` if
58     /// that lane mask is true, and `false_values` if that lane mask is false.
59     ///
60     /// ```
61     /// # #![feature(portable_simd)]
62     /// # #[cfg(feature = "std")] use core_simd::{Simd, Mask};
63     /// # #[cfg(not(feature = "std"))] use core::simd::{Simd, Mask};
64     /// let a = Simd::from_array([0, 1, 2, 3]);
65     /// let b = Simd::from_array([4, 5, 6, 7]);
66     /// let mask = Mask::from_array([true, false, false, true]);
67     /// let c = mask.select(a, b);
68     /// assert_eq!(c.to_array(), [0, 5, 6, 3]);
69     /// ```
70     ///
71     /// `select` can also be used on masks:
72     /// ```
73     /// # #![feature(portable_simd)]
74     /// # #[cfg(feature = "std")] use core_simd::Mask;
75     /// # #[cfg(not(feature = "std"))] use core::simd::Mask;
76     /// let a = Mask::<i32, 4>::from_array([true, true, false, false]);
77     /// let b = Mask::<i32, 4>::from_array([false, false, true, true]);
78     /// let mask = Mask::<i32, 4>::from_array([true, false, false, true]);
79     /// let c = mask.select(a, b);
80     /// assert_eq!(c.to_array(), [true, false, true, false]);
81     /// ```
82     #[inline]
83     pub fn select<S: Select<Self>>(self, true_values: S, false_values: S) -> S {
84         S::select(self, true_values, false_values)
85     }
86 }