microsoft/onnxruntime-extensions

Public

mirrored from https://github.com/microsoft/onnxruntime-extensionsAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
leca/groupQueryAttention

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

cmake/externals/farmhash/dev/farmhashcc.cc

306lines · modecode

1// This file provides a 32-bit hash equivalent to CityHash32 (v1.1.1)
2// and a 128-bit hash equivalent to CityHash128 (v1.1.1). It also provides
3// a seeded 32-bit hash function similar to CityHash32.
4
5#undef Fetch
6#define Fetch Fetch32
7
8#undef Rotate
9#define Rotate Rotate32
10
11#undef Bswap
12#define Bswap Bswap32
13
14STATIC_INLINE uint32_t Hash32Len13to24(const char *s, size_t len) {
15 uint32_t a = Fetch(s - 4 + (len >> 1));
16 uint32_t b = Fetch(s + 4);
17 uint32_t c = Fetch(s + len - 8);
18 uint32_t d = Fetch(s + (len >> 1));
19 uint32_t e = Fetch(s);
20 uint32_t f = Fetch(s + len - 4);
21 uint32_t h = len;
22
23 return fmix(Mur(f, Mur(e, Mur(d, Mur(c, Mur(b, Mur(a, h)))))));
24}
25
26STATIC_INLINE uint32_t Hash32Len0to4(const char *s, size_t len) {
27 uint32_t b = 0;
28 uint32_t c = 9;
29 for (size_t i = 0; i < len; i++) {
30 signed char v = s[i];
31 b = b * c1 + v;
32 c ^= b;
33 }
34 return fmix(Mur(b, Mur(len, c)));
35}
36
37STATIC_INLINE uint32_t Hash32Len5to12(const char *s, size_t len) {
38 uint32_t a = len, b = len * 5, c = 9, d = b;
39 a += Fetch(s);
40 b += Fetch(s + len - 4);
41 c += Fetch(s + ((len >> 1) & 4));
42 return fmix(Mur(c, Mur(b, Mur(a, d))));
43}
44
45uint32_t Hash32(const char *s, size_t len) {
46 if (len <= 24) {
47 return len <= 12 ?
48 (len <= 4 ? Hash32Len0to4(s, len) : Hash32Len5to12(s, len)) :
49 Hash32Len13to24(s, len);
50 }
51
52 // len > 24
53 uint32_t h = len, g = c1 * len, f = g;
54 uint32_t a0 = Rotate(Fetch(s + len - 4) * c1, 17) * c2;
55 uint32_t a1 = Rotate(Fetch(s + len - 8) * c1, 17) * c2;
56 uint32_t a2 = Rotate(Fetch(s + len - 16) * c1, 17) * c2;
57 uint32_t a3 = Rotate(Fetch(s + len - 12) * c1, 17) * c2;
58 uint32_t a4 = Rotate(Fetch(s + len - 20) * c1, 17) * c2;
59 h ^= a0;
60 h = Rotate(h, 19);
61 h = h * 5 + 0xe6546b64;
62 h ^= a2;
63 h = Rotate(h, 19);
64 h = h * 5 + 0xe6546b64;
65 g ^= a1;
66 g = Rotate(g, 19);
67 g = g * 5 + 0xe6546b64;
68 g ^= a3;
69 g = Rotate(g, 19);
70 g = g * 5 + 0xe6546b64;
71 f += a4;
72 f = Rotate(f, 19);
73 f = f * 5 + 0xe6546b64;
74 size_t iters = (len - 1) / 20;
75 do {
76 uint32_t a0 = Rotate(Fetch(s) * c1, 17) * c2;
77 uint32_t a1 = Fetch(s + 4);
78 uint32_t a2 = Rotate(Fetch(s + 8) * c1, 17) * c2;
79 uint32_t a3 = Rotate(Fetch(s + 12) * c1, 17) * c2;
80 uint32_t a4 = Fetch(s + 16);
81 h ^= a0;
82 h = Rotate(h, 18);
83 h = h * 5 + 0xe6546b64;
84 f += a1;
85 f = Rotate(f, 19);
86 f = f * c1;
87 g += a2;
88 g = Rotate(g, 18);
89 g = g * 5 + 0xe6546b64;
90 h ^= a3 + a1;
91 h = Rotate(h, 19);
92 h = h * 5 + 0xe6546b64;
93 g ^= a4;
94 g = Bswap(g) * 5;
95 h += a4 * 5;
96 h = Bswap(h);
97 f += a0;
98 PERMUTE3(f, h, g);
99 s += 20;
100 } while (--iters != 0);
101 g = Rotate(g, 11) * c1;
102 g = Rotate(g, 17) * c1;
103 f = Rotate(f, 11) * c1;
104 f = Rotate(f, 17) * c1;
105 h = Rotate(h + g, 19);
106 h = h * 5 + 0xe6546b64;
107 h = Rotate(h, 17) * c1;
108 h = Rotate(h + f, 19);
109 h = h * 5 + 0xe6546b64;
110 h = Rotate(h, 17) * c1;
111 return h;
112}
113
114uint32_t Hash32WithSeed(const char *s, size_t len, uint32_t seed) {
115 if (len <= 24) {
116 if (len >= 13) return farmhashmk::Hash32Len13to24(s, len, seed * c1);
117 else if (len >= 5) return farmhashmk::Hash32Len5to12(s, len, seed);
118 else return farmhashmk::Hash32Len0to4(s, len, seed);
119 }
120 uint32_t h = farmhashmk::Hash32Len13to24(s, 24, seed ^ len);
121 return Mur(Hash32(s + 24, len - 24) + seed, h);
122}
123
124#undef Fetch
125#define Fetch Fetch64
126
127#undef Rotate
128#define Rotate Rotate64
129
130#undef Bswap
131#define Bswap Bswap64
132
133STATIC_INLINE uint64_t ShiftMix(uint64_t val) {
134 return val ^ (val >> 47);
135}
136
137STATIC_INLINE uint64_t HashLen16(uint64_t u, uint64_t v) {
138 return Hash128to64(Uint128(u, v));
139}
140
141STATIC_INLINE uint64_t HashLen16(uint64_t u, uint64_t v, uint64_t mul) {
142 // Murmur-inspired hashing.
143 uint64_t a = (u ^ v) * mul;
144 a ^= (a >> 47);
145 uint64_t b = (v ^ a) * mul;
146 b ^= (b >> 47);
147 b *= mul;
148 return b;
149}
150
151STATIC_INLINE uint64_t HashLen0to16(const char *s, size_t len) {
152 if (len >= 8) {
153 uint64_t mul = k2 + len * 2;
154 uint64_t a = Fetch(s) + k2;
155 uint64_t b = Fetch(s + len - 8);
156 uint64_t c = Rotate(b, 37) * mul + a;
157 uint64_t d = (Rotate(a, 25) + b) * mul;
158 return HashLen16(c, d, mul);
159 }
160 if (len >= 4) {
161 uint64_t mul = k2 + len * 2;
162 uint64_t a = Fetch32(s);
163 return HashLen16(len + (a << 3), Fetch32(s + len - 4), mul);
164 }
165 if (len > 0) {
166 uint8_t a = s[0];
167 uint8_t b = s[len >> 1];
168 uint8_t c = s[len - 1];
169 uint32_t y = static_cast<uint32_t>(a) + (static_cast<uint32_t>(b) << 8);
170 uint32_t z = len + (static_cast<uint32_t>(c) << 2);
171 return ShiftMix(y * k2 ^ z * k0) * k2;
172 }
173 return k2;
174}
175
176// Return a 16-byte hash for 48 bytes. Quick and dirty.
177// Callers do best to use "random-looking" values for a and b.
178STATIC_INLINE pair<uint64_t, uint64_t> WeakHashLen32WithSeeds(
179 uint64_t w, uint64_t x, uint64_t y, uint64_t z, uint64_t a, uint64_t b) {
180 a += w;
181 b = Rotate(b + a + z, 21);
182 uint64_t c = a;
183 a += x;
184 a += y;
185 b += Rotate(a, 44);
186 return make_pair(a + z, b + c);
187}
188
189// Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
190STATIC_INLINE pair<uint64_t, uint64_t> WeakHashLen32WithSeeds(
191 const char* s, uint64_t a, uint64_t b) {
192 return WeakHashLen32WithSeeds(Fetch(s),
193 Fetch(s + 8),
194 Fetch(s + 16),
195 Fetch(s + 24),
196 a,
197 b);
198}
199
200
201
202// A subroutine for CityHash128(). Returns a decent 128-bit hash for strings
203// of any length representable in signed long. Based on City and Murmur.
204STATIC_INLINE NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t CityMurmur(const char *s, size_t len, NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t seed) {
205 uint64_t a = Uint128Low64(seed);
206 uint64_t b = Uint128High64(seed);
207 uint64_t c = 0;
208 uint64_t d = 0;
209 signed long l = len - 16;
210 if (l <= 0) { // len <= 16
211 a = ShiftMix(a * k1) * k1;
212 c = b * k1 + HashLen0to16(s, len);
213 d = ShiftMix(a + (len >= 8 ? Fetch(s) : c));
214 } else { // len > 16
215 c = HashLen16(Fetch(s + len - 8) + k1, a);
216 d = HashLen16(b + len, c + Fetch(s + len - 16));
217 a += d;
218 do {
219 a ^= ShiftMix(Fetch(s) * k1) * k1;
220 a *= k1;
221 b ^= a;
222 c ^= ShiftMix(Fetch(s + 8) * k1) * k1;
223 c *= k1;
224 d ^= c;
225 s += 16;
226 l -= 16;
227 } while (l > 0);
228 }
229 a = HashLen16(a, c);
230 b = HashLen16(d, b);
231 return Uint128(a ^ b, HashLen16(b, a));
232}
233
234NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t CityHash128WithSeed(const char *s, size_t len, NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t seed) {
235 if (len < 128) {
236 return CityMurmur(s, len, seed);
237 }
238
239 // We expect len >= 128 to be the common case. Keep 56 bytes of state:
240 // v, w, x, y, and z.
241 pair<uint64_t, uint64_t> v, w;
242 uint64_t x = Uint128Low64(seed);
243 uint64_t y = Uint128High64(seed);
244 uint64_t z = len * k1;
245 v.first = Rotate(y ^ k1, 49) * k1 + Fetch(s);
246 v.second = Rotate(v.first, 42) * k1 + Fetch(s + 8);
247 w.first = Rotate(y + z, 35) * k1 + x;
248 w.second = Rotate(x + Fetch(s + 88), 53) * k1;
249
250 // This is the same inner loop as CityHash64(), manually unrolled.
251 do {
252 x = Rotate(x + y + v.first + Fetch(s + 8), 37) * k1;
253 y = Rotate(y + v.second + Fetch(s + 48), 42) * k1;
254 x ^= w.second;
255 y += v.first + Fetch(s + 40);
256 z = Rotate(z + w.first, 33) * k1;
257 v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
258 w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch(s + 16));
259 std::swap(z, x);
260 s += 64;
261 x = Rotate(x + y + v.first + Fetch(s + 8), 37) * k1;
262 y = Rotate(y + v.second + Fetch(s + 48), 42) * k1;
263 x ^= w.second;
264 y += v.first + Fetch(s + 40);
265 z = Rotate(z + w.first, 33) * k1;
266 v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
267 w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch(s + 16));
268 std::swap(z, x);
269 s += 64;
270 len -= 128;
271 } while (LIKELY(len >= 128));
272 x += Rotate(v.first + z, 49) * k0;
273 y = y * k0 + Rotate(w.second, 37);
274 z = z * k0 + Rotate(w.first, 27);
275 w.first *= 9;
276 v.first *= k0;
277 // If 0 < len < 128, hash up to 4 chunks of 32 bytes each from the end of s.
278 for (size_t tail_done = 0; tail_done < len; ) {
279 tail_done += 32;
280 y = Rotate(x + y, 42) * k0 + v.second;
281 w.first += Fetch(s + len - tail_done + 16);
282 x = x * k0 + w.first;
283 z += w.second + Fetch(s + len - tail_done);
284 w.second += v.first;
285 v = WeakHashLen32WithSeeds(s + len - tail_done, v.first + z, v.second);
286 v.first *= k0;
287 }
288 // At this point our 56 bytes of state should contain more than
289 // enough information for a strong 128-bit hash. We use two
290 // different 56-byte-to-8-byte hashes to get a 16-byte final result.
291 x = HashLen16(x, v.first);
292 y = HashLen16(y + z, w.first);
293 return Uint128(HashLen16(x + v.second, w.second) + y,
294 HashLen16(x + w.second, y + v.second));
295}
296
297STATIC_INLINE NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t CityHash128(const char *s, size_t len) {
298 return len >= 16 ?
299 CityHash128WithSeed(s + 16, len - 16,
300 Uint128(Fetch(s), Fetch(s + 8) + k0)) :
301 CityHash128WithSeed(s, len, Uint128(k0, k1));
302}
303
304NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t Fingerprint128(const char* s, size_t len) {
305 return CityHash128(s, len);
306}