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
#![no_std]
#[cfg(any(test, feature = "std"))]
extern crate std;
#[macro_use]
extern crate cfg_if;
use core::hash::{BuildHasherDefault, Hasher};
mod bits;
mod nightly;
mod t1ha0;
mod t1ha1;
mod t1ha2;
pub use t1ha0::{t1ha0_32be, t1ha0_32le};
pub use t1ha1::{t1ha1_be, t1ha1_le};
pub use t1ha2::{t1ha2_atonce, t1ha2_atonce128, T1ha2Hasher};
cfg_if! {
if #[cfg(target_endian = "little")] {
pub use t1ha0::t1ha0_32le as t1ha0_32;
pub use t1ha1::t1ha1_le as t1ha1;
} else {
pub use t1ha0::t1ha0_32be as t1ha0_32;
pub use t1ha1::t1ha1_be as t1ha1;
}
}
cfg_if! {
if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
cfg_if! {
if #[cfg(any(target_feature = "aes", target_feature = "avx", target_feature = "avx2"))] {
mod t1ha0_aes;
}
}
cfg_if! {
if #[cfg(target_feature = "aes")] {
pub use t1ha0_aes::t1ha0_ia32aes as t1ha0_ia32aes_noavx;
}
}
cfg_if! {
if #[cfg(target_feature = "avx")] {
pub use t1ha0_aes::t1ha0_ia32aes as t1ha0_ia32aes_avx;
}
}
cfg_if! {
if #[cfg(target_feature = "avx2")] {
pub use t1ha0_aes::t1ha0_ia32aes_avx2;
}
}
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct T1haHasher(u64);
impl T1haHasher {
#[inline]
pub fn with_seed(seed: u64) -> Self {
Self(seed)
}
}
impl Hasher for T1haHasher {
#[inline]
fn finish(&self) -> u64 {
self.0
}
#[inline]
fn write(&mut self, bytes: &[u8]) {
*self = T1haHasher(t1ha0(bytes, self.0));
}
}
pub type T1haBuildHasher = BuildHasherDefault<T1haHasher>;
cfg_if! {
if #[cfg(feature = "std")] {
use std::collections::{HashMap, HashSet};
pub type T1haHashMap<K, V> = HashMap<K, V, T1haBuildHasher>;
pub type T1haHashSet<T> = HashSet<T, T1haBuildHasher>;
}
}
pub fn t1ha0(data: &[u8], seed: u64) -> u64 {
prefer::HASH(data, seed)
}
mod prefer {
cfg_if! {
if #[cfg(target_pointer_width = "64")] {
pub use super::t1ha1 as t1ha0_fallback;
} else {
pub use super::t1ha0_32 as t1ha0_fallback;
}
}
cfg_if! {
if #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "runtime_select", feature = "std"))] {
use std::is_x86_feature_detected;
use lazy_static::lazy_static;
use super::*;
lazy_static! {
pub static ref HASH: fn(data: &[u8], seed: u64) -> u64 = t1ha0_resolve();
}
#[cfg(not(target_feature = "aes"))]
pub use self::t1ha0_fallback as t1ha0_ia32aes_noavx;
#[cfg(not(target_feature = "avx"))]
pub use self::t1ha0_fallback as t1ha0_ia32aes_avx;
#[cfg(not(target_feature = "avx2"))]
pub use self::t1ha0_fallback as t1ha0_ia32aes_avx2;
fn t1ha0_resolve() -> fn(&[u8], u64) -> u64 {
if is_x86_feature_detected!("avx2") {
t1ha0_ia32aes_avx2
} else if is_x86_feature_detected!("avx") {
t1ha0_ia32aes_avx
} else if is_x86_feature_detected!("aes") {
t1ha0_ia32aes_noavx
} else {
t1ha0_fallback
}
}
} else {
pub use self::t1ha0_fallback as HASH;
}
}
}
#[cfg(test)]
mod selfcheck;