-
Notifications
You must be signed in to change notification settings - Fork 49
/
choc_xxHash.h
363 lines (292 loc) · 11.8 KB
/
choc_xxHash.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//
// ██████ ██ ██ ██████ ██████
// ██ ██ ██ ██ ██ ██ ** Classy Header-Only Classes **
// ██ ███████ ██ ██ ██
// ██ ██ ██ ██ ██ ██ https://github.com/Tracktion/choc
// ██████ ██ ██ ██████ ██████
//
// CHOC is (C)2022 Tracktion Corporation, and is offered under the terms of the ISC license:
//
// Permission to use, copy, modify, and/or distribute this software for any purpose with or
// without fee is hereby granted, provided that the above copyright notice and this permission
// notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#ifndef CHOC_XXHASH_HEADER_INCLUDED
#define CHOC_XXHASH_HEADER_INCLUDED
#include <cstring>
#include <memory>
namespace choc::hash
{
//==============================================================================
/// An implementation of the 32-bit xxHash algorithm, which is a hashing function
/// optimised for speed rather than cryptographic strength.
///
/// See https://github.com/Cyan4973/xxHash
///
/// See also xxHash64
class xxHash32
{
public:
/// Creates a hashing state object with an optional seed value
/// After creating an object, you can repeatedly call addInput() to feed
/// the data into it, and when done, call getHash() to get the resulting
/// hash value.
explicit xxHash32 (uint32_t seed = 0);
xxHash32 (const xxHash32&) = default;
xxHash32& operator= (const xxHash32&) = default;
/// Performs a hash on a single chunk of memory and returns the result with
/// one function call.
static uint32_t hash (const void* inputData, size_t numBytes, uint32_t seed = 0);
/// Feeds a chunk of data into the hash state.
void addInput (const void* inputData, size_t numBytes) noexcept;
/// Adds a string to the hash state.
void addInput (std::string_view) noexcept;
/// Returns the finished hash value.
uint32_t getHash() const;
private:
//==============================================================================
static constexpr uint32_t prime1 = 2654435761u, prime2 = 2246822519u,
prime3 = 3266489917u, prime4 = 668265263u,
prime5 = 374761393u;
struct State
{
State (uint32_t) noexcept;
void process (const void*) noexcept;
uint32_t s0, s1, s2, s3;
};
uint64_t totalLength = 0;
State state;
static constexpr size_t bytesPerBlock = 16;
uint8_t currentBlock[bytesPerBlock];
uint32_t currentBlockSize = 0;
};
//==============================================================================
/// An implementation of the 64-bit xxHash algorithm, which is a hashing function
/// optimised for speed rather than cryptographic strength.
///
/// See https://github.com/Cyan4973/xxHash
///
/// See also xxHash32
class xxHash64
{
public:
/// Creates a hashing state object with an optional seed value
/// After creating an object, you can repeatedly call addInput() to feed
/// the data into it, and when done, call getHash() to get the resulting
/// hash value.
explicit xxHash64 (uint64_t seed = 0);
xxHash64 (const xxHash64&) = default;
xxHash64& operator= (const xxHash64&) = default;
/// Performs a hash on a single chunk of memory and returns the result with
/// one function call.
static uint64_t hash (const void* inputData, size_t numBytes, uint64_t seed = 0);
/// Feeds a chunk of data into the hash state.
void addInput (const void* inputData, size_t numBytes) noexcept;
/// Adds a string to the hash state.
void addInput (std::string_view) noexcept;
/// Returns the finished hash value.
uint64_t getHash() const;
private:
//==============================================================================
static constexpr uint64_t prime1 = 11400714785074694791ull,
prime2 = 14029467366897019727ull,
prime3 = 1609587929392839161ull,
prime4 = 9650029242287828579ull,
prime5 = 2870177450012600261ull;
struct State
{
State (uint64_t) noexcept;
void process (const void*) noexcept;
uint64_t s0, s1, s2, s3;
};
uint64_t totalLength = 0;
State state;
static constexpr size_t bytesPerBlock = 32;
uint8_t currentBlock[bytesPerBlock];
uint32_t currentBlockSize = 0;
};
//==============================================================================
// _ _ _ _
// __| | ___ | |_ __ _ (_)| | ___
// / _` | / _ \| __| / _` || || |/ __|
// | (_| || __/| |_ | (_| || || |\__ \ _ _ _
// \__,_| \___| \__| \__,_||_||_||___/(_)(_)(_)
//
// Code beyond this point is implementation detail...
//
//==============================================================================
static inline uint32_t rotl (uint32_t value, uint32_t numBits) noexcept { return (value << numBits) | (value >> (32 - numBits)); }
static inline uint64_t rotl (uint64_t value, uint32_t numBits) noexcept { return (value << numBits) | (value >> (64 - numBits)); }
inline xxHash32::xxHash32 (uint32_t seed) : state (seed) {}
inline uint32_t xxHash32::hash (const void* inputData, size_t numBytes, uint32_t seed)
{
xxHash32 h (seed);
h.addInput (inputData, numBytes);
return h.getHash();
}
inline void xxHash32::addInput (const void* inputData, size_t numBytes) noexcept
{
if (inputData == nullptr || numBytes == 0)
return;
totalLength += numBytes;
auto input = static_cast<const uint8_t*> (inputData);
if (currentBlockSize + numBytes < bytesPerBlock)
{
memcpy (currentBlock + currentBlockSize, input, numBytes);
currentBlockSize += static_cast<uint32_t> (numBytes);
return;
}
auto inputEnd = input + numBytes;
auto lastFullInputBlock = inputEnd - bytesPerBlock;
auto s = state;
if (currentBlockSize != 0)
{
while (currentBlockSize < bytesPerBlock)
currentBlock[currentBlockSize++] = *input++;
s.process (currentBlock);
}
while (input <= lastFullInputBlock)
{
s.process (input);
input += bytesPerBlock;
}
state = s;
currentBlockSize = static_cast<uint32_t> (inputEnd - input);
memcpy (currentBlock, input, currentBlockSize);
}
inline void xxHash32::addInput (std::string_view s) noexcept { addInput (s.data(), s.length()); }
inline uint32_t xxHash32::getHash() const
{
auto hash = static_cast<uint32_t> (totalLength);
if (totalLength >= bytesPerBlock)
hash += rotl (state.s0, 1)
+ rotl (state.s1, 7)
+ rotl (state.s2, 12)
+ rotl (state.s3, 18);
else
hash += state.s2 + prime5;
uint32_t asInt;
uint32_t i = 0;
for (; i + sizeof (asInt) <= currentBlockSize; i += static_cast<uint32_t> (sizeof (asInt)))
{
memcpy (std::addressof (asInt), currentBlock + i, sizeof (asInt));
hash = rotl (hash + asInt * prime3, 17) * prime4;
}
while (i < currentBlockSize)
hash = rotl (hash + currentBlock[i++] * prime5, 11) * prime1;
hash ^= hash >> 15; hash *= prime2;
hash ^= hash >> 13; hash *= prime3;
hash ^= hash >> 16;
return hash;
}
inline xxHash32::State::State (uint32_t seed) noexcept
: s0 (seed + prime1 + prime2), s1 (seed + prime2),
s2 (seed), s3 (seed - prime1)
{}
inline void xxHash32::State::process (const void* block) noexcept
{
uint32_t ints[4];
memcpy (ints, block, bytesPerBlock);
s0 = rotl (s0 + ints[0] * prime2, 13) * prime1;
s1 = rotl (s1 + ints[1] * prime2, 13) * prime1;
s2 = rotl (s2 + ints[2] * prime2, 13) * prime1;
s3 = rotl (s3 + ints[3] * prime2, 13) * prime1;
}
//==============================================================================
inline xxHash64::xxHash64 (uint64_t seed) : state (seed) {}
inline uint64_t xxHash64::hash (const void* inputData, size_t numBytes, uint64_t seed)
{
xxHash64 h (seed);
h.addInput (inputData, numBytes);
return h.getHash();
}
inline void xxHash64::addInput (const void* inputData, size_t numBytes) noexcept
{
if (inputData == nullptr || numBytes == 0)
return;
totalLength += numBytes;
auto input = static_cast<const uint8_t*> (inputData);
if (currentBlockSize + numBytes < bytesPerBlock)
{
memcpy (currentBlock + currentBlockSize, input, numBytes);
currentBlockSize += static_cast<uint32_t> (numBytes);
return;
}
auto inputEnd = input + numBytes;
auto lastFullInputBlock = inputEnd - bytesPerBlock;
auto s = state;
if (currentBlockSize != 0)
{
while (currentBlockSize < bytesPerBlock)
currentBlock[currentBlockSize++] = *input++;
s.process (currentBlock);
}
while (input <= lastFullInputBlock)
{
s.process (input);
input += bytesPerBlock;
}
state = s;
currentBlockSize = static_cast<uint32_t> (inputEnd - input);
memcpy (currentBlock, input, currentBlockSize);
}
inline void xxHash64::addInput (std::string_view s) noexcept { addInput (s.data(), s.length()); }
inline uint64_t xxHash64::getHash() const
{
uint64_t hash;
if (totalLength >= bytesPerBlock)
{
hash = rotl (state.s0, 1)
+ rotl (state.s1, 7)
+ rotl (state.s2, 12)
+ rotl (state.s3, 18);
hash = (hash ^ (prime1 * rotl (state.s0 * prime2, 31))) * prime1 + prime4;
hash = (hash ^ (prime1 * rotl (state.s1 * prime2, 31))) * prime1 + prime4;
hash = (hash ^ (prime1 * rotl (state.s2 * prime2, 31))) * prime1 + prime4;
hash = (hash ^ (prime1 * rotl (state.s3 * prime2, 31))) * prime1 + prime4;
}
else
{
hash = state.s2 + prime5;
}
hash += totalLength;
uint32_t i = 0;
for (; i + sizeof (uint64_t) <= currentBlockSize; i += static_cast<uint32_t> (sizeof (uint64_t)))
{
uint64_t asInt;
memcpy (std::addressof (asInt), currentBlock + i, sizeof (asInt));
hash = rotl (hash ^ (rotl (asInt * prime2, 31) * prime1), 27) * prime1 + prime4;
}
if (i + sizeof (uint32_t) <= currentBlockSize)
{
uint32_t asInt;
memcpy (std::addressof (asInt), currentBlock + i, sizeof (asInt));
hash = rotl (hash ^ asInt * prime1, 23) * prime2 + prime3;
i += static_cast<uint32_t> (sizeof (uint32_t));
}
while (i < currentBlockSize)
hash = rotl (hash ^ currentBlock[i++] * prime5, 11) * prime1;
hash ^= hash >> 33; hash *= prime2;
hash ^= hash >> 29; hash *= prime3;
hash ^= hash >> 32;
return hash;
}
inline xxHash64::State::State (uint64_t seed) noexcept
: s0 (seed + prime1 + prime2), s1 (seed + prime2),
s2 (seed), s3 (seed - prime1)
{}
inline void xxHash64::State::process (const void* block) noexcept
{
uint64_t ints[4];
memcpy (ints, block, bytesPerBlock);
s0 = rotl (s0 + ints[0] * prime2, 31) * prime1;
s1 = rotl (s1 + ints[1] * prime2, 31) * prime1;
s2 = rotl (s2 + ints[2] * prime2, 31) * prime1;
s3 = rotl (s3 + ints[3] * prime2, 31) * prime1;
}
} // namespace choc::hash
#endif // CHOC_XXHASH_HEADER_INCLUDED