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::{SimdI16, SimdI32, SimdI64, SimdI8, SimdIsize};
21 /// Helper trait for mask types.
22 pub trait Mask: sealed::Sealed {
23 /// The number of lanes for this mask.
26 /// Generates a mask with the same value in every lane.
28 fn splat(val: bool) -> Self;
31 macro_rules! define_opaque_mask {
34 struct $name:ident<const $lanes:ident: usize>($inner_ty:ty);
38 #[allow(non_camel_case_types)]
39 pub struct $name<const LANES: usize>($inner_ty)
41 crate::LaneCount<LANES>: crate::SupportedLaneCount;
43 impl<const LANES: usize> sealed::Sealed for $name<LANES>
45 crate::LaneCount<LANES>: crate::SupportedLaneCount,
48 impl<const LANES: usize> Mask for $name<LANES>
50 crate::LaneCount<LANES>: crate::SupportedLaneCount,
52 const LANES: usize = LANES;
55 fn splat(value: bool) -> Self {
60 impl_opaque_mask_reductions! { $name, $bits_ty }
62 impl<const LANES: usize> $name<LANES>
64 crate::LaneCount<LANES>: crate::SupportedLaneCount,
66 /// Construct a mask by setting all lanes to the given value.
67 pub fn splat(value: bool) -> Self {
68 Self(<$inner_ty>::splat(value))
71 /// Converts an array to a SIMD vector.
72 pub fn from_array(array: [bool; LANES]) -> Self {
73 let mut vector = Self::splat(false);
76 vector.set(i, array[i]);
82 /// Converts a SIMD vector to an array.
83 pub fn to_array(self) -> [bool; LANES] {
84 let mut array = [false; LANES];
87 array[i] = self.test(i);
93 /// Converts a vector of integers to a mask, where 0 represents `false` and -1
94 /// represents `true`.
97 /// All lanes must be either 0 or -1.
99 pub unsafe fn from_int_unchecked(value: $bits_ty<LANES>) -> Self {
100 Self(<$inner_ty>::from_int_unchecked(value))
103 /// Converts a vector of integers to a mask, where 0 represents `false` and -1
104 /// represents `true`.
107 /// Panics if any lane is not 0 or -1.
109 pub fn from_int(value: $bits_ty<LANES>) -> Self {
111 (value.lanes_eq($bits_ty::splat(0)) | value.lanes_eq($bits_ty::splat(-1))).all(),
112 "all values must be either 0 or -1",
114 unsafe { Self::from_int_unchecked(value) }
117 /// Converts the mask to a vector of integers, where 0 represents `false` and -1
118 /// represents `true`.
120 pub fn to_int(self) -> $bits_ty<LANES> {
124 /// Tests the value of the specified lane.
127 /// `lane` must be less than `LANES`.
129 pub unsafe fn test_unchecked(&self, lane: usize) -> bool {
130 self.0.test_unchecked(lane)
133 /// Tests the value of the specified lane.
136 /// Panics if `lane` is greater than or equal to the number of lanes in the vector.
138 pub fn test(&self, lane: usize) -> bool {
139 assert!(lane < LANES, "lane index out of range");
140 unsafe { self.test_unchecked(lane) }
143 /// Sets the value of the specified lane.
146 /// `lane` must be less than `LANES`.
148 pub unsafe fn set_unchecked(&mut self, lane: usize, value: bool) {
149 self.0.set_unchecked(lane, value);
152 /// Sets the value of the specified lane.
155 /// Panics if `lane` is greater than or equal to the number of lanes in the vector.
157 pub fn set(&mut self, lane: usize, value: bool) {
158 assert!(lane < LANES, "lane index out of range");
159 unsafe { self.set_unchecked(lane, value); }
162 /// Convert this mask to a bitmask, with one bit set per lane.
163 pub fn to_bitmask(self) -> [u8; crate::LaneCount::<LANES>::BITMASK_LEN] {
167 /// Convert a bitmask to a mask.
168 pub fn from_bitmask(bitmask: [u8; crate::LaneCount::<LANES>::BITMASK_LEN]) -> Self {
169 Self(<$inner_ty>::from_bitmask(bitmask))
173 // vector/array conversion
174 impl<const LANES: usize> From<[bool; LANES]> for $name<LANES>
176 crate::LaneCount<LANES>: crate::SupportedLaneCount,
178 fn from(array: [bool; LANES]) -> Self {
179 Self::from_array(array)
183 impl <const LANES: usize> From<$name<LANES>> for [bool; LANES]
185 crate::LaneCount<LANES>: crate::SupportedLaneCount,
187 fn from(vector: $name<LANES>) -> Self {
192 impl<const LANES: usize> Copy for $name<LANES>
194 crate::LaneCount<LANES>: crate::SupportedLaneCount,
197 impl<const LANES: usize> Clone for $name<LANES>
199 crate::LaneCount<LANES>: crate::SupportedLaneCount,
202 fn clone(&self) -> Self {
207 impl<const LANES: usize> Default for $name<LANES>
209 crate::LaneCount<LANES>: crate::SupportedLaneCount,
212 fn default() -> Self {
217 impl<const LANES: usize> PartialEq for $name<LANES>
219 crate::LaneCount<LANES>: crate::SupportedLaneCount,
222 fn eq(&self, other: &Self) -> bool {
227 impl<const LANES: usize> PartialOrd for $name<LANES>
229 crate::LaneCount<LANES>: crate::SupportedLaneCount,
232 fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
233 self.0.partial_cmp(&other.0)
237 impl<const LANES: usize> core::fmt::Debug for $name<LANES>
239 crate::LaneCount<LANES>: crate::SupportedLaneCount,
241 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
243 .entries((0..LANES).map(|lane| self.test(lane)))
248 impl<const LANES: usize> core::ops::BitAnd for $name<LANES>
250 crate::LaneCount<LANES>: crate::SupportedLaneCount,
254 fn bitand(self, rhs: Self) -> Self {
259 impl<const LANES: usize> core::ops::BitAnd<bool> for $name<LANES>
261 crate::LaneCount<LANES>: crate::SupportedLaneCount,
265 fn bitand(self, rhs: bool) -> Self {
266 self & Self::splat(rhs)
270 impl<const LANES: usize> core::ops::BitAnd<$name<LANES>> for bool
272 crate::LaneCount<LANES>: crate::SupportedLaneCount,
274 type Output = $name<LANES>;
276 fn bitand(self, rhs: $name<LANES>) -> $name<LANES> {
277 $name::<LANES>::splat(self) & rhs
281 impl<const LANES: usize> core::ops::BitOr for $name<LANES>
283 crate::LaneCount<LANES>: crate::SupportedLaneCount,
287 fn bitor(self, rhs: Self) -> Self {
292 impl<const LANES: usize> core::ops::BitOr<bool> for $name<LANES>
294 crate::LaneCount<LANES>: crate::SupportedLaneCount,
298 fn bitor(self, rhs: bool) -> Self {
299 self | Self::splat(rhs)
303 impl<const LANES: usize> core::ops::BitOr<$name<LANES>> for bool
305 crate::LaneCount<LANES>: crate::SupportedLaneCount,
307 type Output = $name<LANES>;
309 fn bitor(self, rhs: $name<LANES>) -> $name<LANES> {
310 $name::<LANES>::splat(self) | rhs
314 impl<const LANES: usize> core::ops::BitXor for $name<LANES>
316 crate::LaneCount<LANES>: crate::SupportedLaneCount,
320 fn bitxor(self, rhs: Self) -> Self::Output {
325 impl<const LANES: usize> core::ops::BitXor<bool> for $name<LANES>
327 crate::LaneCount<LANES>: crate::SupportedLaneCount,
331 fn bitxor(self, rhs: bool) -> Self::Output {
332 self ^ Self::splat(rhs)
336 impl<const LANES: usize> core::ops::BitXor<$name<LANES>> for bool
338 crate::LaneCount<LANES>: crate::SupportedLaneCount,
340 type Output = $name<LANES>;
342 fn bitxor(self, rhs: $name<LANES>) -> Self::Output {
343 $name::<LANES>::splat(self) ^ rhs
347 impl<const LANES: usize> core::ops::Not for $name<LANES>
349 crate::LaneCount<LANES>: crate::SupportedLaneCount,
351 type Output = $name<LANES>;
353 fn not(self) -> Self::Output {
358 impl<const LANES: usize> core::ops::BitAndAssign for $name<LANES>
360 crate::LaneCount<LANES>: crate::SupportedLaneCount,
363 fn bitand_assign(&mut self, rhs: Self) {
364 self.0 = self.0 & rhs.0;
368 impl<const LANES: usize> core::ops::BitAndAssign<bool> for $name<LANES>
370 crate::LaneCount<LANES>: crate::SupportedLaneCount,
373 fn bitand_assign(&mut self, rhs: bool) {
374 *self &= Self::splat(rhs);
378 impl<const LANES: usize> core::ops::BitOrAssign for $name<LANES>
380 crate::LaneCount<LANES>: crate::SupportedLaneCount,
383 fn bitor_assign(&mut self, rhs: Self) {
384 self.0 = self.0 | rhs.0;
388 impl<const LANES: usize> core::ops::BitOrAssign<bool> for $name<LANES>
390 crate::LaneCount<LANES>: crate::SupportedLaneCount,
393 fn bitor_assign(&mut self, rhs: bool) {
394 *self |= Self::splat(rhs);
398 impl<const LANES: usize> core::ops::BitXorAssign for $name<LANES>
400 crate::LaneCount<LANES>: crate::SupportedLaneCount,
403 fn bitxor_assign(&mut self, rhs: Self) {
404 self.0 = self.0 ^ rhs.0;
408 impl<const LANES: usize> core::ops::BitXorAssign<bool> for $name<LANES>
410 crate::LaneCount<LANES>: crate::SupportedLaneCount,
413 fn bitxor_assign(&mut self, rhs: bool) {
414 *self ^= Self::splat(rhs);
420 define_opaque_mask! {
421 /// Mask for vectors with `LANES` 8-bit elements.
423 /// The layout of this type is unspecified.
424 struct Mask8<const LANES: usize>(mask_impl::Mask8<LANES>);
428 define_opaque_mask! {
429 /// Mask for vectors with `LANES` 16-bit elements.
431 /// The layout of this type is unspecified.
432 struct Mask16<const LANES: usize>(mask_impl::Mask16<LANES>);
436 define_opaque_mask! {
437 /// Mask for vectors with `LANES` 32-bit elements.
439 /// The layout of this type is unspecified.
440 struct Mask32<const LANES: usize>(mask_impl::Mask32<LANES>);
444 define_opaque_mask! {
445 /// Mask for vectors with `LANES` 64-bit elements.
447 /// The layout of this type is unspecified.
448 struct Mask64<const LANES: usize>(mask_impl::Mask64<LANES>);
452 define_opaque_mask! {
453 /// Mask for vectors with `LANES` pointer-width elements.
455 /// The layout of this type is unspecified.
456 struct MaskSize<const LANES: usize>(mask_impl::MaskSize<LANES>);
460 /// Vector of eight 8-bit masks
461 pub type mask8x8 = Mask8<8>;
463 /// Vector of 16 8-bit masks
464 pub type mask8x16 = Mask8<16>;
466 /// Vector of 32 8-bit masks
467 pub type mask8x32 = Mask8<32>;
469 /// Vector of 16 8-bit masks
470 pub type mask8x64 = Mask8<64>;
472 /// Vector of four 16-bit masks
473 pub type mask16x4 = Mask16<4>;
475 /// Vector of eight 16-bit masks
476 pub type mask16x8 = Mask16<8>;
478 /// Vector of 16 16-bit masks
479 pub type mask16x16 = Mask16<16>;
481 /// Vector of 32 16-bit masks
482 pub type mask16x32 = Mask32<32>;
484 /// Vector of two 32-bit masks
485 pub type mask32x2 = Mask32<2>;
487 /// Vector of four 32-bit masks
488 pub type mask32x4 = Mask32<4>;
490 /// Vector of eight 32-bit masks
491 pub type mask32x8 = Mask32<8>;
493 /// Vector of 16 32-bit masks
494 pub type mask32x16 = Mask32<16>;
496 /// Vector of two 64-bit masks
497 pub type mask64x2 = Mask64<2>;
499 /// Vector of four 64-bit masks
500 pub type mask64x4 = Mask64<4>;
502 /// Vector of eight 64-bit masks
503 pub type mask64x8 = Mask64<8>;
505 /// Vector of two pointer-width masks
506 pub type masksizex2 = MaskSize<2>;
508 /// Vector of four pointer-width masks
509 pub type masksizex4 = MaskSize<4>;
511 /// Vector of eight pointer-width masks
512 pub type masksizex8 = MaskSize<8>;
514 macro_rules! impl_from {
515 { $from:ident ($from_inner:ident) => $($to:ident ($to_inner:ident)),* } => {
517 impl<const LANES: usize> From<$from<LANES>> for $to<LANES>
519 crate::LaneCount<LANES>: crate::SupportedLaneCount,
521 fn from(value: $from<LANES>) -> Self {
528 impl_from! { Mask8 (SimdI8) => Mask16 (SimdI16), Mask32 (SimdI32), Mask64 (SimdI64), MaskSize (SimdIsize) }
529 impl_from! { Mask16 (SimdI16) => Mask32 (SimdI32), Mask64 (SimdI64), MaskSize (SimdIsize), Mask8 (SimdI8) }
530 impl_from! { Mask32 (SimdI32) => Mask64 (SimdI64), MaskSize (SimdIsize), Mask8 (SimdI8), Mask16 (SimdI16) }
531 impl_from! { Mask64 (SimdI64) => MaskSize (SimdIsize), Mask8 (SimdI8), Mask16 (SimdI16), Mask32 (SimdI32) }
532 impl_from! { MaskSize (SimdIsize) => Mask8 (SimdI8), Mask16 (SimdI16), Mask32 (SimdI32), Mask64 (SimdI64) }