Debian Codegraph @

debian/missing-sources/glam-0.17.3/src/vec_mask.rs

raw symbols SCIP
1 use crate::core::traits::vector::{
2 MaskVector, MaskVector2, MaskVector3, MaskVector4, MaskVectorConst,
3 };
4 #[cfg(not(target_arch = "spirv"))]
5 use core::fmt;
6 use core::{hash, ops::*};
7
8 #[cfg(all(
9 target_arch = "x86",
10 target_feature = "sse2",
11 not(feature = "scalar-math")
12 ))]
13 use core::arch::x86::*;
14 #[cfg(all(
15 target_arch = "x86_64",
16 target_feature = "sse2",
17 not(feature = "scalar-math")
18 ))]
19 use core::arch::x86_64::*;
20
21 macro_rules! impl_vecnmask_methods {
22 ($vecnmask:ident, $trait:ident) => {
23 /// Returns a bitmask with the lowest two bits set from the elements of `self`.
24 ///
25 /// A true element results in a `1` bit and a false element in a `0` bit. Element `x` goes
26 /// into the first lowest bit, element `y` into the second, etc.
27 #[inline]
28 pub fn bitmask(self) -> u32 {
29 $trait::bitmask(self.0)
30 }
31
32 /// Returns true if any of the elements are true, false otherwise.
33 #[inline]
34 pub fn any(self) -> bool {
35 $trait::any(self.0)
36 }
37
38 /// Returns true if all the elements are true, false otherwise.
39 #[inline]
40 pub fn all(self) -> bool {
41 $trait::all(self.0)
42 }
43 };
44 }
45
46 macro_rules! impl_vecnmask_traits {
47 ($vecnmask:ident, $inner:ident) => {
48 impl Default for $vecnmask {
49 #[inline]
50 fn default() -> Self {
51 Self($inner::FALSE)
52 }
53 }
54
55 impl PartialEq for $vecnmask {
56 #[inline]
57 fn eq(&self, other: &Self) -> bool {
58 self.bitmask().eq(&other.bitmask())
59 }
60 }
61
62 impl Eq for $vecnmask {}
63
64 impl hash::Hash for $vecnmask {
65 #[inline]
66 fn hash<H: hash::Hasher>(&self, state: &mut H) {
67 self.bitmask().hash(state);
68 }
69 }
70
71 impl BitAnd for $vecnmask {
72 type Output = Self;
73 #[inline]
74 fn bitand(self, other: Self) -> Self {
75 Self(self.0.bitand(other.0))
76 }
77 }
78
79 impl BitAndAssign for $vecnmask {
80 #[inline]
81 fn bitand_assign(&mut self, other: Self) {
82 self.0 = self.0.bitand(other.0);
83 }
84 }
85
86 impl BitOr for $vecnmask {
87 type Output = Self;
88 #[inline]
89 fn bitor(self, other: Self) -> Self {
90 Self(self.0.bitor(other.0))
91 }
92 }
93
94 impl BitOrAssign for $vecnmask {
95 #[inline]
96 fn bitor_assign(&mut self, other: Self) {
97 self.0 = self.0.bitor(other.0);
98 }
99 }
100
101 impl Not for $vecnmask {
102 type Output = Self;
103 #[inline]
104 fn not(self) -> Self {
105 Self(self.0.not())
106 }
107 }
108
109 impl From<$vecnmask> for $inner {
110 #[inline]
111 fn from(t: $vecnmask) -> Self {
112 t.0
113 }
114 }
115 };
116 }
117
118 macro_rules! impl_vec2mask {
119 ($vec2mask:ident, $t:ty, $inner:ident) => {
120 impl $vec2mask {
121 /// Creates a new vector mask.
122 #[inline]
123 pub fn new(x: bool, y: bool) -> Self {
124 Self(MaskVector2::new(x, y))
125 }
126
127 impl_vecnmask_methods!($vec2mask, MaskVector2);
128 }
129
130 impl_vecnmask_traits!($vec2mask, $inner);
131
132 #[cfg(not(target_arch = "spirv"))]
133 impl fmt::Debug for $vec2mask {
134 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
135 let arr = self.0.into_u32_array();
136 write!(f, "{}({:#x}, {:#x})", stringify!($vec2mask), arr[0], arr[1])
137 }
138 }
139
140 #[cfg(not(target_arch = "spirv"))]
141 impl fmt::Display for $vec2mask {
142 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
143 let arr = self.0.into_bool_array();
144 write!(f, "[{}, {}]", arr[0], arr[1])
145 }
146 }
147
148 impl From<$vec2mask> for [bool; 2] {
149 #[inline]
150 fn from(mask: $vec2mask) -> Self {
151 mask.0.into_bool_array()
152 }
153 }
154
155 impl From<$vec2mask> for [u32; 2] {
156 #[inline]
157 fn from(mask: $vec2mask) -> Self {
158 mask.0.into_u32_array()
159 }
160 }
161
162 #[cfg(not(target_arch = "spriv"))]
163 impl AsRef<[$t; 2]> for $vec2mask {
164 #[inline]
165 fn as_ref(&self) -> &[$t; 2] {
166 unsafe { &*(self as *const Self as *const [$t; 2]) }
167 }
168 }
169 };
170 }
171
172 macro_rules! impl_vec3mask {
173 ($vec3mask:ident, $t:ty, $inner:ident) => {
174 impl $vec3mask {
175 /// Creates a new vector mask.
176 #[inline]
177 pub fn new(x: bool, y: bool, z: bool) -> Self {
178 Self(MaskVector3::new(x, y, z))
179 }
180
181 impl_vecnmask_methods!($vec3mask, MaskVector3);
182 }
183
184 impl_vecnmask_traits!($vec3mask, $inner);
185
186 #[cfg(not(target_arch = "spirv"))]
187 impl fmt::Debug for $vec3mask {
188 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
189 let arr = MaskVector3::into_u32_array(self.0);
190 write!(
191 f,
192 "{}({:#x}, {:#x}, {:#x})",
193 stringify!($vec3mask),
194 arr[0],
195 arr[1],
196 arr[2]
197 )
198 }
199 }
200
201 #[cfg(not(target_arch = "spirv"))]
202 impl fmt::Display for $vec3mask {
203 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
204 let arr = MaskVector3::into_bool_array(self.0);
205 write!(f, "[{}, {}, {}]", arr[0], arr[1], arr[2])
206 }
207 }
208
209 impl From<$vec3mask> for [bool; 3] {
210 #[inline]
211 fn from(mask: $vec3mask) -> Self {
212 MaskVector3::into_bool_array(mask.0)
213 }
214 }
215
216 impl From<$vec3mask> for [u32; 3] {
217 #[inline]
218 fn from(mask: $vec3mask) -> Self {
219 MaskVector3::into_u32_array(mask.0)
220 }
221 }
222
223 #[cfg(not(target_arch = "spriv"))]
224 impl AsRef<[$t; 3]> for $vec3mask {
225 #[inline]
226 fn as_ref(&self) -> &[$t; 3] {
227 unsafe { &*(self as *const Self as *const [$t; 3]) }
228 }
229 }
230 };
231 }
232
233 macro_rules! impl_vec4mask {
234 ($vec4mask:ident, $t:ty, $inner:ident) => {
235 impl $vec4mask {
236 /// Creates a new vector mask.
237 #[inline]
238 pub fn new(x: bool, y: bool, z: bool, w: bool) -> Self {
239 Self(MaskVector4::new(x, y, z, w))
240 }
241
242 impl_vecnmask_methods!($vec4mask, MaskVector4);
243 }
244
245 impl_vecnmask_traits!($vec4mask, $inner);
246
247 #[cfg(not(target_arch = "spirv"))]
248 impl fmt::Debug for $vec4mask {
249 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
250 let arr = MaskVector4::into_u32_array(self.0);
251 write!(
252 f,
253 "{}({:#x}, {:#x}, {:#x}, {:#x})",
254 stringify!($vec4mask),
255 arr[0],
256 arr[1],
257 arr[2],
258 arr[3]
259 )
260 }
261 }
262
263 #[cfg(not(target_arch = "spirv"))]
264 impl fmt::Display for $vec4mask {
265 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
266 let arr = MaskVector4::into_bool_array(self.0);
267 write!(f, "[{}, {}, {}, {}]", arr[0], arr[1], arr[2], arr[3])
268 }
269 }
270
271 impl From<$vec4mask> for [bool; 4] {
272 #[inline]
273 fn from(mask: $vec4mask) -> Self {
274 MaskVector4::into_bool_array(mask.0)
275 }
276 }
277
278 impl From<$vec4mask> for [u32; 4] {
279 #[inline]
280 fn from(mask: $vec4mask) -> Self {
281 MaskVector4::into_u32_array(mask.0)
282 }
283 }
284
285 #[cfg(not(target_arch = "spriv"))]
286 impl AsRef<[$t; 4]> for $vec4mask {
287 #[inline]
288 fn as_ref(&self) -> &[$t; 4] {
289 unsafe { &*(self as *const Self as *const [$t; 4]) }
290 }
291 }
292 };
293 }
294
295 // BVec3A /////////////////////////////////////////////////////////////////////////////////////////
296
297 /// A 3-dimensional SIMD vector mask.
298 ///
299 /// This type is 16 byte aligned and is backed by a SIMD vector. If SIMD is not available `BVec3A`
300 /// will be a type alias for `BVec3`.
301 #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))]
302 #[derive(Clone, Copy)]
303 #[repr(transparent)]
304 pub struct BVec3A(pub(crate) __m128);
305
306 #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))]
307 impl_vec3mask!(BVec3A, u32, __m128);
308
309 #[cfg(any(not(target_feature = "sse2"), feature = "scalar-math"))]
310 pub type BVec3A = BVec3;
311
312 #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))]
313 impl From<BVec3> for BVec3A {
314 #[inline]
315 fn from(b: BVec3) -> Self {
316 Self::new(b.0.x, b.0.y, b.0.z)
317 }
318 }
319
320 #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))]
321 impl From<BVec3A> for BVec3 {
322 #[inline]
323 fn from(b: BVec3A) -> Self {
324 let b: [bool; 3] = b.into();
325 Self::new(b[0], b[1], b[2])
326 }
327 }
328
329 // BVec4A ////////////////////////////////////////////////////////////////////////////////////////
330
331 /// A 4-dimensional SIMD vector mask.
332 ///
333 /// This type is 16 byte aligned and is backed by a SIMD vector. If SIMD is not available `BVec4A`
334 /// will be a type alias for `BVec4`.
335 #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))]
336 #[derive(Clone, Copy)]
337 #[repr(transparent)]
338 pub struct BVec4A(pub(crate) __m128);
339
340 #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))]
341 impl_vec4mask!(BVec4A, u32, __m128);
342
343 #[cfg(any(not(target_feature = "sse2"), feature = "scalar-math"))]
344 pub type BVec4A = BVec4;
345
346 #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))]
347 impl From<BVec4> for BVec4A {
348 #[inline]
349 fn from(b: BVec4) -> Self {
350 Self::new(b.0.x, b.0.y, b.0.z, b.0.w)
351 }
352 }
353
354 #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))]
355 impl From<BVec4A> for BVec4 {
356 #[inline]
357 fn from(b: BVec4A) -> Self {
358 let b: [bool; 4] = b.into();
359 Self::new(b[0], b[1], b[2], b[3])
360 }
361 }
362
363 // boolean vectors ////////////////////////////////////////////////////////////////////////////////
364 type XYBool = crate::XY<bool>;
365
366 /// A 2-dimensional boolean vector.
367 #[derive(Copy, Clone)]
368 #[repr(transparent)]
369 pub struct BVec2(pub(crate) XYBool);
370 impl_vec2mask!(BVec2, bool, XYBool);
371
372 type XYZBool = crate::XYZ<bool>;
373
374 /// A 3-dimensional boolean vector.
375 #[derive(Copy, Clone)]
376 #[repr(transparent)]
377 pub struct BVec3(pub(crate) XYZBool);
378 impl_vec3mask!(BVec3, bool, XYZBool);
379
380 type XYZWBool = crate::XYZW<bool>;
381
382 /// A 4-dimensional boolean vector.
383 #[derive(Copy, Clone)]
384 #[repr(transparent)]
385 pub struct BVec4(pub(crate) XYZWBool);
386 impl_vec4mask!(BVec4, bool, XYZWBool);
387
388 mod const_test_bvec2 {
389 const_assert_eq!(
390 core::mem::align_of::<bool>(),
391 core::mem::align_of::<super::BVec2>()
392 );
393 const_assert_eq!(2, core::mem::size_of::<super::BVec2>());
394 }
395
396 mod const_test_bvec3 {
397 const_assert_eq!(
398 core::mem::align_of::<bool>(),
399 core::mem::align_of::<super::BVec3>()
400 );
401 const_assert_eq!(3, core::mem::size_of::<super::BVec3>());
402 }
403
404 #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))]
405 mod const_test_bvec3a {
406 const_assert_eq!(16, core::mem::align_of::<super::BVec3A>());
407 const_assert_eq!(16, core::mem::size_of::<super::BVec3A>());
408 }
409
410 mod const_test_bvec4 {
411 const_assert_eq!(
412 core::mem::align_of::<bool>(),
413 core::mem::align_of::<super::BVec4>()
414 );
415 const_assert_eq!(4, core::mem::size_of::<super::BVec4>());
416 }
417
418 #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))]
419 mod const_test_bvec4a {
420 const_assert_eq!(16, core::mem::align_of::<super::BVec4A>());
421 const_assert_eq!(16, core::mem::size_of::<super::BVec4A>());
422 }