• 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::arch::wasm32::*;
7 
8 /// Creates a 4-dimensional `bool` vector mask.
9 #[inline(always)]
10 #[must_use]
bvec4a(x: bool, y: bool, z: bool, w: bool) -> BVec4A11 pub const fn bvec4a(x: bool, y: bool, z: bool, w: bool) -> BVec4A {
12     BVec4A::new(x, y, z, w)
13 }
14 
15 /// A 4-dimensional SIMD vector mask.
16 ///
17 /// This type is 16 byte aligned.
18 #[derive(Clone, Copy)]
19 #[repr(transparent)]
20 pub struct BVec4A(pub(crate) v128);
21 
22 const MASK: [u32; 2] = [0, 0xff_ff_ff_ff];
23 
24 impl BVec4A {
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]
new(x: bool, y: bool, z: bool, w: bool) -> Self34     pub const fn new(x: bool, y: bool, z: bool, w: bool) -> Self {
35         Self(u32x4(
36             MASK[x as usize],
37             MASK[y as usize],
38             MASK[z as usize],
39             MASK[w as usize],
40         ))
41     }
42 
43     /// Creates a vector mask with all elements set to `v`.
44     #[inline]
45     #[must_use]
splat(v: bool) -> Self46     pub const fn splat(v: bool) -> Self {
47         Self::new(v, v, v, v)
48     }
49 
50     /// Creates a new vector mask from a bool array.
51     #[inline]
52     #[must_use]
from_array(a: [bool; 4]) -> Self53     pub const fn from_array(a: [bool; 4]) -> Self {
54         Self::new(a[0], a[1], a[2], a[3])
55     }
56 
57     /// Returns a bitmask with the lowest 4 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]
bitmask(self) -> u3263     pub fn bitmask(self) -> u32 {
64         u32x4_bitmask(self.0) as u32
65     }
66 
67     /// Returns true if any of the elements are true, false otherwise.
68     #[inline]
69     #[must_use]
any(self) -> bool70     pub fn any(self) -> bool {
71         self.bitmask() != 0
72     }
73 
74     /// Returns true if all the elements are true, false otherwise.
75     #[inline]
76     #[must_use]
all(self) -> bool77     pub fn all(self) -> bool {
78         self.bitmask() == 0xf
79     }
80 
81     /// Tests the value at `index`.
82     ///
83     /// Panics if `index` is greater than 3.
84     #[inline]
85     #[must_use]
test(&self, index: usize) -> bool86     pub fn test(&self, index: usize) -> bool {
87         match index {
88             0 => (self.bitmask() & (1 << 0)) != 0,
89             1 => (self.bitmask() & (1 << 1)) != 0,
90             2 => (self.bitmask() & (1 << 2)) != 0,
91             3 => (self.bitmask() & (1 << 3)) != 0,
92             _ => panic!("index out of bounds"),
93         }
94     }
95 
96     /// Sets the element at `index`.
97     ///
98     /// Panics if `index` is greater than 3.
99     #[inline]
set(&mut self, index: usize, value: bool)100     pub fn set(&mut self, index: usize, value: bool) {
101         use crate::Vec4;
102         let mut v = Vec4(self.0);
103         v[index] = f32::from_bits(MASK[value as usize]);
104         self.0 = v.0;
105     }
106 
107     #[inline]
108     #[must_use]
into_bool_array(self) -> [bool; 4]109     fn into_bool_array(self) -> [bool; 4] {
110         let bitmask = self.bitmask();
111         [
112             (bitmask & 1) != 0,
113             (bitmask & 2) != 0,
114             (bitmask & 4) != 0,
115             (bitmask & 8) != 0,
116         ]
117     }
118 
119     #[inline]
120     #[must_use]
into_u32_array(self) -> [u32; 4]121     fn into_u32_array(self) -> [u32; 4] {
122         let bitmask = self.bitmask();
123         [
124             MASK[(bitmask & 1) as usize],
125             MASK[((bitmask >> 1) & 1) as usize],
126             MASK[((bitmask >> 2) & 1) as usize],
127             MASK[((bitmask >> 3) & 1) as usize],
128         ]
129     }
130 }
131 
132 impl Default for BVec4A {
133     #[inline]
default() -> Self134     fn default() -> Self {
135         Self::FALSE
136     }
137 }
138 
139 impl PartialEq for BVec4A {
140     #[inline]
eq(&self, rhs: &Self) -> bool141     fn eq(&self, rhs: &Self) -> bool {
142         self.bitmask().eq(&rhs.bitmask())
143     }
144 }
145 
146 impl Eq for BVec4A {}
147 
148 impl core::hash::Hash for BVec4A {
149     #[inline]
hash<H: core::hash::Hasher>(&self, state: &mut H)150     fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
151         self.bitmask().hash(state);
152     }
153 }
154 
155 impl BitAnd for BVec4A {
156     type Output = Self;
157     #[inline]
bitand(self, rhs: Self) -> Self158     fn bitand(self, rhs: Self) -> Self {
159         Self(v128_and(self.0, rhs.0))
160     }
161 }
162 
163 impl BitAndAssign for BVec4A {
164     #[inline]
bitand_assign(&mut self, rhs: Self)165     fn bitand_assign(&mut self, rhs: Self) {
166         *self = self.bitand(rhs);
167     }
168 }
169 
170 impl BitOr for BVec4A {
171     type Output = Self;
172     #[inline]
bitor(self, rhs: Self) -> Self173     fn bitor(self, rhs: Self) -> Self {
174         Self(v128_or(self.0, rhs.0))
175     }
176 }
177 
178 impl BitOrAssign for BVec4A {
179     #[inline]
bitor_assign(&mut self, rhs: Self)180     fn bitor_assign(&mut self, rhs: Self) {
181         *self = self.bitor(rhs);
182     }
183 }
184 
185 impl BitXor for BVec4A {
186     type Output = Self;
187     #[inline]
bitxor(self, rhs: Self) -> Self188     fn bitxor(self, rhs: Self) -> Self {
189         Self(v128_xor(self.0, rhs.0))
190     }
191 }
192 
193 impl BitXorAssign for BVec4A {
194     #[inline]
bitxor_assign(&mut self, rhs: Self)195     fn bitxor_assign(&mut self, rhs: Self) {
196         *self = self.bitxor(rhs);
197     }
198 }
199 
200 impl Not for BVec4A {
201     type Output = Self;
202     #[inline]
not(self) -> Self203     fn not(self) -> Self {
204         Self(v128_not(self.0))
205     }
206 }
207 
208 impl From<BVec4A> for v128 {
209     #[inline]
from(t: BVec4A) -> Self210     fn from(t: BVec4A) -> Self {
211         t.0
212     }
213 }
214 
215 impl fmt::Debug for BVec4A {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result216     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
217         let arr = self.into_u32_array();
218         write!(
219             f,
220             "{}({:#x}, {:#x}, {:#x}, {:#x})",
221             stringify!(BVec4A),
222             arr[0],
223             arr[1],
224             arr[2],
225             arr[3]
226         )
227     }
228 }
229 
230 impl fmt::Display for BVec4A {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result231     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
232         let arr = self.into_bool_array();
233         write!(f, "[{}, {}, {}, {}]", arr[0], arr[1], arr[2], arr[3])
234     }
235 }
236 
237 impl From<[bool; 4]> for BVec4A {
238     #[inline]
from(a: [bool; 4]) -> Self239     fn from(a: [bool; 4]) -> Self {
240         Self::from_array(a)
241     }
242 }
243 
244 impl From<BVec4A> for [bool; 4] {
245     #[inline]
from(mask: BVec4A) -> Self246     fn from(mask: BVec4A) -> Self {
247         mask.into_bool_array()
248     }
249 }
250 
251 impl From<BVec4A> for [u32; 4] {
252     #[inline]
from(mask: BVec4A) -> Self253     fn from(mask: BVec4A) -> Self {
254         mask.into_u32_array()
255     }
256 }
257