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
use key::Key;
use portable::PortableHash;
use traits::HighwayHash;
use std::default::Default;
#[cfg(target_arch = "x86_64")]
use avx::AvxHash;
#[cfg(target_arch = "x86_64")]
use sse::SseHash;
#[derive(Debug, Clone)]
enum HighwayChoices {
Portable(PortableHash),
#[cfg(target_arch = "x86_64")]
Sse(SseHash),
#[cfg(target_arch = "x86_64")]
Avx(AvxHash),
}
#[derive(Debug, Clone)]
pub struct HighwayBuilder(HighwayChoices);
impl HighwayHash for HighwayBuilder {
fn hash64(self, data: &[u8]) -> u64 {
match self.0 {
HighwayChoices::Portable(x) => x.hash64(data),
#[cfg(target_arch = "x86_64")]
HighwayChoices::Avx(x) => x.hash64(data),
#[cfg(target_arch = "x86_64")]
HighwayChoices::Sse(x) => x.hash64(data),
}
}
fn hash128(self, data: &[u8]) -> [u64; 2] {
match self.0 {
HighwayChoices::Portable(x) => x.hash128(data),
#[cfg(target_arch = "x86_64")]
HighwayChoices::Avx(x) => x.hash128(data),
#[cfg(target_arch = "x86_64")]
HighwayChoices::Sse(x) => x.hash128(data),
}
}
fn hash256(self, data: &[u8]) -> [u64; 4] {
match self.0 {
HighwayChoices::Portable(x) => x.hash256(data),
#[cfg(target_arch = "x86_64")]
HighwayChoices::Avx(x) => x.hash256(data),
#[cfg(target_arch = "x86_64")]
HighwayChoices::Sse(x) => x.hash256(data),
}
}
fn append(&mut self, data: &[u8]) {
match &mut self.0 {
HighwayChoices::Portable(x) => x.append(data),
#[cfg(target_arch = "x86_64")]
HighwayChoices::Avx(x) => x.append(data),
#[cfg(target_arch = "x86_64")]
HighwayChoices::Sse(x) => x.append(data),
}
}
fn finalize64(self) -> u64 {
match self.0 {
HighwayChoices::Portable(x) => x.finalize64(),
#[cfg(target_arch = "x86_64")]
HighwayChoices::Avx(x) => x.finalize64(),
#[cfg(target_arch = "x86_64")]
HighwayChoices::Sse(x) => x.finalize64(),
}
}
fn finalize128(self) -> [u64; 2] {
match self.0 {
HighwayChoices::Portable(x) => x.finalize128(),
#[cfg(target_arch = "x86_64")]
HighwayChoices::Avx(x) => x.finalize128(),
#[cfg(target_arch = "x86_64")]
HighwayChoices::Sse(x) => x.finalize128(),
}
}
fn finalize256(self) -> [u64; 4] {
match self.0 {
HighwayChoices::Portable(x) => x.finalize256(),
#[cfg(target_arch = "x86_64")]
HighwayChoices::Avx(x) => x.finalize256(),
#[cfg(target_arch = "x86_64")]
HighwayChoices::Sse(x) => x.finalize256(),
}
}
}
impl HighwayBuilder {
pub fn new(key: &Key) -> Self {
#[cfg(target_arch = "x86_64")]
{
if let Some(h) = AvxHash::new(key) {
return HighwayBuilder(HighwayChoices::Avx(h));
}
if let Some(h) = SseHash::new(key) {
return HighwayBuilder(HighwayChoices::Sse(h));
}
}
HighwayBuilder(HighwayChoices::Portable(PortableHash::new(key)))
}
}
impl Default for HighwayBuilder {
fn default() -> Self {
HighwayBuilder::new(&Key::default())
}
}