• 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 3-dimensional `bool` vector mask.
9 #[inline(always)]
10 #[must_use]
bvec3a(x: bool, y: bool, z: bool) -> BVec3A11 pub const fn bvec3a(x: bool, y: bool, z: bool) -> BVec3A {
12     BVec3A::new(x, y, z)
13 }
14 
15 /// A 3-dimensional SIMD vector mask.
16 ///
17 /// This type is 16 byte aligned.
18 #[derive(Clone, Copy)]
19 #[repr(transparent)]
20 pub struct BVec3A(pub(crate) v128);
21 
22 const MASK: [u32; 2] = [0, 0xff_ff_ff_ff];
23 
24 impl BVec3A {
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) -> Self34     pub const fn new(x: bool, y: bool, z: bool) -> Self {
35         Self(u32x4(
36             MASK[x as usize],
37             MASK[y as usize],
38             MASK[z as usize],
39             0,
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)
48     }
49 
50     /// Creates a new vector mask from a bool array.
51     #[inline]
52     #[must_use]
from_array(a: [bool; 3]) -> Self53     pub const fn from_array(a: [bool; 3]) -> Self {
54         Self::new(a[0], a[1], a[2])
55     }
56 
57     /// Returns a bitmask with the lowest 3 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) & 0x7) 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() == 0x7
79     }
80 
81     /// Tests the value at `index`.
82     ///
83     /// Panics if `index` is greater than 2.
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             _ => panic!("index out of bounds"),
92         }
93     }
94 
95     /// Sets the element at `index`.
96     ///
97     /// Panics if `index` is greater than 2.
98     #[inline]
set(&mut self, index: usize, value: bool)99     pub fn set(&mut self, index: usize, value: bool) {
100         use crate::Vec3A;
101         let mut v = Vec3A(self.0);
102         v[index] = f32::from_bits(MASK[value as usize]);
103         self.0 = v.0;
104     }
105 
106     #[inline]
107     #[must_use]
into_bool_array(self) -> [bool; 3]108     fn into_bool_array(self) -> [bool; 3] {
109         let bitmask = self.bitmask();
110         [(bitmask & 1) != 0, (bitmask & 2) != 0, (bitmask & 4) != 0]
111     }
112 
113     #[inline]
114     #[must_use]
into_u32_array(self) -> [u32; 3]115     fn into_u32_array(self) -> [u32; 3] {
116         let bitmask = self.bitmask();
117         [
118             MASK[(bitmask & 1) as usize],
119             MASK[((bitmask >> 1) & 1) as usize],
120             MASK[((bitmask >> 2) & 1) as usize],
121         ]
122     }
123 }
124 
125 impl Default for BVec3A {
126     #[inline]
default() -> Self127     fn default() -> Self {
128         Self::FALSE
129     }
130 }
131 
132 impl PartialEq for BVec3A {
133     #[inline]
eq(&self, rhs: &Self) -> bool134     fn eq(&self, rhs: &Self) -> bool {
135         self.bitmask().eq(&rhs.bitmask())
136     }
137 }
138 
139 impl Eq for BVec3A {}
140 
141 impl core::hash::Hash for BVec3A {
142     #[inline]
hash<H: core::hash::Hasher>(&self, state: &mut H)143     fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
144         self.bitmask().hash(state);
145     }
146 }
147 
148 impl BitAnd for BVec3A {
149     type Output = Self;
150     #[inline]
bitand(self, rhs: Self) -> Self151     fn bitand(self, rhs: Self) -> Self {
152         Self(v128_and(self.0, rhs.0))
153     }
154 }
155 
156 impl BitAndAssign for BVec3A {
157     #[inline]
bitand_assign(&mut self, rhs: Self)158     fn bitand_assign(&mut self, rhs: Self) {
159         *self = self.bitand(rhs);
160     }
161 }
162 
163 impl BitOr for BVec3A {
164     type Output = Self;
165     #[inline]
bitor(self, rhs: Self) -> Self166     fn bitor(self, rhs: Self) -> Self {
167         Self(v128_or(self.0, rhs.0))
168     }
169 }
170 
171 impl BitOrAssign for BVec3A {
172     #[inline]
bitor_assign(&mut self, rhs: Self)173     fn bitor_assign(&mut self, rhs: Self) {
174         *self = self.bitor(rhs);
175     }
176 }
177 
178 impl BitXor for BVec3A {
179     type Output = Self;
180     #[inline]
bitxor(self, rhs: Self) -> Self181     fn bitxor(self, rhs: Self) -> Self {
182         Self(v128_xor(self.0, rhs.0))
183     }
184 }
185 
186 impl BitXorAssign for BVec3A {
187     #[inline]
bitxor_assign(&mut self, rhs: Self)188     fn bitxor_assign(&mut self, rhs: Self) {
189         *self = self.bitxor(rhs);
190     }
191 }
192 
193 impl Not for BVec3A {
194     type Output = Self;
195     #[inline]
not(self) -> Self196     fn not(self) -> Self {
197         Self(v128_not(self.0))
198     }
199 }
200 
201 impl From<BVec3A> for v128 {
202     #[inline]
from(t: BVec3A) -> Self203     fn from(t: BVec3A) -> Self {
204         t.0
205     }
206 }
207 
208 impl fmt::Debug for BVec3A {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result209     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
210         let arr = self.into_u32_array();
211         write!(
212             f,
213             "{}({:#x}, {:#x}, {:#x})",
214             stringify!(BVec3A),
215             arr[0],
216             arr[1],
217             arr[2]
218         )
219     }
220 }
221 
222 impl fmt::Display for BVec3A {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result223     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
224         let arr = self.into_bool_array();
225         write!(f, "[{}, {}, {}]", arr[0], arr[1], arr[2])
226     }
227 }
228 
229 impl From<[bool; 3]> for BVec3A {
230     #[inline]
from(a: [bool; 3]) -> Self231     fn from(a: [bool; 3]) -> Self {
232         Self::from_array(a)
233     }
234 }
235 
236 impl From<BVec3A> for [bool; 3] {
237     #[inline]
from(mask: BVec3A) -> Self238     fn from(mask: BVec3A) -> Self {
239         mask.into_bool_array()
240     }
241 }
242 
243 impl From<BVec3A> for [u32; 3] {
244     #[inline]
from(mask: BVec3A) -> Self245     fn from(mask: BVec3A) -> Self {
246         mask.into_u32_array()
247     }
248 }
249