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
#![cfg_attr(not(test), no_std)]
extern crate const_random;
extern crate no_panic;
extern crate alloc;
#[macro_use]
mod convert;
mod fallback_hash;
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes"))]
mod aes_hash;
#[cfg(test)]
mod hash_quality_test;
use const_random::const_random;
use core::hash::{BuildHasher};
use core::sync::atomic::AtomicUsize;
use core::sync::atomic::Ordering;
use no_panic::no_panic;
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes"))]
pub use crate::aes_hash::AHasher;
#[cfg(not(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes")))]
pub use crate::fallback_hash::AHasher;
const MULTIPLE: u64 = 6364136223846793005;
static SEED: AtomicUsize = AtomicUsize::new(const_random!(u64));
impl Default for AHasher {
#[inline]
fn default() -> AHasher {
return AHasher::new_with_keys(const_random!(u64), const_random!(u64));
}
}
#[derive(Clone)]
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes"))]
pub struct ABuildHasher {
k0: u64,
k1: u64,
}
#[derive(Clone)]
#[cfg(not(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes")))]
pub struct ABuildHasher {
key: u64,
}
impl ABuildHasher {
#[inline]
pub fn new() -> ABuildHasher {
let previous = SEED.load(Ordering::Relaxed) as u64;
let stack_mem_loc = &previous as *const _ as u64;
let current_seed = (previous ^ stack_mem_loc).wrapping_mul(MULTIPLE).rotate_left(31);
SEED.store(current_seed as usize, Ordering::Relaxed);
let mut k0 = &SEED as *const _ as u64;
let mut k1 = current_seed ^ k0;
k0 = k0.rotate_left(24) ^ k1 ^ (k1 << 16);
k1 = k1.rotate_left(37);
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes"))]
return ABuildHasher { k0: k0, k1: k1 };
#[cfg(not(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes")))]
return ABuildHasher { key: k0.wrapping_add(k1) };
}
}
impl Default for ABuildHasher {
#[inline]
fn default() -> ABuildHasher {
ABuildHasher::new()
}
}
impl BuildHasher for ABuildHasher {
type Hasher = AHasher;
#[inline]
fn build_hasher(&self) -> AHasher {
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes"))]
return AHasher::new_with_keys(self.k0, self.k1);
#[cfg(not(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes")))]
return AHasher::new_with_key(self.key);
}
}
#[inline(never)]
#[no_panic]
#[no_mangle]
fn hash_test_final(num: i32, string: &str) -> (u64, u64) {
use core::hash::{Hasher};
let builder = ABuildHasher::default();
let mut hasher1 = builder.build_hasher();
let mut hasher2 = builder.build_hasher();
hasher1.write_i32(num);
hasher2.write(string.as_bytes());
(hasher1.finish(), hasher2.finish())
}
#[cfg(test)]
mod test {
use core::hash::BuildHasherDefault;
use std::collections::HashMap;
use crate::convert::Convert;
use crate::*;
#[test]
fn test_no_panic() {
hash_test_final(2, "");
}
#[test]
fn test_default_builder() {
let mut map = HashMap::<u32, u64, BuildHasherDefault<AHasher>>::default();
map.insert(1, 3);
}
#[test]
fn test_builder() {
let mut map = HashMap::<u32, u64, ABuildHasher>::default();
map.insert(1, 3);
}
#[test]
fn test_conversion() {
let input: &[u8] = "dddddddd".as_bytes();
let bytes: u64 = as_array!(input, 8).convert();
assert_eq!(bytes, 0x6464646464646464);
}
}