InfiniSQL  v0.1.2-alpha
Massive Scale Transaction Processing
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
spooky.h
Go to the documentation of this file.
1 //
2 // SpookyHash: a 128-bit noncryptographic hash function
3 // By Bob Jenkins, public domain
4 // Oct 31 2010: alpha, framework + SpookyHash::Mix appears right
5 // Oct 31 2011: alpha again, Mix only good to 2^^69 but rest appears right
6 // Dec 31 2011: beta, improved Mix, tested it for 2-bit deltas
7 // Feb 2 2012: production, same bits as beta
8 // Feb 5 2012: adjusted definitions of uint* to be more portable
9 // Mar 30 2012: 3 bytes/cycle, not 4. Alpha was 4 but wasn't thorough enough.
10 //
11 // Up to 3 bytes/cycle for long messages. Reasonably fast for short messages.
12 // All 1 or 2 bit deltas achieve avalanche within 1% bias per output bit.
13 //
14 // This was developed for and tested on 64-bit x86-compatible processors.
15 // It assumes the processor is little-endian. There is a macro
16 // controlling whether unaligned reads are allowed (by default they are).
17 // This should be an equally good hash on big-endian machines, but it will
18 // compute different results on them than on little-endian machines.
19 //
20 // Google's CityHash has similar specs to SpookyHash, and CityHash is faster
21 // on some platforms. MD4 and MD5 also have similar specs, but they are orders
22 // of magnitude slower. CRCs are two or more times slower, but unlike
23 // SpookyHash, they have nice math for combining the CRCs of pieces to form
24 // the CRCs of wholes. There are also cryptographic hashes, but those are even
25 // slower than MD5.
26 //
27 
36 #ifndef INFINISQLSPOOKY_H
37 #define INFINISQLSPOOKY_H
38 
39 #include <stddef.h>
40 
41 #ifdef _MSC_VER
42 # define INLINE __forceinline
43 typedef unsigned __int64 uint64;
44 typedef unsigned __int32 uint32;
45 typedef unsigned __int16 uint16;
46 typedef unsigned __int8 uint8;
47 #else
48 # include <stdint.h>
49 # define INLINE inline
50 typedef uint64_t uint64;
51 typedef uint32_t uint32;
52 typedef uint16_t uint16;
53 typedef uint8_t uint8;
54 #endif
55 
56 
58 {
59 public:
60  //
61  // SpookyHash: hash a single message in one call, produce 128-bit output
62  //
63  static void Hash128(
64  const void *message, // message to hash
65  size_t length, // length of message in bytes
66  uint64 *hash1, // in/out: in seed 1, out hash value 1
67  uint64 *hash2); // in/out: in seed 2, out hash value 2
68 
69  //
70  // Hash64: hash a single message in one call, return 64-bit output
71  //
72  static uint64 Hash64(
73  const void *message, // message to hash
74  size_t length, // length of message in bytes
75  uint64 seed) // seed
76  {
77  uint64 hash1 = seed;
78  Hash128(message, length, &hash1, &seed);
79  return hash1;
80  }
81 
82  //
83  // Hash32: hash a single message in one call, produce 32-bit output
84  //
85  static uint32 Hash32(
86  const void *message, // message to hash
87  size_t length, // length of message in bytes
88  uint32 seed) // seed
89  {
90  uint64 hash1 = seed, hash2 = seed;
91  Hash128(message, length, &hash1, &hash2);
92  return (uint32)hash1;
93  }
94 
95  //
96  // Init: initialize the context of a SpookyHash
97  //
98  void Init(
99  uint64 seed1, // any 64-bit value will do, including 0
100  uint64 seed2); // different seeds produce independent hashes
101 
102  //
103  // Update: add a piece of a message to a SpookyHash state
104  //
105  void Update(
106  const void *message, // message fragment
107  size_t length); // length of message fragment in bytes
108 
109 
110  //
111  // Final: compute the hash for the current SpookyHash state
112  //
113  // This does not modify the state; you can keep updating it afterward
114  //
115  // The result is the same as if SpookyHash() had been called with
116  // all the pieces concatenated into one message.
117  //
118  void Final(
119  uint64 *hash1, // out only: first 64 bits of hash value.
120  uint64 *hash2); // out only: second 64 bits of hash value.
121 
122  //
123  // left rotate a 64-bit value by k bytes
124  //
125  static INLINE uint64 Rot64(uint64 x, int k)
126  {
127  return (x << k) | (x >> (64 - k));
128  }
129 
130  //
131  // This is used if the input is 96 bytes long or longer.
132  //
133  // The internal state is fully overwritten every 96 bytes.
134  // Every input bit appears to cause at least 128 bits of entropy
135  // before 96 other bytes are combined, when run forward or backward
136  // For every input bit,
137  // Two inputs differing in just that input bit
138  // Where "differ" means xor or subtraction
139  // And the base value is random
140  // When run forward or backwards one Mix
141  // I tried 3 pairs of each; they all differed by at least 212 bits.
142  //
143  static INLINE void Mix(
144  const uint64 *data,
145  uint64 &s0, uint64 &s1, uint64 &s2, uint64 &s3,
146  uint64 &s4, uint64 &s5, uint64 &s6, uint64 &s7,
147  uint64 &s8, uint64 &s9, uint64 &s10,uint64 &s11)
148  {
149  s0 += data[0];
150  s2 ^= s10;
151  s11 ^= s0;
152  s0 = Rot64(s0,11);
153  s11 += s1;
154  s1 += data[1];
155  s3 ^= s11;
156  s0 ^= s1;
157  s1 = Rot64(s1,32);
158  s0 += s2;
159  s2 += data[2];
160  s4 ^= s0;
161  s1 ^= s2;
162  s2 = Rot64(s2,43);
163  s1 += s3;
164  s3 += data[3];
165  s5 ^= s1;
166  s2 ^= s3;
167  s3 = Rot64(s3,31);
168  s2 += s4;
169  s4 += data[4];
170  s6 ^= s2;
171  s3 ^= s4;
172  s4 = Rot64(s4,17);
173  s3 += s5;
174  s5 += data[5];
175  s7 ^= s3;
176  s4 ^= s5;
177  s5 = Rot64(s5,28);
178  s4 += s6;
179  s6 += data[6];
180  s8 ^= s4;
181  s5 ^= s6;
182  s6 = Rot64(s6,39);
183  s5 += s7;
184  s7 += data[7];
185  s9 ^= s5;
186  s6 ^= s7;
187  s7 = Rot64(s7,57);
188  s6 += s8;
189  s8 += data[8];
190  s10 ^= s6;
191  s7 ^= s8;
192  s8 = Rot64(s8,55);
193  s7 += s9;
194  s9 += data[9];
195  s11 ^= s7;
196  s8 ^= s9;
197  s9 = Rot64(s9,54);
198  s8 += s10;
199  s10 += data[10];
200  s0 ^= s8;
201  s9 ^= s10;
202  s10 = Rot64(s10,22);
203  s9 += s11;
204  s11 += data[11];
205  s1 ^= s9;
206  s10 ^= s11;
207  s11 = Rot64(s11,46);
208  s10 += s0;
209  }
210 
211  //
212  // Mix all 12 inputs together so that h0, h1 are a hash of them all.
213  //
214  // For two inputs differing in just the input bits
215  // Where "differ" means xor or subtraction
216  // And the base value is random, or a counting value starting at that bit
217  // The final result will have each bit of h0, h1 flip
218  // For every input bit,
219  // with probability 50 +- .3%
220  // For every pair of input bits,
221  // with probability 50 +- 3%
222  //
223  // This does not rely on the last Mix() call having already mixed some.
224  // Two iterations was almost good enough for a 64-bit result, but a
225  // 128-bit result is reported, so End() does three iterations.
226  //
227  static INLINE void EndPartial(
228  uint64 &h0, uint64 &h1, uint64 &h2, uint64 &h3,
229  uint64 &h4, uint64 &h5, uint64 &h6, uint64 &h7,
230  uint64 &h8, uint64 &h9, uint64 &h10,uint64 &h11)
231  {
232  h11+= h1;
233  h2 ^= h11;
234  h1 = Rot64(h1,44);
235  h0 += h2;
236  h3 ^= h0;
237  h2 = Rot64(h2,15);
238  h1 += h3;
239  h4 ^= h1;
240  h3 = Rot64(h3,34);
241  h2 += h4;
242  h5 ^= h2;
243  h4 = Rot64(h4,21);
244  h3 += h5;
245  h6 ^= h3;
246  h5 = Rot64(h5,38);
247  h4 += h6;
248  h7 ^= h4;
249  h6 = Rot64(h6,33);
250  h5 += h7;
251  h8 ^= h5;
252  h7 = Rot64(h7,10);
253  h6 += h8;
254  h9 ^= h6;
255  h8 = Rot64(h8,13);
256  h7 += h9;
257  h10^= h7;
258  h9 = Rot64(h9,38);
259  h8 += h10;
260  h11^= h8;
261  h10= Rot64(h10,53);
262  h9 += h11;
263  h0 ^= h9;
264  h11= Rot64(h11,42);
265  h10+= h0;
266  h1 ^= h10;
267  h0 = Rot64(h0,54);
268  }
269 
270  static INLINE void End(
271  uint64 &h0, uint64 &h1, uint64 &h2, uint64 &h3,
272  uint64 &h4, uint64 &h5, uint64 &h6, uint64 &h7,
273  uint64 &h8, uint64 &h9, uint64 &h10,uint64 &h11)
274  {
275  EndPartial(h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
276  EndPartial(h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
277  EndPartial(h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
278  }
279 
280  //
281  // The goal is for each bit of the input to expand into 128 bits of
282  // apparent entropy before it is fully overwritten.
283  // n trials both set and cleared at least m bits of h0 h1 h2 h3
284  // n: 2 m: 29
285  // n: 3 m: 46
286  // n: 4 m: 57
287  // n: 5 m: 107
288  // n: 6 m: 146
289  // n: 7 m: 152
290  // when run forwards or backwards
291  // for all 1-bit and 2-bit diffs
292  // with diffs defined by either xor or subtraction
293  // with a base of all zeros plus a counter, or plus another bit, or random
294  //
295  static INLINE void ShortMix(uint64 &h0, uint64 &h1, uint64 &h2, uint64 &h3)
296  {
297  h2 = Rot64(h2,50);
298  h2 += h3;
299  h0 ^= h2;
300  h3 = Rot64(h3,52);
301  h3 += h0;
302  h1 ^= h3;
303  h0 = Rot64(h0,30);
304  h0 += h1;
305  h2 ^= h0;
306  h1 = Rot64(h1,41);
307  h1 += h2;
308  h3 ^= h1;
309  h2 = Rot64(h2,54);
310  h2 += h3;
311  h0 ^= h2;
312  h3 = Rot64(h3,48);
313  h3 += h0;
314  h1 ^= h3;
315  h0 = Rot64(h0,38);
316  h0 += h1;
317  h2 ^= h0;
318  h1 = Rot64(h1,37);
319  h1 += h2;
320  h3 ^= h1;
321  h2 = Rot64(h2,62);
322  h2 += h3;
323  h0 ^= h2;
324  h3 = Rot64(h3,34);
325  h3 += h0;
326  h1 ^= h3;
327  h0 = Rot64(h0,5);
328  h0 += h1;
329  h2 ^= h0;
330  h1 = Rot64(h1,36);
331  h1 += h2;
332  h3 ^= h1;
333  }
334 
335  //
336  // Mix all 4 inputs together so that h0, h1 are a hash of them all.
337  //
338  // For two inputs differing in just the input bits
339  // Where "differ" means xor or subtraction
340  // And the base value is random, or a counting value starting at that bit
341  // The final result will have each bit of h0, h1 flip
342  // For every input bit,
343  // with probability 50 +- .3% (it is probably better than that)
344  // For every pair of input bits,
345  // with probability 50 +- .75% (the worst case is approximately that)
346  //
347  static INLINE void ShortEnd(uint64 &h0, uint64 &h1, uint64 &h2, uint64 &h3)
348  {
349  h3 ^= h2;
350  h2 = Rot64(h2,15);
351  h3 += h2;
352  h0 ^= h3;
353  h3 = Rot64(h3,52);
354  h0 += h3;
355  h1 ^= h0;
356  h0 = Rot64(h0,26);
357  h1 += h0;
358  h2 ^= h1;
359  h1 = Rot64(h1,51);
360  h2 += h1;
361  h3 ^= h2;
362  h2 = Rot64(h2,28);
363  h3 += h2;
364  h0 ^= h3;
365  h3 = Rot64(h3,9);
366  h0 += h3;
367  h1 ^= h0;
368  h0 = Rot64(h0,47);
369  h1 += h0;
370  h2 ^= h1;
371  h1 = Rot64(h1,54);
372  h2 += h1;
373  h3 ^= h2;
374  h2 = Rot64(h2,32);
375  h3 += h2;
376  h0 ^= h3;
377  h3 = Rot64(h3,25);
378  h0 += h3;
379  h1 ^= h0;
380  h0 = Rot64(h0,63);
381  h1 += h0;
382  }
383 
384 private:
385 
386  //
387  // Short is used for messages under 192 bytes in length
388  // Short has a low startup cost, the normal mode is good for long
389  // keys, the cost crossover is at about 192 bytes. The two modes were
390  // held to the same quality bar.
391  //
392  static void Short(
393  const void *message,
394  size_t length,
395  uint64 *hash1,
396  uint64 *hash2);
397 
398  // number of uint64's in internal state
399  static const size_t sc_numVars = 12;
400 
401  // size of the internal state
402  static const size_t sc_blockSize = sc_numVars*8;
403 
404  // size of buffer of unhashed data, in bytes
405  static const size_t sc_bufSize = 2*sc_blockSize;
406 
407  //
408  // sc_const: a constant which:
409  // * is not zero
410  // * is odd
411  // * is a not-very-regular mix of 1's and 0's
412  // * does not need any other special mathematical properties
413  //
414  static const uint64 sc_const = 0xdeadbeefdeadbeefLL;
415 
416  uint64 m_data[2*sc_numVars]; // unhashed data, for partial messages
417  uint64 m_state[sc_numVars]; // internal state of the hash
418  size_t m_length; // total length of the input so far
419  uint8 m_remainder; // length of unhashed data stashed in m_data
420 };
421 
422 #endif /* INFINISQLSPOOKY_H */
423