jenkins_hash.c revision 218965
1123445Sjkoshy#ifndef __LIBKERN_JENKINS_H__
2208291Suqs#define __LIBKERN_JENKINS_H__
3123445Sjkoshy/*
4123445Sjkoshy * Taken from http://burtleburtle.net/bob/c/lookup3.c
5123445Sjkoshy * $FreeBSD: head/sys/libkern/jenkins.h 218965 2011-02-23 09:22:33Z brucec $
6123445Sjkoshy */
7123445Sjkoshy
8123445Sjkoshy/*
9123445Sjkoshy-------------------------------------------------------------------------------
10123445Sjkoshy  lookup3.c, by Bob Jenkins, May 2006, Public Domain.
11123445Sjkoshy
12123445Sjkoshy  These are functions for producing 32-bit hashes for hash table lookup.
13123445Sjkoshy  hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
14123445Sjkoshy  are externally useful functions.  Routines to test the hash are included
15123445Sjkoshy  if SELF_TEST is defined.  You can use this free for any purpose.  It's in
16123445Sjkoshy  the public domain.  It has no warranty.
17123445Sjkoshy
18123445Sjkoshy  You probably want to use hashlittle().  hashlittle() and hashbig()
19123445Sjkoshy  hash byte arrays.  hashlittle() is faster than hashbig() on
20123445Sjkoshy  little-endian machines.  Intel and AMD are little-endian machines.
21123445Sjkoshy  On second thought, you probably want hashlittle2(), which is identical to
22123445Sjkoshy  hashlittle() except it returns two 32-bit hashes for the price of one.
23123445Sjkoshy  You could implement hashbig2() if you wanted but I haven't bothered here.
24123445Sjkoshy
25123445Sjkoshy  If you want to find a hash of, say, exactly 7 integers, do
26123445Sjkoshy    a = i1;  b = i2;  c = i3;
27123445Sjkoshy    mix(a,b,c);
28123445Sjkoshy    a += i4; b += i5; c += i6;
29131683Sru    mix(a,b,c);
30123445Sjkoshy    a += i7;
31206622Suqs    final(a,b,c);
32123445Sjkoshy  then use c as the hash value.  If you have a variable length array of
33123445Sjkoshy  4-byte integers to hash, use hashword().  If you have a byte array (like
34123445Sjkoshy  a character string), use hashlittle().  If you have several byte arrays, or
35123445Sjkoshy  a mix of things, see the comments above hashlittle().
36123445Sjkoshy
37123445Sjkoshy  Why is this so big?  I read 12 bytes at a time into 3 4-byte integers,
38123445Sjkoshy  then mix those integers.  This is fast (you can do a lot more thorough
39123445Sjkoshy  mixing with 12*3 instructions on 3 integers than you can with 3 instructions
40123445Sjkoshy  on 1 byte), but shoehorning those bytes into integers efficiently is messy.
41131683Sru-------------------------------------------------------------------------------
42123445Sjkoshy*/
43123445Sjkoshy
44123445Sjkoshy#define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
45123445Sjkoshy
46123445Sjkoshy/*
47123445Sjkoshy-------------------------------------------------------------------------------
48123445Sjkoshymix -- mix 3 32-bit values reversibly.
49131683Sru
50123445SjkoshyThis is reversible, so any information in (a,b,c) before mix() is
51131683Srustill in (a,b,c) after mix().
52131683Sru
53131683SruIf four pairs of (a,b,c) inputs are run through mix(), or through
54123445Sjkoshymix() in reverse, there are at least 32 bits of the output that
55123445Sjkoshyare sometimes the same for one pair and different for another pair.
56123445SjkoshyThis was tested for:
57123445Sjkoshy* pairs that differed by one bit, by two bits, in any combination
58123445Sjkoshy  of top bits of (a,b,c), or in any combination of bottom bits of
59123445Sjkoshy  (a,b,c).
60123445Sjkoshy* "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
61123445Sjkoshy  the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
62123445Sjkoshy  is commonly produced by subtraction) look like a single 1-bit
63131683Sru  difference.
64131683Sru* the base values were pseudorandom, all zero but one bit set, or
65123445Sjkoshy  all zero plus a counter that starts at zero.
66123445Sjkoshy
67131683SruSome k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
68123445Sjkoshysatisfy this are
69123445Sjkoshy    4  6  8 16 19  4
70123445Sjkoshy    9 15  3 18 27 15
71130843Smpp   14  9  3  7 17  3
72123445SjkoshyWell, "9 15 3 18 27 15" didn't quite get 32 bits diffing
73123445Sjkoshyfor "differ" defined as + with a one-bit base and a two-bit delta.  I
74123445Sjkoshyused http://burtleburtle.net/bob/hash/avalanche.html to choose
75123445Sjkoshythe operations, constants, and arrangements of the variables.
76123445Sjkoshy
77123445SjkoshyThis does not achieve avalanche.  There are input bits of (a,b,c)
78123445Sjkoshythat fail to affect some output bits of (a,b,c), especially of a.  The
79123445Sjkoshymost thoroughly mixed value is c, but it doesn't really even achieve
80123445Sjkoshyavalanche in c.
81123445Sjkoshy
82131683SruThis allows some parallelism.  Read-after-writes are good at doubling
83the number of bits affected, so the goal of mixing pulls in the opposite
84direction as the goal of parallelism.  I did what I could.  Rotates
85seem to cost as much as shifts on every machine I could lay my hands
86on, and rotates are much kinder to the top and bottom bits, so I used
87rotates.
88-------------------------------------------------------------------------------
89*/
90#define mix(a,b,c) \
91{ \
92  a -= c;  a ^= rot(c, 4);  c += b; \
93  b -= a;  b ^= rot(a, 6);  a += c; \
94  c -= b;  c ^= rot(b, 8);  b += a; \
95  a -= c;  a ^= rot(c,16);  c += b; \
96  b -= a;  b ^= rot(a,19);  a += c; \
97  c -= b;  c ^= rot(b, 4);  b += a; \
98}
99
100/*
101-------------------------------------------------------------------------------
102final -- final mixing of 3 32-bit values (a,b,c) into c
103
104Pairs of (a,b,c) values differing in only a few bits will usually
105produce values of c that look totally different.  This was tested for
106* pairs that differed by one bit, by two bits, in any combination
107  of top bits of (a,b,c), or in any combination of bottom bits of
108  (a,b,c).
109* "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
110  the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
111  is commonly produced by subtraction) look like a single 1-bit
112  difference.
113* the base values were pseudorandom, all zero but one bit set, or
114  all zero plus a counter that starts at zero.
115
116These constants passed:
117 14 11 25 16 4 14 24
118 12 14 25 16 4 14 24
119and these came close:
120  4  8 15 26 3 22 24
121 10  8 15 26 3 22 24
122 11  8 15 26 3 22 24
123-------------------------------------------------------------------------------
124*/
125#define final(a,b,c) \
126{ \
127  c ^= b; c -= rot(b,14); \
128  a ^= c; a -= rot(c,11); \
129  b ^= a; b -= rot(a,25); \
130  c ^= b; c -= rot(b,16); \
131  a ^= c; a -= rot(c,4);  \
132  b ^= a; b -= rot(a,14); \
133  c ^= b; c -= rot(b,24); \
134}
135
136/*
137--------------------------------------------------------------------
138 This works on all machines.  To be useful, it requires
139 -- that the key be an array of uint32_t's, and
140 -- that the length be the number of uint32_t's in the key
141
142 The function hashword() is identical to hashlittle() on little-endian
143 machines, and identical to hashbig() on big-endian machines,
144 except that the length has to be measured in uint32_ts rather than in
145 bytes.  hashlittle() is more complicated than hashword() only because
146 hashlittle() has to dance around fitting the key bytes into registers.
147--------------------------------------------------------------------
148*/
149static uint32_t
150jenkins_hashword(
151                const uint32_t *k,  /* the key, an array of uint32_t values */
152                size_t length,      /* the length of the key, in uint32_ts */
153                uint32_t initval    /* the previous hash, or an arbitrary value */
154)
155{
156  uint32_t a,b,c;
157
158  /* Set up the internal state */
159  a = b = c = 0xdeadbeef + (((uint32_t)length)<<2) + initval;
160
161  /*------------------------------------------------- handle most of the key */
162  while (length > 3)
163  {
164    a += k[0];
165    b += k[1];
166    c += k[2];
167    mix(a,b,c);
168    length -= 3;
169    k += 3;
170  }
171
172  /*------------------------------------------- handle the last 3 uint32_t's */
173  switch(length)                     /* all the case statements fall through */
174  {
175  case 3 : c+=k[2];
176  case 2 : b+=k[1];
177  case 1 : a+=k[0];
178    final(a,b,c);
179  case 0:     /* case 0: nothing left to add */
180    break;
181  }
182  /*------------------------------------------------------ report the result */
183  return c;
184}
185#endif
186