| 1 | // Generated from vec_mask.rs.tera template. Edit the template, not the generated file. |
| 2 | |
| 3 | #[cfg(not(target_arch = "spirv"))] |
| 4 | use core::fmt; |
| 5 | use core::ops::*; |
| 6 | |
| 7 | #[cfg(target_arch = "x86")] |
| 8 | use core::arch::x86::*; |
| 9 | #[cfg(target_arch = "x86_64")] |
| 10 | use core::arch::x86_64::*; |
| 11 | |
| 12 | union UnionCast { |
| 13 | a: [u32; 4], |
| 14 | v: BVec4A, |
| 15 | } |
| 16 | |
| 17 | /// A 4-dimensional SIMD vector mask. |
| 18 | /// |
| 19 | /// This type is 16 byte aligned and is backed by a SIMD vector. If SIMD is not available |
| 20 | /// `BVec4A` will be a type alias for `BVec4`. |
| 21 | #[derive(Clone, Copy)] |
| 22 | #[repr(transparent)] |
| 23 | pub struct BVec4A(pub(crate) __m128); |
| 24 | |
| 25 | const MASK: [u32; 2] = [0, 0xff_ff_ff_ff]; |
| 26 | |
| 27 | impl BVec4A { |
| 28 | /// All false. |
| 29 | pub const FALSE: Self = Self::splat(false); |
| 30 | |
| 31 | /// All true. |
| 32 | pub const TRUE: Self = Self::splat(true); |
| 33 | |
| 34 | /// Creates a new vector mask. |
| 35 | #[inline(always)] |
| 36 | pub const fn new(x: bool, y: bool, z: bool, w: bool) -> Self { |
| 37 | unsafe { |
| 38 | UnionCast { |
| 39 | a: [ |
| 40 | MASK[x as usize], |
| 41 | MASK[y as usize], |
| 42 | MASK[z as usize], |
| 43 | MASK[w as usize], |
| 44 | ], |
| 45 | } |
| 46 | .v |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /// Creates a vector with all elements set to `v`. |
| 51 | #[inline] |
| 52 | pub const fn splat(v: bool) -> Self { |
| 53 | Self::new(v, v, v, v) |
| 54 | } |
| 55 | |
| 56 | /// Returns a bitmask with the lowest 4 bits set from the elements of `self`. |
| 57 | /// |
| 58 | /// A true element results in a `1` bit and a false element in a `0` bit. Element `x` goes |
| 59 | /// into the first lowest bit, element `y` into the second, etc. |
| 60 | #[inline] |
| 61 | pub fn bitmask(self) -> u32 { |
| 62 | unsafe { _mm_movemask_ps(self.0) as u32 } |
| 63 | } |
| 64 | |
| 65 | /// Returns true if any of the elements are true, false otherwise. |
| 66 | #[inline] |
| 67 | pub fn any(self) -> bool { |
| 68 | self.bitmask() != 0 |
| 69 | } |
| 70 | |
| 71 | /// Returns true if all the elements are true, false otherwise. |
| 72 | #[inline] |
| 73 | pub fn all(self) -> bool { |
| 74 | self.bitmask() == 0xf |
| 75 | } |
| 76 | |
| 77 | #[inline] |
| 78 | fn into_bool_array(self) -> [bool; 4] { |
| 79 | let bitmask = self.bitmask(); |
| 80 | [ |
| 81 | (bitmask & 1) != 0, |
| 82 | (bitmask & 2) != 0, |
| 83 | (bitmask & 4) != 0, |
| 84 | (bitmask & 8) != 0, |
| 85 | ] |
| 86 | } |
| 87 | |
| 88 | #[inline] |
| 89 | fn into_u32_array(self) -> [u32; 4] { |
| 90 | let bitmask = self.bitmask(); |
| 91 | [ |
| 92 | MASK[(bitmask & 1) as usize], |
| 93 | MASK[((bitmask >> 1) & 1) as usize], |
| 94 | MASK[((bitmask >> 2) & 1) as usize], |
| 95 | MASK[((bitmask >> 3) & 1) as usize], |
| 96 | ] |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | impl Default for BVec4A { |
| 101 | #[inline] |
| 102 | fn default() -> Self { |
| 103 | Self::FALSE |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | impl PartialEq for BVec4A { |
| 108 | #[inline] |
| 109 | fn eq(&self, rhs: &Self) -> bool { |
| 110 | self.bitmask().eq(&rhs.bitmask()) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | impl Eq for BVec4A {} |
| 115 | |
| 116 | impl core::hash::Hash for BVec4A { |
| 117 | #[inline] |
| 118 | fn hash<H: core::hash::Hasher>(&self, state: &mut H) { |
| 119 | self.bitmask().hash(state); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | impl BitAnd for BVec4A { |
| 124 | type Output = Self; |
| 125 | #[inline] |
| 126 | fn bitand(self, rhs: Self) -> Self { |
| 127 | Self(unsafe { _mm_and_ps(self.0, rhs.0) }) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | impl BitAndAssign for BVec4A { |
| 132 | #[inline] |
| 133 | fn bitand_assign(&mut self, rhs: Self) { |
| 134 | *self = self.bitand(rhs); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | impl BitOr for BVec4A { |
| 139 | type Output = Self; |
| 140 | #[inline] |
| 141 | fn bitor(self, rhs: Self) -> Self { |
| 142 | Self(unsafe { _mm_or_ps(self.0, rhs.0) }) |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | impl BitOrAssign for BVec4A { |
| 147 | #[inline] |
| 148 | fn bitor_assign(&mut self, rhs: Self) { |
| 149 | *self = self.bitor(rhs); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | impl BitXor for BVec4A { |
| 154 | type Output = Self; |
| 155 | #[inline] |
| 156 | fn bitxor(self, rhs: Self) -> Self { |
| 157 | Self(unsafe { _mm_xor_ps(self.0, rhs.0) }) |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | impl BitXorAssign for BVec4A { |
| 162 | #[inline] |
| 163 | fn bitxor_assign(&mut self, rhs: Self) { |
| 164 | *self = self.bitxor(rhs); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | impl Not for BVec4A { |
| 169 | type Output = Self; |
| 170 | #[inline] |
| 171 | fn not(self) -> Self { |
| 172 | Self(unsafe { _mm_andnot_ps(self.0, _mm_set_ps1(f32::from_bits(0xff_ff_ff_ff))) }) |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | impl From<BVec4A> for __m128 { |
| 177 | #[inline] |
| 178 | fn from(t: BVec4A) -> Self { |
| 179 | t.0 |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | #[cfg(not(target_arch = "spirv"))] |
| 184 | impl fmt::Debug for BVec4A { |
| 185 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 186 | let arr = self.into_u32_array(); |
| 187 | write!( |
| 188 | f, |
| 189 | "{}({:#x}, {:#x}, {:#x}, {:#x})", |
| 190 | stringify!(BVec4A), |
| 191 | arr[0], |
| 192 | arr[1], |
| 193 | arr[2], |
| 194 | arr[3] |
| 195 | ) |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | #[cfg(not(target_arch = "spirv"))] |
| 200 | impl fmt::Display for BVec4A { |
| 201 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 202 | let arr = self.into_bool_array(); |
| 203 | write!(f, "[{}, {}, {}, {}]", arr[0], arr[1], arr[2], arr[3]) |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | impl From<BVec4A> for [bool; 4] { |
| 208 | #[inline] |
| 209 | fn from(mask: BVec4A) -> Self { |
| 210 | mask.into_bool_array() |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | impl From<BVec4A> for [u32; 4] { |
| 215 | #[inline] |
| 216 | fn from(mask: BVec4A) -> Self { |
| 217 | mask.into_u32_array() |
| 218 | } |
| 219 | } |