Skip to content

Commit 34504e5

Browse files
committed
What if we use simd equality instead of array equality?
1 parent ad5ee52 commit 34504e5

File tree

1 file changed

+100
-8
lines changed

1 file changed

+100
-8
lines changed

crates/core_arch/src/simd.rs

+100-8
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,31 @@
22
33
#![allow(non_camel_case_types)]
44

5+
trait SimdLookup<const N: usize> {
6+
type Vector;
7+
}
8+
macro_rules! as_signed {
9+
(i8) => {i8};
10+
(i16) => {i16};
11+
(i32) => {i32};
12+
(i64) => {i64};
13+
(i128) => {i128};
14+
15+
(u8) => {i8};
16+
(u16) => {i16};
17+
(u32) => {i32};
18+
(u64) => {i64};
19+
(u128) => {i128};
20+
21+
(f8) => {i8};
22+
(f16) => {i16};
23+
(f32) => {i32};
24+
(f64) => {i64};
25+
(f128) => {i128};
26+
}
27+
528
macro_rules! simd_ty {
6-
($id:ident [$elem_type:ty ; $len:literal]: $($param_name:ident),*) => {
29+
($id:ident [$elem_type:ident ; $len:literal]: $($param_name:ident),*) => {
730
#[repr(simd)]
831
#[derive(Copy, Clone)]
932
pub(crate) struct $id([$elem_type; $len]);
@@ -55,7 +78,11 @@ macro_rules! simd_ty {
5578

5679
impl core::cmp::PartialEq for $id {
5780
fn eq(&self, other: &Self) -> bool {
58-
self.as_array() == other.as_array()
81+
type Mask = <as_signed!($elem_type) as SimdLookup<$len>>::Vector;
82+
unsafe {
83+
let mask = core::intrinsics::simd::simd_eq::<Self, Mask>(*self, *other);
84+
core::intrinsics::simd::simd_reduce_all(mask)
85+
}
5986
}
6087
}
6188

@@ -65,6 +92,10 @@ macro_rules! simd_ty {
6592
debug_simd_finish(f, stringify!($id), self.as_array())
6693
}
6794
}
95+
96+
impl SimdLookup<$len> for $elem_type {
97+
type Vector = $id;
98+
}
6899
}
69100
}
70101

@@ -109,12 +140,6 @@ macro_rules! simd_m_ty {
109140
}
110141
}
111142

112-
impl core::cmp::PartialEq for $id {
113-
fn eq(&self, other: &Self) -> bool {
114-
self.as_array() == other.as_array()
115-
}
116-
}
117-
118143
impl core::fmt::Debug for $id {
119144
#[inline]
120145
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
@@ -865,6 +890,73 @@ simd_ty!(
865890
);
866891

867892
// 1024-bit wide types:
893+
simd_ty!(
894+
i16x64[i16;64]:
895+
x0,
896+
x1,
897+
x2,
898+
x3,
899+
x4,
900+
x5,
901+
x6,
902+
x7,
903+
x8,
904+
x9,
905+
x10,
906+
x11,
907+
x12,
908+
x13,
909+
x14,
910+
x15,
911+
x16,
912+
x17,
913+
x18,
914+
x19,
915+
x20,
916+
x21,
917+
x22,
918+
x23,
919+
x24,
920+
x25,
921+
x26,
922+
x27,
923+
x28,
924+
x29,
925+
x30,
926+
x31,
927+
x32,
928+
x33,
929+
x34,
930+
x35,
931+
x36,
932+
x37,
933+
x38,
934+
x39,
935+
x40,
936+
x41,
937+
x42,
938+
x43,
939+
x44,
940+
x45,
941+
x46,
942+
x47,
943+
x48,
944+
x49,
945+
x50,
946+
x51,
947+
x52,
948+
x53,
949+
x54,
950+
x55,
951+
x56,
952+
x57,
953+
x58,
954+
x59,
955+
x60,
956+
x61,
957+
x62,
958+
x63
959+
);
868960
simd_ty!(
869961
u16x64[u16;64]:
870962
x0,

0 commit comments

Comments
 (0)