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::simd::{LaneCount, Simd, SimdElement, SupportedLaneCount};
16 use core::cmp::Ordering;
19 /// Marker trait for types that may be used as SIMD mask elements.
20 pub unsafe trait MaskElement: SimdElement {
22 fn valid<const LANES: usize>(values: Simd<Self, LANES>) -> bool
24 LaneCount<LANES>: SupportedLaneCount;
27 fn eq(self, other: Self) -> bool;
36 macro_rules! impl_element {
38 unsafe impl MaskElement for $ty {
39 fn valid<const LANES: usize>(value: Simd<Self, LANES>) -> bool
41 LaneCount<LANES>: SupportedLaneCount,
43 (value.lanes_eq(Simd::splat(0)) | value.lanes_eq(Simd::splat(-1))).all()
46 fn eq(self, other: Self) -> bool { self == other }
48 const TRUE: Self = -1;
49 const FALSE: Self = 0;
58 impl_element! { isize }
60 /// A SIMD vector mask for `LANES` elements of width specified by `Element`.
62 /// The layout of this type is unspecified.
64 pub struct Mask<T, const LANES: usize>(mask_impl::Mask<T, LANES>)
67 LaneCount<LANES>: SupportedLaneCount;
69 impl<T, const LANES: usize> Copy for Mask<T, LANES>
72 LaneCount<LANES>: SupportedLaneCount,
76 impl<T, const LANES: usize> Clone for Mask<T, LANES>
79 LaneCount<LANES>: SupportedLaneCount,
81 fn clone(&self) -> Self {
86 impl<T, const LANES: usize> Mask<T, LANES>
89 LaneCount<LANES>: SupportedLaneCount,
91 /// Construct a mask by setting all lanes to the given value.
92 pub fn splat(value: bool) -> Self {
93 Self(mask_impl::Mask::splat(value))
96 /// Converts an array to a SIMD vector.
97 pub fn from_array(array: [bool; LANES]) -> Self {
98 let mut vector = Self::splat(false);
99 for (i, v) in array.iter().enumerate() {
105 /// Converts a SIMD vector to an array.
106 pub fn to_array(self) -> [bool; LANES] {
107 let mut array = [false; LANES];
108 for (i, v) in array.iter_mut().enumerate() {
114 /// Converts a vector of integers to a mask, where 0 represents `false` and -1
115 /// represents `true`.
118 /// All lanes must be either 0 or -1.
120 pub unsafe fn from_int_unchecked(value: Simd<T, LANES>) -> Self {
121 unsafe { Self(mask_impl::Mask::from_int_unchecked(value)) }
124 /// Converts a vector of integers to a mask, where 0 represents `false` and -1
125 /// represents `true`.
128 /// Panics if any lane is not 0 or -1.
130 pub fn from_int(value: Simd<T, LANES>) -> Self {
131 assert!(T::valid(value), "all values must be either 0 or -1",);
132 unsafe { Self::from_int_unchecked(value) }
135 /// Converts the mask to a vector of integers, where 0 represents `false` and -1
136 /// represents `true`.
138 pub fn to_int(self) -> Simd<T, LANES> {
142 /// Tests the value of the specified lane.
145 /// `lane` must be less than `LANES`.
147 pub unsafe fn test_unchecked(&self, lane: usize) -> bool {
148 unsafe { self.0.test_unchecked(lane) }
151 /// Tests the value of the specified lane.
154 /// Panics if `lane` is greater than or equal to the number of lanes in the vector.
156 pub fn test(&self, lane: usize) -> bool {
157 assert!(lane < LANES, "lane index out of range");
158 unsafe { self.test_unchecked(lane) }
161 /// Sets the value of the specified lane.
164 /// `lane` must be less than `LANES`.
166 pub unsafe fn set_unchecked(&mut self, lane: usize, value: bool) {
168 self.0.set_unchecked(lane, value);
172 /// Sets the value of the specified lane.
175 /// Panics if `lane` is greater than or equal to the number of lanes in the vector.
177 pub fn set(&mut self, lane: usize, value: bool) {
178 assert!(lane < LANES, "lane index out of range");
180 self.set_unchecked(lane, value);
184 /// Convert this mask to a bitmask, with one bit set per lane.
185 #[cfg(feature = "generic_const_exprs")]
186 pub fn to_bitmask(self) -> [u8; LaneCount::<LANES>::BITMASK_LEN] {
190 /// Convert a bitmask to a mask.
191 #[cfg(feature = "generic_const_exprs")]
192 pub fn from_bitmask(bitmask: [u8; LaneCount::<LANES>::BITMASK_LEN]) -> Self {
193 Self(mask_impl::Mask::from_bitmask(bitmask))
196 /// Returns true if any lane is set, or false otherwise.
198 pub fn any(self) -> bool {
202 /// Returns true if all lanes are set, or false otherwise.
204 pub fn all(self) -> bool {
209 // vector/array conversion
210 impl<T, const LANES: usize> From<[bool; LANES]> for Mask<T, LANES>
213 LaneCount<LANES>: SupportedLaneCount,
215 fn from(array: [bool; LANES]) -> Self {
216 Self::from_array(array)
220 impl<T, const LANES: usize> From<Mask<T, LANES>> for [bool; LANES]
223 LaneCount<LANES>: SupportedLaneCount,
225 fn from(vector: Mask<T, LANES>) -> Self {
230 impl<T, const LANES: usize> Default for Mask<T, LANES>
233 LaneCount<LANES>: SupportedLaneCount,
236 fn default() -> Self {
241 impl<T, const LANES: usize> PartialEq for Mask<T, LANES>
243 T: MaskElement + PartialEq,
244 LaneCount<LANES>: SupportedLaneCount,
247 fn eq(&self, other: &Self) -> bool {
252 impl<T, const LANES: usize> PartialOrd for Mask<T, LANES>
254 T: MaskElement + PartialOrd,
255 LaneCount<LANES>: SupportedLaneCount,
258 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
259 self.0.partial_cmp(&other.0)
263 impl<T, const LANES: usize> fmt::Debug for Mask<T, LANES>
265 T: MaskElement + fmt::Debug,
266 LaneCount<LANES>: SupportedLaneCount,
268 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
270 .entries((0..LANES).map(|lane| self.test(lane)))
275 impl<T, const LANES: usize> core::ops::BitAnd for Mask<T, LANES>
278 LaneCount<LANES>: SupportedLaneCount,
282 fn bitand(self, rhs: Self) -> Self {
287 impl<T, const LANES: usize> core::ops::BitAnd<bool> for Mask<T, LANES>
290 LaneCount<LANES>: SupportedLaneCount,
294 fn bitand(self, rhs: bool) -> Self {
295 self & Self::splat(rhs)
299 impl<T, const LANES: usize> core::ops::BitAnd<Mask<T, LANES>> for bool
302 LaneCount<LANES>: SupportedLaneCount,
304 type Output = Mask<T, LANES>;
306 fn bitand(self, rhs: Mask<T, LANES>) -> Mask<T, LANES> {
307 Mask::splat(self) & rhs
311 impl<T, const LANES: usize> core::ops::BitOr for Mask<T, LANES>
314 LaneCount<LANES>: SupportedLaneCount,
318 fn bitor(self, rhs: Self) -> Self {
323 impl<T, const LANES: usize> core::ops::BitOr<bool> for Mask<T, LANES>
326 LaneCount<LANES>: SupportedLaneCount,
330 fn bitor(self, rhs: bool) -> Self {
331 self | Self::splat(rhs)
335 impl<T, const LANES: usize> core::ops::BitOr<Mask<T, LANES>> for bool
338 LaneCount<LANES>: SupportedLaneCount,
340 type Output = Mask<T, LANES>;
342 fn bitor(self, rhs: Mask<T, LANES>) -> Mask<T, LANES> {
343 Mask::splat(self) | rhs
347 impl<T, const LANES: usize> core::ops::BitXor for Mask<T, LANES>
350 LaneCount<LANES>: SupportedLaneCount,
354 fn bitxor(self, rhs: Self) -> Self::Output {
359 impl<T, const LANES: usize> core::ops::BitXor<bool> for Mask<T, LANES>
362 LaneCount<LANES>: SupportedLaneCount,
366 fn bitxor(self, rhs: bool) -> Self::Output {
367 self ^ Self::splat(rhs)
371 impl<T, const LANES: usize> core::ops::BitXor<Mask<T, LANES>> for bool
374 LaneCount<LANES>: SupportedLaneCount,
376 type Output = Mask<T, LANES>;
378 fn bitxor(self, rhs: Mask<T, LANES>) -> Self::Output {
379 Mask::splat(self) ^ rhs
383 impl<T, const LANES: usize> core::ops::Not for Mask<T, LANES>
386 LaneCount<LANES>: SupportedLaneCount,
388 type Output = Mask<T, LANES>;
390 fn not(self) -> Self::Output {
395 impl<T, const LANES: usize> core::ops::BitAndAssign for Mask<T, LANES>
398 LaneCount<LANES>: SupportedLaneCount,
401 fn bitand_assign(&mut self, rhs: Self) {
402 self.0 = self.0 & rhs.0;
406 impl<T, const LANES: usize> core::ops::BitAndAssign<bool> for Mask<T, LANES>
409 LaneCount<LANES>: SupportedLaneCount,
412 fn bitand_assign(&mut self, rhs: bool) {
413 *self &= Self::splat(rhs);
417 impl<T, const LANES: usize> core::ops::BitOrAssign for Mask<T, LANES>
420 LaneCount<LANES>: SupportedLaneCount,
423 fn bitor_assign(&mut self, rhs: Self) {
424 self.0 = self.0 | rhs.0;
428 impl<T, const LANES: usize> core::ops::BitOrAssign<bool> for Mask<T, LANES>
431 LaneCount<LANES>: SupportedLaneCount,
434 fn bitor_assign(&mut self, rhs: bool) {
435 *self |= Self::splat(rhs);
439 impl<T, const LANES: usize> core::ops::BitXorAssign for Mask<T, LANES>
442 LaneCount<LANES>: SupportedLaneCount,
445 fn bitxor_assign(&mut self, rhs: Self) {
446 self.0 = self.0 ^ rhs.0;
450 impl<T, const LANES: usize> core::ops::BitXorAssign<bool> for Mask<T, LANES>
453 LaneCount<LANES>: SupportedLaneCount,
456 fn bitxor_assign(&mut self, rhs: bool) {
457 *self ^= Self::splat(rhs);
461 /// Vector of eight 8-bit masks
462 pub type mask8x8 = Mask<i8, 8>;
464 /// Vector of 16 8-bit masks
465 pub type mask8x16 = Mask<i8, 16>;
467 /// Vector of 32 8-bit masks
468 pub type mask8x32 = Mask<i8, 32>;
470 /// Vector of 16 8-bit masks
471 pub type mask8x64 = Mask<i8, 64>;
473 /// Vector of four 16-bit masks
474 pub type mask16x4 = Mask<i16, 4>;
476 /// Vector of eight 16-bit masks
477 pub type mask16x8 = Mask<i16, 8>;
479 /// Vector of 16 16-bit masks
480 pub type mask16x16 = Mask<i16, 16>;
482 /// Vector of 32 16-bit masks
483 pub type mask16x32 = Mask<i32, 32>;
485 /// Vector of two 32-bit masks
486 pub type mask32x2 = Mask<i32, 2>;
488 /// Vector of four 32-bit masks
489 pub type mask32x4 = Mask<i32, 4>;
491 /// Vector of eight 32-bit masks
492 pub type mask32x8 = Mask<i32, 8>;
494 /// Vector of 16 32-bit masks
495 pub type mask32x16 = Mask<i32, 16>;
497 /// Vector of two 64-bit masks
498 pub type mask64x2 = Mask<i64, 2>;
500 /// Vector of four 64-bit masks
501 pub type mask64x4 = Mask<i64, 4>;
503 /// Vector of eight 64-bit masks
504 pub type mask64x8 = Mask<i64, 8>;
506 /// Vector of two pointer-width masks
507 pub type masksizex2 = Mask<isize, 2>;
509 /// Vector of four pointer-width masks
510 pub type masksizex4 = Mask<isize, 4>;
512 /// Vector of eight pointer-width masks
513 pub type masksizex8 = Mask<isize, 8>;
515 macro_rules! impl_from {
516 { $from:ty => $($to:ty),* } => {
518 impl<const LANES: usize> From<Mask<$from, LANES>> for Mask<$to, LANES>
520 LaneCount<LANES>: SupportedLaneCount,
522 fn from(value: Mask<$from, LANES>) -> Self {
523 Self(value.0.convert())
529 impl_from! { i8 => i16, i32, i64, isize }
530 impl_from! { i16 => i32, i64, isize, i8 }
531 impl_from! { i32 => i64, isize, i8, i16 }
532 impl_from! { i64 => isize, i8, i16, i32 }
533 impl_from! { isize => i8, i16, i32, i64 }