-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbuilder.rs
288 lines (268 loc) · 10 KB
/
builder.rs
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
use crate::key::Key;
use crate::traits::HighwayHash;
use core::{default::Default, fmt::Debug, mem::ManuallyDrop};
#[cfg(target_arch = "aarch64")]
use crate::aarch64::NeonHash;
#[cfg(not(any(
all(target_family = "wasm", target_feature = "simd128"),
target_arch = "aarch64"
)))]
use crate::portable::PortableHash;
#[cfg(all(target_family = "wasm", target_feature = "simd128"))]
use crate::wasm::WasmHash;
#[cfg(target_arch = "x86_64")]
use crate::{AvxHash, SseHash};
/// This union is purely for performance. Originally it was an enum, but Rust /
/// LLVM had a hard time optimizing it and would include memcpy's that would
/// dominate profiles.
union HighwayChoices {
#[cfg(not(any(
all(target_family = "wasm", target_feature = "simd128"),
target_arch = "aarch64"
)))]
portable: ManuallyDrop<PortableHash>,
#[cfg(target_arch = "x86_64")]
avx: ManuallyDrop<AvxHash>,
#[cfg(target_arch = "x86_64")]
sse: ManuallyDrop<SseHash>,
#[cfg(target_arch = "aarch64")]
neon: ManuallyDrop<NeonHash>,
#[cfg(all(target_family = "wasm", target_feature = "simd128"))]
wasm: ManuallyDrop<WasmHash>,
}
/// `HighwayHash` implementation that selects best hash implementation at runtime.
pub struct HighwayHasher {
tag: u8,
inner: HighwayChoices,
}
impl Debug for HighwayHasher {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("HighwayHasher")
.field("tag", &self.tag)
.finish()
}
}
impl Clone for HighwayHasher {
fn clone(&self) -> Self {
let tag = self.tag;
match tag {
#[cfg(not(any(
all(target_family = "wasm", target_feature = "simd128"),
target_arch = "aarch64"
)))]
0 => HighwayHasher {
tag,
inner: HighwayChoices {
portable: unsafe { self.inner.portable.clone() },
},
},
#[cfg(target_arch = "x86_64")]
1 => HighwayHasher {
tag,
inner: HighwayChoices {
avx: unsafe { self.inner.avx.clone() },
},
},
#[cfg(target_arch = "x86_64")]
2 => HighwayHasher {
tag,
inner: HighwayChoices {
sse: unsafe { self.inner.sse.clone() },
},
},
#[cfg(target_arch = "aarch64")]
3 => HighwayHasher {
tag,
inner: HighwayChoices {
neon: unsafe { self.inner.neon.clone() },
},
},
#[cfg(all(target_family = "wasm", target_feature = "simd128"))]
4 => HighwayHasher {
tag,
inner: HighwayChoices {
wasm: unsafe { self.inner.wasm.clone() },
},
},
_ => unsafe { core::hint::unreachable_unchecked() },
}
}
}
impl HighwayHash for HighwayHasher {
#[inline]
fn append(&mut self, data: &[u8]) {
self.append(data)
}
#[inline]
fn finalize64(mut self) -> u64 {
Self::finalize64(&mut self)
}
#[inline]
fn finalize128(mut self) -> [u64; 2] {
Self::finalize128(&mut self)
}
#[inline]
fn finalize256(mut self) -> [u64; 4] {
Self::finalize256(&mut self)
}
}
impl HighwayHasher {
/// Creates a new hasher based on compilation and runtime capabilities
#[must_use]
pub fn new(key: Key) -> Self {
#[cfg(target_arch = "x86_64")]
{
if cfg!(target_feature = "avx2") {
let avx = ManuallyDrop::new(unsafe { AvxHash::force_new(key) });
return HighwayHasher {
tag: 1,
inner: HighwayChoices { avx },
};
} else if cfg!(target_feature = "sse4.1") {
let sse = ManuallyDrop::new(unsafe { SseHash::force_new(key) });
return HighwayHasher {
tag: 2,
inner: HighwayChoices { sse },
};
} else {
// Ideally we'd use `AvxHash::new` here, but it triggers a memcpy, so we
// duplicate the same logic to know if hasher can be enabled.
#[cfg(feature = "std")]
if is_x86_feature_detected!("avx2") {
let avx = ManuallyDrop::new(unsafe { AvxHash::force_new(key) });
return HighwayHasher {
tag: 1,
inner: HighwayChoices { avx },
};
}
#[cfg(feature = "std")]
if is_x86_feature_detected!("sse4.1") {
let sse = ManuallyDrop::new(unsafe { SseHash::force_new(key) });
return HighwayHasher {
tag: 2,
inner: HighwayChoices { sse },
};
}
}
}
#[cfg(target_arch = "aarch64")]
{
// Based on discussions here:
// https://github.com/nickbabcock/highway-rs/pull/51#discussion_r815247129
//
// The following aren't stable:
// - std::is_aarch64_feature_detected
// - aarch64 target features
//
// We're not really able to detect at runtime or compile time
// if neon support is available
//
// The good news is that it seems reasonable to assume the
// aarch64 environment is neon capable. If a use case is found
// where neon is not available, we can patch that in later.
let neon = ManuallyDrop::new(unsafe { NeonHash::force_new(key) });
HighwayHasher {
tag: 3,
inner: HighwayChoices { neon },
}
}
#[cfg(all(target_family = "wasm", target_feature = "simd128"))]
{
let wasm = ManuallyDrop::new(WasmHash::new(key));
HighwayHasher {
tag: 4,
inner: HighwayChoices { wasm },
}
}
#[cfg(not(any(
all(target_family = "wasm", target_feature = "simd128"),
target_arch = "aarch64"
)))]
{
let portable = ManuallyDrop::new(PortableHash::new(key));
HighwayHasher {
tag: 0,
inner: HighwayChoices { portable },
}
}
}
fn append(&mut self, data: &[u8]) {
match self.tag {
#[cfg(not(any(
all(target_family = "wasm", target_feature = "simd128"),
target_arch = "aarch64"
)))]
0 => unsafe { &mut self.inner.portable }.append(data),
#[cfg(target_arch = "x86_64")]
1 => unsafe { &mut self.inner.avx }.append(data),
#[cfg(target_arch = "x86_64")]
2 => unsafe { &mut self.inner.sse }.append(data),
#[cfg(target_arch = "aarch64")]
3 => unsafe { &mut self.inner.neon }.append(data),
#[cfg(all(target_family = "wasm", target_feature = "simd128"))]
4 => unsafe { &mut self.inner.wasm }.append(data),
_ => unsafe { core::hint::unreachable_unchecked() },
}
}
fn finalize64(&mut self) -> u64 {
match self.tag {
#[cfg(not(any(
all(target_family = "wasm", target_feature = "simd128"),
target_arch = "aarch64"
)))]
0 => unsafe { PortableHash::finalize64(&mut self.inner.portable) },
#[cfg(target_arch = "x86_64")]
1 => unsafe { AvxHash::finalize64(&mut self.inner.avx) },
#[cfg(target_arch = "x86_64")]
2 => unsafe { SseHash::finalize64(&mut self.inner.sse) },
#[cfg(target_arch = "aarch64")]
3 => unsafe { NeonHash::finalize64(&mut self.inner.neon) },
#[cfg(all(target_family = "wasm", target_feature = "simd128"))]
4 => unsafe { WasmHash::finalize64(&mut self.inner.wasm) },
_ => unsafe { core::hint::unreachable_unchecked() },
}
}
fn finalize128(&mut self) -> [u64; 2] {
match self.tag {
#[cfg(not(any(
all(target_family = "wasm", target_feature = "simd128"),
target_arch = "aarch64"
)))]
0 => unsafe { PortableHash::finalize128(&mut self.inner.portable) },
#[cfg(target_arch = "x86_64")]
1 => unsafe { AvxHash::finalize128(&mut self.inner.avx) },
#[cfg(target_arch = "x86_64")]
2 => unsafe { SseHash::finalize128(&mut self.inner.sse) },
#[cfg(target_arch = "aarch64")]
3 => unsafe { NeonHash::finalize128(&mut self.inner.neon) },
#[cfg(all(target_family = "wasm", target_feature = "simd128"))]
4 => unsafe { WasmHash::finalize128(&mut self.inner.wasm) },
_ => unsafe { core::hint::unreachable_unchecked() },
}
}
fn finalize256(&mut self) -> [u64; 4] {
match self.tag {
#[cfg(not(any(
all(target_family = "wasm", target_feature = "simd128"),
target_arch = "aarch64"
)))]
0 => unsafe { PortableHash::finalize256(&mut self.inner.portable) },
#[cfg(target_arch = "x86_64")]
1 => unsafe { AvxHash::finalize256(&mut self.inner.avx) },
#[cfg(target_arch = "x86_64")]
2 => unsafe { SseHash::finalize256(&mut self.inner.sse) },
#[cfg(target_arch = "aarch64")]
3 => unsafe { NeonHash::finalize256(&mut self.inner.neon) },
#[cfg(all(target_family = "wasm", target_feature = "simd128"))]
4 => unsafe { WasmHash::finalize256(&mut self.inner.wasm) },
_ => unsafe { core::hint::unreachable_unchecked() },
}
}
}
impl Default for HighwayHasher {
fn default() -> Self {
HighwayHasher::new(Key::default())
}
}
impl_write!(HighwayHasher);
impl_hasher!(HighwayHasher);