Marlin  1.17.1
 All Classes Namespaces Functions Variables Enumerations Friends Pages
jenkinsHash.h
1 #ifndef JENKINSHASH_INCLUDED
2 #define JENKINSHASH_INCLUDED
3 
4 /*
5 
6 Original source by Bob Jenkins
7 
8 http://www.burtleburtle.net/bob/hash/doobs.html
9 
10 Hash a variable-length key into a 32-bit value
11 
12 */
13 
14 #define hashsize(n) ( 1U << (n) )
15 #define hashmask(n) ( hashsize ( n ) - 1 )
16 
17 
18 /*
19 --------------------------------------------------------------------
20 mix -- mix 3 32-bit values reversibly.
21 For every delta with one or two bits set, and the deltas of all three
22  high bits or all three low bits, whether the original value of a,b,c
23  is almost all zero or is uniformly distributed,
24 * If mix() is run forward or backward, at least 32 bits in a,b,c
25  have at least 1/4 probability of changing.
26 * If mix() is run forward, every bit of c will change between 1/3 and
27  2/3 of the time. (Well, 22/100 and 78/100 for some 2-bit deltas.)
28 mix() was built out of 36 single-cycle latency instructions in a
29  structure that could supported 2x parallelism, like so:
30  a -= b;
31  a -= c; x = (c>>13);
32  b -= c; a ^= x;
33  b -= a; x = (a<<8);
34  c -= a; b ^= x;
35  c -= b; x = (b>>13);
36  ...
37  Unfortunately, superscalar Pentiums and Sparcs can't take advantage
38  of that parallelism. They've also turned some of those single-cycle
39  latency instructions into multi-cycle latency instructions. Still,
40  this is the fastest good hash I could find. There were about 2^^68
41  to choose from. I only looked at a billion or so.
42 --------------------------------------------------------------------
43 */
44 #define mix(a,b,c) \
45 { \
46  a -= b; a -= c; a ^= (c>>13); \
47  b -= c; b -= a; b ^= (a<<8); \
48  c -= a; c -= b; c ^= (b>>13); \
49  a -= b; a -= c; a ^= (c>>12); \
50  b -= c; b -= a; b ^= (a<<16); \
51  c -= a; c -= b; c ^= (b>>5); \
52  a -= b; a -= c; a ^= (c>>3); \
53  b -= c; b -= a; b ^= (a<<10); \
54  c -= a; c -= b; c ^= (b>>15); \
55 }
56 
57 /*
58 --------------------------------------------------------------------
59 jenkins_hash() -- hash a variable-length key into a 32-bit value
60  k : the key (the unaligned variable-length array of bytes)
61  len : the length of the key, counting by bytes
62  initval : can be any 4-byte value
63 Returns a 32-bit value. Every bit of the key affects every bit of
64 the return value. Every 1-bit and 2-bit delta achieves avalanche.
65 About 6*len+35 instructions.
66 
67 The best hash table sizes are powers of 2. There is no need to do
68 mod a prime (mod is sooo slow!). If you need less than 32 bits,
69 use a bitmask. For example, if you need only 10 bits, do
70  h = (h & hashmask(10));
71 In which case, the hash table should have hashsize(10) elements.
72 
73 If you are hashing n strings (ub1 **)k, do it like this:
74  for (i=0, h=0; i<n; ++i) h = hash( k[i], len[i], h);
75 
76 By Bob Jenkins, 1996. bob_jenkins@burtleburtle.net. You may use this
77 code any way you wish, private, educational, or commercial. It's free.
78 
79 See http://burtleburtle.net/bob/hash/evahash.html
80 Use for hash table lookup, or anything where one collision in 2^^32 is
81 acceptable. Do NOT use for cryptographic purposes.
82 --------------------------------------------------------------------
83 */
84 unsigned jenkins_hash ( unsigned char *k, unsigned length, unsigned initval )
85 {
86  unsigned a, b;
87  unsigned c = initval;
88  unsigned len = length;
89 
90  a = b = 0x9e3779b9;
91 
92  while ( len >= 12 ) {
93  a += ( k[0] + ( (unsigned)k[1] << 8 )
94  + ( (unsigned)k[2] << 16 )
95  + ( (unsigned)k[3] << 24 ) );
96  b += ( k[4] + ( (unsigned)k[5] << 8 )
97  + ( (unsigned)k[6] << 16 )
98  + ( (unsigned)k[7] << 24 ) );
99  c += ( k[8] + ( (unsigned)k[9] << 8 )
100  + ( (unsigned)k[10] << 16 )
101  + ( (unsigned)k[11] << 24 ) );
102 
103  mix ( a, b, c );
104 
105  k += 12;
106  len -= 12;
107  }
108 
109  c += length;
110 
111  switch ( len ) {
112  case 11: c += ( (unsigned)k[10] << 24 );
113  case 10: c += ( (unsigned)k[9] << 16 );
114  case 9 : c += ( (unsigned)k[8] << 8 );
115  /* First byte of c reserved for length */
116  case 8 : b += ( (unsigned)k[7] << 24 );
117  case 7 : b += ( (unsigned)k[6] << 16 );
118  case 6 : b += ( (unsigned)k[5] << 8 );
119  case 5 : b += k[4];
120  case 4 : a += ( (unsigned)k[3] << 24 );
121  case 3 : a += ( (unsigned)k[2] << 16 );
122  case 2 : a += ( (unsigned)k[1] << 8 );
123  case 1 : a += k[0];
124  }
125 
126  mix ( a, b, c );
127 
128  return c;
129 }
130 
131 #endif