1 #![allow(unused_imports)]
2 use super::MaskElement;
3 use crate::simd::intrinsics;
4 use crate::simd::{LaneCount, Simd, SupportedLaneCount};
5 use core::marker::PhantomData;
7 /// A mask where each lane is represented by a single bit.
9 pub struct Mask<T, const LANES: usize>(
10 <LaneCount<LANES> as SupportedLaneCount>::BitMask,
15 LaneCount<LANES>: SupportedLaneCount;
17 impl<T, const LANES: usize> Copy for Mask<T, LANES>
20 LaneCount<LANES>: SupportedLaneCount,
24 impl<T, const LANES: usize> Clone for Mask<T, LANES>
27 LaneCount<LANES>: SupportedLaneCount,
29 fn clone(&self) -> Self {
34 impl<T, const LANES: usize> PartialEq for Mask<T, LANES>
37 LaneCount<LANES>: SupportedLaneCount,
39 fn eq(&self, other: &Self) -> bool {
40 self.0.as_ref() == other.0.as_ref()
44 impl<T, const LANES: usize> PartialOrd for Mask<T, LANES>
47 LaneCount<LANES>: SupportedLaneCount,
49 fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
50 self.0.as_ref().partial_cmp(other.0.as_ref())
54 impl<T, const LANES: usize> Eq for Mask<T, LANES>
57 LaneCount<LANES>: SupportedLaneCount,
61 impl<T, const LANES: usize> Ord for Mask<T, LANES>
64 LaneCount<LANES>: SupportedLaneCount,
66 fn cmp(&self, other: &Self) -> core::cmp::Ordering {
67 self.0.as_ref().cmp(other.0.as_ref())
71 impl<T, const LANES: usize> Mask<T, LANES>
74 LaneCount<LANES>: SupportedLaneCount,
77 #[must_use = "method returns a new mask and does not mutate the original value"]
78 pub fn splat(value: bool) -> Self {
79 let mut mask = <LaneCount<LANES> as SupportedLaneCount>::BitMask::default();
81 mask.as_mut().fill(u8::MAX)
83 mask.as_mut().fill(u8::MIN)
86 *mask.as_mut().last_mut().unwrap() &= u8::MAX >> (8 - LANES % 8);
88 Self(mask, PhantomData)
92 #[must_use = "method returns a new bool and does not mutate the original value"]
93 pub unsafe fn test_unchecked(&self, lane: usize) -> bool {
94 (self.0.as_ref()[lane / 8] >> (lane % 8)) & 0x1 > 0
98 pub unsafe fn set_unchecked(&mut self, lane: usize, value: bool) {
100 self.0.as_mut()[lane / 8] ^= ((value ^ self.test_unchecked(lane)) as u8) << (lane % 8)
105 #[must_use = "method returns a new vector and does not mutate the original value"]
106 pub fn to_int(self) -> Simd<T, LANES> {
108 intrinsics::simd_select_bitmask(self.0, Simd::splat(T::TRUE), Simd::splat(T::FALSE))
113 #[must_use = "method returns a new mask and does not mutate the original value"]
114 pub unsafe fn from_int_unchecked(value: Simd<T, LANES>) -> Self {
115 unsafe { Self(intrinsics::simd_bitmask(value), PhantomData) }
118 #[cfg(feature = "generic_const_exprs")]
120 #[must_use = "method returns a new array and does not mutate the original value"]
121 pub fn to_bitmask(self) -> [u8; LaneCount::<LANES>::BITMASK_LEN] {
122 // Safety: these are the same type and we are laundering the generic
123 unsafe { core::mem::transmute_copy(&self.0) }
126 #[cfg(feature = "generic_const_exprs")]
128 #[must_use = "method returns a new mask and does not mutate the original value"]
129 pub fn from_bitmask(bitmask: [u8; LaneCount::<LANES>::BITMASK_LEN]) -> Self {
130 // Safety: these are the same type and we are laundering the generic
131 Self(unsafe { core::mem::transmute_copy(&bitmask) }, PhantomData)
135 #[must_use = "method returns a new mask and does not mutate the original value"]
136 pub fn convert<U>(self) -> Mask<U, LANES>
140 unsafe { core::mem::transmute_copy(&self) }
144 #[must_use = "method returns a new bool and does not mutate the original value"]
145 pub fn any(self) -> bool {
146 self != Self::splat(false)
150 #[must_use = "method returns a new bool and does not mutate the original value"]
151 pub fn all(self) -> bool {
152 self == Self::splat(true)
156 impl<T, const LANES: usize> core::ops::BitAnd for Mask<T, LANES>
159 LaneCount<LANES>: SupportedLaneCount,
160 <LaneCount<LANES> as SupportedLaneCount>::BitMask: AsRef<[u8]> + AsMut<[u8]>,
164 #[must_use = "method returns a new mask and does not mutate the original value"]
165 fn bitand(mut self, rhs: Self) -> Self {
166 for (l, r) in self.0.as_mut().iter_mut().zip(rhs.0.as_ref().iter()) {
173 impl<T, const LANES: usize> core::ops::BitOr for Mask<T, LANES>
176 LaneCount<LANES>: SupportedLaneCount,
177 <LaneCount<LANES> as SupportedLaneCount>::BitMask: AsRef<[u8]> + AsMut<[u8]>,
181 #[must_use = "method returns a new mask and does not mutate the original value"]
182 fn bitor(mut self, rhs: Self) -> Self {
183 for (l, r) in self.0.as_mut().iter_mut().zip(rhs.0.as_ref().iter()) {
190 impl<T, const LANES: usize> core::ops::BitXor for Mask<T, LANES>
193 LaneCount<LANES>: SupportedLaneCount,
197 #[must_use = "method returns a new mask and does not mutate the original value"]
198 fn bitxor(mut self, rhs: Self) -> Self::Output {
199 for (l, r) in self.0.as_mut().iter_mut().zip(rhs.0.as_ref().iter()) {
206 impl<T, const LANES: usize> core::ops::Not for Mask<T, LANES>
209 LaneCount<LANES>: SupportedLaneCount,
213 #[must_use = "method returns a new mask and does not mutate the original value"]
214 fn not(mut self) -> Self::Output {
215 for x in self.0.as_mut() {
219 *self.0.as_mut().last_mut().unwrap() &= u8::MAX >> (8 - LANES % 8);