• 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: BVec4A,
12 }
13 
14 /// Creates a 4-dimensional `bool` vector mask.
15 #[inline(always)]
16 #[must_use]
bvec4a(x: bool, y: bool, z: bool, w: bool) -> BVec4A17 pub const fn bvec4a(x: bool, y: bool, z: bool, w: bool) -> BVec4A {
18     BVec4A::new(x, y, z, w)
19 }
20 
21 /// A 4-dimensional SIMD vector mask.
22 ///
23 /// This type is 16 byte aligned.
24 #[derive(Clone, Copy)]
25 #[repr(transparent)]
26 pub struct BVec4A(pub(crate) mask32x4);
27 
28 const MASK: [u32; 2] = [0, 0xff_ff_ff_ff];
29 
30 impl BVec4A {
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, w: bool) -> Self40     pub const fn new(x: bool, y: bool, z: bool, w: bool) -> Self {
41         unsafe {
42             UnionCast {
43                 a: [
44                     MASK[x as usize],
45                     MASK[y as usize],
46                     MASK[z as usize],
47                     MASK[w as usize],
48                 ],
49             }
50             .v
51         }
52     }
53 
54     /// Creates a vector mask with all elements set to `v`.
55     #[inline]
56     #[must_use]
splat(v: bool) -> Self57     pub const fn splat(v: bool) -> Self {
58         Self::new(v, v, v, v)
59     }
60 
61     /// Creates a new vector mask from a bool array.
62     #[inline]
63     #[must_use]
from_array(a: [bool; 4]) -> Self64     pub const fn from_array(a: [bool; 4]) -> Self {
65         Self::new(a[0], a[1], a[2], a[3])
66     }
67 
68     /// Returns a bitmask with the lowest 4 bits set from the elements of `self`.
69     ///
70     /// A true element results in a `1` bit and a false element in a `0` bit.  Element `x` goes
71     /// into the first lowest bit, element `y` into the second, etc.
72     #[inline]
73     #[must_use]
bitmask(self) -> u3274     pub fn bitmask(self) -> u32 {
75         self.0.to_bitmask() as u32
76     }
77 
78     /// Returns true if any of the elements are true, false otherwise.
79     #[inline]
80     #[must_use]
any(self) -> bool81     pub fn any(self) -> bool {
82         self.bitmask() != 0
83     }
84 
85     /// Returns true if all the elements are true, false otherwise.
86     #[inline]
87     #[must_use]
all(self) -> bool88     pub fn all(self) -> bool {
89         self.bitmask() == 0xf
90     }
91 
92     /// Tests the value at `index`.
93     ///
94     /// Panics if `index` is greater than 3.
95     #[inline]
96     #[must_use]
test(&self, index: usize) -> bool97     pub fn test(&self, index: usize) -> bool {
98         self.0.test(index)
99     }
100 
101     /// Sets the element at `index`.
102     ///
103     /// Panics if `index` is greater than 3.
104     #[inline]
set(&mut self, index: usize, value: bool)105     pub fn set(&mut self, index: usize, value: bool) {
106         self.0.set(index, value)
107     }
108 
109     #[inline]
110     #[must_use]
into_bool_array(self) -> [bool; 4]111     fn into_bool_array(self) -> [bool; 4] {
112         let bitmask = self.bitmask();
113         [
114             (bitmask & 1) != 0,
115             (bitmask & 2) != 0,
116             (bitmask & 4) != 0,
117             (bitmask & 8) != 0,
118         ]
119     }
120 
121     #[inline]
122     #[must_use]
into_u32_array(self) -> [u32; 4]123     fn into_u32_array(self) -> [u32; 4] {
124         let bitmask = self.bitmask();
125         [
126             MASK[(bitmask & 1) as usize],
127             MASK[((bitmask >> 1) & 1) as usize],
128             MASK[((bitmask >> 2) & 1) as usize],
129             MASK[((bitmask >> 3) & 1) as usize],
130         ]
131     }
132 }
133 
134 impl Default for BVec4A {
135     #[inline]
default() -> Self136     fn default() -> Self {
137         Self::FALSE
138     }
139 }
140 
141 impl PartialEq for BVec4A {
142     #[inline]
eq(&self, rhs: &Self) -> bool143     fn eq(&self, rhs: &Self) -> bool {
144         self.bitmask().eq(&rhs.bitmask())
145     }
146 }
147 
148 impl Eq for BVec4A {}
149 
150 impl core::hash::Hash for BVec4A {
151     #[inline]
hash<H: core::hash::Hasher>(&self, state: &mut H)152     fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
153         self.bitmask().hash(state);
154     }
155 }
156 
157 impl BitAnd for BVec4A {
158     type Output = Self;
159     #[inline]
bitand(self, rhs: Self) -> Self160     fn bitand(self, rhs: Self) -> Self {
161         Self(self.0 & rhs.0)
162     }
163 }
164 
165 impl BitAndAssign for BVec4A {
166     #[inline]
bitand_assign(&mut self, rhs: Self)167     fn bitand_assign(&mut self, rhs: Self) {
168         *self = self.bitand(rhs);
169     }
170 }
171 
172 impl BitOr for BVec4A {
173     type Output = Self;
174     #[inline]
bitor(self, rhs: Self) -> Self175     fn bitor(self, rhs: Self) -> Self {
176         Self(self.0 | rhs.0)
177     }
178 }
179 
180 impl BitOrAssign for BVec4A {
181     #[inline]
bitor_assign(&mut self, rhs: Self)182     fn bitor_assign(&mut self, rhs: Self) {
183         *self = self.bitor(rhs);
184     }
185 }
186 
187 impl BitXor for BVec4A {
188     type Output = Self;
189     #[inline]
bitxor(self, rhs: Self) -> Self190     fn bitxor(self, rhs: Self) -> Self {
191         Self(self.0 ^ rhs.0)
192     }
193 }
194 
195 impl BitXorAssign for BVec4A {
196     #[inline]
bitxor_assign(&mut self, rhs: Self)197     fn bitxor_assign(&mut self, rhs: Self) {
198         *self = self.bitxor(rhs);
199     }
200 }
201 
202 impl Not for BVec4A {
203     type Output = Self;
204     #[inline]
not(self) -> Self205     fn not(self) -> Self {
206         Self(!self.0)
207     }
208 }
209 
210 impl From<BVec4A> for mask32x4 {
211     #[inline]
from(t: BVec4A) -> Self212     fn from(t: BVec4A) -> Self {
213         t.0
214     }
215 }
216 
217 impl fmt::Debug for BVec4A {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result218     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
219         let arr = self.into_u32_array();
220         write!(
221             f,
222             "{}({:#x}, {:#x}, {:#x}, {:#x})",
223             stringify!(BVec4A),
224             arr[0],
225             arr[1],
226             arr[2],
227             arr[3]
228         )
229     }
230 }
231 
232 impl fmt::Display for BVec4A {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result233     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
234         let arr = self.into_bool_array();
235         write!(f, "[{}, {}, {}, {}]", arr[0], arr[1], arr[2], arr[3])
236     }
237 }
238 
239 impl From<[bool; 4]> for BVec4A {
240     #[inline]
from(a: [bool; 4]) -> Self241     fn from(a: [bool; 4]) -> Self {
242         Self::from_array(a)
243     }
244 }
245 
246 impl From<BVec4A> for [bool; 4] {
247     #[inline]
from(mask: BVec4A) -> Self248     fn from(mask: BVec4A) -> Self {
249         mask.into_bool_array()
250     }
251 }
252 
253 impl From<BVec4A> for [u32; 4] {
254     #[inline]
from(mask: BVec4A) -> Self255     fn from(mask: BVec4A) -> Self {
256         mask.into_u32_array()
257     }
258 }
259