• 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 /// Creates a 4-dimensional `bool` vector mask.
7 #[inline(always)]
8 #[must_use]
bvec4a(x: bool, y: bool, z: bool, w: bool) -> BVec4A9 pub const fn bvec4a(x: bool, y: bool, z: bool, w: bool) -> BVec4A {
10     BVec4A::new(x, y, z, w)
11 }
12 
13 /// A 4-dimensional `u32` vector mask.
14 #[derive(Clone, Copy, PartialEq, Eq, Hash)]
15 #[repr(C, align(16))]
16 pub struct BVec4A {
17     pub x: u32,
18     pub y: u32,
19     pub z: u32,
20     pub w: u32,
21 }
22 
23 const MASK: [u32; 2] = [0, 0xff_ff_ff_ff];
24 
25 impl BVec4A {
26     /// All false.
27     pub const FALSE: Self = Self::splat(false);
28 
29     /// All true.
30     pub const TRUE: Self = Self::splat(true);
31 
32     /// Creates a new vector mask.
33     #[inline(always)]
34     #[must_use]
new(x: bool, y: bool, z: bool, w: bool) -> Self35     pub const fn new(x: bool, y: bool, z: bool, w: bool) -> Self {
36         Self {
37             x: MASK[x as usize],
38             y: MASK[y as usize],
39             z: MASK[z as usize],
40             w: MASK[w as usize],
41         }
42     }
43 
44     /// Creates a vector mask with all elements set to `v`.
45     #[inline]
46     #[must_use]
splat(v: bool) -> Self47     pub const fn splat(v: bool) -> Self {
48         Self::new(v, v, v, v)
49     }
50 
51     /// Creates a new vector mask from a bool array.
52     #[inline]
53     #[must_use]
from_array(a: [bool; 4]) -> Self54     pub const fn from_array(a: [bool; 4]) -> Self {
55         Self::new(a[0], a[1], a[2], a[3])
56     }
57 
58     /// Returns a bitmask with the lowest 4 bits set from the elements of `self`.
59     ///
60     /// A true element results in a `1` bit and a false element in a `0` bit.  Element `x` goes
61     /// into the first lowest bit, element `y` into the second, etc.
62     #[inline]
63     #[must_use]
bitmask(self) -> u3264     pub fn bitmask(self) -> u32 {
65         (self.x & 0x1) | (self.y & 0x1) << 1 | (self.z & 0x1) << 2 | (self.w & 0x1) << 3
66     }
67 
68     /// Returns true if any of the elements are true, false otherwise.
69     #[inline]
70     #[must_use]
any(self) -> bool71     pub fn any(self) -> bool {
72         ((self.x | self.y | self.z | self.w) & 0x1) != 0
73     }
74 
75     /// Returns true if all the elements are true, false otherwise.
76     #[inline]
77     #[must_use]
all(self) -> bool78     pub fn all(self) -> bool {
79         ((self.x & self.y & self.z & self.w) & 0x1) != 0
80     }
81 
82     /// Tests the value at `index`.
83     ///
84     /// Panics if `index` is greater than 3.
85     #[inline]
86     #[must_use]
test(&self, index: usize) -> bool87     pub fn test(&self, index: usize) -> bool {
88         match index {
89             0 => (self.x & 0x1) != 0,
90             1 => (self.y & 0x1) != 0,
91             2 => (self.z & 0x1) != 0,
92             3 => (self.w & 0x1) != 0,
93             _ => panic!("index out of bounds"),
94         }
95     }
96 
97     /// Sets the element at `index`.
98     ///
99     /// Panics if `index` is greater than 3.
100     #[inline]
set(&mut self, index: usize, value: bool)101     pub fn set(&mut self, index: usize, value: bool) {
102         match index {
103             0 => self.x = MASK[value as usize],
104             1 => self.y = MASK[value as usize],
105             2 => self.z = MASK[value as usize],
106             3 => self.w = MASK[value as usize],
107             _ => panic!("index out of bounds"),
108         }
109     }
110 
111     #[inline]
112     #[must_use]
into_bool_array(self) -> [bool; 4]113     fn into_bool_array(self) -> [bool; 4] {
114         [
115             (self.x & 0x1) != 0,
116             (self.y & 0x1) != 0,
117             (self.z & 0x1) != 0,
118             (self.w & 0x1) != 0,
119         ]
120     }
121 
122     #[inline]
123     #[must_use]
into_u32_array(self) -> [u32; 4]124     fn into_u32_array(self) -> [u32; 4] {
125         [self.x, self.y, self.z, self.w]
126     }
127 }
128 
129 impl Default for BVec4A {
130     #[inline]
default() -> Self131     fn default() -> Self {
132         Self::FALSE
133     }
134 }
135 
136 impl BitAnd for BVec4A {
137     type Output = Self;
138     #[inline]
bitand(self, rhs: Self) -> Self139     fn bitand(self, rhs: Self) -> Self {
140         Self {
141             x: self.x & rhs.x,
142             y: self.y & rhs.y,
143             z: self.z & rhs.z,
144             w: self.w & rhs.w,
145         }
146     }
147 }
148 
149 impl BitAndAssign for BVec4A {
150     #[inline]
bitand_assign(&mut self, rhs: Self)151     fn bitand_assign(&mut self, rhs: Self) {
152         *self = self.bitand(rhs);
153     }
154 }
155 
156 impl BitOr for BVec4A {
157     type Output = Self;
158     #[inline]
bitor(self, rhs: Self) -> Self159     fn bitor(self, rhs: Self) -> Self {
160         Self {
161             x: self.x | rhs.x,
162             y: self.y | rhs.y,
163             z: self.z | rhs.z,
164             w: self.w | rhs.w,
165         }
166     }
167 }
168 
169 impl BitOrAssign for BVec4A {
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 BVec4A {
177     type Output = Self;
178     #[inline]
bitxor(self, rhs: Self) -> Self179     fn bitxor(self, rhs: Self) -> Self {
180         Self {
181             x: self.x ^ rhs.x,
182             y: self.y ^ rhs.y,
183             z: self.z ^ rhs.z,
184             w: self.w ^ rhs.w,
185         }
186     }
187 }
188 
189 impl BitXorAssign for BVec4A {
190     #[inline]
bitxor_assign(&mut self, rhs: Self)191     fn bitxor_assign(&mut self, rhs: Self) {
192         *self = self.bitxor(rhs);
193     }
194 }
195 
196 impl Not for BVec4A {
197     type Output = Self;
198     #[inline]
not(self) -> Self199     fn not(self) -> Self {
200         Self {
201             x: !self.x,
202             y: !self.y,
203             z: !self.z,
204             w: !self.w,
205         }
206     }
207 }
208 
209 impl fmt::Debug for BVec4A {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result210     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
211         let arr = self.into_u32_array();
212         write!(
213             f,
214             "{}({:#x}, {:#x}, {:#x}, {:#x})",
215             stringify!(BVec4A),
216             arr[0],
217             arr[1],
218             arr[2],
219             arr[3]
220         )
221     }
222 }
223 
224 impl fmt::Display for BVec4A {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result225     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
226         let arr = self.into_bool_array();
227         write!(f, "[{}, {}, {}, {}]", arr[0], arr[1], arr[2], arr[3])
228     }
229 }
230 
231 impl From<[bool; 4]> for BVec4A {
232     #[inline]
from(a: [bool; 4]) -> Self233     fn from(a: [bool; 4]) -> Self {
234         Self::from_array(a)
235     }
236 }
237 
238 impl From<BVec4A> for [bool; 4] {
239     #[inline]
from(mask: BVec4A) -> Self240     fn from(mask: BVec4A) -> Self {
241         mask.into_bool_array()
242     }
243 }
244 
245 impl From<BVec4A> for [u32; 4] {
246     #[inline]
from(mask: BVec4A) -> Self247     fn from(mask: BVec4A) -> Self {
248         mask.into_u32_array()
249     }
250 }
251