1 //! Types and traits associated with masking lanes of vectors.
3 #![allow(non_camel_case_types)]
6 not(all(target_arch = "x86_64", target_feature = "avx512f")),
7 path = "masks/full_masks.rs"
10 all(target_arch = "x86_64", target_feature = "avx512f"),
11 path = "masks/bitmask.rs"
15 use crate::{LaneCount, Simd, SimdElement, SupportedLaneCount};
17 /// Marker trait for types that may be used as SIMD mask elements.
18 pub unsafe trait MaskElement: SimdElement {
20 fn valid<const LANES: usize>(values: Simd<Self, LANES>) -> bool
22 LaneCount<LANES>: SupportedLaneCount;
25 fn eq(self, other: Self) -> bool;
34 macro_rules! impl_element {
36 unsafe impl MaskElement for $ty {
37 fn valid<const LANES: usize>(value: Simd<Self, LANES>) -> bool
39 LaneCount<LANES>: SupportedLaneCount,
41 (value.lanes_eq(Simd::splat(0)) | value.lanes_eq(Simd::splat(-1))).all()
44 fn eq(self, other: Self) -> bool { self == other }
46 const TRUE: Self = -1;
47 const FALSE: Self = 0;
56 impl_element! { isize }
58 /// A SIMD vector mask for `LANES` elements of width specified by `Element`.
60 /// The layout of this type is unspecified.
62 pub struct Mask<T, const LANES: usize>(mask_impl::Mask<T, LANES>)
65 LaneCount<LANES>: SupportedLaneCount;
67 impl<T, const LANES: usize> Copy for Mask<T, LANES>
70 LaneCount<LANES>: SupportedLaneCount,
74 impl<T, const LANES: usize> Clone for Mask<T, LANES>
77 LaneCount<LANES>: SupportedLaneCount,
79 fn clone(&self) -> Self {
84 impl<T, const LANES: usize> Mask<T, LANES>
87 LaneCount<LANES>: SupportedLaneCount,
89 /// Construct a mask by setting all lanes to the given value.
90 pub fn splat(value: bool) -> Self {
91 Self(mask_impl::Mask::splat(value))
94 /// Converts an array to a SIMD vector.
95 pub fn from_array(array: [bool; LANES]) -> Self {
96 let mut vector = Self::splat(false);
97 for (i, v) in array.iter().enumerate() {
103 /// Converts a SIMD vector to an array.
104 pub fn to_array(self) -> [bool; LANES] {
105 let mut array = [false; LANES];
106 for (i, v) in array.iter_mut().enumerate() {
112 /// Converts a vector of integers to a mask, where 0 represents `false` and -1
113 /// represents `true`.
116 /// All lanes must be either 0 or -1.
118 pub unsafe fn from_int_unchecked(value: Simd<T, LANES>) -> Self {
119 Self(mask_impl::Mask::from_int_unchecked(value))
122 /// Converts a vector of integers to a mask, where 0 represents `false` and -1
123 /// represents `true`.
126 /// Panics if any lane is not 0 or -1.
128 pub fn from_int(value: Simd<T, LANES>) -> Self {
129 assert!(T::valid(value), "all values must be either 0 or -1",);
130 unsafe { Self::from_int_unchecked(value) }
133 /// Converts the mask to a vector of integers, where 0 represents `false` and -1
134 /// represents `true`.
136 pub fn to_int(self) -> Simd<T, LANES> {
140 /// Tests the value of the specified lane.
143 /// `lane` must be less than `LANES`.
145 pub unsafe fn test_unchecked(&self, lane: usize) -> bool {
146 self.0.test_unchecked(lane)
149 /// Tests the value of the specified lane.
152 /// Panics if `lane` is greater than or equal to the number of lanes in the vector.
154 pub fn test(&self, lane: usize) -> bool {
155 assert!(lane < LANES, "lane index out of range");
156 unsafe { self.test_unchecked(lane) }
159 /// Sets the value of the specified lane.
162 /// `lane` must be less than `LANES`.
164 pub unsafe fn set_unchecked(&mut self, lane: usize, value: bool) {
165 self.0.set_unchecked(lane, value);
168 /// Sets the value of the specified lane.
171 /// Panics if `lane` is greater than or equal to the number of lanes in the vector.
173 pub fn set(&mut self, lane: usize, value: bool) {
174 assert!(lane < LANES, "lane index out of range");
176 self.set_unchecked(lane, value);
180 /// Convert this mask to a bitmask, with one bit set per lane.
181 pub fn to_bitmask(self) -> [u8; LaneCount::<LANES>::BITMASK_LEN] {
185 /// Convert a bitmask to a mask.
186 pub fn from_bitmask(bitmask: [u8; LaneCount::<LANES>::BITMASK_LEN]) -> Self {
187 Self(mask_impl::Mask::from_bitmask(bitmask))
190 /// Returns true if any lane is set, or false otherwise.
192 pub fn any(self) -> bool {
196 /// Returns true if all lanes are set, or false otherwise.
198 pub fn all(self) -> bool {
203 // vector/array conversion
204 impl<T, const LANES: usize> From<[bool; LANES]> for Mask<T, LANES>
207 LaneCount<LANES>: SupportedLaneCount,
209 fn from(array: [bool; LANES]) -> Self {
210 Self::from_array(array)
214 impl<T, const LANES: usize> From<Mask<T, LANES>> for [bool; LANES]
217 LaneCount<LANES>: SupportedLaneCount,
219 fn from(vector: Mask<T, LANES>) -> Self {
224 impl<T, const LANES: usize> Default for Mask<T, LANES>
227 LaneCount<LANES>: SupportedLaneCount,
230 fn default() -> Self {
235 impl<T, const LANES: usize> PartialEq for Mask<T, LANES>
237 T: MaskElement + PartialEq,
238 LaneCount<LANES>: SupportedLaneCount,
241 fn eq(&self, other: &Self) -> bool {
246 impl<T, const LANES: usize> PartialOrd for Mask<T, LANES>
248 T: MaskElement + PartialOrd,
249 LaneCount<LANES>: SupportedLaneCount,
252 fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
253 self.0.partial_cmp(&other.0)
257 impl<T, const LANES: usize> core::fmt::Debug for Mask<T, LANES>
259 T: MaskElement + core::fmt::Debug,
260 LaneCount<LANES>: SupportedLaneCount,
262 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
264 .entries((0..LANES).map(|lane| self.test(lane)))
269 impl<T, const LANES: usize> core::ops::BitAnd for Mask<T, LANES>
272 LaneCount<LANES>: SupportedLaneCount,
276 fn bitand(self, rhs: Self) -> Self {
281 impl<T, const LANES: usize> core::ops::BitAnd<bool> for Mask<T, LANES>
284 LaneCount<LANES>: SupportedLaneCount,
288 fn bitand(self, rhs: bool) -> Self {
289 self & Self::splat(rhs)
293 impl<T, const LANES: usize> core::ops::BitAnd<Mask<T, LANES>> for bool
296 LaneCount<LANES>: SupportedLaneCount,
298 type Output = Mask<T, LANES>;
300 fn bitand(self, rhs: Mask<T, LANES>) -> Mask<T, LANES> {
301 Mask::splat(self) & rhs
305 impl<T, const LANES: usize> core::ops::BitOr for Mask<T, LANES>
308 LaneCount<LANES>: SupportedLaneCount,
312 fn bitor(self, rhs: Self) -> Self {
317 impl<T, const LANES: usize> core::ops::BitOr<bool> for Mask<T, LANES>
320 LaneCount<LANES>: SupportedLaneCount,
324 fn bitor(self, rhs: bool) -> Self {
325 self | Self::splat(rhs)
329 impl<T, const LANES: usize> core::ops::BitOr<Mask<T, LANES>> for bool
332 LaneCount<LANES>: SupportedLaneCount,
334 type Output = Mask<T, LANES>;
336 fn bitor(self, rhs: Mask<T, LANES>) -> Mask<T, LANES> {
337 Mask::splat(self) | rhs
341 impl<T, const LANES: usize> core::ops::BitXor for Mask<T, LANES>
344 LaneCount<LANES>: SupportedLaneCount,
348 fn bitxor(self, rhs: Self) -> Self::Output {
353 impl<T, const LANES: usize> core::ops::BitXor<bool> for Mask<T, LANES>
356 LaneCount<LANES>: SupportedLaneCount,
360 fn bitxor(self, rhs: bool) -> Self::Output {
361 self ^ Self::splat(rhs)
365 impl<T, const LANES: usize> core::ops::BitXor<Mask<T, LANES>> for bool
368 LaneCount<LANES>: SupportedLaneCount,
370 type Output = Mask<T, LANES>;
372 fn bitxor(self, rhs: Mask<T, LANES>) -> Self::Output {
373 Mask::splat(self) ^ rhs
377 impl<T, const LANES: usize> core::ops::Not for Mask<T, LANES>
380 LaneCount<LANES>: SupportedLaneCount,
382 type Output = Mask<T, LANES>;
384 fn not(self) -> Self::Output {
389 impl<T, const LANES: usize> core::ops::BitAndAssign for Mask<T, LANES>
392 LaneCount<LANES>: SupportedLaneCount,
395 fn bitand_assign(&mut self, rhs: Self) {
396 self.0 = self.0 & rhs.0;
400 impl<T, const LANES: usize> core::ops::BitAndAssign<bool> for Mask<T, LANES>
403 LaneCount<LANES>: SupportedLaneCount,
406 fn bitand_assign(&mut self, rhs: bool) {
407 *self &= Self::splat(rhs);
411 impl<T, const LANES: usize> core::ops::BitOrAssign for Mask<T, LANES>
414 LaneCount<LANES>: SupportedLaneCount,
417 fn bitor_assign(&mut self, rhs: Self) {
418 self.0 = self.0 | rhs.0;
422 impl<T, const LANES: usize> core::ops::BitOrAssign<bool> for Mask<T, LANES>
425 LaneCount<LANES>: SupportedLaneCount,
428 fn bitor_assign(&mut self, rhs: bool) {
429 *self |= Self::splat(rhs);
433 impl<T, const LANES: usize> core::ops::BitXorAssign for Mask<T, LANES>
436 LaneCount<LANES>: SupportedLaneCount,
439 fn bitxor_assign(&mut self, rhs: Self) {
440 self.0 = self.0 ^ rhs.0;
444 impl<T, const LANES: usize> core::ops::BitXorAssign<bool> for Mask<T, LANES>
447 LaneCount<LANES>: SupportedLaneCount,
450 fn bitxor_assign(&mut self, rhs: bool) {
451 *self ^= Self::splat(rhs);
455 /// Vector of eight 8-bit masks
456 pub type mask8x8 = Mask<i8, 8>;
458 /// Vector of 16 8-bit masks
459 pub type mask8x16 = Mask<i8, 16>;
461 /// Vector of 32 8-bit masks
462 pub type mask8x32 = Mask<i8, 32>;
464 /// Vector of 16 8-bit masks
465 pub type mask8x64 = Mask<i8, 64>;
467 /// Vector of four 16-bit masks
468 pub type mask16x4 = Mask<i16, 4>;
470 /// Vector of eight 16-bit masks
471 pub type mask16x8 = Mask<i16, 8>;
473 /// Vector of 16 16-bit masks
474 pub type mask16x16 = Mask<i16, 16>;
476 /// Vector of 32 16-bit masks
477 pub type mask16x32 = Mask<i32, 32>;
479 /// Vector of two 32-bit masks
480 pub type mask32x2 = Mask<i32, 2>;
482 /// Vector of four 32-bit masks
483 pub type mask32x4 = Mask<i32, 4>;
485 /// Vector of eight 32-bit masks
486 pub type mask32x8 = Mask<i32, 8>;
488 /// Vector of 16 32-bit masks
489 pub type mask32x16 = Mask<i32, 16>;
491 /// Vector of two 64-bit masks
492 pub type mask64x2 = Mask<i64, 2>;
494 /// Vector of four 64-bit masks
495 pub type mask64x4 = Mask<i64, 4>;
497 /// Vector of eight 64-bit masks
498 pub type mask64x8 = Mask<i64, 8>;
500 /// Vector of two pointer-width masks
501 pub type masksizex2 = Mask<isize, 2>;
503 /// Vector of four pointer-width masks
504 pub type masksizex4 = Mask<isize, 4>;
506 /// Vector of eight pointer-width masks
507 pub type masksizex8 = Mask<isize, 8>;
509 macro_rules! impl_from {
510 { $from:ty => $($to:ty),* } => {
512 impl<const LANES: usize> From<Mask<$from, LANES>> for Mask<$to, LANES>
514 LaneCount<LANES>: SupportedLaneCount,
516 fn from(value: Mask<$from, LANES>) -> Self {
517 Self(value.0.convert())
523 impl_from! { i8 => i16, i32, i64, isize }
524 impl_from! { i16 => i32, i64, isize, i8 }
525 impl_from! { i32 => i64, isize, i8, i16 }
526 impl_from! { i64 => isize, i8, i16, i32 }
527 impl_from! { isize => i8, i16, i32, i64 }