| 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] |
| 11 |
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] |
| 34 |
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] |
| 46 |
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] |
| 53 |
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] |
| 63 |
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] |
| 70 |
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] |
| 77 |
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] |
| 86 |
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] |
| 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] |
| 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] |
| 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] |
| 127 |
fn default() -> Self { |
| 128 |
Self::FALSE |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
impl PartialEq for BVec3A { |
| 133 |
#[inline] |
| 134 |
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] |
| 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] |
| 151 |
fn bitand(self, rhs: Self) -> Self { |
| 152 |
Self(v128_and(self.0, rhs.0)) |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
impl BitAnd<&Self> for BVec3A { |
| 157 |
type Output = Self; |
| 158 |
#[inline] |
| 159 |
fn bitand(self, rhs: &Self) -> Self { |
| 160 |
self.bitand(*rhs) |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
impl BitAnd<&BVec3A> for &BVec3A { |
| 165 |
type Output = BVec3A; |
| 166 |
#[inline] |
| 167 |
fn bitand(self, rhs: &BVec3A) -> BVec3A { |
| 168 |
(*self).bitand(*rhs) |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
impl BitAnd<BVec3A> for &BVec3A { |
| 173 |
type Output = BVec3A; |
| 174 |
#[inline] |
| 175 |
fn bitand(self, rhs: BVec3A) -> BVec3A { |
| 176 |
(*self).bitand(rhs) |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
impl BitAndAssign for BVec3A { |
| 181 |
#[inline] |
| 182 |
fn bitand_assign(&mut self, rhs: Self) { |
| 183 |
*self = self.bitand(rhs); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
impl BitAndAssign<&Self> for BVec3A { |
| 188 |
#[inline] |
| 189 |
fn bitand_assign(&mut self, rhs: &Self) { |
| 190 |
self.bitand_assign(*rhs); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
impl BitOr for BVec3A { |
| 195 |
type Output = Self; |
| 196 |
#[inline] |
| 197 |
fn bitor(self, rhs: Self) -> Self { |
| 198 |
Self(v128_or(self.0, rhs.0)) |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
impl BitOr<&Self> for BVec3A { |
| 203 |
type Output = Self; |
| 204 |
#[inline] |
| 205 |
fn bitor(self, rhs: &Self) -> Self { |
| 206 |
self.bitor(*rhs) |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
impl BitOr<&BVec3A> for &BVec3A { |
| 211 |
type Output = BVec3A; |
| 212 |
#[inline] |
| 213 |
fn bitor(self, rhs: &BVec3A) -> BVec3A { |
| 214 |
(*self).bitor(*rhs) |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
impl BitOr<BVec3A> for &BVec3A { |
| 219 |
type Output = BVec3A; |
| 220 |
#[inline] |
| 221 |
fn bitor(self, rhs: BVec3A) -> BVec3A { |
| 222 |
(*self).bitor(rhs) |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
impl BitOrAssign for BVec3A { |
| 227 |
#[inline] |
| 228 |
fn bitor_assign(&mut self, rhs: Self) { |
| 229 |
*self = self.bitor(rhs); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
impl BitOrAssign<&Self> for BVec3A { |
| 234 |
#[inline] |
| 235 |
fn bitor_assign(&mut self, rhs: &Self) { |
| 236 |
self.bitor_assign(*rhs); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
impl BitXor for BVec3A { |
| 241 |
type Output = Self; |
| 242 |
#[inline] |
| 243 |
fn bitxor(self, rhs: Self) -> Self { |
| 244 |
Self(v128_xor(self.0, rhs.0)) |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
impl BitXor<&Self> for BVec3A { |
| 249 |
type Output = Self; |
| 250 |
#[inline] |
| 251 |
fn bitxor(self, rhs: &Self) -> Self { |
| 252 |
self.bitxor(*rhs) |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
impl BitXor<&BVec3A> for &BVec3A { |
| 257 |
type Output = BVec3A; |
| 258 |
#[inline] |
| 259 |
fn bitxor(self, rhs: &BVec3A) -> BVec3A { |
| 260 |
(*self).bitxor(*rhs) |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
impl BitXor<BVec3A> for &BVec3A { |
| 265 |
type Output = BVec3A; |
| 266 |
#[inline] |
| 267 |
fn bitxor(self, rhs: BVec3A) -> BVec3A { |
| 268 |
(*self).bitxor(rhs) |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
impl BitXorAssign for BVec3A { |
| 273 |
#[inline] |
| 274 |
fn bitxor_assign(&mut self, rhs: Self) { |
| 275 |
*self = self.bitxor(rhs); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
impl BitXorAssign<&Self> for BVec3A { |
| 280 |
#[inline] |
| 281 |
fn bitxor_assign(&mut self, rhs: &Self) { |
| 282 |
self.bitxor_assign(*rhs); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
impl Not for BVec3A { |
| 287 |
type Output = Self; |
| 288 |
#[inline] |
| 289 |
fn not(self) -> Self { |
| 290 |
Self(v128_not(self.0)) |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
impl Not for &BVec3A { |
| 295 |
type Output = BVec3A; |
| 296 |
#[inline] |
| 297 |
fn not(self) -> BVec3A { |
| 298 |
(*self).not() |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
impl From<BVec3A> for v128 { |
| 303 |
#[inline] |
| 304 |
fn from(t: BVec3A) -> Self { |
| 305 |
t.0 |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
impl fmt::Debug for BVec3A { |
| 310 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 311 |
let arr = self.into_u32_array(); |
| 312 |
write!( |
| 313 |
f, |
| 314 |
"{}({:#x}, {:#x}, {:#x})", |
| 315 |
stringify!(BVec3A), |
| 316 |
arr[0], |
| 317 |
arr[1], |
| 318 |
arr[2] |
| 319 |
) |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
impl fmt::Display for BVec3A { |
| 324 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 325 |
let arr = self.into_bool_array(); |
| 326 |
write!(f, "[{}, {}, {}]", arr[0], arr[1], arr[2]) |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
impl From<[bool; 3]> for BVec3A { |
| 331 |
#[inline] |
| 332 |
fn from(a: [bool; 3]) -> Self { |
| 333 |
Self::from_array(a) |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
impl From<BVec3A> for [bool; 3] { |
| 338 |
#[inline] |
| 339 |
fn from(mask: BVec3A) -> Self { |
| 340 |
mask.into_bool_array() |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
impl From<BVec3A> for [u32; 3] { |
| 345 |
#[inline] |
| 346 |
fn from(mask: BVec3A) -> Self { |
| 347 |
mask.into_u32_array() |
| 348 |
} |
| 349 |
} |