1 //! Types and traits associated with masking lanes of vectors.
2 #![allow(non_camel_case_types)]
10 macro_rules! define_opaque_mask {
13 struct $name:ident<const $lanes:ident: usize>($inner_ty:ty);
16 #[allow(non_camel_case_types)]
17 pub struct $name<const $lanes: usize>($inner_ty) where BitMask<LANES>: LanesAtMost64;
19 impl<const $lanes: usize> $name<$lanes> where BitMask<$lanes>: LanesAtMost64 {
20 /// Construct a mask by setting all lanes to the given value.
21 pub fn splat(value: bool) -> Self {
22 Self(<$inner_ty>::splat(value))
25 /// Tests the value of the specified lane.
28 /// Panics if `lane` is greater than or equal to the number of lanes in the vector.
30 pub fn test(&self, lane: usize) -> bool {
34 /// Sets the value of the specified lane.
37 /// Panics if `lane` is greater than or equal to the number of lanes in the vector.
39 pub fn set(&mut self, lane: usize, value: bool) {
40 self.0.set(lane, value);
44 impl<const $lanes: usize> From<BitMask<$lanes>> for $name<$lanes>
46 BitMask<$lanes>: LanesAtMost64,
48 fn from(value: BitMask<$lanes>) -> Self {
53 impl<const $lanes: usize> From<$name<$lanes>> for crate::BitMask<$lanes>
55 BitMask<$lanes>: LanesAtMost64,
57 fn from(value: $name<$lanes>) -> Self {
62 impl<const $lanes: usize> Copy for $name<$lanes> where BitMask<$lanes>: LanesAtMost64 {}
64 impl<const $lanes: usize> Clone for $name<$lanes> where BitMask<$lanes>: LanesAtMost64 {
66 fn clone(&self) -> Self {
71 impl<const $lanes: usize> Default for $name<$lanes> where BitMask<$lanes>: LanesAtMost64 {
73 fn default() -> Self {
78 impl<const $lanes: usize> PartialEq for $name<$lanes> where BitMask<$lanes>: LanesAtMost64 {
80 fn eq(&self, other: &Self) -> bool {
85 impl<const $lanes: usize> PartialOrd for $name<$lanes> where BitMask<$lanes>: LanesAtMost64 {
87 fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
88 self.0.partial_cmp(&other.0)
92 impl<const $lanes: usize> core::fmt::Debug for $name<$lanes> where BitMask<$lanes>: LanesAtMost64 {
93 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
94 core::fmt::Debug::fmt(&self.0, f)
98 impl<const LANES: usize> core::ops::BitAnd for $name<LANES> where BitMask<LANES>: LanesAtMost64 {
101 fn bitand(self, rhs: Self) -> Self {
106 impl<const LANES: usize> core::ops::BitAnd<bool> for $name<LANES> where BitMask<LANES>: LanesAtMost64 {
109 fn bitand(self, rhs: bool) -> Self {
110 self & Self::splat(rhs)
114 impl<const LANES: usize> core::ops::BitAnd<$name<LANES>> for bool where BitMask<LANES>: LanesAtMost64 {
115 type Output = $name<LANES>;
117 fn bitand(self, rhs: $name<LANES>) -> $name<LANES> {
118 $name::<LANES>::splat(self) & rhs
122 impl<const LANES: usize> core::ops::BitOr for $name<LANES> where BitMask<LANES>: LanesAtMost64 {
125 fn bitor(self, rhs: Self) -> Self {
130 impl<const LANES: usize> core::ops::BitOr<bool> for $name<LANES> where BitMask<LANES>: LanesAtMost64 {
133 fn bitor(self, rhs: bool) -> Self {
134 self | Self::splat(rhs)
138 impl<const LANES: usize> core::ops::BitOr<$name<LANES>> for bool where BitMask<LANES>: LanesAtMost64 {
139 type Output = $name<LANES>;
141 fn bitor(self, rhs: $name<LANES>) -> $name<LANES> {
142 $name::<LANES>::splat(self) | rhs
146 impl<const LANES: usize> core::ops::BitXor for $name<LANES> where BitMask<LANES>: LanesAtMost64 {
149 fn bitxor(self, rhs: Self) -> Self::Output {
154 impl<const LANES: usize> core::ops::BitXor<bool> for $name<LANES> where BitMask<LANES>: LanesAtMost64 {
157 fn bitxor(self, rhs: bool) -> Self::Output {
158 self ^ Self::splat(rhs)
162 impl<const LANES: usize> core::ops::BitXor<$name<LANES>> for bool where BitMask<LANES>: LanesAtMost64 {
163 type Output = $name<LANES>;
165 fn bitxor(self, rhs: $name<LANES>) -> Self::Output {
166 $name::<LANES>::splat(self) ^ rhs
170 impl<const LANES: usize> core::ops::Not for $name<LANES> where BitMask<LANES>: LanesAtMost64 {
171 type Output = $name<LANES>;
173 fn not(self) -> Self::Output {
178 impl<const LANES: usize> core::ops::BitAndAssign for $name<LANES> where BitMask<LANES>: LanesAtMost64 {
180 fn bitand_assign(&mut self, rhs: Self) {
185 impl<const LANES: usize> core::ops::BitAndAssign<bool> for $name<LANES> where BitMask<LANES>: LanesAtMost64 {
187 fn bitand_assign(&mut self, rhs: bool) {
188 *self &= Self::splat(rhs);
192 impl<const LANES: usize> core::ops::BitOrAssign for $name<LANES> where BitMask<LANES>: LanesAtMost64 {
194 fn bitor_assign(&mut self, rhs: Self) {
199 impl<const LANES: usize> core::ops::BitOrAssign<bool> for $name<LANES> where BitMask<LANES>: LanesAtMost64 {
201 fn bitor_assign(&mut self, rhs: bool) {
202 *self |= Self::splat(rhs);
206 impl<const LANES: usize> core::ops::BitXorAssign for $name<LANES> where BitMask<LANES>: LanesAtMost64 {
208 fn bitxor_assign(&mut self, rhs: Self) {
213 impl<const LANES: usize> core::ops::BitXorAssign<bool> for $name<LANES> where BitMask<LANES>: LanesAtMost64 {
215 fn bitxor_assign(&mut self, rhs: bool) {
216 *self ^= Self::splat(rhs);
222 define_opaque_mask! {
223 /// Mask for vectors with `LANES` 8-bit elements.
225 /// The layout of this type is unspecified.
226 struct Mask8<const LANES: usize>(SimdMask8<LANES>);
229 define_opaque_mask! {
230 /// Mask for vectors with `LANES` 16-bit elements.
232 /// The layout of this type is unspecified.
233 struct Mask16<const LANES: usize>(SimdMask16<LANES>);
236 define_opaque_mask! {
237 /// Mask for vectors with `LANES` 32-bit elements.
239 /// The layout of this type is unspecified.
240 struct Mask32<const LANES: usize>(SimdMask32<LANES>);
243 define_opaque_mask! {
244 /// Mask for vectors with `LANES` 64-bit elements.
246 /// The layout of this type is unspecified.
247 struct Mask64<const LANES: usize>(SimdMask64<LANES>);
250 define_opaque_mask! {
251 /// Mask for vectors with `LANES` 128-bit elements.
253 /// The layout of this type is unspecified.
254 struct Mask128<const LANES: usize>(SimdMask128<LANES>);
257 define_opaque_mask! {
258 /// Mask for vectors with `LANES` pointer-width elements.
260 /// The layout of this type is unspecified.
261 struct MaskSize<const LANES: usize>(SimdMaskSize<LANES>);
264 macro_rules! implement_mask_ops {
265 { $($vector:ident => $mask:ident,)* } => {
267 impl<const LANES: usize> crate::$vector<LANES> where BitMask<LANES>: LanesAtMost64 {
268 /// Test if each lane is equal to the corresponding lane in `other`.
270 pub fn lanes_eq(&self, other: &Self) -> $mask<LANES> {
271 unsafe { $mask(crate::intrinsics::simd_eq(self, other)) }
274 /// Test if each lane is not equal to the corresponding lane in `other`.
276 pub fn lanes_ne(&self, other: &Self) -> $mask<LANES> {
277 unsafe { $mask(crate::intrinsics::simd_ne(self, other)) }
280 /// Test if each lane is less than the corresponding lane in `other`.
282 pub fn lanes_lt(&self, other: &Self) -> $mask<LANES> {
283 unsafe { $mask(crate::intrinsics::simd_lt(self, other)) }
286 /// Test if each lane is greater than the corresponding lane in `other`.
288 pub fn lanes_gt(&self, other: &Self) -> $mask<LANES> {
289 unsafe { $mask(crate::intrinsics::simd_gt(self, other)) }
292 /// Test if each lane is less than or equal to the corresponding lane in `other`.
294 pub fn lanes_le(&self, other: &Self) -> $mask<LANES> {
295 unsafe { $mask(crate::intrinsics::simd_le(self, other)) }
298 /// Test if each lane is greater than or equal to the corresponding lane in `other`.
300 pub fn lanes_ge(&self, other: &Self) -> $mask<LANES> {
301 unsafe { $mask(crate::intrinsics::simd_ge(self, other)) }
308 implement_mask_ops! {
314 SimdIsize => MaskSize,
321 SimdUsize => MaskSize,
327 /// Vector of eight 8-bit masks
328 pub type mask8x8 = Mask8<8>;
330 /// Vector of 16 8-bit masks
331 pub type mask8x16 = Mask8<16>;
333 /// Vector of 32 8-bit masks
334 pub type mask8x32 = Mask8<32>;
336 /// Vector of 16 8-bit masks
337 pub type mask8x64 = Mask8<64>;
339 /// Vector of four 16-bit masks
340 pub type mask16x4 = Mask16<4>;
342 /// Vector of eight 16-bit masks
343 pub type mask16x8 = Mask16<8>;
345 /// Vector of 16 16-bit masks
346 pub type mask16x16 = Mask16<16>;
348 /// Vector of 32 16-bit masks
349 pub type mask16x32 = Mask32<32>;
351 /// Vector of two 32-bit masks
352 pub type mask32x2 = Mask32<2>;
354 /// Vector of four 32-bit masks
355 pub type mask32x4 = Mask32<4>;
357 /// Vector of eight 32-bit masks
358 pub type mask32x8 = Mask32<8>;
360 /// Vector of 16 32-bit masks
361 pub type mask32x16 = Mask32<16>;
363 /// Vector of two 64-bit masks
364 pub type mask64x2 = Mask64<2>;
366 /// Vector of four 64-bit masks
367 pub type mask64x4 = Mask64<4>;
369 /// Vector of eight 64-bit masks
370 pub type mask64x8 = Mask64<8>;
372 /// Vector of two 128-bit masks
373 pub type mask128x2 = Mask128<2>;
375 /// Vector of four 128-bit masks
376 pub type mask128x4 = Mask128<4>;
378 /// Vector of two pointer-width masks
379 pub type masksizex2 = MaskSize<2>;
381 /// Vector of four pointer-width masks
382 pub type masksizex4 = MaskSize<4>;
384 /// Vector of eight pointer-width masks
385 pub type masksizex8 = MaskSize<8>;