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"
16 pub use to_bitmask::ToBitMask;
18 #[cfg(feature = "generic_const_exprs")]
19 pub use to_bitmask::{bitmask_len, ToBitMaskArray};
21 use crate::simd::{intrinsics, LaneCount, Simd, SimdElement, SimdPartialEq, SupportedLaneCount};
22 use core::cmp::Ordering;
28 /// Not only does this seal the `MaskElement` trait, but these functions prevent other traits
29 /// from bleeding into the parent bounds.
31 /// For example, `eq` could be provided by requiring `MaskElement: PartialEq`, but that would
32 /// prevent us from ever removing that bound, or from implementing `MaskElement` on
33 /// non-`PartialEq` types in the future.
35 fn valid<const LANES: usize>(values: Simd<Self, LANES>) -> bool
37 LaneCount<LANES>: SupportedLaneCount,
40 fn eq(self, other: Self) -> bool;
49 /// Marker trait for types that may be used as SIMD mask elements.
52 /// Type must be a signed integer.
53 pub unsafe trait MaskElement: SimdElement + Sealed {}
55 macro_rules! impl_element {
58 fn valid<const LANES: usize>(value: Simd<Self, LANES>) -> bool
60 LaneCount<LANES>: SupportedLaneCount,
62 (value.simd_eq(Simd::splat(0 as _)) | value.simd_eq(Simd::splat(-1 as _))).all()
65 fn eq(self, other: Self) -> bool { self == other }
67 const TRUE: Self = -1;
68 const FALSE: Self = 0;
71 // Safety: this is a valid mask element type
72 unsafe impl MaskElement for $ty {}
80 impl_element! { isize }
82 /// A SIMD vector mask for `LANES` elements of width specified by `Element`.
84 /// Masks represent boolean inclusion/exclusion on a per-lane basis.
86 /// The layout of this type is unspecified.
88 pub struct Mask<T, const LANES: usize>(mask_impl::Mask<T, LANES>)
91 LaneCount<LANES>: SupportedLaneCount;
93 impl<T, const LANES: usize> Copy for Mask<T, LANES>
96 LaneCount<LANES>: SupportedLaneCount,
100 impl<T, const LANES: usize> Clone for Mask<T, LANES>
103 LaneCount<LANES>: SupportedLaneCount,
105 fn clone(&self) -> Self {
110 impl<T, const LANES: usize> Mask<T, LANES>
113 LaneCount<LANES>: SupportedLaneCount,
115 /// Construct a mask by setting all lanes to the given value.
116 pub fn splat(value: bool) -> Self {
117 Self(mask_impl::Mask::splat(value))
120 /// Converts an array of bools to a SIMD mask.
121 pub fn from_array(array: [bool; LANES]) -> Self {
122 // SAFETY: Rust's bool has a layout of 1 byte (u8) with a value of
123 // true: 0b_0000_0001
124 // false: 0b_0000_0000
125 // Thus, an array of bools is also a valid array of bytes: [u8; N]
126 // This would be hypothetically valid as an "in-place" transmute,
127 // but these are "dependently-sized" types, so copy elision it is!
129 let bytes: [u8; LANES] = mem::transmute_copy(&array);
130 let bools: Simd<i8, LANES> =
131 intrinsics::simd_ne(Simd::from_array(bytes), Simd::splat(0u8));
132 Mask::from_int_unchecked(intrinsics::simd_cast(bools))
136 /// Converts a SIMD mask to an array of bools.
137 pub fn to_array(self) -> [bool; LANES] {
138 // This follows mostly the same logic as from_array.
139 // SAFETY: Rust's bool has a layout of 1 byte (u8) with a value of
140 // true: 0b_0000_0001
141 // false: 0b_0000_0000
142 // Thus, an array of bools is also a valid array of bytes: [u8; N]
143 // Since our masks are equal to integers where all bits are set,
144 // we can simply convert them to i8s, and then bitand them by the
145 // bitpattern for Rust's "true" bool.
146 // This would be hypothetically valid as an "in-place" transmute,
147 // but these are "dependently-sized" types, so copy elision it is!
149 let mut bytes: Simd<i8, LANES> = intrinsics::simd_cast(self.to_int());
150 bytes &= Simd::splat(1i8);
151 mem::transmute_copy(&bytes)
155 /// Converts a vector of integers to a mask, where 0 represents `false` and -1
156 /// represents `true`.
159 /// All lanes must be either 0 or -1.
161 #[must_use = "method returns a new mask and does not mutate the original value"]
162 pub unsafe fn from_int_unchecked(value: Simd<T, LANES>) -> Self {
163 // Safety: the caller must confirm this invariant
164 unsafe { Self(mask_impl::Mask::from_int_unchecked(value)) }
167 /// Converts a vector of integers to a mask, where 0 represents `false` and -1
168 /// represents `true`.
171 /// Panics if any lane is not 0 or -1.
173 #[must_use = "method returns a new mask and does not mutate the original value"]
174 pub fn from_int(value: Simd<T, LANES>) -> Self {
175 assert!(T::valid(value), "all values must be either 0 or -1",);
176 // Safety: the validity has been checked
177 unsafe { Self::from_int_unchecked(value) }
180 /// Converts the mask to a vector of integers, where 0 represents `false` and -1
181 /// represents `true`.
183 #[must_use = "method returns a new vector and does not mutate the original value"]
184 pub fn to_int(self) -> Simd<T, LANES> {
188 /// Converts the mask to a mask of any other lane size.
190 #[must_use = "method returns a new mask and does not mutate the original value"]
191 pub fn cast<U: MaskElement>(self) -> Mask<U, LANES> {
192 Mask(self.0.convert())
195 /// Tests the value of the specified lane.
198 /// `lane` must be less than `LANES`.
200 #[must_use = "method returns a new bool and does not mutate the original value"]
201 pub unsafe fn test_unchecked(&self, lane: usize) -> bool {
202 // Safety: the caller must confirm this invariant
203 unsafe { self.0.test_unchecked(lane) }
206 /// Tests the value of the specified lane.
209 /// Panics if `lane` is greater than or equal to the number of lanes in the vector.
211 #[must_use = "method returns a new bool and does not mutate the original value"]
212 pub fn test(&self, lane: usize) -> bool {
213 assert!(lane < LANES, "lane index out of range");
214 // Safety: the lane index has been checked
215 unsafe { self.test_unchecked(lane) }
218 /// Sets the value of the specified lane.
221 /// `lane` must be less than `LANES`.
223 pub unsafe fn set_unchecked(&mut self, lane: usize, value: bool) {
224 // Safety: the caller must confirm this invariant
226 self.0.set_unchecked(lane, value);
230 /// Sets the value of the specified lane.
233 /// Panics if `lane` is greater than or equal to the number of lanes in the vector.
235 pub fn set(&mut self, lane: usize, value: bool) {
236 assert!(lane < LANES, "lane index out of range");
237 // Safety: the lane index has been checked
239 self.set_unchecked(lane, value);
243 /// Returns true if any lane is set, or false otherwise.
245 #[must_use = "method returns a new bool and does not mutate the original value"]
246 pub fn any(self) -> bool {
250 /// Returns true if all lanes are set, or false otherwise.
252 #[must_use = "method returns a new bool and does not mutate the original value"]
253 pub fn all(self) -> bool {
258 // vector/array conversion
259 impl<T, const LANES: usize> From<[bool; LANES]> for Mask<T, LANES>
262 LaneCount<LANES>: SupportedLaneCount,
264 fn from(array: [bool; LANES]) -> Self {
265 Self::from_array(array)
269 impl<T, const LANES: usize> From<Mask<T, LANES>> for [bool; LANES]
272 LaneCount<LANES>: SupportedLaneCount,
274 fn from(vector: Mask<T, LANES>) -> Self {
279 impl<T, const LANES: usize> Default for Mask<T, LANES>
282 LaneCount<LANES>: SupportedLaneCount,
285 #[must_use = "method returns a defaulted mask with all lanes set to false (0)"]
286 fn default() -> Self {
291 impl<T, const LANES: usize> PartialEq for Mask<T, LANES>
293 T: MaskElement + PartialEq,
294 LaneCount<LANES>: SupportedLaneCount,
297 #[must_use = "method returns a new bool and does not mutate the original value"]
298 fn eq(&self, other: &Self) -> bool {
303 impl<T, const LANES: usize> PartialOrd for Mask<T, LANES>
305 T: MaskElement + PartialOrd,
306 LaneCount<LANES>: SupportedLaneCount,
309 #[must_use = "method returns a new Ordering and does not mutate the original value"]
310 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
311 self.0.partial_cmp(&other.0)
315 impl<T, const LANES: usize> fmt::Debug for Mask<T, LANES>
317 T: MaskElement + fmt::Debug,
318 LaneCount<LANES>: SupportedLaneCount,
320 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
322 .entries((0..LANES).map(|lane| self.test(lane)))
327 impl<T, const LANES: usize> core::ops::BitAnd for Mask<T, LANES>
330 LaneCount<LANES>: SupportedLaneCount,
334 #[must_use = "method returns a new mask and does not mutate the original value"]
335 fn bitand(self, rhs: Self) -> Self {
340 impl<T, const LANES: usize> core::ops::BitAnd<bool> for Mask<T, LANES>
343 LaneCount<LANES>: SupportedLaneCount,
347 #[must_use = "method returns a new mask and does not mutate the original value"]
348 fn bitand(self, rhs: bool) -> Self {
349 self & Self::splat(rhs)
353 impl<T, const LANES: usize> core::ops::BitAnd<Mask<T, LANES>> for bool
356 LaneCount<LANES>: SupportedLaneCount,
358 type Output = Mask<T, LANES>;
360 #[must_use = "method returns a new mask and does not mutate the original value"]
361 fn bitand(self, rhs: Mask<T, LANES>) -> Mask<T, LANES> {
362 Mask::splat(self) & rhs
366 impl<T, const LANES: usize> core::ops::BitOr for Mask<T, LANES>
369 LaneCount<LANES>: SupportedLaneCount,
373 #[must_use = "method returns a new mask and does not mutate the original value"]
374 fn bitor(self, rhs: Self) -> Self {
379 impl<T, const LANES: usize> core::ops::BitOr<bool> for Mask<T, LANES>
382 LaneCount<LANES>: SupportedLaneCount,
386 #[must_use = "method returns a new mask and does not mutate the original value"]
387 fn bitor(self, rhs: bool) -> Self {
388 self | Self::splat(rhs)
392 impl<T, const LANES: usize> core::ops::BitOr<Mask<T, LANES>> for bool
395 LaneCount<LANES>: SupportedLaneCount,
397 type Output = Mask<T, LANES>;
399 #[must_use = "method returns a new mask and does not mutate the original value"]
400 fn bitor(self, rhs: Mask<T, LANES>) -> Mask<T, LANES> {
401 Mask::splat(self) | rhs
405 impl<T, const LANES: usize> core::ops::BitXor for Mask<T, LANES>
408 LaneCount<LANES>: SupportedLaneCount,
412 #[must_use = "method returns a new mask and does not mutate the original value"]
413 fn bitxor(self, rhs: Self) -> Self::Output {
418 impl<T, const LANES: usize> core::ops::BitXor<bool> for Mask<T, LANES>
421 LaneCount<LANES>: SupportedLaneCount,
425 #[must_use = "method returns a new mask and does not mutate the original value"]
426 fn bitxor(self, rhs: bool) -> Self::Output {
427 self ^ Self::splat(rhs)
431 impl<T, const LANES: usize> core::ops::BitXor<Mask<T, LANES>> for bool
434 LaneCount<LANES>: SupportedLaneCount,
436 type Output = Mask<T, LANES>;
438 #[must_use = "method returns a new mask and does not mutate the original value"]
439 fn bitxor(self, rhs: Mask<T, LANES>) -> Self::Output {
440 Mask::splat(self) ^ rhs
444 impl<T, const LANES: usize> core::ops::Not for Mask<T, LANES>
447 LaneCount<LANES>: SupportedLaneCount,
449 type Output = Mask<T, LANES>;
451 #[must_use = "method returns a new mask and does not mutate the original value"]
452 fn not(self) -> Self::Output {
457 impl<T, const LANES: usize> core::ops::BitAndAssign for Mask<T, LANES>
460 LaneCount<LANES>: SupportedLaneCount,
463 fn bitand_assign(&mut self, rhs: Self) {
464 self.0 = self.0 & rhs.0;
468 impl<T, const LANES: usize> core::ops::BitAndAssign<bool> for Mask<T, LANES>
471 LaneCount<LANES>: SupportedLaneCount,
474 fn bitand_assign(&mut self, rhs: bool) {
475 *self &= Self::splat(rhs);
479 impl<T, const LANES: usize> core::ops::BitOrAssign for Mask<T, LANES>
482 LaneCount<LANES>: SupportedLaneCount,
485 fn bitor_assign(&mut self, rhs: Self) {
486 self.0 = self.0 | rhs.0;
490 impl<T, const LANES: usize> core::ops::BitOrAssign<bool> for Mask<T, LANES>
493 LaneCount<LANES>: SupportedLaneCount,
496 fn bitor_assign(&mut self, rhs: bool) {
497 *self |= Self::splat(rhs);
501 impl<T, const LANES: usize> core::ops::BitXorAssign for Mask<T, LANES>
504 LaneCount<LANES>: SupportedLaneCount,
507 fn bitxor_assign(&mut self, rhs: Self) {
508 self.0 = self.0 ^ rhs.0;
512 impl<T, const LANES: usize> core::ops::BitXorAssign<bool> for Mask<T, LANES>
515 LaneCount<LANES>: SupportedLaneCount,
518 fn bitxor_assign(&mut self, rhs: bool) {
519 *self ^= Self::splat(rhs);
523 /// A mask for SIMD vectors with eight elements of 8 bits.
524 pub type mask8x8 = Mask<i8, 8>;
526 /// A mask for SIMD vectors with 16 elements of 8 bits.
527 pub type mask8x16 = Mask<i8, 16>;
529 /// A mask for SIMD vectors with 32 elements of 8 bits.
530 pub type mask8x32 = Mask<i8, 32>;
532 /// A mask for SIMD vectors with 64 elements of 8 bits.
533 pub type mask8x64 = Mask<i8, 64>;
535 /// A mask for SIMD vectors with four elements of 16 bits.
536 pub type mask16x4 = Mask<i16, 4>;
538 /// A mask for SIMD vectors with eight elements of 16 bits.
539 pub type mask16x8 = Mask<i16, 8>;
541 /// A mask for SIMD vectors with 16 elements of 16 bits.
542 pub type mask16x16 = Mask<i16, 16>;
544 /// A mask for SIMD vectors with 32 elements of 16 bits.
545 pub type mask16x32 = Mask<i16, 32>;
547 /// A mask for SIMD vectors with two elements of 32 bits.
548 pub type mask32x2 = Mask<i32, 2>;
550 /// A mask for SIMD vectors with four elements of 32 bits.
551 pub type mask32x4 = Mask<i32, 4>;
553 /// A mask for SIMD vectors with eight elements of 32 bits.
554 pub type mask32x8 = Mask<i32, 8>;
556 /// A mask for SIMD vectors with 16 elements of 32 bits.
557 pub type mask32x16 = Mask<i32, 16>;
559 /// A mask for SIMD vectors with two elements of 64 bits.
560 pub type mask64x2 = Mask<i64, 2>;
562 /// A mask for SIMD vectors with four elements of 64 bits.
563 pub type mask64x4 = Mask<i64, 4>;
565 /// A mask for SIMD vectors with eight elements of 64 bits.
566 pub type mask64x8 = Mask<i64, 8>;
568 /// A mask for SIMD vectors with two elements of pointer width.
569 pub type masksizex2 = Mask<isize, 2>;
571 /// A mask for SIMD vectors with four elements of pointer width.
572 pub type masksizex4 = Mask<isize, 4>;
574 /// A mask for SIMD vectors with eight elements of pointer width.
575 pub type masksizex8 = Mask<isize, 8>;
577 macro_rules! impl_from {
578 { $from:ty => $($to:ty),* } => {
580 impl<const LANES: usize> From<Mask<$from, LANES>> for Mask<$to, LANES>
582 LaneCount<LANES>: SupportedLaneCount,
584 fn from(value: Mask<$from, LANES>) -> Self {
591 impl_from! { i8 => i16, i32, i64, isize }
592 impl_from! { i16 => i32, i64, isize, i8 }
593 impl_from! { i32 => i64, isize, i8, i16 }
594 impl_from! { i64 => isize, i8, i16, i32 }
595 impl_from! { isize => i8, i16, i32, i64 }