Debian Codegraph @

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