Skip to content

Commit

Permalink
Merge pull request #8 from cessen/vec4mask_doc
Browse files Browse the repository at this point in the history
Method documentation for `Vec4Mask`.
  • Loading branch information
bitshifter committed Jul 22, 2019
2 parents 3d25930 + bd1fbff commit 570062d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/f32/vec4_f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,13 +550,18 @@ impl Distribution<Vec4> for Standard {
}
}

/// A 4-dimensional vector mask.
///
/// This type is typically created by comparison methods on `Vec4`. It is
/// essentially a vector of four boolean values.
#[derive(Clone, Copy, Default)]
// if compiling with simd enabled assume alignment needs to match the simd type
#[cfg_attr(not(feature = "scalar-math"), repr(align(16)))]
#[repr(C)]
pub struct Vec4Mask(u32, u32, u32, u32);

impl Vec4Mask {
/// Creates a new `Vec4Mask`.
pub(crate) fn new(x: bool, y: bool, z: bool, w: bool) -> Self {
const MASK: [u32; 2] = [0, 0xff_ff_ff_ff];
Self(
Expand All @@ -573,21 +578,38 @@ impl Vec4Mask {
self.bitmask()
}

/// Returns a bitmask with the lowest four bits set from the elements of
/// the `Vec4Mask`.
///
/// 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]
pub fn bitmask(&self) -> u32 {
(self.0 & 0x1) | (self.1 & 0x1) << 1 | (self.2 & 0x1) << 2 | (self.3 & 0x1) << 3
}

/// Returns true if any of the elements are true, false otherwise.
///
/// In other words: `x || y || z || w`.
#[inline]
pub fn any(&self) -> bool {
(self.0 != 0) || (self.1 != 0) || (self.2 != 0) || (self.3 != 0)
}

/// Returns true if all the elements are true, false otherwise.
///
/// In other words: `x && y && z && w`.
#[inline]
pub fn all(&self) -> bool {
(self.0 != 0) && (self.1 != 0) && (self.2 != 0) && (self.3 != 0)
}

/// Creates a new `Vec4` from the elements in `if_true` and `if_false`,
/// selecting which to use for each element based on the `Vec4Mask`.
///
/// A true element in the mask uses the corresponding element from
/// `if_true`, and false uses the element from `if_false`.
#[inline]
pub fn select(self, if_true: Vec4, if_false: Vec4) -> Vec4 {
Vec4(
Expand Down
20 changes: 19 additions & 1 deletion src/f32/vec4_sse2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,8 @@ impl Distribution<Vec4> for Standard {

/// A 4-dimensional vector mask.
///
/// This type is typically created by comparison methods on `Vec4`.
/// This type is typically created by comparison methods on `Vec4`. It is
/// essentially a vector of four boolean values.
#[derive(Clone, Copy)]
#[repr(C)]
pub struct Vec4Mask(__m128);
Expand All @@ -609,21 +610,38 @@ impl Vec4Mask {
self.bitmask()
}

/// Returns a bitmask with the lowest four bits set from the elements of
/// the `Vec4Mask`.
///
/// 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]
pub fn bitmask(self) -> u32 {
unsafe { (_mm_movemask_ps(self.0) as u32) }
}

/// Returns true if any of the elements are true, false otherwise.
///
/// In other words: `x || y || z || w`.
#[inline]
pub fn any(self) -> bool {
unsafe { _mm_movemask_ps(self.0) != 0 }
}

/// Returns true if all the elements are true, false otherwise.
///
/// In other words: `x && y && z && w`.
#[inline]
pub fn all(self) -> bool {
unsafe { _mm_movemask_ps(self.0) == 0xf }
}

/// Creates a new `Vec4` from the elements in `if_true` and `if_false`,
/// selecting which to use for each element based on the `Vec4Mask`.
///
/// A true element in the mask uses the corresponding element from
/// `if_true`, and false uses the element from `if_false`.
#[inline]
pub fn select(self, if_true: Vec4, if_false: Vec4) -> Vec4 {
unsafe {
Expand Down

0 comments on commit 570062d

Please sign in to comment.