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 /// A 4-dimensional `u32` vector mask. 8 #[derive(Clone, Copy, PartialEq, Eq, Hash)] 9 #[repr(C, align(16))] 10 pub struct BVec4A { 11 pub x: u32, 12 pub y: u32, 13 pub z: u32, 14 pub w: u32, 15 } 16 17 const MASK: [u32; 2] = [0, 0xff_ff_ff_ff]; 18 19 impl BVec4A { 20 /// All false. 21 pub const FALSE: Self = Self::splat(false); 22 23 /// All true. 24 pub const TRUE: Self = Self::splat(true); 25 26 /// Creates a new vector mask. 27 #[inline(always)] 28 #[must_use] new(x: bool, y: bool, z: bool, w: bool) -> Self29 pub const fn new(x: bool, y: bool, z: bool, w: bool) -> Self { 30 Self { 31 x: MASK[x as usize], 32 y: MASK[y as usize], 33 z: MASK[z as usize], 34 w: MASK[w as usize], 35 } 36 } 37 38 /// Creates a vector with all elements set to `v`. 39 #[inline] 40 #[must_use] splat(v: bool) -> Self41 pub const fn splat(v: bool) -> Self { 42 Self::new(v, v, v, v) 43 } 44 45 /// Returns a bitmask with the lowest 4 bits set from the elements of `self`. 46 /// 47 /// A true element results in a `1` bit and a false element in a `0` bit. Element `x` goes 48 /// into the first lowest bit, element `y` into the second, etc. 49 #[inline] 50 #[must_use] bitmask(self) -> u3251 pub fn bitmask(self) -> u32 { 52 (self.x & 0x1) | (self.y & 0x1) << 1 | (self.z & 0x1) << 2 | (self.w & 0x1) << 3 53 } 54 55 /// Returns true if any of the elements are true, false otherwise. 56 #[inline] 57 #[must_use] any(self) -> bool58 pub fn any(self) -> bool { 59 ((self.x | self.y | self.z | self.w) & 0x1) != 0 60 } 61 62 /// Returns true if all the elements are true, false otherwise. 63 #[inline] 64 #[must_use] all(self) -> bool65 pub fn all(self) -> bool { 66 ((self.x & self.y & self.z & self.w) & 0x1) != 0 67 } 68 69 /// Tests the value at `index`. 70 /// 71 /// Panics if `index` is greater than 3. 72 #[inline] 73 #[must_use] test(&self, index: usize) -> bool74 pub fn test(&self, index: usize) -> bool { 75 match index { 76 0 => (self.x & 0x1) != 0, 77 1 => (self.y & 0x1) != 0, 78 2 => (self.z & 0x1) != 0, 79 3 => (self.w & 0x1) != 0, 80 _ => panic!("index out of bounds"), 81 } 82 } 83 84 /// Sets the element at `index`. 85 /// 86 /// Panics if `index` is greater than 3. 87 #[inline] set(&mut self, index: usize, value: bool)88 pub fn set(&mut self, index: usize, value: bool) { 89 match index { 90 0 => self.x = MASK[value as usize], 91 1 => self.y = MASK[value as usize], 92 2 => self.z = MASK[value as usize], 93 3 => self.w = MASK[value as usize], 94 _ => panic!("index out of bounds"), 95 } 96 } 97 98 #[inline] 99 #[must_use] into_bool_array(self) -> [bool; 4]100 fn into_bool_array(self) -> [bool; 4] { 101 [ 102 (self.x & 0x1) != 0, 103 (self.y & 0x1) != 0, 104 (self.z & 0x1) != 0, 105 (self.w & 0x1) != 0, 106 ] 107 } 108 109 #[inline] 110 #[must_use] into_u32_array(self) -> [u32; 4]111 fn into_u32_array(self) -> [u32; 4] { 112 [self.x, self.y, self.z, self.w] 113 } 114 } 115 116 impl Default for BVec4A { 117 #[inline] default() -> Self118 fn default() -> Self { 119 Self::FALSE 120 } 121 } 122 123 impl BitAnd for BVec4A { 124 type Output = Self; 125 #[inline] bitand(self, rhs: Self) -> Self126 fn bitand(self, rhs: Self) -> Self { 127 Self { 128 x: self.x & rhs.x, 129 y: self.y & rhs.y, 130 z: self.z & rhs.z, 131 w: self.w & rhs.w, 132 } 133 } 134 } 135 136 impl BitAndAssign for BVec4A { 137 #[inline] bitand_assign(&mut self, rhs: Self)138 fn bitand_assign(&mut self, rhs: Self) { 139 *self = self.bitand(rhs); 140 } 141 } 142 143 impl BitOr for BVec4A { 144 type Output = Self; 145 #[inline] bitor(self, rhs: Self) -> Self146 fn bitor(self, rhs: Self) -> Self { 147 Self { 148 x: self.x | rhs.x, 149 y: self.y | rhs.y, 150 z: self.z | rhs.z, 151 w: self.w | rhs.w, 152 } 153 } 154 } 155 156 impl BitOrAssign for BVec4A { 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 BVec4A { 164 type Output = Self; 165 #[inline] bitxor(self, rhs: Self) -> Self166 fn bitxor(self, rhs: Self) -> Self { 167 Self { 168 x: self.x ^ rhs.x, 169 y: self.y ^ rhs.y, 170 z: self.z ^ rhs.z, 171 w: self.w ^ rhs.w, 172 } 173 } 174 } 175 176 impl BitXorAssign for BVec4A { 177 #[inline] bitxor_assign(&mut self, rhs: Self)178 fn bitxor_assign(&mut self, rhs: Self) { 179 *self = self.bitxor(rhs); 180 } 181 } 182 183 impl Not for BVec4A { 184 type Output = Self; 185 #[inline] not(self) -> Self186 fn not(self) -> Self { 187 Self { 188 x: !self.x, 189 y: !self.y, 190 z: !self.z, 191 w: !self.w, 192 } 193 } 194 } 195 196 #[cfg(not(target_arch = "spirv"))] 197 impl fmt::Debug for BVec4A { fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result198 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 199 let arr = self.into_u32_array(); 200 write!( 201 f, 202 "{}({:#x}, {:#x}, {:#x}, {:#x})", 203 stringify!(BVec4A), 204 arr[0], 205 arr[1], 206 arr[2], 207 arr[3] 208 ) 209 } 210 } 211 212 #[cfg(not(target_arch = "spirv"))] 213 impl fmt::Display for BVec4A { fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result214 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 215 let arr = self.into_bool_array(); 216 write!(f, "[{}, {}, {}, {}]", arr[0], arr[1], arr[2], arr[3]) 217 } 218 } 219 220 impl From<BVec4A> for [bool; 4] { 221 #[inline] from(mask: BVec4A) -> Self222 fn from(mask: BVec4A) -> Self { 223 mask.into_bool_array() 224 } 225 } 226 227 impl From<BVec4A> for [u32; 4] { 228 #[inline] from(mask: BVec4A) -> Self229 fn from(mask: BVec4A) -> Self { 230 mask.into_u32_array() 231 } 232 } 233