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<Element, const LANES: usize>(mask_impl::Mask<Element, LANES>)
65 LaneCount<LANES>: SupportedLaneCount;
67 impl<Element, const LANES: usize> Copy for Mask<Element, LANES>
70 LaneCount<LANES>: SupportedLaneCount,
74 impl<Element, const LANES: usize> Clone for Mask<Element, LANES>
77 LaneCount<LANES>: SupportedLaneCount,
79 fn clone(&self) -> Self {
84 impl<Element, const LANES: usize> Mask<Element, 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<Element, 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<Element, LANES>) -> Self {
129 assert!(Element::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<Element, 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<Element, const LANES: usize> From<[bool; LANES]> for Mask<Element, LANES>
206 Element: MaskElement,
207 LaneCount<LANES>: SupportedLaneCount,
209 fn from(array: [bool; LANES]) -> Self {
210 Self::from_array(array)
214 impl<Element, const LANES: usize> From<Mask<Element, LANES>> for [bool; LANES]
216 Element: MaskElement,
217 LaneCount<LANES>: SupportedLaneCount,
219 fn from(vector: Mask<Element, LANES>) -> Self {
224 impl<Element, const LANES: usize> Default for Mask<Element, LANES>
226 Element: MaskElement,
227 LaneCount<LANES>: SupportedLaneCount,
230 fn default() -> Self {
235 impl<Element, const LANES: usize> PartialEq for Mask<Element, LANES>
237 Element: MaskElement + PartialEq,
238 LaneCount<LANES>: SupportedLaneCount,
241 fn eq(&self, other: &Self) -> bool {
246 impl<Element, const LANES: usize> PartialOrd for Mask<Element, LANES>
248 Element: 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<Element, const LANES: usize> core::fmt::Debug for Mask<Element, LANES>
259 Element: 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<Element, const LANES: usize> core::ops::BitAnd for Mask<Element, LANES>
271 Element: MaskElement,
272 LaneCount<LANES>: SupportedLaneCount,
276 fn bitand(self, rhs: Self) -> Self {
281 impl<Element, const LANES: usize> core::ops::BitAnd<bool> for Mask<Element, LANES>
283 Element: MaskElement,
284 LaneCount<LANES>: SupportedLaneCount,
288 fn bitand(self, rhs: bool) -> Self {
289 self & Self::splat(rhs)
293 impl<Element, const LANES: usize> core::ops::BitAnd<Mask<Element, LANES>> for bool
295 Element: MaskElement,
296 LaneCount<LANES>: SupportedLaneCount,
298 type Output = Mask<Element, LANES>;
300 fn bitand(self, rhs: Mask<Element, LANES>) -> Mask<Element, LANES> {
301 Mask::splat(self) & rhs
305 impl<Element, const LANES: usize> core::ops::BitOr for Mask<Element, LANES>
307 Element: MaskElement,
308 LaneCount<LANES>: SupportedLaneCount,
312 fn bitor(self, rhs: Self) -> Self {
317 impl<Element, const LANES: usize> core::ops::BitOr<bool> for Mask<Element, LANES>
319 Element: MaskElement,
320 LaneCount<LANES>: SupportedLaneCount,
324 fn bitor(self, rhs: bool) -> Self {
325 self | Self::splat(rhs)
329 impl<Element, const LANES: usize> core::ops::BitOr<Mask<Element, LANES>> for bool
331 Element: MaskElement,
332 LaneCount<LANES>: SupportedLaneCount,
334 type Output = Mask<Element, LANES>;
336 fn bitor(self, rhs: Mask<Element, LANES>) -> Mask<Element, LANES> {
337 Mask::splat(self) | rhs
341 impl<Element, const LANES: usize> core::ops::BitXor for Mask<Element, LANES>
343 Element: MaskElement,
344 LaneCount<LANES>: SupportedLaneCount,
348 fn bitxor(self, rhs: Self) -> Self::Output {
353 impl<Element, const LANES: usize> core::ops::BitXor<bool> for Mask<Element, LANES>
355 Element: MaskElement,
356 LaneCount<LANES>: SupportedLaneCount,
360 fn bitxor(self, rhs: bool) -> Self::Output {
361 self ^ Self::splat(rhs)
365 impl<Element, const LANES: usize> core::ops::BitXor<Mask<Element, LANES>> for bool
367 Element: MaskElement,
368 LaneCount<LANES>: SupportedLaneCount,
370 type Output = Mask<Element, LANES>;
372 fn bitxor(self, rhs: Mask<Element, LANES>) -> Self::Output {
373 Mask::splat(self) ^ rhs
377 impl<Element, const LANES: usize> core::ops::Not for Mask<Element, LANES>
379 Element: MaskElement,
380 LaneCount<LANES>: SupportedLaneCount,
382 type Output = Mask<Element, LANES>;
384 fn not(self) -> Self::Output {
389 impl<Element, const LANES: usize> core::ops::BitAndAssign for Mask<Element, LANES>
391 Element: MaskElement,
392 LaneCount<LANES>: SupportedLaneCount,
395 fn bitand_assign(&mut self, rhs: Self) {
396 self.0 = self.0 & rhs.0;
400 impl<Element, const LANES: usize> core::ops::BitAndAssign<bool> for Mask<Element, LANES>
402 Element: MaskElement,
403 LaneCount<LANES>: SupportedLaneCount,
406 fn bitand_assign(&mut self, rhs: bool) {
407 *self &= Self::splat(rhs);
411 impl<Element, const LANES: usize> core::ops::BitOrAssign for Mask<Element, LANES>
413 Element: MaskElement,
414 LaneCount<LANES>: SupportedLaneCount,
417 fn bitor_assign(&mut self, rhs: Self) {
418 self.0 = self.0 | rhs.0;
422 impl<Element, const LANES: usize> core::ops::BitOrAssign<bool> for Mask<Element, LANES>
424 Element: MaskElement,
425 LaneCount<LANES>: SupportedLaneCount,
428 fn bitor_assign(&mut self, rhs: bool) {
429 *self |= Self::splat(rhs);
433 impl<Element, const LANES: usize> core::ops::BitXorAssign for Mask<Element, LANES>
435 Element: MaskElement,
436 LaneCount<LANES>: SupportedLaneCount,
439 fn bitxor_assign(&mut self, rhs: Self) {
440 self.0 = self.0 ^ rhs.0;
444 impl<Element, const LANES: usize> core::ops::BitXorAssign<bool> for Mask<Element, LANES>
446 Element: MaskElement,
447 LaneCount<LANES>: SupportedLaneCount,
450 fn bitxor_assign(&mut self, rhs: bool) {
451 *self ^= Self::splat(rhs);
455 /// A SIMD mask of `LANES` 8-bit values.
456 pub type Mask8<const LANES: usize> = Mask<i8, LANES>;
458 /// A SIMD mask of `LANES` 16-bit values.
459 pub type Mask16<const LANES: usize> = Mask<i16, LANES>;
461 /// A SIMD mask of `LANES` 32-bit values.
462 pub type Mask32<const LANES: usize> = Mask<i32, LANES>;
464 /// A SIMD mask of `LANES` 64-bit values.
465 pub type Mask64<const LANES: usize> = Mask<i64, LANES>;
467 /// A SIMD mask of `LANES` pointer-width values.
468 pub type MaskSize<const LANES: usize> = Mask<isize, LANES>;
470 /// Vector of eight 8-bit masks
471 pub type mask8x8 = Mask8<8>;
473 /// Vector of 16 8-bit masks
474 pub type mask8x16 = Mask8<16>;
476 /// Vector of 32 8-bit masks
477 pub type mask8x32 = Mask8<32>;
479 /// Vector of 16 8-bit masks
480 pub type mask8x64 = Mask8<64>;
482 /// Vector of four 16-bit masks
483 pub type mask16x4 = Mask16<4>;
485 /// Vector of eight 16-bit masks
486 pub type mask16x8 = Mask16<8>;
488 /// Vector of 16 16-bit masks
489 pub type mask16x16 = Mask16<16>;
491 /// Vector of 32 16-bit masks
492 pub type mask16x32 = Mask32<32>;
494 /// Vector of two 32-bit masks
495 pub type mask32x2 = Mask32<2>;
497 /// Vector of four 32-bit masks
498 pub type mask32x4 = Mask32<4>;
500 /// Vector of eight 32-bit masks
501 pub type mask32x8 = Mask32<8>;
503 /// Vector of 16 32-bit masks
504 pub type mask32x16 = Mask32<16>;
506 /// Vector of two 64-bit masks
507 pub type mask64x2 = Mask64<2>;
509 /// Vector of four 64-bit masks
510 pub type mask64x4 = Mask64<4>;
512 /// Vector of eight 64-bit masks
513 pub type mask64x8 = Mask64<8>;
515 /// Vector of two pointer-width masks
516 pub type masksizex2 = MaskSize<2>;
518 /// Vector of four pointer-width masks
519 pub type masksizex4 = MaskSize<4>;
521 /// Vector of eight pointer-width masks
522 pub type masksizex8 = MaskSize<8>;
524 macro_rules! impl_from {
525 { $from:ident ($from_inner:ident) => $($to:ident ($to_inner:ident)),* } => {
527 impl<const LANES: usize> From<$from<LANES>> for $to<LANES>
529 crate::LaneCount<LANES>: crate::SupportedLaneCount,
531 fn from(value: $from<LANES>) -> Self {
532 Self(value.0.convert())
538 impl_from! { Mask8 (SimdI8) => Mask16 (SimdI16), Mask32 (SimdI32), Mask64 (SimdI64), MaskSize (SimdIsize) }
539 impl_from! { Mask16 (SimdI16) => Mask32 (SimdI32), Mask64 (SimdI64), MaskSize (SimdIsize), Mask8 (SimdI8) }
540 impl_from! { Mask32 (SimdI32) => Mask64 (SimdI64), MaskSize (SimdIsize), Mask8 (SimdI8), Mask16 (SimdI16) }
541 impl_from! { Mask64 (SimdI64) => MaskSize (SimdIsize), Mask8 (SimdI8), Mask16 (SimdI16), Mask32 (SimdI32) }
542 impl_from! { MaskSize (SimdIsize) => Mask8 (SimdI8), Mask16 (SimdI16), Mask32 (SimdI32), Mask64 (SimdI64) }