summaryrefslogtreecommitdiff
path: root/src/common/cityhash.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/cityhash.h')
-rw-r--r--src/common/cityhash.h33
1 files changed, 11 insertions, 22 deletions
diff --git a/src/common/cityhash.h b/src/common/cityhash.h
index a00804e01..d74fc7639 100644
--- a/src/common/cityhash.h
+++ b/src/common/cityhash.h
@@ -62,49 +62,38 @@
62#pragma once 62#pragma once
63 63
64#include <cstddef> 64#include <cstddef>
65#include <cstdint> 65#include "common/common_types.h"
66#include <utility>
67 66
68namespace Common { 67namespace Common {
69 68
70using uint128 = std::pair<uint64_t, uint64_t>;
71
72[[nodiscard]] inline uint64_t Uint128Low64(const uint128& x) {
73 return x.first;
74}
75[[nodiscard]] inline uint64_t Uint128High64(const uint128& x) {
76 return x.second;
77}
78
79// Hash function for a byte array. 69// Hash function for a byte array.
80[[nodiscard]] uint64_t CityHash64(const char* buf, std::size_t len); 70[[nodiscard]] u64 CityHash64(const char* buf, size_t len);
81 71
82// Hash function for a byte array. For convenience, a 64-bit seed is also 72// Hash function for a byte array. For convenience, a 64-bit seed is also
83// hashed into the result. 73// hashed into the result.
84[[nodiscard]] uint64_t CityHash64WithSeed(const char* buf, std::size_t len, uint64_t seed); 74[[nodiscard]] u64 CityHash64WithSeed(const char* buf, size_t len, u64 seed);
85 75
86// Hash function for a byte array. For convenience, two seeds are also 76// Hash function for a byte array. For convenience, two seeds are also
87// hashed into the result. 77// hashed into the result.
88[[nodiscard]] uint64_t CityHash64WithSeeds(const char* buf, std::size_t len, uint64_t seed0, 78[[nodiscard]] u64 CityHash64WithSeeds(const char* buf, size_t len, u64 seed0, u64 seed1);
89 uint64_t seed1);
90 79
91// Hash function for a byte array. 80// Hash function for a byte array.
92[[nodiscard]] uint128 CityHash128(const char* s, std::size_t len); 81[[nodiscard]] u128 CityHash128(const char* s, size_t len);
93 82
94// Hash function for a byte array. For convenience, a 128-bit seed is also 83// Hash function for a byte array. For convenience, a 128-bit seed is also
95// hashed into the result. 84// hashed into the result.
96[[nodiscard]] uint128 CityHash128WithSeed(const char* s, std::size_t len, uint128 seed); 85[[nodiscard]] u128 CityHash128WithSeed(const char* s, size_t len, u128 seed);
97 86
98// Hash 128 input bits down to 64 bits of output. 87// Hash 128 input bits down to 64 bits of output.
99// This is intended to be a reasonably good hash function. 88// This is intended to be a reasonably good hash function.
100[[nodiscard]] inline uint64_t Hash128to64(const uint128& x) { 89[[nodiscard]] inline u64 Hash128to64(const u128& x) {
101 // Murmur-inspired hashing. 90 // Murmur-inspired hashing.
102 const uint64_t kMul = 0x9ddfea08eb382d69ULL; 91 const u64 mul = 0x9ddfea08eb382d69ULL;
103 uint64_t a = (Uint128Low64(x) ^ Uint128High64(x)) * kMul; 92 u64 a = (x[0] ^ x[1]) * mul;
104 a ^= (a >> 47); 93 a ^= (a >> 47);
105 uint64_t b = (Uint128High64(x) ^ a) * kMul; 94 u64 b = (x[1] ^ a) * mul;
106 b ^= (b >> 47); 95 b ^= (b >> 47);
107 b *= kMul; 96 b *= mul;
108 return b; 97 return b;
109} 98}
110 99