1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use crate::{Hamming, MetricPoint};
use core::fmt::{Debug, Error, Formatter};
use core::hash::{Hash, Hasher};
#[cfg(feature = "serde")]
use serde::{
de::{self, SeqAccess, Visitor},
Deserialize, Deserializer, Serialize, Serializer,
};
macro_rules! simd_impl {
($name:ident, $bytes:expr) => {
#[repr(align($bytes))]
#[derive(Copy, Clone)]
pub struct $name(pub [u8; $bytes]);
impl MetricPoint for Hamming<$name> {
#[inline]
fn distance(&self, rhs: &Self) -> u32 {
let simd_left_base = self as *const _ as *const packed_simd::u128x1;
let simd_right_base = rhs as *const _ as *const packed_simd::u128x1;
(0..$bytes / 16)
.map(|i| {
let left = unsafe { *simd_left_base.offset(i) };
let right = unsafe { *simd_right_base.offset(i) };
(left ^ right).count_ones().wrapping_sum() as u32
})
.sum()
}
}
impl Debug for $name {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
Debug::fmt(&self.0[..], f)
}
}
impl PartialEq for $name {
fn eq(&self, other: &Self) -> bool {
self.0[..] == other.0[..]
}
}
impl Eq for $name {}
impl Hash for $name {
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
{
self.0.hash(state)
}
fn hash_slice<H>(data: &[Self], state: &mut H)
where
H: Hasher,
{
for s in data {
s.hash(state);
}
}
}
impl Into<[u8; $bytes]> for $name {
fn into(self) -> [u8; $bytes] {
self.0
}
}
impl From<[u8; $bytes]> for $name {
fn from(a: [u8; $bytes]) -> Self {
Self(a)
}
}
#[cfg(feature = "serde")]
impl Serialize for $name {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let a: [u8; $bytes] = self.clone().into();
a.serialize(serializer)
}
}
#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for $name {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct SimdVisitor($name, usize);
impl<'de> Visitor<'de> for SimdVisitor {
type Value = $name;
fn expecting(&self, formatter: &mut Formatter) -> Result<(), Error> {
formatter.write_str("a sequence of $bytes bytes")
}
fn visit_seq<S>(mut self, mut seq: S) -> Result<$name, S::Error>
where
S: SeqAccess<'de>,
{
while let Some(value) = seq.next_element()? {
if self.1 == $bytes {
return Err(de::Error::custom(
"cannot have more than $bytes bytes in sequence",
));
}
(self.0).0[self.1] = value;
self.1 += 1;
}
if self.1 != $bytes {
Err(de::Error::custom(
"must have exactly $bytes bytes in sequence",
))
} else {
Ok(self.0)
}
}
}
let visitor = SimdVisitor(Self([0; $bytes]), 0);
deserializer.deserialize_seq(visitor)
}
}
};
}
simd_impl!(Bits128, 16);
simd_impl!(Bits256, 32);
simd_impl!(Bits512, 64);
simd_impl!(Bits1024, 128);
simd_impl!(Bits2048, 256);
simd_impl!(Bits4096, 512);