Debian Codegraph @

debian/missing-sources/glam-0.28.0/src/bool/wasm32/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 use core::arch::wasm32::*;
8
9 /// A 4-dimensional SIMD vector mask.
10 ///
11 /// This type is 16 byte aligned.
12 #[derive(Clone, Copy)]
13 #[repr(transparent)]
14 pub struct BVec4A(pub(crate) v128);
15
16 const MASK: [u32; 2] = [0, 0xff_ff_ff_ff];
17
18 impl BVec4A {
19 /// All false.
20 pub const FALSE: Self = Self::splat(false);
21
22 /// All true.
23 pub const TRUE: Self = Self::splat(true);
24
25 /// Creates a new vector mask.
26 #[inline(always)]
27 #[must_use]
28 pub const fn new(x: bool, y: bool, z: bool, w: bool) -> Self {
29 Self(u32x4(
30 MASK[x as usize],
31 MASK[y as usize],
32 MASK[z as usize],
33 MASK[w as usize],
34 ))
35 }
36
37 /// Creates a vector mask with all elements set to `v`.
38 #[inline]
39 #[must_use]
40 pub const fn splat(v: bool) -> Self {
41 Self::new(v, v, v, v)
42 }
43
44 /// Creates a new vector mask from a bool array.
45 #[inline]
46 #[must_use]
47 pub const fn from_array(a: [bool; 4]) -> Self {
48 Self::new(a[0], a[1], a[2], a[3])
49 }
50
51 /// Returns a bitmask with the lowest 4 bits set from the elements of `self`.
52 ///
53 /// A true element results in a `1` bit and a false element in a `0` bit. Element `x` goes
54 /// into the first lowest bit, element `y` into the second, etc.
55 #[inline]
56 #[must_use]
57 pub fn bitmask(self) -> u32 {
58 u32x4_bitmask(self.0) as u32
59 }
60
61 /// Returns true if any of the elements are true, false otherwise.
62 #[inline]
63 #[must_use]
64 pub fn any(self) -> bool {
65 self.bitmask() != 0
66 }
67
68 /// Returns true if all the elements are true, false otherwise.
69 #[inline]
70 #[must_use]
71 pub fn all(self) -> bool {
72 self.bitmask() == 0xf
73 }
74
75 /// Tests the value at `index`.
76 ///
77 /// Panics if `index` is greater than 3.
78 #[inline]
79 #[must_use]
80 pub fn test(&self, index: usize) -> bool {
81 match index {
82 0 => (self.bitmask() & (1 << 0)) != 0,
83 1 => (self.bitmask() & (1 << 1)) != 0,
84 2 => (self.bitmask() & (1 << 2)) != 0,
85 3 => (self.bitmask() & (1 << 3)) != 0,
86 _ => panic!("index out of bounds"),
87 }
88 }
89
90 /// Sets the element at `index`.
91 ///
92 /// Panics if `index` is greater than 3.
93 #[inline]
94 pub fn set(&mut self, index: usize, value: bool) {
95 use crate::Vec4;
96 let mut v = Vec4(self.0);
97 v[index] = f32::from_bits(MASK[value as usize]);
98 self.0 = v.0;
99 }
100
101 #[inline]
102 #[must_use]
103 fn into_bool_array(self) -> [bool; 4] {
104 let bitmask = self.bitmask();
105 [
106 (bitmask & 1) != 0,
107 (bitmask & 2) != 0,
108 (bitmask & 4) != 0,
109 (bitmask & 8) != 0,
110 ]
111 }
112
113 #[inline]
114 #[must_use]
115 fn into_u32_array(self) -> [u32; 4] {
116 let bitmask = self.bitmask();
117 [
118 MASK[(bitmask & 1) as usize],
119 MASK[((bitmask >> 1) & 1) as usize],
120 MASK[((bitmask >> 2) & 1) as usize],
121 MASK[((bitmask >> 3) & 1) as usize],
122 ]
123 }
124 }
125
126 impl Default for BVec4A {
127 #[inline]
128 fn default() -> Self {
129 Self::FALSE
130 }
131 }
132
133 impl PartialEq for BVec4A {
134 #[inline]
135 fn eq(&self, rhs: &Self) -> bool {
136 self.bitmask().eq(&rhs.bitmask())
137 }
138 }
139
140 impl Eq for BVec4A {}
141
142 impl core::hash::Hash for BVec4A {
143 #[inline]
144 fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
145 self.bitmask().hash(state);
146 }
147 }
148
149 impl BitAnd for BVec4A {
150 type Output = Self;
151 #[inline]
152 fn bitand(self, rhs: Self) -> Self {
153 Self(v128_and(self.0, rhs.0))
154 }
155 }
156
157 impl BitAndAssign for BVec4A {
158 #[inline]
159 fn bitand_assign(&mut self, rhs: Self) {
160 *self = self.bitand(rhs);
161 }
162 }
163
164 impl BitOr for BVec4A {
165 type Output = Self;
166 #[inline]
167 fn bitor(self, rhs: Self) -> Self {
168 Self(v128_or(self.0, rhs.0))
169 }
170 }
171
172 impl BitOrAssign for BVec4A {
173 #[inline]
174 fn bitor_assign(&mut self, rhs: Self) {
175 *self = self.bitor(rhs);
176 }
177 }
178
179 impl BitXor for BVec4A {
180 type Output = Self;
181 #[inline]
182 fn bitxor(self, rhs: Self) -> Self {
183 Self(v128_xor(self.0, rhs.0))
184 }
185 }
186
187 impl BitXorAssign for BVec4A {
188 #[inline]
189 fn bitxor_assign(&mut self, rhs: Self) {
190 *self = self.bitxor(rhs);
191 }
192 }
193
194 impl Not for BVec4A {
195 type Output = Self;
196 #[inline]
197 fn not(self) -> Self {
198 Self(v128_not(self.0))
199 }
200 }
201
202 impl From<BVec4A> for v128 {
203 #[inline]
204 fn from(t: BVec4A) -> Self {
205 t.0
206 }
207 }
208
209 #[cfg(not(target_arch = "spirv"))]
210 impl fmt::Debug for BVec4A {
211 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
212 let arr = self.into_u32_array();
213 write!(
214 f,
215 "{}({:#x}, {:#x}, {:#x}, {:#x})",
216 stringify!(BVec4A),
217 arr[0],
218 arr[1],
219 arr[2],
220 arr[3]
221 )
222 }
223 }
224
225 #[cfg(not(target_arch = "spirv"))]
226 impl fmt::Display for BVec4A {
227 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
228 let arr = self.into_bool_array();
229 write!(f, "[{}, {}, {}, {}]", arr[0], arr[1], arr[2], arr[3])
230 }
231 }
232
233 impl From<[bool; 4]> for BVec4A {
234 #[inline]
235 fn from(a: [bool; 4]) -> Self {
236 Self::from_array(a)
237 }
238 }
239
240 impl From<BVec4A> for [bool; 4] {
241 #[inline]
242 fn from(mask: BVec4A) -> Self {
243 mask.into_bool_array()
244 }
245 }
246
247 impl From<BVec4A> for [u32; 4] {
248 #[inline]
249 fn from(mask: BVec4A) -> Self {
250 mask.into_u32_array()
251 }
252 }