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 use core::simd::*; 8 9 #[repr(C)] 10 union UnionCast { 11 a: [u32; 4], 12 v: BVec3A, 13 } 14 15 /// A 3-dimensional SIMD vector mask. 16 /// 17 /// This type is 16 byte aligned. 18 #[derive(Clone, Copy)] 19 #[repr(transparent)] 20 pub struct BVec3A(pub(crate) mask32x4); 21 22 const MASK: [u32; 2] = [0, 0xff_ff_ff_ff]; 23 24 impl BVec3A { 25 /// All false. 26 pub const FALSE: Self = Self::splat(false); 27 28 /// All true. 29 pub const TRUE: Self = Self::splat(true); 30 31 /// Creates a new vector mask. 32 #[inline(always)] 33 #[must_use] new(x: bool, y: bool, z: bool) -> Self34 pub const fn new(x: bool, y: bool, z: bool) -> Self { 35 unsafe { 36 UnionCast { 37 a: [MASK[x as usize], MASK[y as usize], MASK[z as usize], 0], 38 } 39 .v 40 } 41 } 42 43 /// Creates a vector with all elements set to `v`. 44 #[inline] 45 #[must_use] splat(v: bool) -> Self46 pub const fn splat(v: bool) -> Self { 47 Self::new(v, v, v) 48 } 49 50 /// Returns a bitmask with the lowest 3 bits set from the elements of `self`. 51 /// 52 /// A true element results in a `1` bit and a false element in a `0` bit. Element `x` goes 53 /// into the first lowest bit, element `y` into the second, etc. 54 #[inline] 55 #[must_use] bitmask(self) -> u3256 pub fn bitmask(self) -> u32 { 57 (self.0.to_bitmask() & 0x7) as u32 58 } 59 60 /// Returns true if any of the elements are true, false otherwise. 61 #[inline] 62 #[must_use] any(self) -> bool63 pub fn any(self) -> bool { 64 self.bitmask() != 0 65 } 66 67 /// Returns true if all the elements are true, false otherwise. 68 #[inline] 69 #[must_use] all(self) -> bool70 pub fn all(self) -> bool { 71 self.bitmask() == 0x7 72 } 73 74 /// Tests the value at `index`. 75 /// 76 /// Panics if `index` is greater than 2. 77 #[inline] 78 #[must_use] test(&self, index: usize) -> bool79 pub fn test(&self, index: usize) -> bool { 80 self.0.test(index) 81 } 82 83 /// Sets the element at `index`. 84 /// 85 /// Panics if `index` is greater than 2. 86 #[inline] set(&mut self, index: usize, value: bool)87 pub fn set(&mut self, index: usize, value: bool) { 88 self.0.set(index, value) 89 } 90 91 #[inline] 92 #[must_use] into_bool_array(self) -> [bool; 3]93 fn into_bool_array(self) -> [bool; 3] { 94 let bitmask = self.bitmask(); 95 [(bitmask & 1) != 0, (bitmask & 2) != 0, (bitmask & 4) != 0] 96 } 97 98 #[inline] 99 #[must_use] into_u32_array(self) -> [u32; 3]100 fn into_u32_array(self) -> [u32; 3] { 101 let bitmask = self.bitmask(); 102 [ 103 MASK[(bitmask & 1) as usize], 104 MASK[((bitmask >> 1) & 1) as usize], 105 MASK[((bitmask >> 2) & 1) as usize], 106 ] 107 } 108 } 109 110 impl Default for BVec3A { 111 #[inline] default() -> Self112 fn default() -> Self { 113 Self::FALSE 114 } 115 } 116 117 impl PartialEq for BVec3A { 118 #[inline] eq(&self, rhs: &Self) -> bool119 fn eq(&self, rhs: &Self) -> bool { 120 self.bitmask().eq(&rhs.bitmask()) 121 } 122 } 123 124 impl Eq for BVec3A {} 125 126 impl core::hash::Hash for BVec3A { 127 #[inline] hash<H: core::hash::Hasher>(&self, state: &mut H)128 fn hash<H: core::hash::Hasher>(&self, state: &mut H) { 129 self.bitmask().hash(state); 130 } 131 } 132 133 impl BitAnd for BVec3A { 134 type Output = Self; 135 #[inline] bitand(self, rhs: Self) -> Self136 fn bitand(self, rhs: Self) -> Self { 137 Self(self.0 & rhs.0) 138 } 139 } 140 141 impl BitAndAssign for BVec3A { 142 #[inline] bitand_assign(&mut self, rhs: Self)143 fn bitand_assign(&mut self, rhs: Self) { 144 *self = self.bitand(rhs); 145 } 146 } 147 148 impl BitOr for BVec3A { 149 type Output = Self; 150 #[inline] bitor(self, rhs: Self) -> Self151 fn bitor(self, rhs: Self) -> Self { 152 Self(self.0 | rhs.0) 153 } 154 } 155 156 impl BitOrAssign for BVec3A { 157 #[inline] bitor_assign(&mut self, rhs: Self)158 fn bitor_assign(&mut self, rhs: Self) { 159 *self = self.bitor(rhs); 160 } 161 } 162 163 impl BitXor for BVec3A { 164 type Output = Self; 165 #[inline] bitxor(self, rhs: Self) -> Self166 fn bitxor(self, rhs: Self) -> Self { 167 Self(self.0 ^ rhs.0) 168 } 169 } 170 171 impl BitXorAssign for BVec3A { 172 #[inline] bitxor_assign(&mut self, rhs: Self)173 fn bitxor_assign(&mut self, rhs: Self) { 174 *self = self.bitxor(rhs); 175 } 176 } 177 178 impl Not for BVec3A { 179 type Output = Self; 180 #[inline] not(self) -> Self181 fn not(self) -> Self { 182 Self(!self.0) 183 } 184 } 185 186 impl From<BVec3A> for mask32x4 { 187 #[inline] from(t: BVec3A) -> Self188 fn from(t: BVec3A) -> Self { 189 t.0 190 } 191 } 192 193 #[cfg(not(target_arch = "spirv"))] 194 impl fmt::Debug for BVec3A { fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result195 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 196 let arr = self.into_u32_array(); 197 write!( 198 f, 199 "{}({:#x}, {:#x}, {:#x})", 200 stringify!(BVec3A), 201 arr[0], 202 arr[1], 203 arr[2] 204 ) 205 } 206 } 207 208 #[cfg(not(target_arch = "spirv"))] 209 impl fmt::Display for BVec3A { fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result210 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 211 let arr = self.into_bool_array(); 212 write!(f, "[{}, {}, {}]", arr[0], arr[1], arr[2]) 213 } 214 } 215 216 impl From<BVec3A> for [bool; 3] { 217 #[inline] from(mask: BVec3A) -> Self218 fn from(mask: BVec3A) -> Self { 219 mask.into_bool_array() 220 } 221 } 222 223 impl From<BVec3A> for [u32; 3] { 224 #[inline] from(mask: BVec3A) -> Self225 fn from(mask: BVec3A) -> Self { 226 mask.into_u32_array() 227 } 228 } 229