1 use crate::simd::intrinsics;
2 use crate::simd::{LaneCount, Mask, MaskElement, Simd, SimdElement, SupportedLaneCount};
4 impl<T, const LANES: usize> Mask<T, LANES>
7 LaneCount<LANES>: SupportedLaneCount,
9 /// Choose lanes from two vectors.
11 /// For each lane in the mask, choose the corresponding lane from `true_values` if
12 /// that lane mask is true, and `false_values` if that lane mask is false.
16 /// # #![feature(portable_simd)]
17 /// # use core::simd::{Simd, Mask};
18 /// let a = Simd::from_array([0, 1, 2, 3]);
19 /// let b = Simd::from_array([4, 5, 6, 7]);
20 /// let mask = Mask::from_array([true, false, false, true]);
21 /// let c = mask.select(a, b);
22 /// assert_eq!(c.to_array(), [0, 5, 6, 3]);
25 #[must_use = "method returns a new vector and does not mutate the original inputs"]
28 true_values: Simd<U, LANES>,
29 false_values: Simd<U, LANES>,
32 U: SimdElement<Mask = T>,
34 // Safety: The mask has been cast to a vector of integers,
35 // and the operands to select between are vectors of the same type and length.
36 unsafe { intrinsics::simd_select(self.to_int(), true_values, false_values) }
39 /// Choose lanes from two masks.
41 /// For each lane in the mask, choose the corresponding lane from `true_values` if
42 /// that lane mask is true, and `false_values` if that lane mask is false.
46 /// # #![feature(portable_simd)]
47 /// # use core::simd::Mask;
48 /// let a = Mask::<i32, 4>::from_array([true, true, false, false]);
49 /// let b = Mask::<i32, 4>::from_array([false, false, true, true]);
50 /// let mask = Mask::<i32, 4>::from_array([true, false, false, true]);
51 /// let c = mask.select_mask(a, b);
52 /// assert_eq!(c.to_array(), [true, false, true, false]);
55 #[must_use = "method returns a new mask and does not mutate the original inputs"]
56 pub fn select_mask(self, true_values: Self, false_values: Self) -> Self {
57 self & true_values | !self & false_values