]> git.lizzy.rs Git - rust.git/blob - library/portable-simd/crates/core_simd/src/select.rs
Merge commit '57b3c4b90f4346b3990c1be387c3b3ca7b78412c' into clippyup
[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     /// ```
15     /// # #![feature(portable_simd)]
16     /// # #[cfg(feature = "std")] use core_simd::{Simd, Mask};
17     /// # #[cfg(not(feature = "std"))] 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         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.
41     ///
42     /// ```
43     /// # #![feature(portable_simd)]
44     /// # #[cfg(feature = "std")] use core_simd::Mask;
45     /// # #[cfg(not(feature = "std"))] use core::simd::Mask;
46     /// let a = Mask::<i32, 4>::from_array([true, true, false, false]);
47     /// let b = Mask::<i32, 4>::from_array([false, false, true, true]);
48     /// let mask = Mask::<i32, 4>::from_array([true, false, false, true]);
49     /// let c = mask.select_mask(a, b);
50     /// assert_eq!(c.to_array(), [true, false, true, false]);
51     /// ```
52     #[inline]
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
56     }
57 }