Skip to content

Commit 7136841

Browse files
authored
#274: Use SIMD equality for PartialEq on SIMD vectors
1 parent 1ec010d commit 7136841

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

crates/core_simd/src/vector.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,26 @@ where
429429
{
430430
#[inline]
431431
fn eq(&self, other: &Self) -> bool {
432-
// TODO use SIMD equality
433-
self.to_array() == other.to_array()
432+
// Safety: All SIMD vectors are SimdPartialEq, and the comparison produces a valid mask.
433+
let mask = unsafe {
434+
let tfvec: Simd<<T as SimdElement>::Mask, LANES> = intrinsics::simd_eq(*self, *other);
435+
Mask::from_int_unchecked(tfvec)
436+
};
437+
438+
// Two vectors are equal if all lanes tested true for vertical equality.
439+
mask.all()
440+
}
441+
442+
#[inline]
443+
fn ne(&self, other: &Self) -> bool {
444+
// Safety: All SIMD vectors are SimdPartialEq, and the comparison produces a valid mask.
445+
let mask = unsafe {
446+
let tfvec: Simd<<T as SimdElement>::Mask, LANES> = intrinsics::simd_ne(*self, *other);
447+
Mask::from_int_unchecked(tfvec)
448+
};
449+
450+
// Two vectors are non-equal if any lane tested true for vertical non-equality.
451+
mask.any()
434452
}
435453
}
436454

0 commit comments

Comments
 (0)