31 state[0] = seed + Prime1 + Prime2;
32 state[1] = seed + Prime2;
34 state[3] = seed - Prime1;
43 bool add(
const void* input, uint64_t length)
46 if (!input || length == 0)
49 totalLength += length;
51 const unsigned char* data = (
const unsigned char*)input;
54 if (bufferSize + length < MaxBufferSize)
58 buffer[bufferSize++] = *data++;
63 const unsigned char* stop = data + length;
64 const unsigned char* stopBlock = stop - MaxBufferSize;
70 while (bufferSize < MaxBufferSize)
71 buffer[bufferSize++] = *data++;
74 process(buffer, state[0], state[1], state[2], state[3]);
78 uint64_t s0 = state[0], s1 = state[1], s2 = state[2], s3 = state[3];
80 while (data <= stopBlock)
83 process(data, s0, s1, s2, s3);
87 state[0] = s0; state[1] = s1; state[2] = s2; state[3] = s3;
90 bufferSize = stop - data;
91 for (uint64_t i = 0; i < bufferSize; i++)
104 if (totalLength >= MaxBufferSize)
106 result = rotateLeft(state[0], 1) +
107 rotateLeft(state[1], 7) +
108 rotateLeft(state[2], 12) +
109 rotateLeft(state[3], 18);
110 result = (result ^ processSingle(0, state[0])) * Prime1 + Prime4;
111 result = (result ^ processSingle(0, state[1])) * Prime1 + Prime4;
112 result = (result ^ processSingle(0, state[2])) * Prime1 + Prime4;
113 result = (result ^ processSingle(0, state[3])) * Prime1 + Prime4;
118 result = state[2] + Prime5;
121 result += totalLength;
124 const unsigned char* data = buffer;
126 const unsigned char* stop = data + bufferSize;
129 for (; data + 8 <= stop; data += 8)
130 result = rotateLeft(result ^ processSingle(0, *(uint64_t*)data), 27) * Prime1 + Prime4;
133 if (data + 4 <= stop)
135 result = rotateLeft(result ^ (*(uint32_t*)data) * Prime1, 23) * Prime2 + Prime3;
141 result = rotateLeft(result ^ (*data++) * Prime5, 11) * Prime1;
144 result ^= result >> 33;
146 result ^= result >> 29;
148 result ^= result >> 32;
158 static uint64_t
hash(
const void* input, uint64_t length, uint64_t seed)
161 hasher.
add(input, length);
162 return hasher.
hash();
167 static const uint64_t Prime1 = 11400714785074694791ULL;
168 static const uint64_t Prime2 = 14029467366897019727ULL;
169 static const uint64_t Prime3 = 1609587929392839161ULL;
170 static const uint64_t Prime4 = 9650029242287828579ULL;
171 static const uint64_t Prime5 = 2870177450012600261ULL;
174 static const uint64_t MaxBufferSize = 31+1;
177 unsigned char buffer[MaxBufferSize];
179 uint64_t totalLength;
182 static inline uint64_t rotateLeft(uint64_t x,
unsigned char bits)
184 return (x << bits) | (x >> (64 - bits));
188 static inline uint64_t processSingle(uint64_t previous, uint64_t input)
190 return rotateLeft(previous + input * Prime2, 31) * Prime1;
194 static inline void process(
const void* data, uint64_t& state0, uint64_t& state1, uint64_t& state2, uint64_t& state3)
196 const uint64_t* block = (
const uint64_t*) data;
197 state0 = processSingle(state0, block[0]);
198 state1 = processSingle(state1, block[1]);
199 state2 = processSingle(state2, block[2]);
200 state3 = processSingle(state3, block[3]);
XXHash (64 bit), based on Yann Collet's descriptions, see http://cyan4973.github.io/xxHash/.
Definition: xxhash64.h:25
uint64_t hash() const
get current hash
Definition: xxhash64.h:100
static uint64_t hash(const void *input, uint64_t length, uint64_t seed)
combine constructor, add() and hash() in one static function (C style)
Definition: xxhash64.h:158
XXHash64(uint64_t seed)
create new XXHash (64 bit)
Definition: xxhash64.h:29
bool add(const void *input, uint64_t length)
add a chunk of bytes
Definition: xxhash64.h:43