Debian Codegraph @

debian/missing-sources/glam/src/bool/scalar/bvec4a.rs

raw
1 // Generated from vec_mask.rs.tera template. Edit the template, not the generated file.
2
3 use core::fmt;
4 use core::ops::*;
5
6 /// Creates a 4-dimensional `bool` vector mask.
7 #[inline(always)]
8 #[must_use]
9 pub const fn bvec4a(x: bool, y: bool, z: bool, w: bool) -> BVec4A {
10 BVec4A::new(x, y, z, w)
11 }
12
13 /// A 4-dimensional `u32` vector mask.
14 #[derive(Clone, Copy, PartialEq, Eq, Hash)]
15 #[repr(C, align(16))]
16 #[cfg_attr(target_arch = "spirv", rust_gpu::vector::v1)]
17 pub struct BVec4A {
18 pub x: u32,
19 pub y: u32,
20 pub z: u32,
21 pub w: u32,
22 }
23
24 const MASK: [u32; 2] = [0, 0xff_ff_ff_ff];
25
26 impl BVec4A {
27 /// All false.
28 pub const FALSE: Self = Self::splat(false);
29
30 /// All true.
31 pub const TRUE: Self = Self::splat(true);
32
33 /// Creates a new vector mask.
34 #[inline(always)]
35 #[must_use]
36 pub const fn new(x: bool, y: bool, z: bool, w: bool) -> Self {
37 Self {
38 x: MASK[x as usize],
39 y: MASK[y as usize],
40 z: MASK[z as usize],
41 w: MASK[w as usize],
42 }
43 }
44
45 /// Creates a vector mask with all elements set to `v`.
46 #[inline]
47 #[must_use]
48 pub const fn splat(v: bool) -> Self {
49 Self::new(v, v, v, v)
50 }
51
52 /// Creates a new vector mask from a bool array.
53 #[inline]
54 #[must_use]
55 pub const fn from_array(a: [bool; 4]) -> Self {
56 Self::new(a[0], a[1], a[2], a[3])
57 }
58
59 /// Returns a bitmask with the lowest 4 bits set from the elements of `self`.
60 ///
61 /// A true element results in a `1` bit and a false element in a `0` bit. Element `x` goes
62 /// into the first lowest bit, element `y` into the second, etc.
63 #[inline]
64 #[must_use]
65 pub fn bitmask(self) -> u32 {
66 (self.x & 0x1) | ((self.y & 0x1) << 1) | ((self.z & 0x1) << 2) | ((self.w & 0x1) << 3)
67 }
68
69 /// Returns true if any of the elements are true, false otherwise.
70 #[inline]
71 #[must_use]
72 pub fn any(self) -> bool {
73 ((self.x | self.y | self.z | self.w) & 0x1) != 0
74 }
75
76 /// Returns true if all the elements are true, false otherwise.
77 #[inline]
78 #[must_use]
79 pub fn all(self) -> bool {
80 ((self.x & self.y & self.z & self.w) & 0x1) != 0
81 }
82
83 /// Tests the value at `index`.
84 ///
85 /// Panics if `index` is greater than 3.
86 #[inline]
87 #[must_use]
88 pub fn test(&self, index: usize) -> bool {
89 match index {
90 0 => (self.x & 0x1) != 0,
91 1 => (self.y & 0x1) != 0,
92 2 => (self.z & 0x1) != 0,
93 3 => (self.w & 0x1) != 0,
94 _ => panic!("index out of bounds"),
95 }
96 }
97
98 /// Sets the element at `index`.
99 ///
100 /// Panics if `index` is greater than 3.
101 #[inline]
102 pub fn set(&mut self, index: usize, value: bool) {
103 match index {
104 0 => self.x = MASK[value as usize],
105 1 => self.y = MASK[value as usize],
106 2 => self.z = MASK[value as usize],
107 3 => self.w = MASK[value as usize],
108 _ => panic!("index out of bounds"),
109 }
110 }
111
112 #[inline]
113 #[must_use]
114 fn into_bool_array(self) -> [bool; 4] {
115 [
116 (self.x & 0x1) != 0,
117 (self.y & 0x1) != 0,
118 (self.z & 0x1) != 0,
119 (self.w & 0x1) != 0,
120 ]
121 }
122
123 #[inline]
124 #[must_use]
125 fn into_u32_array(self) -> [u32; 4] {
126 [self.x, self.y, self.z, self.w]
127 }
128 }
129
130 impl Default for BVec4A {
131 #[inline]
132 fn default() -> Self {
133 Self::FALSE
134 }
135 }
136
137 impl BitAnd for BVec4A {
138 type Output = Self;
139 #[inline]
140 fn bitand(self, rhs: Self) -> Self {
141 Self {
142 x: self.x & rhs.x,
143 y: self.y & rhs.y,
144 z: self.z & rhs.z,
145 w: self.w & rhs.w,
146 }
147 }
148 }
149
150 impl BitAnd<&Self> for BVec4A {
151 type Output = Self;
152 #[inline]
153 fn bitand(self, rhs: &Self) -> Self {
154 self.bitand(*rhs)
155 }
156 }
157
158 impl BitAnd<&BVec4A> for &BVec4A {
159 type Output = BVec4A;
160 #[inline]
161 fn bitand(self, rhs: &BVec4A) -> BVec4A {
162 (*self).bitand(*rhs)
163 }
164 }
165
166 impl BitAnd<BVec4A> for &BVec4A {
167 type Output = BVec4A;
168 #[inline]
169 fn bitand(self, rhs: BVec4A) -> BVec4A {
170 (*self).bitand(rhs)
171 }
172 }
173
174 impl BitAndAssign for BVec4A {
175 #[inline]
176 fn bitand_assign(&mut self, rhs: Self) {
177 *self = self.bitand(rhs);
178 }
179 }
180
181 impl BitAndAssign<&Self> for BVec4A {
182 #[inline]
183 fn bitand_assign(&mut self, rhs: &Self) {
184 self.bitand_assign(*rhs);
185 }
186 }
187
188 impl BitOr for BVec4A {
189 type Output = Self;
190 #[inline]
191 fn bitor(self, rhs: Self) -> Self {
192 Self {
193 x: self.x | rhs.x,
194 y: self.y | rhs.y,
195 z: self.z | rhs.z,
196 w: self.w | rhs.w,
197 }
198 }
199 }
200
201 impl BitOr<&Self> for BVec4A {
202 type Output = Self;
203 #[inline]
204 fn bitor(self, rhs: &Self) -> Self {
205 self.bitor(*rhs)
206 }
207 }
208
209 impl BitOr<&BVec4A> for &BVec4A {
210 type Output = BVec4A;
211 #[inline]
212 fn bitor(self, rhs: &BVec4A) -> BVec4A {
213 (*self).bitor(*rhs)
214 }
215 }
216
217 impl BitOr<BVec4A> for &BVec4A {
218 type Output = BVec4A;
219 #[inline]
220 fn bitor(self, rhs: BVec4A) -> BVec4A {
221 (*self).bitor(rhs)
222 }
223 }
224
225 impl BitOrAssign for BVec4A {
226 #[inline]
227 fn bitor_assign(&mut self, rhs: Self) {
228 *self = self.bitor(rhs);
229 }
230 }
231
232 impl BitOrAssign<&Self> for BVec4A {
233 #[inline]
234 fn bitor_assign(&mut self, rhs: &Self) {
235 self.bitor_assign(*rhs);
236 }
237 }
238
239 impl BitXor for BVec4A {
240 type Output = Self;
241 #[inline]
242 fn bitxor(self, rhs: Self) -> Self {
243 Self {
244 x: self.x ^ rhs.x,
245 y: self.y ^ rhs.y,
246 z: self.z ^ rhs.z,
247 w: self.w ^ rhs.w,
248 }
249 }
250 }
251
252 impl BitXor<&Self> for BVec4A {
253 type Output = Self;
254 #[inline]
255 fn bitxor(self, rhs: &Self) -> Self {
256 self.bitxor(*rhs)
257 }
258 }
259
260 impl BitXor<&BVec4A> for &BVec4A {
261 type Output = BVec4A;
262 #[inline]
263 fn bitxor(self, rhs: &BVec4A) -> BVec4A {
264 (*self).bitxor(*rhs)
265 }
266 }
267
268 impl BitXor<BVec4A> for &BVec4A {
269 type Output = BVec4A;
270 #[inline]
271 fn bitxor(self, rhs: BVec4A) -> BVec4A {
272 (*self).bitxor(rhs)
273 }
274 }
275
276 impl BitXorAssign for BVec4A {
277 #[inline]
278 fn bitxor_assign(&mut self, rhs: Self) {
279 *self = self.bitxor(rhs);
280 }
281 }
282
283 impl BitXorAssign<&Self> for BVec4A {
284 #[inline]
285 fn bitxor_assign(&mut self, rhs: &Self) {
286 self.bitxor_assign(*rhs);
287 }
288 }
289
290 impl Not for BVec4A {
291 type Output = Self;
292 #[inline]
293 fn not(self) -> Self {
294 Self {
295 x: !self.x,
296 y: !self.y,
297 z: !self.z,
298 w: !self.w,
299 }
300 }
301 }
302
303 impl Not for &BVec4A {
304 type Output = BVec4A;
305 #[inline]
306 fn not(self) -> BVec4A {
307 (*self).not()
308 }
309 }
310
311 impl fmt::Debug for BVec4A {
312 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
313 let arr = self.into_u32_array();
314 write!(
315 f,
316 "{}({:#x}, {:#x}, {:#x}, {:#x})",
317 stringify!(BVec4A),
318 arr[0],
319 arr[1],
320 arr[2],
321 arr[3]
322 )
323 }
324 }
325
326 impl fmt::Display for BVec4A {
327 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
328 let arr = self.into_bool_array();
329 write!(f, "[{}, {}, {}, {}]", arr[0], arr[1], arr[2], arr[3])
330 }
331 }
332
333 impl From<[bool; 4]> for BVec4A {
334 #[inline]
335 fn from(a: [bool; 4]) -> Self {
336 Self::from_array(a)
337 }
338 }
339
340 impl From<BVec4A> for [bool; 4] {
341 #[inline]
342 fn from(mask: BVec4A) -> Self {
343 mask.into_bool_array()
344 }
345 }
346
347 impl From<BVec4A> for [u32; 4] {
348 #[inline]
349 fn from(mask: BVec4A) -> Self {
350 mask.into_u32_array()
351 }
352 }