|
| 1 | +mod sealed { |
| 2 | +pub trait Sealed {} |
| 3 | +} |
| 4 | +use sealed::Sealed; |
| 5 | + |
| 6 | +/// Supporting trait for vector `select` function |
| 7 | +pub trait Select<Mask>: Sealed {} |
| 8 | + |
| 9 | +macro_rules! impl_select { |
| 10 | + { |
| 11 | + $mask:ident ($bits_ty:ident): $($type:ident),* |
| 12 | + } => { |
| 13 | + $( |
| 14 | + impl<const LANES: usize> Sealed for crate::$type<LANES> where Self: crate::LanesAtMost32 {} |
| 15 | + impl<const LANES: usize> Select<crate::$mask<LANES>> for crate::$type<LANES> |
| 16 | + where |
| 17 | + crate::$mask<LANES>: crate::Mask, |
| 18 | + crate::$bits_ty<LANES>: crate::LanesAtMost32, |
| 19 | + Self: crate::LanesAtMost32, |
| 20 | + {} |
| 21 | + )* |
| 22 | + |
| 23 | + impl<const LANES: usize> crate::$mask<LANES> |
| 24 | + where |
| 25 | + Self: crate::Mask, |
| 26 | + crate::$bits_ty<LANES>: crate::LanesAtMost32, |
| 27 | + { |
| 28 | + /// Choose lanes from two vectors. |
| 29 | + /// |
| 30 | + /// For each lane in the mask, choose the corresponding lane from `true_values` if |
| 31 | + /// that lane mask is true, and `false_values` if that lane mask is false. |
| 32 | + /// |
| 33 | + /// ``` |
| 34 | + /// # use core_simd::{Mask32, SimdI32}; |
| 35 | + /// let a = SimdI32::from_array([0, 1, 2, 3]); |
| 36 | + /// let b = SimdI32::from_array([4, 5, 6, 7]); |
| 37 | + /// let mask = Mask32::from_array([true, false, false, true]); |
| 38 | + /// let c = mask.select(a, b); |
| 39 | + /// assert_eq!(c.to_array(), [0, 5, 6, 3]); |
| 40 | + /// ``` |
| 41 | + pub fn select<S: Select<Self>>(self, true_values: S, false_values: S) -> S { |
| 42 | + unsafe { crate::intrinsics::simd_select(self.to_int(), true_values, false_values) } |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl_select! { Mask8 (SimdI8): SimdU8, SimdI8 } |
| 49 | +impl_select! { Mask16 (SimdI16): SimdU16, SimdI16 } |
| 50 | +impl_select! { Mask32 (SimdI32): SimdU32, SimdI32, SimdF32} |
| 51 | +impl_select! { Mask64 (SimdI64): SimdU64, SimdI64, SimdF64} |
| 52 | +impl_select! { MaskSize (SimdIsize): SimdUsize, SimdIsize } |
0 commit comments