Debian Codegraph @

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