Debian Codegraph @

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