Debian Codegraph @

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

raw
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 pub const fn new(x: bool, y: bool, z: bool, w: bool) -> Self {
29 Self {
30 x: MASK[x as usize],
31 y: MASK[y as usize],
32 z: MASK[z as usize],
33 w: MASK[w as usize],
34 }
35 }
36
37 /// Creates a vector with all elements set to `v`.
38 #[inline]
39 pub const fn splat(v: bool) -> Self {
40 Self::new(v, v, v, v)
41 }
42
43 /// Returns a bitmask with the lowest 4 bits set from the elements of `self`.
44 ///
45 /// A true element results in a `1` bit and a false element in a `0` bit. Element `x` goes
46 /// into the first lowest bit, element `y` into the second, etc.
47 #[inline]
48 pub fn bitmask(self) -> u32 {
49 (self.x & 0x1) | (self.y & 0x1) << 1 | (self.z & 0x1) << 2 | (self.w & 0x1) << 3
50 }
51
52 /// Returns true if any of the elements are true, false otherwise.
53 #[inline]
54 pub fn any(self) -> bool {
55 ((self.x | self.y | self.z | self.w) & 0x1) != 0
56 }
57
58 /// Returns true if all the elements are true, false otherwise.
59 #[inline]
60 pub fn all(self) -> bool {
61 ((self.x & self.y & self.z & self.w) & 0x1) != 0
62 }
63
64 /// Tests the value at `index`.
65 ///
66 /// Panics if `index` is greater than 3.
67 #[inline]
68 pub fn test(&self, index: usize) -> bool {
69 match index {
70 0 => (self.x & 0x1) != 0,
71 1 => (self.y & 0x1) != 0,
72 2 => (self.z & 0x1) != 0,
73 3 => (self.w & 0x1) != 0,
74 _ => panic!("index out of bounds"),
75 }
76 }
77
78 /// Sets the element at `index`.
79 ///
80 /// Panics if `index` is greater than 3.
81 #[inline]
82 pub fn set(&mut self, index: usize, value: bool) {
83 match index {
84 0 => self.x = MASK[value as usize],
85 1 => self.y = MASK[value as usize],
86 2 => self.z = MASK[value as usize],
87 3 => self.w = MASK[value as usize],
88 _ => panic!("index out of bounds"),
89 }
90 }
91
92 #[inline]
93 fn into_bool_array(self) -> [bool; 4] {
94 [
95 (self.x & 0x1) != 0,
96 (self.y & 0x1) != 0,
97 (self.z & 0x1) != 0,
98 (self.w & 0x1) != 0,
99 ]
100 }
101
102 #[inline]
103 fn into_u32_array(self) -> [u32; 4] {
104 [self.x, self.y, self.z, self.w]
105 }
106 }
107
108 impl Default for BVec4A {
109 #[inline]
110 fn default() -> Self {
111 Self::FALSE
112 }
113 }
114
115 impl BitAnd for BVec4A {
116 type Output = Self;
117 #[inline]
118 fn bitand(self, rhs: Self) -> Self {
119 Self {
120 x: self.x & rhs.x,
121 y: self.y & rhs.y,
122 z: self.z & rhs.z,
123 w: self.w & rhs.w,
124 }
125 }
126 }
127
128 impl BitAndAssign for BVec4A {
129 #[inline]
130 fn bitand_assign(&mut self, rhs: Self) {
131 *self = self.bitand(rhs);
132 }
133 }
134
135 impl BitOr for BVec4A {
136 type Output = Self;
137 #[inline]
138 fn bitor(self, rhs: Self) -> Self {
139 Self {
140 x: self.x | rhs.x,
141 y: self.y | rhs.y,
142 z: self.z | rhs.z,
143 w: self.w | rhs.w,
144 }
145 }
146 }
147
148 impl BitOrAssign for BVec4A {
149 #[inline]
150 fn bitor_assign(&mut self, rhs: Self) {
151 *self = self.bitor(rhs);
152 }
153 }
154
155 impl BitXor for BVec4A {
156 type Output = Self;
157 #[inline]
158 fn bitxor(self, rhs: Self) -> Self {
159 Self {
160 x: self.x ^ rhs.x,
161 y: self.y ^ rhs.y,
162 z: self.z ^ rhs.z,
163 w: self.w ^ rhs.w,
164 }
165 }
166 }
167
168 impl BitXorAssign for BVec4A {
169 #[inline]
170 fn bitxor_assign(&mut self, rhs: Self) {
171 *self = self.bitxor(rhs);
172 }
173 }
174
175 impl Not for BVec4A {
176 type Output = Self;
177 #[inline]
178 fn not(self) -> Self {
179 Self {
180 x: !self.x,
181 y: !self.y,
182 z: !self.z,
183 w: !self.w,
184 }
185 }
186 }
187
188 #[cfg(not(target_arch = "spirv"))]
189 impl fmt::Debug for BVec4A {
190 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
191 let arr = self.into_u32_array();
192 write!(
193 f,
194 "{}({:#x}, {:#x}, {:#x}, {:#x})",
195 stringify!(BVec4A),
196 arr[0],
197 arr[1],
198 arr[2],
199 arr[3]
200 )
201 }
202 }
203
204 #[cfg(not(target_arch = "spirv"))]
205 impl fmt::Display for BVec4A {
206 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
207 let arr = self.into_bool_array();
208 write!(f, "[{}, {}, {}, {}]", arr[0], arr[1], arr[2], arr[3])
209 }
210 }
211
212 impl From<BVec4A> for [bool; 4] {
213 #[inline]
214 fn from(mask: BVec4A) -> Self {
215 mask.into_bool_array()
216 }
217 }
218
219 impl From<BVec4A> for [u32; 4] {
220 #[inline]
221 fn from(mask: BVec4A) -> Self {
222 mask.into_u32_array()
223 }
224 }