aboutsummaryrefslogtreecommitdiff
path: root/src/bool/wasm32/bvec4a.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/bool/wasm32/bvec4a.rs')
-rw-r--r--src/bool/wasm32/bvec4a.rs36
1 files changed, 34 insertions, 2 deletions
diff --git a/src/bool/wasm32/bvec4a.rs b/src/bool/wasm32/bvec4a.rs
index cdec83f..3813354 100644
--- a/src/bool/wasm32/bvec4a.rs
+++ b/src/bool/wasm32/bvec4a.rs
@@ -8,8 +8,7 @@ use core::arch::wasm32::*;
/// A 4-dimensional SIMD vector mask.
///
-/// This type is 16 byte aligned and is backed by a SIMD vector. If SIMD is not available
-/// `BVec4A` will be a type alias for `BVec4`.
+/// This type is 16 byte aligned.
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct BVec4A(pub(crate) v128);
@@ -25,6 +24,7 @@ impl BVec4A {
/// Creates a new vector mask.
#[inline(always)]
+ #[must_use]
pub const fn new(x: bool, y: bool, z: bool, w: bool) -> Self {
Self(u32x4(
MASK[x as usize],
@@ -36,6 +36,7 @@ impl BVec4A {
/// Creates a vector with all elements set to `v`.
#[inline]
+ #[must_use]
pub const fn splat(v: bool) -> Self {
Self::new(v, v, v, v)
}
@@ -45,23 +46,53 @@ impl BVec4A {
/// A true element results in a `1` bit and a false element in a `0` bit. Element `x` goes
/// into the first lowest bit, element `y` into the second, etc.
#[inline]
+ #[must_use]
pub fn bitmask(self) -> u32 {
u32x4_bitmask(self.0) as u32
}
/// Returns true if any of the elements are true, false otherwise.
#[inline]
+ #[must_use]
pub fn any(self) -> bool {
self.bitmask() != 0
}
/// Returns true if all the elements are true, false otherwise.
#[inline]
+ #[must_use]
pub fn all(self) -> bool {
self.bitmask() == 0xf
}
+ /// Tests the value at `index`.
+ ///
+ /// Panics if `index` is greater than 3.
+ #[inline]
+ #[must_use]
+ pub fn test(&self, index: usize) -> bool {
+ match index {
+ 0 => (self.bitmask() & (1 << 0)) != 0,
+ 1 => (self.bitmask() & (1 << 1)) != 0,
+ 2 => (self.bitmask() & (1 << 2)) != 0,
+ 3 => (self.bitmask() & (1 << 3)) != 0,
+ _ => panic!("index out of bounds"),
+ }
+ }
+
+ /// Sets the element at `index`.
+ ///
+ /// Panics if `index` is greater than 3.
+ #[inline]
+ pub fn set(&mut self, index: usize, value: bool) {
+ use crate::Vec4;
+ let mut v = Vec4(self.0);
+ v[index] = f32::from_bits(MASK[value as usize]);
+ *self = Self(v.0);
+ }
+
#[inline]
+ #[must_use]
fn into_bool_array(self) -> [bool; 4] {
let bitmask = self.bitmask();
[
@@ -73,6 +104,7 @@ impl BVec4A {
}
#[inline]
+ #[must_use]
fn into_u32_array(self) -> [u32; 4] {
let bitmask = self.bitmask();
[