Debian Codegraph @

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