glam/bool/neon/
bvec3a.rs

1// Generated from vec_mask.rs.tera template. Edit the template, not the generated file.
2
3use core::fmt;
4use core::ops::*;
5
6use core::arch::aarch64::*;
7
8#[repr(C)]
9union UnionCast {
10    a: [u32; 4],
11    v: BVec3A,
12}
13
14/// Creates a 3-dimensional `bool` vector mask.
15#[inline(always)]
16#[must_use]
17pub const fn bvec3a(x: bool, y: bool, z: bool) -> BVec3A {
18    BVec3A::new(x, y, z)
19}
20
21/// A 3-dimensional SIMD vector mask.
22///
23/// This type is 16 byte aligned.
24#[derive(Clone, Copy)]
25#[repr(transparent)]
26pub struct BVec3A(pub(crate) uint32x4_t);
27
28const MASK: [u32; 2] = [0, 0xff_ff_ff_ff];
29
30impl BVec3A {
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]
40    pub const fn new(x: bool, y: bool, z: bool) -> Self {
41        unsafe {
42            UnionCast {
43                a: [MASK[x as usize], MASK[y as usize], MASK[z as usize], 0],
44            }
45            .v
46        }
47    }
48
49    /// Creates a vector mask with all elements set to `v`.
50    #[inline]
51    #[must_use]
52    pub const fn splat(v: bool) -> Self {
53        Self::new(v, v, v)
54    }
55
56    /// Creates a new vector mask from a bool array.
57    #[inline]
58    #[must_use]
59    pub const fn from_array(a: [bool; 3]) -> Self {
60        Self::new(a[0], a[1], a[2])
61    }
62
63    /// Returns a bitmask with the lowest 3 bits set from the elements of `self`.
64    ///
65    /// A true element results in a `1` bit and a false element in a `0` bit.  Element `x` goes
66    /// into the first lowest bit, element `y` into the second, etc.
67    #[inline]
68    #[must_use]
69    pub fn bitmask(self) -> u32 {
70        let movemask = unsafe {
71            let mma = vandq_u32(self.0, vld1q_u32([1, 2, 4, 8].as_ptr())); // [0 1 2 3]
72            let mmb = vextq_u32(mma, mma, 2); // [2 3 0 1]
73            let mmc = vorrq_u32(mma, mmb); // [0+2 1+3 0+2 1+3]
74            let mmd = vextq_u32(mmc, mmc, 3); // [1+3 0+2 1+3 0+2]
75            let mme = vorrq_u32(mmc, mmd); // [0+1+2+3 ...]
76            vgetq_lane_u32(mme, 0)
77        };
78
79        movemask & 0x7
80    }
81
82    /// Returns true if any of the elements are true, false otherwise.
83    #[inline]
84    #[must_use]
85    pub fn any(self) -> bool {
86        self.bitmask() != 0
87    }
88
89    /// Returns true if all the elements are true, false otherwise.
90    #[inline]
91    #[must_use]
92    pub fn all(self) -> bool {
93        self.bitmask() == 0x7
94    }
95
96    /// Tests the value at `index`.
97    ///
98    /// Panics if `index` is greater than 2.
99    #[inline]
100    #[must_use]
101    pub fn test(&self, index: usize) -> bool {
102        match index {
103            0 => (self.bitmask() & (1 << 0)) != 0,
104            1 => (self.bitmask() & (1 << 1)) != 0,
105            2 => (self.bitmask() & (1 << 2)) != 0,
106            _ => panic!("index out of bounds"),
107        }
108    }
109
110    /// Sets the element at `index`.
111    ///
112    /// Panics if `index` is greater than 2.
113    #[inline]
114    pub fn set(&mut self, index: usize, value: bool) {
115        self.0 = match index {
116            0 => unsafe { vsetq_lane_u32(MASK[value as usize], self.0, 0) },
117            1 => unsafe { vsetq_lane_u32(MASK[value as usize], self.0, 1) },
118            2 => unsafe { vsetq_lane_u32(MASK[value as usize], self.0, 2) },
119            _ => panic!("index out of bounds"),
120        }
121    }
122
123    #[inline]
124    #[must_use]
125    fn into_bool_array(self) -> [bool; 3] {
126        let bitmask = self.bitmask();
127        [(bitmask & 1) != 0, (bitmask & 2) != 0, (bitmask & 4) != 0]
128    }
129
130    #[inline]
131    #[must_use]
132    fn into_u32_array(self) -> [u32; 3] {
133        let bitmask = self.bitmask();
134        [
135            MASK[(bitmask & 1) as usize],
136            MASK[((bitmask >> 1) & 1) as usize],
137            MASK[((bitmask >> 2) & 1) as usize],
138        ]
139    }
140}
141
142impl Default for BVec3A {
143    #[inline]
144    fn default() -> Self {
145        Self::FALSE
146    }
147}
148
149impl PartialEq for BVec3A {
150    #[inline]
151    fn eq(&self, rhs: &Self) -> bool {
152        self.bitmask().eq(&rhs.bitmask())
153    }
154}
155
156impl Eq for BVec3A {}
157
158impl core::hash::Hash for BVec3A {
159    #[inline]
160    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
161        self.bitmask().hash(state);
162    }
163}
164
165impl BitAnd for BVec3A {
166    type Output = Self;
167    #[inline]
168    fn bitand(self, rhs: Self) -> Self {
169        Self(unsafe { vandq_u32(self.0, rhs.0) })
170    }
171}
172
173impl BitAndAssign for BVec3A {
174    #[inline]
175    fn bitand_assign(&mut self, rhs: Self) {
176        *self = self.bitand(rhs);
177    }
178}
179
180impl BitOr for BVec3A {
181    type Output = Self;
182    #[inline]
183    fn bitor(self, rhs: Self) -> Self {
184        Self(unsafe { vorrq_u32(self.0, rhs.0) })
185    }
186}
187
188impl BitOrAssign for BVec3A {
189    #[inline]
190    fn bitor_assign(&mut self, rhs: Self) {
191        *self = self.bitor(rhs);
192    }
193}
194
195impl BitXor for BVec3A {
196    type Output = Self;
197    #[inline]
198    fn bitxor(self, rhs: Self) -> Self {
199        Self(unsafe { veorq_u32(self.0, rhs.0) })
200    }
201}
202
203impl BitXorAssign for BVec3A {
204    #[inline]
205    fn bitxor_assign(&mut self, rhs: Self) {
206        *self = self.bitxor(rhs);
207    }
208}
209
210impl Not for BVec3A {
211    type Output = Self;
212    #[inline]
213    fn not(self) -> Self {
214        Self(unsafe { vmvnq_u32(self.0) })
215    }
216}
217
218impl From<BVec3A> for uint32x4_t {
219    #[inline]
220    fn from(t: BVec3A) -> Self {
221        t.0
222    }
223}
224
225impl fmt::Debug for BVec3A {
226    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
227        let arr = self.into_u32_array();
228        write!(
229            f,
230            "{}({:#x}, {:#x}, {:#x})",
231            stringify!(BVec3A),
232            arr[0],
233            arr[1],
234            arr[2]
235        )
236    }
237}
238
239impl fmt::Display for BVec3A {
240    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
241        let arr = self.into_bool_array();
242        write!(f, "[{}, {}, {}]", arr[0], arr[1], arr[2])
243    }
244}
245
246impl From<[bool; 3]> for BVec3A {
247    #[inline]
248    fn from(a: [bool; 3]) -> Self {
249        Self::from_array(a)
250    }
251}
252
253impl From<BVec3A> for [bool; 3] {
254    #[inline]
255    fn from(mask: BVec3A) -> Self {
256        mask.into_bool_array()
257    }
258}
259
260impl From<BVec3A> for [u32; 3] {
261    #[inline]
262    fn from(mask: BVec3A) -> Self {
263        mask.into_u32_array()
264    }
265}