]> git.lizzy.rs Git - rust.git/blob - library/portable-simd/crates/core_simd/src/select.rs
Rollup merge of #107108 - sulami:issue-83968-doc-alias-typo-suggestions, r=compiler...
[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 impl<T, const LANES: usize> Mask<T, LANES>
5 where
6     T: MaskElement,
7     LaneCount<LANES>: SupportedLaneCount,
8 {
9     /// Choose lanes from two vectors.
10     ///
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.
13     ///
14     /// # Examples
15     /// ```
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]);
23     /// ```
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         // 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) }
37     }
38
39     /// Choose lanes from two masks.
40     ///
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.
43     ///
44     /// # Examples
45     /// ```
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]);
53     /// ```
54     #[inline]
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
58     }
59 }