• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Generated from vec_mask.rs.tera template. Edit the template, not the generated file.
2 
3 use core::fmt;
4 use core::ops::*;
5 
6 use core::simd::*;
7 
8 #[repr(C)]
9 union UnionCast {
10     a: [u32; 4],
11     v: BVec3A,
12 }
13 
14 /// Creates a 3-dimensional `bool` vector mask.
15 #[inline(always)]
16 #[must_use]
bvec3a(x: bool, y: bool, z: bool) -> BVec3A17 pub const fn bvec3a(x: bool, y: bool, z: bool) -> BVec3A {
18     BVec3A::new(x, y, z)
19 }
20 
21 /// A 3-dimensional SIMD vector mask.
22 ///
23 /// This type is 16 byte aligned.
24 #[derive(Clone, Copy)]
25 #[repr(transparent)]
26 pub struct BVec3A(pub(crate) mask32x4);
27 
28 const MASK: [u32; 2] = [0, 0xff_ff_ff_ff];
29 
30 impl BVec3A {
31     /// All false.
32     pub const FALSE: Self = Self::splat(false);
33 
34     /// All true.
35     pub const TRUE: Self = Self::splat(true);
36 
37     /// Creates a new vector mask.
38     #[inline(always)]
39     #[must_use]
new(x: bool, y: bool, z: bool) -> Self40     pub const fn new(x: bool, y: bool, z: bool) -> Self {
41         unsafe {
42             UnionCast {
43                 a: [MASK[x as usize], MASK[y as usize], MASK[z as usize], 0],
44             }
45             .v
46         }
47     }
48 
49     /// Creates a vector mask with all elements set to `v`.
50     #[inline]
51     #[must_use]
splat(v: bool) -> Self52     pub const fn splat(v: bool) -> Self {
53         Self::new(v, v, v)
54     }
55 
56     /// Creates a new vector mask from a bool array.
57     #[inline]
58     #[must_use]
from_array(a: [bool; 3]) -> Self59     pub const fn from_array(a: [bool; 3]) -> Self {
60         Self::new(a[0], a[1], a[2])
61     }
62 
63     /// Returns a bitmask with the lowest 3 bits set from the elements of `self`.
64     ///
65     /// A true element results in a `1` bit and a false element in a `0` bit.  Element `x` goes
66     /// into the first lowest bit, element `y` into the second, etc.
67     #[inline]
68     #[must_use]
bitmask(self) -> u3269     pub fn bitmask(self) -> u32 {
70         (self.0.to_bitmask() & 0x7) as u32
71     }
72 
73     /// Returns true if any of the elements are true, false otherwise.
74     #[inline]
75     #[must_use]
any(self) -> bool76     pub fn any(self) -> bool {
77         self.bitmask() != 0
78     }
79 
80     /// Returns true if all the elements are true, false otherwise.
81     #[inline]
82     #[must_use]
all(self) -> bool83     pub fn all(self) -> bool {
84         self.bitmask() == 0x7
85     }
86 
87     /// Tests the value at `index`.
88     ///
89     /// Panics if `index` is greater than 2.
90     #[inline]
91     #[must_use]
test(&self, index: usize) -> bool92     pub fn test(&self, index: usize) -> bool {
93         self.0.test(index)
94     }
95 
96     /// Sets the element at `index`.
97     ///
98     /// Panics if `index` is greater than 2.
99     #[inline]
set(&mut self, index: usize, value: bool)100     pub fn set(&mut self, index: usize, value: bool) {
101         self.0.set(index, value)
102     }
103 
104     #[inline]
105     #[must_use]
into_bool_array(self) -> [bool; 3]106     fn into_bool_array(self) -> [bool; 3] {
107         let bitmask = self.bitmask();
108         [(bitmask & 1) != 0, (bitmask & 2) != 0, (bitmask & 4) != 0]
109     }
110 
111     #[inline]
112     #[must_use]
into_u32_array(self) -> [u32; 3]113     fn into_u32_array(self) -> [u32; 3] {
114         let bitmask = self.bitmask();
115         [
116             MASK[(bitmask & 1) as usize],
117             MASK[((bitmask >> 1) & 1) as usize],
118             MASK[((bitmask >> 2) & 1) as usize],
119         ]
120     }
121 }
122 
123 impl Default for BVec3A {
124     #[inline]
default() -> Self125     fn default() -> Self {
126         Self::FALSE
127     }
128 }
129 
130 impl PartialEq for BVec3A {
131     #[inline]
eq(&self, rhs: &Self) -> bool132     fn eq(&self, rhs: &Self) -> bool {
133         self.bitmask().eq(&rhs.bitmask())
134     }
135 }
136 
137 impl Eq for BVec3A {}
138 
139 impl core::hash::Hash for BVec3A {
140     #[inline]
hash<H: core::hash::Hasher>(&self, state: &mut H)141     fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
142         self.bitmask().hash(state);
143     }
144 }
145 
146 impl BitAnd for BVec3A {
147     type Output = Self;
148     #[inline]
bitand(self, rhs: Self) -> Self149     fn bitand(self, rhs: Self) -> Self {
150         Self(self.0 & rhs.0)
151     }
152 }
153 
154 impl BitAndAssign for BVec3A {
155     #[inline]
bitand_assign(&mut self, rhs: Self)156     fn bitand_assign(&mut self, rhs: Self) {
157         *self = self.bitand(rhs);
158     }
159 }
160 
161 impl BitOr for BVec3A {
162     type Output = Self;
163     #[inline]
bitor(self, rhs: Self) -> Self164     fn bitor(self, rhs: Self) -> Self {
165         Self(self.0 | rhs.0)
166     }
167 }
168 
169 impl BitOrAssign for BVec3A {
170     #[inline]
bitor_assign(&mut self, rhs: Self)171     fn bitor_assign(&mut self, rhs: Self) {
172         *self = self.bitor(rhs);
173     }
174 }
175 
176 impl BitXor for BVec3A {
177     type Output = Self;
178     #[inline]
bitxor(self, rhs: Self) -> Self179     fn bitxor(self, rhs: Self) -> Self {
180         Self(self.0 ^ rhs.0)
181     }
182 }
183 
184 impl BitXorAssign for BVec3A {
185     #[inline]
bitxor_assign(&mut self, rhs: Self)186     fn bitxor_assign(&mut self, rhs: Self) {
187         *self = self.bitxor(rhs);
188     }
189 }
190 
191 impl Not for BVec3A {
192     type Output = Self;
193     #[inline]
not(self) -> Self194     fn not(self) -> Self {
195         Self(!self.0)
196     }
197 }
198 
199 impl From<BVec3A> for mask32x4 {
200     #[inline]
from(t: BVec3A) -> Self201     fn from(t: BVec3A) -> Self {
202         t.0
203     }
204 }
205 
206 impl fmt::Debug for BVec3A {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result207     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
208         let arr = self.into_u32_array();
209         write!(
210             f,
211             "{}({:#x}, {:#x}, {:#x})",
212             stringify!(BVec3A),
213             arr[0],
214             arr[1],
215             arr[2]
216         )
217     }
218 }
219 
220 impl fmt::Display for BVec3A {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result221     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
222         let arr = self.into_bool_array();
223         write!(f, "[{}, {}, {}]", arr[0], arr[1], arr[2])
224     }
225 }
226 
227 impl From<[bool; 3]> for BVec3A {
228     #[inline]
from(a: [bool; 3]) -> Self229     fn from(a: [bool; 3]) -> Self {
230         Self::from_array(a)
231     }
232 }
233 
234 impl From<BVec3A> for [bool; 3] {
235     #[inline]
from(mask: BVec3A) -> Self236     fn from(mask: BVec3A) -> Self {
237         mask.into_bool_array()
238     }
239 }
240 
241 impl From<BVec3A> for [u32; 3] {
242     #[inline]
from(mask: BVec3A) -> Self243     fn from(mask: BVec3A) -> Self {
244         mask.into_u32_array()
245     }
246 }
247