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;
22 /// Not only does this seal the `MaskElement` trait, but these functions prevent other traits
23 /// from bleeding into the parent bounds.
25 /// For example, `eq` could be provided by requiring `MaskElement: PartialEq`, but that would
26 /// prevent us from ever removing that bound, or from implementing `MaskElement` on
27 /// non-`PartialEq` types in the future.
29 fn valid<const LANES: usize>(values: Simd<Self, LANES>) -> bool
31 LaneCount<LANES>: SupportedLaneCount,
34 fn eq(self, other: Self) -> bool;
43 /// Marker trait for types that may be used as SIMD mask elements.
44 pub unsafe trait MaskElement: SimdElement + Sealed {}
46 macro_rules! impl_element {
49 fn valid<const LANES: usize>(value: Simd<Self, LANES>) -> bool
51 LaneCount<LANES>: SupportedLaneCount,
53 (value.lanes_eq(Simd::splat(0)) | value.lanes_eq(Simd::splat(-1))).all()
56 fn eq(self, other: Self) -> bool { self == other }
58 const TRUE: Self = -1;
59 const FALSE: Self = 0;
62 unsafe impl MaskElement for $ty {}
70 impl_element! { isize }
72 /// A SIMD vector mask for `LANES` elements of width specified by `Element`.
74 /// The layout of this type is unspecified.
76 pub struct Mask<T, const LANES: usize>(mask_impl::Mask<T, LANES>)
79 LaneCount<LANES>: SupportedLaneCount;
81 impl<T, const LANES: usize> Copy for Mask<T, LANES>
84 LaneCount<LANES>: SupportedLaneCount,
88 impl<T, const LANES: usize> Clone for Mask<T, LANES>
91 LaneCount<LANES>: SupportedLaneCount,
93 fn clone(&self) -> Self {
98 impl<T, const LANES: usize> Mask<T, LANES>
101 LaneCount<LANES>: SupportedLaneCount,
103 /// Construct a mask by setting all lanes to the given value.
104 pub fn splat(value: bool) -> Self {
105 Self(mask_impl::Mask::splat(value))
108 /// Converts an array to a SIMD vector.
109 pub fn from_array(array: [bool; LANES]) -> Self {
110 let mut vector = Self::splat(false);
111 for (i, v) in array.iter().enumerate() {
117 /// Converts a SIMD vector to an array.
118 pub fn to_array(self) -> [bool; LANES] {
119 let mut array = [false; LANES];
120 for (i, v) in array.iter_mut().enumerate() {
126 /// Converts a vector of integers to a mask, where 0 represents `false` and -1
127 /// represents `true`.
130 /// All lanes must be either 0 or -1.
132 pub unsafe fn from_int_unchecked(value: Simd<T, LANES>) -> Self {
133 unsafe { Self(mask_impl::Mask::from_int_unchecked(value)) }
136 /// Converts a vector of integers to a mask, where 0 represents `false` and -1
137 /// represents `true`.
140 /// Panics if any lane is not 0 or -1.
142 pub fn from_int(value: Simd<T, LANES>) -> Self {
143 assert!(T::valid(value), "all values must be either 0 or -1",);
144 unsafe { Self::from_int_unchecked(value) }
147 /// Converts the mask to a vector of integers, where 0 represents `false` and -1
148 /// represents `true`.
150 pub fn to_int(self) -> Simd<T, LANES> {
154 /// Tests the value of the specified lane.
157 /// `lane` must be less than `LANES`.
159 pub unsafe fn test_unchecked(&self, lane: usize) -> bool {
160 unsafe { self.0.test_unchecked(lane) }
163 /// Tests the value of the specified lane.
166 /// Panics if `lane` is greater than or equal to the number of lanes in the vector.
168 pub fn test(&self, lane: usize) -> bool {
169 assert!(lane < LANES, "lane index out of range");
170 unsafe { self.test_unchecked(lane) }
173 /// Sets the value of the specified lane.
176 /// `lane` must be less than `LANES`.
178 pub unsafe fn set_unchecked(&mut self, lane: usize, value: bool) {
180 self.0.set_unchecked(lane, value);
184 /// Sets the value of the specified lane.
187 /// Panics if `lane` is greater than or equal to the number of lanes in the vector.
189 pub fn set(&mut self, lane: usize, value: bool) {
190 assert!(lane < LANES, "lane index out of range");
192 self.set_unchecked(lane, value);
196 /// Convert this mask to a bitmask, with one bit set per lane.
197 #[cfg(feature = "generic_const_exprs")]
198 pub fn to_bitmask(self) -> [u8; LaneCount::<LANES>::BITMASK_LEN] {
202 /// Convert a bitmask to a mask.
203 #[cfg(feature = "generic_const_exprs")]
204 pub fn from_bitmask(bitmask: [u8; LaneCount::<LANES>::BITMASK_LEN]) -> Self {
205 Self(mask_impl::Mask::from_bitmask(bitmask))
208 /// Returns true if any lane is set, or false otherwise.
210 pub fn any(self) -> bool {
214 /// Returns true if all lanes are set, or false otherwise.
216 pub fn all(self) -> bool {
221 // vector/array conversion
222 impl<T, const LANES: usize> From<[bool; LANES]> for Mask<T, LANES>
225 LaneCount<LANES>: SupportedLaneCount,
227 fn from(array: [bool; LANES]) -> Self {
228 Self::from_array(array)
232 impl<T, const LANES: usize> From<Mask<T, LANES>> for [bool; LANES]
235 LaneCount<LANES>: SupportedLaneCount,
237 fn from(vector: Mask<T, LANES>) -> Self {
242 impl<T, const LANES: usize> Default for Mask<T, LANES>
245 LaneCount<LANES>: SupportedLaneCount,
248 fn default() -> Self {
253 impl<T, const LANES: usize> PartialEq for Mask<T, LANES>
255 T: MaskElement + PartialEq,
256 LaneCount<LANES>: SupportedLaneCount,
259 fn eq(&self, other: &Self) -> bool {
264 impl<T, const LANES: usize> PartialOrd for Mask<T, LANES>
266 T: MaskElement + PartialOrd,
267 LaneCount<LANES>: SupportedLaneCount,
270 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
271 self.0.partial_cmp(&other.0)
275 impl<T, const LANES: usize> fmt::Debug for Mask<T, LANES>
277 T: MaskElement + fmt::Debug,
278 LaneCount<LANES>: SupportedLaneCount,
280 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
282 .entries((0..LANES).map(|lane| self.test(lane)))
287 impl<T, const LANES: usize> core::ops::BitAnd for Mask<T, LANES>
290 LaneCount<LANES>: SupportedLaneCount,
294 fn bitand(self, rhs: Self) -> Self {
299 impl<T, const LANES: usize> core::ops::BitAnd<bool> for Mask<T, LANES>
302 LaneCount<LANES>: SupportedLaneCount,
306 fn bitand(self, rhs: bool) -> Self {
307 self & Self::splat(rhs)
311 impl<T, const LANES: usize> core::ops::BitAnd<Mask<T, LANES>> for bool
314 LaneCount<LANES>: SupportedLaneCount,
316 type Output = Mask<T, LANES>;
318 fn bitand(self, rhs: Mask<T, LANES>) -> Mask<T, LANES> {
319 Mask::splat(self) & rhs
323 impl<T, const LANES: usize> core::ops::BitOr for Mask<T, LANES>
326 LaneCount<LANES>: SupportedLaneCount,
330 fn bitor(self, rhs: Self) -> Self {
335 impl<T, const LANES: usize> core::ops::BitOr<bool> for Mask<T, LANES>
338 LaneCount<LANES>: SupportedLaneCount,
342 fn bitor(self, rhs: bool) -> Self {
343 self | Self::splat(rhs)
347 impl<T, const LANES: usize> core::ops::BitOr<Mask<T, LANES>> for bool
350 LaneCount<LANES>: SupportedLaneCount,
352 type Output = Mask<T, LANES>;
354 fn bitor(self, rhs: Mask<T, LANES>) -> Mask<T, LANES> {
355 Mask::splat(self) | rhs
359 impl<T, const LANES: usize> core::ops::BitXor for Mask<T, LANES>
362 LaneCount<LANES>: SupportedLaneCount,
366 fn bitxor(self, rhs: Self) -> Self::Output {
371 impl<T, const LANES: usize> core::ops::BitXor<bool> for Mask<T, LANES>
374 LaneCount<LANES>: SupportedLaneCount,
378 fn bitxor(self, rhs: bool) -> Self::Output {
379 self ^ Self::splat(rhs)
383 impl<T, const LANES: usize> core::ops::BitXor<Mask<T, LANES>> for bool
386 LaneCount<LANES>: SupportedLaneCount,
388 type Output = Mask<T, LANES>;
390 fn bitxor(self, rhs: Mask<T, LANES>) -> Self::Output {
391 Mask::splat(self) ^ rhs
395 impl<T, const LANES: usize> core::ops::Not for Mask<T, LANES>
398 LaneCount<LANES>: SupportedLaneCount,
400 type Output = Mask<T, LANES>;
402 fn not(self) -> Self::Output {
407 impl<T, const LANES: usize> core::ops::BitAndAssign for Mask<T, LANES>
410 LaneCount<LANES>: SupportedLaneCount,
413 fn bitand_assign(&mut self, rhs: Self) {
414 self.0 = self.0 & rhs.0;
418 impl<T, const LANES: usize> core::ops::BitAndAssign<bool> for Mask<T, LANES>
421 LaneCount<LANES>: SupportedLaneCount,
424 fn bitand_assign(&mut self, rhs: bool) {
425 *self &= Self::splat(rhs);
429 impl<T, const LANES: usize> core::ops::BitOrAssign for Mask<T, LANES>
432 LaneCount<LANES>: SupportedLaneCount,
435 fn bitor_assign(&mut self, rhs: Self) {
436 self.0 = self.0 | rhs.0;
440 impl<T, const LANES: usize> core::ops::BitOrAssign<bool> for Mask<T, LANES>
443 LaneCount<LANES>: SupportedLaneCount,
446 fn bitor_assign(&mut self, rhs: bool) {
447 *self |= Self::splat(rhs);
451 impl<T, const LANES: usize> core::ops::BitXorAssign for Mask<T, LANES>
454 LaneCount<LANES>: SupportedLaneCount,
457 fn bitxor_assign(&mut self, rhs: Self) {
458 self.0 = self.0 ^ rhs.0;
462 impl<T, const LANES: usize> core::ops::BitXorAssign<bool> for Mask<T, LANES>
465 LaneCount<LANES>: SupportedLaneCount,
468 fn bitxor_assign(&mut self, rhs: bool) {
469 *self ^= Self::splat(rhs);
473 /// Vector of eight 8-bit masks
474 pub type mask8x8 = Mask<i8, 8>;
476 /// Vector of 16 8-bit masks
477 pub type mask8x16 = Mask<i8, 16>;
479 /// Vector of 32 8-bit masks
480 pub type mask8x32 = Mask<i8, 32>;
482 /// Vector of 16 8-bit masks
483 pub type mask8x64 = Mask<i8, 64>;
485 /// Vector of four 16-bit masks
486 pub type mask16x4 = Mask<i16, 4>;
488 /// Vector of eight 16-bit masks
489 pub type mask16x8 = Mask<i16, 8>;
491 /// Vector of 16 16-bit masks
492 pub type mask16x16 = Mask<i16, 16>;
494 /// Vector of 32 16-bit masks
495 pub type mask16x32 = Mask<i32, 32>;
497 /// Vector of two 32-bit masks
498 pub type mask32x2 = Mask<i32, 2>;
500 /// Vector of four 32-bit masks
501 pub type mask32x4 = Mask<i32, 4>;
503 /// Vector of eight 32-bit masks
504 pub type mask32x8 = Mask<i32, 8>;
506 /// Vector of 16 32-bit masks
507 pub type mask32x16 = Mask<i32, 16>;
509 /// Vector of two 64-bit masks
510 pub type mask64x2 = Mask<i64, 2>;
512 /// Vector of four 64-bit masks
513 pub type mask64x4 = Mask<i64, 4>;
515 /// Vector of eight 64-bit masks
516 pub type mask64x8 = Mask<i64, 8>;
518 /// Vector of two pointer-width masks
519 pub type masksizex2 = Mask<isize, 2>;
521 /// Vector of four pointer-width masks
522 pub type masksizex4 = Mask<isize, 4>;
524 /// Vector of eight pointer-width masks
525 pub type masksizex8 = Mask<isize, 8>;
527 macro_rules! impl_from {
528 { $from:ty => $($to:ty),* } => {
530 impl<const LANES: usize> From<Mask<$from, LANES>> for Mask<$to, LANES>
532 LaneCount<LANES>: SupportedLaneCount,
534 fn from(value: Mask<$from, LANES>) -> Self {
535 Self(value.0.convert())
541 impl_from! { i8 => i16, i32, i64, isize }
542 impl_from! { i16 => i32, i64, isize, i8 }
543 impl_from! { i32 => i64, isize, i8, i16 }
544 impl_from! { i64 => isize, i8, i16, i32 }
545 impl_from! { isize => i8, i16, i32, i64 }