|
| 1 | +#[cfg(target_arch = "x86")] |
| 2 | +use core::arch::x86::*; |
| 3 | +#[cfg(target_arch = "x86_64")] |
| 4 | +use core::arch::x86_64::*; |
| 5 | +use core::{ |
| 6 | + cmp::Ordering, |
| 7 | + hash::{Hash, Hasher}, |
| 8 | + iter::Iterator, |
| 9 | + ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not}, |
| 10 | +}; |
| 11 | + |
| 12 | +#[derive(Copy, Clone, Debug)] |
| 13 | +#[repr(transparent)] |
| 14 | +pub struct Block(__m256i); |
| 15 | + |
| 16 | +impl Block { |
| 17 | + pub const USIZE_COUNT: usize = core::mem::size_of::<Self>() / core::mem::size_of::<usize>(); |
| 18 | + pub const NONE: Self = Self::from_usize_array([0; Self::USIZE_COUNT]); |
| 19 | + pub const ALL: Self = Self::from_usize_array([core::usize::MAX; Self::USIZE_COUNT]); |
| 20 | + pub const BITS: usize = core::mem::size_of::<Self>() * 8; |
| 21 | + |
| 22 | + #[inline] |
| 23 | + pub fn into_usize_array(self) -> [usize; Self::USIZE_COUNT] { |
| 24 | + unsafe { core::mem::transmute(self.0) } |
| 25 | + } |
| 26 | + |
| 27 | + #[inline] |
| 28 | + pub const fn from_usize_array(array: [usize; Self::USIZE_COUNT]) -> Self { |
| 29 | + Self(unsafe { core::mem::transmute(array) }) |
| 30 | + } |
| 31 | + |
| 32 | + #[inline] |
| 33 | + pub fn is_empty(self) -> bool { |
| 34 | + unsafe { _mm256_testz_si256(self.0, self.0) == 1 } |
| 35 | + } |
| 36 | + |
| 37 | + #[inline] |
| 38 | + pub fn andnot(self, other: Self) -> Self { |
| 39 | + Self(unsafe { _mm256_andnot_si256(other.0, self.0) }) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +impl Not for Block { |
| 44 | + type Output = Block; |
| 45 | + #[inline] |
| 46 | + fn not(self) -> Self::Output { |
| 47 | + unsafe { Self(_mm256_xor_si256(self.0, Self::ALL.0)) } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +impl BitAnd for Block { |
| 52 | + type Output = Block; |
| 53 | + #[inline] |
| 54 | + fn bitand(self, other: Self) -> Self::Output { |
| 55 | + unsafe { Self(_mm256_and_si256(self.0, other.0)) } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +impl BitAndAssign for Block { |
| 60 | + #[inline] |
| 61 | + fn bitand_assign(&mut self, other: Self) { |
| 62 | + unsafe { |
| 63 | + self.0 = _mm256_and_si256(self.0, other.0); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +impl BitOr for Block { |
| 69 | + type Output = Block; |
| 70 | + #[inline] |
| 71 | + fn bitor(self, other: Self) -> Self::Output { |
| 72 | + unsafe { Self(_mm256_or_si256(self.0, other.0)) } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +impl BitOrAssign for Block { |
| 77 | + #[inline] |
| 78 | + fn bitor_assign(&mut self, other: Self) { |
| 79 | + unsafe { |
| 80 | + self.0 = _mm256_or_si256(self.0, other.0); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +impl BitXor for Block { |
| 86 | + type Output = Block; |
| 87 | + #[inline] |
| 88 | + fn bitxor(self, other: Self) -> Self::Output { |
| 89 | + unsafe { Self(_mm256_xor_si256(self.0, other.0)) } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +impl BitXorAssign for Block { |
| 94 | + #[inline] |
| 95 | + fn bitxor_assign(&mut self, other: Self) { |
| 96 | + unsafe { self.0 = _mm256_xor_si256(self.0, other.0) } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +impl PartialEq for Block { |
| 101 | + #[inline] |
| 102 | + fn eq(&self, other: &Self) -> bool { |
| 103 | + unsafe { |
| 104 | + let eq = _mm256_cmpeq_epi8(self.0, other.0); |
| 105 | + _mm256_movemask_epi8(eq) == !(0i32) |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments