xxhash 64位算法核心代码
时间: 2023-09-12 16:13:24 浏览: 33
以下是 xxhash 64 位算法的核心代码实现,使用 C++ 语言编写:
```c++
#include <cstdint>
uint64_t const PRIME64_1 = 11400714785074694791ULL;
uint64_t const PRIME64_2 = 14029467366897019727ULL;
uint64_t const PRIME64_3 = 1609587929392839161ULL;
uint64_t const PRIME64_4 = 9650029242287828579ULL;
uint64_t const PRIME64_5 = 2870177450012600261ULL;
uint64_t xxhash64(void const* input, std::size_t const length, uint64_t const seed) {
uint64_t const* data = static_cast<uint64_t const*>(input);
std::size_t const n_blocks = length / 8;
uint64_t hash = seed + PRIME64_5;
for (std::size_t i = 0; i < n_blocks; ++i) {
uint64_t block = data[i];
block *= PRIME64_2;
block = _rotl64(block, 31);
block *= PRIME64_1;
hash ^= block;
hash = _rotl64(hash, 27) * PRIME64_1 + PRIME64_4;
}
uint8_t const* tail = static_cast<uint8_t const*>(static_cast<void const*>(data + n_blocks));
uint64_t const k1 = tail[0];
uint64_t const k2 = tail[1] << 8 | tail[2];
uint64_t const k3 = tail[3] << 16 | tail[4] << 8 | tail[5];
uint64_t const k4 = tail[6] << 24 | tail[7] << 16 | tail[8] << 8 | tail[9];
uint64_t const k5 = tail[length % 10];
uint64_t block = k1;
block *= PRIME64_1;
block = _rotl64(block, 31);
block *= PRIME64_2;
hash ^= block;
block = k2;
block *= PRIME64_2;
block = _rotl64(block, 31);
block *= PRIME64_1;
hash ^= block;
block = k3;
block *= PRIME64_3;
block = _rotl64(block, 31);
block *= PRIME64_1;
hash ^= block;
block = k4;
block *= PRIME64_4;
block = _rotl64(block, 31);
block *= PRIME64_1;
hash ^= block;
block = k5;
block *= PRIME64_5;
block = _rotl64(block, 31);
block *= PRIME64_1;
hash ^= block;
hash ^= length;
hash = (hash ^ (hash >> 33)) * PRIME64_2;
hash ^= hash >> 29;
hash *= PRIME64_3;
hash ^= hash >> 32;
return hash;
}
```
其中,`PRIME64_1` 到 `PRIME64_5` 是一些预定义的常量。`xxhash64` 函数接受一个指向输入数据的指针 `input`,输入数据的长度 `length`,以及一个种子值 `seed`。函数返回一个 64 位的哈希值。函数内部首先将输入数据按照 8 字节为一组进行分块,然后对每个分块进行一系列的操作,最终得到哈希值。需要注意的是,在处理输入数据尾部时,会先构造一个 10 字节的数据块,然后再进行一次类似的操作。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.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)