1 use super::MaskElement;
2 use crate::simd::intrinsics;
3 use crate::simd::{LaneCount, Simd, SupportedLaneCount};
4 use core::marker::PhantomData;
6 /// A mask where each lane is represented by a single bit.
8 pub struct Mask<T, const LANES: usize>(
9 <LaneCount<LANES> as SupportedLaneCount>::BitMask,
14 LaneCount<LANES>: SupportedLaneCount;
16 impl<T, const LANES: usize> Copy for Mask<T, LANES>
19 LaneCount<LANES>: SupportedLaneCount,
23 impl<T, const LANES: usize> Clone for Mask<T, LANES>
26 LaneCount<LANES>: SupportedLaneCount,
28 fn clone(&self) -> Self {
33 impl<T, const LANES: usize> PartialEq for Mask<T, LANES>
36 LaneCount<LANES>: SupportedLaneCount,
38 fn eq(&self, other: &Self) -> bool {
39 self.0.as_ref() == other.0.as_ref()
43 impl<T, const LANES: usize> PartialOrd for Mask<T, LANES>
46 LaneCount<LANES>: SupportedLaneCount,
48 fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
49 self.0.as_ref().partial_cmp(other.0.as_ref())
53 impl<T, const LANES: usize> Eq for Mask<T, LANES>
56 LaneCount<LANES>: SupportedLaneCount,
60 impl<T, const LANES: usize> Ord for Mask<T, LANES>
63 LaneCount<LANES>: SupportedLaneCount,
65 fn cmp(&self, other: &Self) -> core::cmp::Ordering {
66 self.0.as_ref().cmp(other.0.as_ref())
70 impl<T, const LANES: usize> Mask<T, LANES>
73 LaneCount<LANES>: SupportedLaneCount,
76 pub fn splat(value: bool) -> Self {
77 let mut mask = <LaneCount<LANES> as SupportedLaneCount>::BitMask::default();
79 mask.as_mut().fill(u8::MAX)
81 mask.as_mut().fill(u8::MIN)
84 *mask.as_mut().last_mut().unwrap() &= u8::MAX >> (8 - LANES % 8);
86 Self(mask, PhantomData)
90 pub unsafe fn test_unchecked(&self, lane: usize) -> bool {
91 (self.0.as_ref()[lane / 8] >> (lane % 8)) & 0x1 > 0
95 pub unsafe fn set_unchecked(&mut self, lane: usize, value: bool) {
97 self.0.as_mut()[lane / 8] ^= ((value ^ self.test_unchecked(lane)) as u8) << (lane % 8)
102 pub fn to_int(self) -> Simd<T, LANES> {
104 let mask: <LaneCount<LANES> as SupportedLaneCount>::IntBitMask =
105 core::mem::transmute_copy(&self);
106 intrinsics::simd_select_bitmask(mask, Simd::splat(T::TRUE), Simd::splat(T::FALSE))
111 pub unsafe fn from_int_unchecked(value: Simd<T, LANES>) -> Self {
112 // TODO remove the transmute when rustc is more flexible
114 core::mem::size_of::<<LaneCount::<LANES> as SupportedLaneCount>::BitMask>(),
115 core::mem::size_of::<<LaneCount::<LANES> as SupportedLaneCount>::IntBitMask>(),
118 let mask: <LaneCount<LANES> as SupportedLaneCount>::IntBitMask =
119 intrinsics::simd_bitmask(value);
120 Self(core::mem::transmute_copy(&mask), PhantomData)
124 #[cfg(feature = "generic_const_exprs")]
126 pub fn to_bitmask(self) -> [u8; LaneCount::<LANES>::BITMASK_LEN] {
127 // Safety: these are the same type and we are laundering the generic
128 unsafe { core::mem::transmute_copy(&self.0) }
131 #[cfg(feature = "generic_const_exprs")]
133 pub fn from_bitmask(bitmask: [u8; LaneCount::<LANES>::BITMASK_LEN]) -> Self {
134 // Safety: these are the same type and we are laundering the generic
135 Self(unsafe { core::mem::transmute_copy(&bitmask) }, PhantomData)
139 pub fn convert<U>(self) -> Mask<U, LANES>
143 unsafe { core::mem::transmute_copy(&self) }
147 pub fn any(self) -> bool {
148 self != Self::splat(false)
152 pub fn all(self) -> bool {
153 self == Self::splat(true)
157 impl<T, const LANES: usize> core::ops::BitAnd for Mask<T, LANES>
160 LaneCount<LANES>: SupportedLaneCount,
161 <LaneCount<LANES> as SupportedLaneCount>::BitMask: AsRef<[u8]> + AsMut<[u8]>,
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 fn bitor(mut self, rhs: Self) -> Self {
182 for (l, r) in self.0.as_mut().iter_mut().zip(rhs.0.as_ref().iter()) {
189 impl<T, const LANES: usize> core::ops::BitXor for Mask<T, LANES>
192 LaneCount<LANES>: SupportedLaneCount,
196 fn bitxor(mut self, rhs: Self) -> Self::Output {
197 for (l, r) in self.0.as_mut().iter_mut().zip(rhs.0.as_ref().iter()) {
204 impl<T, const LANES: usize> core::ops::Not for Mask<T, LANES>
207 LaneCount<LANES>: SupportedLaneCount,
211 fn not(mut self) -> Self::Output {
212 for x in self.0.as_mut() {
216 *self.0.as_mut().last_mut().unwrap() &= u8::MAX >> (8 - LANES % 8);