select.rs - source
Description: Source of the Rust file `library/core/src/../../portable-simd/crates/core_simd/src/select.rs`.

#
core/portable-simd/crates/core_simd/src/
select.rs

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