Skip to content

Commit

Permalink
Adds boolean methods to u32x4 and u32x8 (#161)
Browse files Browse the repository at this point in the history
* feat: Adds `all`, `any` and `none` to u32x4

* feat: Adds `all`, `any` and `none` to u32x8
  • Loading branch information
andyquinterom authored Aug 12, 2024
1 parent 05160f2 commit d00b0be
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/u32x4_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,42 @@ impl u32x4 {
}
}

#[inline]
#[must_use]
pub fn any(self) -> bool {
pick! {
if #[cfg(target_feature="sse2")] {
(move_mask_i8_m128i(self.sse) & 0b1000100010001000) != 0
} else if #[cfg(target_feature="simd128")] {
u32x4_bitmask(self.simd) != 0
} else {
let v : [u64;2] = cast(self);
((v[0] | v[1]) & 0x8000000080000000) != 0
}
}
}

#[inline]
#[must_use]
pub fn all(self) -> bool {
pick! {
if #[cfg(target_feature="sse2")] {
(move_mask_i8_m128i(self.sse) & 0b1000100010001000) == 0b1000100010001000
} else if #[cfg(target_feature="simd128")] {
u32x4_bitmask(self.simd) == 0b1111
} else {
let v : [u64;2] = cast(self);
(v[0] & v[1] & 0x8000000080000000) == 0x8000000080000000
}
}
}

#[inline]
#[must_use]
pub fn none(self) -> bool {
!self.any()
}

#[inline]
pub fn to_array(self) -> [u32; 4] {
cast(self)
Expand Down
30 changes: 30 additions & 0 deletions src/u32x8_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,36 @@ impl u32x8 {
}
}

#[inline]
#[must_use]
pub fn any(self) -> bool {
pick! {
if #[cfg(target_feature="avx2")] {
((move_mask_i8_m256i(self.avx2) as u32) & 0b10001000100010001000100010001000) != 0
} else {
(self.a | self.b).any()
}
}
}

#[inline]
#[must_use]
pub fn all(self) -> bool {
pick! {
if #[cfg(target_feature="avx2")] {
((move_mask_i8_m256i(self.avx2) as u32) & 0b10001000100010001000100010001000) == 0b10001000100010001000100010001000
} else {
(self.a & self.b).all()
}
}
}

#[inline]
#[must_use]
pub fn none(self) -> bool {
!self.any()
}

#[inline]
pub fn to_array(self) -> [u32; 8] {
cast(self)
Expand Down
27 changes: 27 additions & 0 deletions tests/all_tests/t_u32x4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,30 @@ fn impl_u32x4_shl_each() {
|a, b| a.wrapping_shl(b),
);
}

#[test]
fn test_u32x4_any() {
let a = u32x4::from([0, 0, 0, u32::MAX]);
assert!(a.any());
//
let a = u32x4::from([0, 0, 0, 0]);
assert!(!a.any());
}

#[test]
fn test_u32x4_all() {
let a = u32x4::from([0, 0, 0, u32::MAX]);
assert!(!a.all());
//
let a = u32x4::from([u32::MAX; 4]);
assert!(a.all());
}

#[test]
fn test_u32x4_none() {
let a = u32x4::from([0, 0, 0, u32::MAX]);
assert!(!a.none());
//
let a = u32x4::from([0; 4]);
assert!(a.none());
}
27 changes: 27 additions & 0 deletions tests/all_tests/t_u32x8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,30 @@ fn impl_u32x8_not() {

crate::test_random_vector_vs_scalar(|a: u32x8, _b| !a, |a, _b| !a);
}

#[test]
fn test_u32x8_any() {
let a = u32x8::from([0, 0, 0, u32::MAX, 0, 0, 0, 0]);
assert!(a.any());
//
let a = u32x8::from([0, 0, 0, 0, 0, 0, 0, 0]);
assert!(!a.any());
}

#[test]
fn test_u32x8_all() {
let a = u32x8::from([0, 0, 0, u32::MAX, 0, 0, 0, 0]);
assert!(!a.all());
//
let a = u32x8::from([u32::MAX; 8]);
assert!(a.all());
}

#[test]
fn test_u32x8_none() {
let a = u32x8::from([0, 0, 0, u32::MAX, 0, 0, 0, 0]);
assert!(!a.none());
//
let a = u32x8::from([0; 8]);
assert!(a.none());
}

0 comments on commit d00b0be

Please sign in to comment.