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