• 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 3-dimensional `bool` vector mask.
7 #[inline(always)]
8 #[must_use]
bvec3a(x: bool, y: bool, z: bool) -> BVec3A9 pub const fn bvec3a(x: bool, y: bool, z: bool) -> BVec3A {
10     BVec3A::new(x, y, z)
11 }
12 
13 /// A 3-dimensional `u32` vector mask.
14 #[derive(Clone, Copy, PartialEq, Eq, Hash)]
15 #[repr(C, align(16))]
16 pub struct BVec3A {
17     pub x: u32,
18     pub y: u32,
19     pub z: u32,
20 }
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 {
36             x: MASK[x as usize],
37             y: MASK[y as usize],
38             z: MASK[z as usize],
39         }
40     }
41 
42     /// Creates a vector mask with all elements set to `v`.
43     #[inline]
44     #[must_use]
splat(v: bool) -> Self45     pub const fn splat(v: bool) -> Self {
46         Self::new(v, v, v)
47     }
48 
49     /// Creates a new vector mask from a bool array.
50     #[inline]
51     #[must_use]
from_array(a: [bool; 3]) -> Self52     pub const fn from_array(a: [bool; 3]) -> Self {
53         Self::new(a[0], a[1], a[2])
54     }
55 
56     /// Returns a bitmask with the lowest 3 bits set from the elements of `self`.
57     ///
58     /// A true element results in a `1` bit and a false element in a `0` bit.  Element `x` goes
59     /// into the first lowest bit, element `y` into the second, etc.
60     #[inline]
61     #[must_use]
bitmask(self) -> u3262     pub fn bitmask(self) -> u32 {
63         (self.x & 0x1) | (self.y & 0x1) << 1 | (self.z & 0x1) << 2
64     }
65 
66     /// Returns true if any of the elements are true, false otherwise.
67     #[inline]
68     #[must_use]
any(self) -> bool69     pub fn any(self) -> bool {
70         ((self.x | self.y | self.z) & 0x1) != 0
71     }
72 
73     /// Returns true if all the elements are true, false otherwise.
74     #[inline]
75     #[must_use]
all(self) -> bool76     pub fn all(self) -> bool {
77         ((self.x & self.y & self.z) & 0x1) != 0
78     }
79 
80     /// Tests the value at `index`.
81     ///
82     /// Panics if `index` is greater than 2.
83     #[inline]
84     #[must_use]
test(&self, index: usize) -> bool85     pub fn test(&self, index: usize) -> bool {
86         match index {
87             0 => (self.x & 0x1) != 0,
88             1 => (self.y & 0x1) != 0,
89             2 => (self.z & 0x1) != 0,
90             _ => panic!("index out of bounds"),
91         }
92     }
93 
94     /// Sets the element at `index`.
95     ///
96     /// Panics if `index` is greater than 2.
97     #[inline]
set(&mut self, index: usize, value: bool)98     pub fn set(&mut self, index: usize, value: bool) {
99         match index {
100             0 => self.x = MASK[value as usize],
101             1 => self.y = MASK[value as usize],
102             2 => self.z = MASK[value as usize],
103             _ => panic!("index out of bounds"),
104         }
105     }
106 
107     #[inline]
108     #[must_use]
into_bool_array(self) -> [bool; 3]109     fn into_bool_array(self) -> [bool; 3] {
110         [
111             (self.x & 0x1) != 0,
112             (self.y & 0x1) != 0,
113             (self.z & 0x1) != 0,
114         ]
115     }
116 
117     #[inline]
118     #[must_use]
into_u32_array(self) -> [u32; 3]119     fn into_u32_array(self) -> [u32; 3] {
120         [self.x, self.y, self.z]
121     }
122 }
123 
124 impl Default for BVec3A {
125     #[inline]
default() -> Self126     fn default() -> Self {
127         Self::FALSE
128     }
129 }
130 
131 impl BitAnd for BVec3A {
132     type Output = Self;
133     #[inline]
bitand(self, rhs: Self) -> Self134     fn bitand(self, rhs: Self) -> Self {
135         Self {
136             x: self.x & rhs.x,
137             y: self.y & rhs.y,
138             z: self.z & rhs.z,
139         }
140     }
141 }
142 
143 impl BitAndAssign for BVec3A {
144     #[inline]
bitand_assign(&mut self, rhs: Self)145     fn bitand_assign(&mut self, rhs: Self) {
146         *self = self.bitand(rhs);
147     }
148 }
149 
150 impl BitOr for BVec3A {
151     type Output = Self;
152     #[inline]
bitor(self, rhs: Self) -> Self153     fn bitor(self, rhs: Self) -> Self {
154         Self {
155             x: self.x | rhs.x,
156             y: self.y | rhs.y,
157             z: self.z | rhs.z,
158         }
159     }
160 }
161 
162 impl BitOrAssign for BVec3A {
163     #[inline]
bitor_assign(&mut self, rhs: Self)164     fn bitor_assign(&mut self, rhs: Self) {
165         *self = self.bitor(rhs);
166     }
167 }
168 
169 impl BitXor for BVec3A {
170     type Output = Self;
171     #[inline]
bitxor(self, rhs: Self) -> Self172     fn bitxor(self, rhs: Self) -> Self {
173         Self {
174             x: self.x ^ rhs.x,
175             y: self.y ^ rhs.y,
176             z: self.z ^ rhs.z,
177         }
178     }
179 }
180 
181 impl BitXorAssign for BVec3A {
182     #[inline]
bitxor_assign(&mut self, rhs: Self)183     fn bitxor_assign(&mut self, rhs: Self) {
184         *self = self.bitxor(rhs);
185     }
186 }
187 
188 impl Not for BVec3A {
189     type Output = Self;
190     #[inline]
not(self) -> Self191     fn not(self) -> Self {
192         Self {
193             x: !self.x,
194             y: !self.y,
195             z: !self.z,
196         }
197     }
198 }
199 
200 impl fmt::Debug for BVec3A {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result201     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
202         let arr = self.into_u32_array();
203         write!(
204             f,
205             "{}({:#x}, {:#x}, {:#x})",
206             stringify!(BVec3A),
207             arr[0],
208             arr[1],
209             arr[2]
210         )
211     }
212 }
213 
214 impl fmt::Display for BVec3A {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result215     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
216         let arr = self.into_bool_array();
217         write!(f, "[{}, {}, {}]", arr[0], arr[1], arr[2])
218     }
219 }
220 
221 impl From<[bool; 3]> for BVec3A {
222     #[inline]
from(a: [bool; 3]) -> Self223     fn from(a: [bool; 3]) -> Self {
224         Self::from_array(a)
225     }
226 }
227 
228 impl From<BVec3A> for [bool; 3] {
229     #[inline]
from(mask: BVec3A) -> Self230     fn from(mask: BVec3A) -> Self {
231         mask.into_bool_array()
232     }
233 }
234 
235 impl From<BVec3A> for [u32; 3] {
236     #[inline]
from(mask: BVec3A) -> Self237     fn from(mask: BVec3A) -> Self {
238         mask.into_u32_array()
239     }
240 }
241