cityhash的核心代码示例
时间: 2024-05-14 20:12:03 浏览: 15
以下是CityHash的C++核心代码示例:
```c++
#include <stdint.h>
#include <stdlib.h>
inline uint64_t UNALIGNED_LOAD64(const char* p) {
uint64_t result;
__builtin_memcpy(&result, p, sizeof(result));
return result;
}
inline uint32_t UNALIGNED_LOAD32(const char* p) {
uint32_t result;
__builtin_memcpy(&result, p, sizeof(result));
return result;
}
inline uint64_t Uint128Low64(const uint128& x) {
return Uint128Low64(x);
}
inline uint64_t Uint128High64(const uint128& x) {
return Uint128High64(x);
}
inline uint64_t Fetch64(const char* p) {
return UNALIGNED_LOAD64(p);
}
inline uint32_t Fetch32(const char* p) {
return UNALIGNED_LOAD32(p);
}
inline uint64_t Rotate(uint64_t val, int shift) {
return shift == 0 ? val : ((val >> shift) | (val << (64 - shift)));
}
inline uint32_t Rotate(uint32_t val, int shift) {
return shift == 0 ? val : ((val >> shift) | (val << (32 - shift)));
}
inline uint64_t ShiftMix(uint64_t val) {
return val ^ (val >> 47);
}
inline uint64_t HashLen16(uint64_t u, uint64_t v) {
return Hash128to64(uint128(u, v));
}
inline uint64_t HashLen0to16(const char* s, size_t len) {
if (len > 8) {
uint64_t a = Fetch64(s);
uint64_t b = Fetch64(s + len - 8);
return HashLen16(a, RotateByAtLeast1(b + len, len)) ^ b;
}
if (len >= 4) {
uint32_t a = Fetch32(s);
return HashLen16(len + (a << 3), Fetch32(s + len - 4));
}
if (len > 0) {
uint8_t a = s[0];
uint8_t b = s[len >> 1];
uint8_t c = s[len - 1];
uint32_t y = static_cast<uint32_t>(a) + (static_cast<uint32_t>(b) << 8);
uint32_t z = len + (static_cast<uint32_t>(c) << 2);
return ShiftMix(y * k2 ^ z * k0) * k2;
}
return k2;
}
inline uint64_t HashLen17to32(const char* s, size_t len) {
uint64_t a = Fetch64(s) * k1;
uint64_t b = Fetch64(s + 8);
uint64_t c = Fetch64(s + len - 8) * k2;
uint64_t d = Fetch64(s + len - 16) * k0;
return HashLen16(Rotate(a - b, 43) + Rotate(c, 30) + d,
a + Rotate(b ^ k3, 20) - c + len);
}
inline uint64_t HashLen33to64(const char* s, size_t len) {
uint64_t z = Fetch64(s + 24);
uint64_t a = Fetch64(s) + (len + Fetch64(s + len - 16)) * k0;
uint64_t b = Rotate(a + z, 52);
uint64_t c = Rotate(a, 37);
a += Fetch64(s + 8);
c += Rotate(a, 7);
a += Fetch64(s + 16);
uint64_t vf = a + z;
uint64_t vs = b + Rotate(a, 31) + c;
a = Fetch64(s + 16) + Fetch64(s + len - 32);
z = Fetch64(s + len - 8);
b = Rotate(a + z, 52);
c = Rotate(a, 37);
a += Fetch64(s + len - 24);
c += Rotate(a, 7);
a += Fetch64(s + len - 16);
uint64_t wf = a + z;
uint64_t ws = b + Rotate(a, 31) + c;
uint64_t r = ShiftMix((vf + ws) * k2 + (wf + vs) * k0);
return ShiftMix(r * k0 + vs) * k2;
}
inline uint64_t CityHash64(const char* buf, size_t len) {
if (len <= 32) {
if (len <= 16) {
return HashLen0to16(buf, len);
} else {
return HashLen17to32(buf, len);
}
} else if (len <= 64) {
return HashLen33to64(buf, len);
}
uint64_t x = Fetch64(buf + len - 40);
uint64_t y = Fetch64(buf + len - 16) + Fetch64(buf + len - 56);
uint64_t z = HashLen16(Fetch64(buf + len - 48) + len,
Fetch64(buf + len - 24));
std::pair<uint64_t, uint64_t> v = WeakHashLen32WithSeeds(
buf + len - 64, len, z);
std::pair<uint64_t, uint64_t> w = WeakHashLen32WithSeeds(buf + len - 32,
len * k1, k2);
x = x * k2 + Rotate(y + v.second, 18) + v.first;
y = y * k1 + w.second + Rotate(x, 22);
x = x * k2 + Rotate(y + v.first, 37) + w.first;
y = y * k2 + Rotate(x + v.second, 18) + v.first;
z = HashLen16(z + w.first, v.first + HashLen16(y, x));
return z;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)