1 use crate::simd::intrinsics;
2 use crate::simd::{LaneCount, Mask, MaskElement, Simd, SimdElement, SupportedLaneCount};
5 pub trait Sealed<Mask> {
6 fn select(mask: Mask, true_values: Self, false_values: Self) -> Self;
11 /// Supporting trait for vector `select` function
12 pub trait Select<Mask>: Sealed<Mask> {}
14 impl<T, const LANES: usize> Sealed<Mask<T::Mask, LANES>> for Simd<T, LANES>
17 LaneCount<LANES>: SupportedLaneCount,
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) }
25 impl<T, const LANES: usize> Select<Mask<T::Mask, LANES>> for Simd<T, LANES>
28 LaneCount<LANES>: SupportedLaneCount,
32 impl<T, const LANES: usize> Sealed<Self> for Mask<T, LANES>
35 LaneCount<LANES>: SupportedLaneCount,
38 fn select(mask: Self, true_values: Self, false_values: Self) -> Self {
39 mask & true_values | !mask & false_values
43 impl<T, const LANES: usize> Select<Self> for Mask<T, LANES>
46 LaneCount<LANES>: SupportedLaneCount,
50 impl<T, const LANES: usize> Mask<T, LANES>
53 LaneCount<LANES>: SupportedLaneCount,
55 /// Choose lanes from two vectors.
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.
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]);
71 /// `select` can also be used on masks:
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]);
83 pub fn select<S: Select<Self>>(self, true_values: S, false_values: S) -> S {
84 S::select(self, true_values, false_values)