Debian Codegraph @

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