-
Notifications
You must be signed in to change notification settings - Fork 5
/
spookyhash.c
376 lines (294 loc) · 9.09 KB
/
spookyhash.c
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
364
365
366
367
368
369
370
371
372
373
374
375
// Spooky Hash
// A 128-bit noncryptographic hash, for checksums and table lookup
// By Bob Jenkins. Public domain.
// Oct 31 2010: published framework, disclaimer ShortHash isn't right
// Nov 7 2010: disabled ShortHash
// Oct 31 2011: replace End, ShortMix, ShortEnd, enable ShortHash again
#include <memory.h>
#include "spookyhash.h"
#define ALLOW_UNALIGNED_READS 1
// @brief spookyhash_short is used for messages under 192 bytes in length
//
// spookyhash_short has a low startup cost, the normal mode is good for long
// keys, the cost crossover is at about 192 bytes. The two modes were
// held to the same quality bar.
//
// @param message (array of bytes, not necessarily aligned)
// @param length length of message in bytes
// @param hash1 in/out: in seed 1, out hash value 1
// @param hash2 in/out: in seed 2, out hash value 2
static void spookyhash_short(const void* message, size_t length,
uint64_t* hash1, uint64_t* hash2) {
// short hash ... it could be used on any message,
// but it's used by spooky just for short messages.
uint64_t buf[24]; // (sc_num_vars * 2)
union {
const uint8_t* p8;
uint32_t* p32;
uint64_t* p64;
size_t i;
} u;
u.p8 = (const uint8_t*)message;
if (!ALLOW_UNALIGNED_READS && (u.i & 0x7)) {
memcpy(buf, message, length);
u.p64 = buf;
}
size_t remainder = length % 32;
uint64_t a = *hash1;
uint64_t b = *hash2;
uint64_t c = sc_const;
uint64_t d = sc_const;
if (length > 15) {
const uint64_t* end = u.p64 + (length / 32) * 4;
// handle all complete sets of 32 bytes
for (; u.p64 < end; u.p64 += 4) {
c += u.p64[0];
d += u.p64[1];
spookyhash_smix(&a, &b, &c, &d);
a += u.p64[2];
b += u.p64[3];
}
// Handle the case of 16+ remaining bytes.
if (remainder >= 16) {
c += u.p64[0];
d += u.p64[1];
spookyhash_smix(&a, &b, &c, &d);
u.p64 += 2;
remainder -= 16;
}
}
// handle the last 0..15 bytes, and its length
d += ((uint64_t)length) << 56;
switch (remainder) {
case 15:
d += ((uint64_t)u.p8[14]) << 48;
// fall-through
case 14:
d += ((uint64_t)u.p8[13]) << 40;
// fall-through
case 13:
d += ((uint64_t)u.p8[12]) << 32;
// fall-through
case 12:
d += u.p32[2];
c += u.p64[0];
break;
case 11:
d += ((uint64_t)u.p8[10]) << 16;
// fall-through
case 10:
d += ((uint64_t)u.p8[9]) << 8;
// fall-through
case 9:
d += (uint64_t)u.p8[8];
// fall-through
case 8:
c += u.p64[0];
break;
case 7:
c += ((uint64_t)u.p8[6]) << 48;
// fall-through
case 6:
c += ((uint64_t)u.p8[5]) << 40;
// fall-through
case 5:
c += ((uint64_t)u.p8[4]) << 32;
// fall-through
case 4:
c += u.p32[0];
break;
case 3:
c += ((uint64_t)u.p8[2]) << 16;
// fall-through
case 2:
c += ((uint64_t)u.p8[1]) << 8;
// fall-through
case 1:
c += (uint64_t)u.p8[0];
break;
case 0:
c += sc_const;
d += sc_const;
}
spookyhash_short_end(&a, &b, &c, &d);
*hash1 = a;
*hash2 = b;
}
// do the whole hash in one call
void spookyhash128(const void* message, size_t length, uint64_t* hash1,
uint64_t* hash2) {
if (length < sc_buf_size) {
spookyhash_short(message, length, hash1, hash2);
return;
}
uint64_t h0, h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, h11;
uint64_t buf[12]; // sc_num_vars
size_t remainder;
uint64_t* end;
union {
const uint8_t* p8;
uint64_t* p64;
size_t i;
} u;
h0 = h3 = h6 = h9 = *hash1;
h1 = h4 = h7 = h10 = *hash2;
h2 = h5 = h8 = h11 = sc_const;
u.p8 = (const uint8_t*)message;
end = u.p64 + (length / sc_block_size) * sc_num_vars;
// handle all whole sc_block_size blocks of bytes
if (ALLOW_UNALIGNED_READS || ((u.i & 0x7) == 0)) {
while (u.p64 < end) {
spookyhash_mix(u.p64, &h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, &h8, &h9,
&h10, &h11);
u.p64 += sc_num_vars;
}
} else {
while (u.p64 < end) {
memcpy(buf, u.p64, sc_block_size);
spookyhash_mix(buf, &h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, &h8, &h9,
&h10, &h11);
u.p64 += sc_num_vars;
}
}
// handle the last partial block of sc_block_size bytes
remainder = (length - ((const uint8_t*)end - (const uint8_t*)message));
memcpy(buf, end, remainder);
memset(((uint8_t*)buf) + remainder, 0, sc_block_size - remainder);
((uint8_t*)buf)[sc_block_size - 1] = remainder;
// do some final mixing
spookyhash_end(buf, &h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, &h8, &h9, &h10, &h11);
*hash1 = h0;
*hash2 = h1;
}
// init spooky state
void spookyhash_init(uint64_t seed1, uint64_t seed2, spooky_state* state) {
state->length = 0;
state->remainder = 0;
state->vars[0] = seed1;
state->vars[1] = seed2;
}
// add a message fragment to the state
void spookyhash_update(const void* message, size_t length,
spooky_state* state) {
uint64_t h0, h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, h11;
size_t new_length = length + state->remainder;
uint8_t remainder;
const uint64_t* end;
union {
const uint8_t* p8;
uint64_t* p64;
size_t i;
} u;
// is this message fragment too short, if it is, stuff it away
if (new_length < sc_buf_size) {
memcpy(&((uint8_t*)state->data)[state->remainder], message, length);
state->length = length + state->length;
state->remainder = (uint8_t)new_length;
return;
}
// init the variables
if (state->length < sc_buf_size) {
h0 = h3 = h6 = h9 = state->vars[0];
h1 = h4 = h7 = h10 = state->vars[1];
h2 = h5 = h8 = h11 = sc_const;
} else {
h0 = state->vars[0];
h1 = state->vars[1];
h2 = state->vars[2];
h3 = state->vars[3];
h4 = state->vars[4];
h5 = state->vars[5];
h6 = state->vars[6];
h7 = state->vars[7];
h8 = state->vars[8];
h9 = state->vars[9];
h10 = state->vars[10];
h11 = state->vars[11];
}
state->length = length + state->length;
// if we've got anything stuffed away, use it now
if (state->remainder) {
uint8_t prefix = sc_buf_size - state->remainder;
memcpy(&(((uint8_t*)state->data)[state->remainder]), message, prefix);
u.p64 = state->data;
spookyhash_mix(u.p64, &h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, &h8, &h9,
&h10, &h11);
spookyhash_mix(&u.p64[sc_num_vars], &h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7,
&h8, &h9, &h10, &h11);
u.p8 = ((const uint8_t*)message) + prefix;
length -= prefix;
} else {
u.p8 = (const uint8_t*)message;
}
// handle all whole blocks of sc_block_size bytes
end = u.p64 + (length / sc_block_size) * sc_num_vars;
remainder = (uint8_t)(length - ((const uint8_t*)end - u.p8));
if (ALLOW_UNALIGNED_READS || (u.i & 0x7) == 0) {
while (u.p64 < end) {
spookyhash_mix(u.p64, &h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, &h8, &h9,
&h10, &h11);
u.p64 += sc_num_vars;
}
} else {
while (u.p64 < end) {
memcpy(state->data, u.p8, sc_block_size);
spookyhash_mix(state->data, &h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, &h8,
&h9, &h10, &h11);
u.p64 += sc_num_vars;
}
}
// stuff away the last few bytes
state->remainder = remainder;
memcpy(state->data, end, remainder);
// stuff away the variables
state->vars[0] = h0;
state->vars[1] = h1;
state->vars[2] = h2;
state->vars[3] = h3;
state->vars[4] = h4;
state->vars[5] = h5;
state->vars[6] = h6;
state->vars[7] = h7;
state->vars[8] = h8;
state->vars[9] = h9;
state->vars[10] = h10;
state->vars[11] = h11;
}
// report the hash for the concatenation of all message fragments so far
void spookyhash_final(uint64_t* hash1, uint64_t* hash2, spooky_state* state) {
// init the variables
if (state->length < sc_buf_size) {
*hash1 = state->vars[0];
*hash2 = state->vars[1];
spookyhash_short(state->data, state->length, hash1, hash2);
return;
}
const uint64_t* data = (const uint64_t*)state->data;
uint8_t remainder = state->remainder;
uint64_t h0 = state->vars[0];
uint64_t h1 = state->vars[1];
uint64_t h2 = state->vars[2];
uint64_t h3 = state->vars[3];
uint64_t h4 = state->vars[4];
uint64_t h5 = state->vars[5];
uint64_t h6 = state->vars[6];
uint64_t h7 = state->vars[7];
uint64_t h8 = state->vars[8];
uint64_t h9 = state->vars[9];
uint64_t h10 = state->vars[10];
uint64_t h11 = state->vars[11];
if (remainder >= sc_block_size) {
// state->data can contain two blocks; handle any whole first block
spookyhash_mix(data, &h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, &h8, &h9, &h10,
&h11);
data += sc_num_vars;
remainder -= sc_block_size;
}
// mix in the last partial block, and the length mod sc_block_size
memset(&((uint8_t*)data)[remainder], 0, sc_block_size - remainder);
((uint8_t*)data)[sc_block_size - 1] = remainder;
// do some final mixing
spookyhash_end(data, &h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, &h8, &h9, &h10, &h11);
*hash1 = h0;
*hash2 = h1;
}