jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | -------------------------------------------------------------------- |
| 3 | lookupa.c, by Bob Jenkins, December 1996. Same as lookup2.c |
| 4 | Use this code however you wish. Public Domain. No warranty. |
| 5 | Source is http://burtleburtle.net/bob/c/lookupa.c |
| 6 | -------------------------------------------------------------------- |
| 7 | */ |
| 8 | #ifndef STANDARD |
| 9 | /* |
| 10 | #include "standard.h" |
| 11 | */ |
| 12 | #endif |
| 13 | #ifndef LOOKUPA |
| 14 | #include "lookupa.h" |
| 15 | #endif |
| 16 | |
| 17 | /* |
| 18 | -------------------------------------------------------------------- |
| 19 | mix -- mix 3 32-bit values reversibly. |
| 20 | For every delta with one or two bit set, and the deltas of all three |
| 21 | high bits or all three low bits, whether the original value of a,b,c |
| 22 | is almost all zero or is uniformly distributed, |
| 23 | * If mix() is run forward or backward, at least 32 bits in a,b,c |
| 24 | have at least 1/4 probability of changing. |
| 25 | * If mix() is run forward, every bit of c will change between 1/3 and |
| 26 | 2/3 of the time. (Well, 22/100 and 78/100 for some 2-bit deltas.) |
| 27 | mix() was built out of 36 single-cycle latency instructions in a |
| 28 | structure that could supported 2x parallelism, like so: |
| 29 | a -= b; |
| 30 | a -= c; x = (c>>13); |
| 31 | b -= c; a ^= x; |
| 32 | b -= a; x = (a<<8); |
| 33 | c -= a; b ^= x; |
| 34 | c -= b; x = (b>>13); |
| 35 | ... |
| 36 | Unfortunately, superscalar Pentiums and Sparcs can't take advantage |
| 37 | of that parallelism. They've also turned some of those single-cycle |
| 38 | latency instructions into multi-cycle latency instructions. Still, |
| 39 | this is the fastest good hash I could find. There were about 2^^68 |
| 40 | to choose from. I only looked at a billion or so. |
| 41 | -------------------------------------------------------------------- |
| 42 | */ |
| 43 | #define mix(a,b,c) \ |
| 44 | { \ |
| 45 | a -= b; a -= c; a ^= (c>>13); \ |
| 46 | b -= c; b -= a; b ^= (a<<8); \ |
| 47 | c -= a; c -= b; c ^= (b>>13); \ |
| 48 | a -= b; a -= c; a ^= (c>>12); \ |
| 49 | b -= c; b -= a; b ^= (a<<16); \ |
| 50 | c -= a; c -= b; c ^= (b>>5); \ |
| 51 | a -= b; a -= c; a ^= (c>>3); \ |
| 52 | b -= c; b -= a; b ^= (a<<10); \ |
| 53 | c -= a; c -= b; c ^= (b>>15); \ |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | -------------------------------------------------------------------- |
| 58 | lookup() -- hash a variable-length key into a 32-bit value |
| 59 | k : the key (the unaligned variable-length array of bytes) |
| 60 | len : the length of the key, counting by bytes |
| 61 | level : can be any 4-byte value |
| 62 | Returns a 32-bit value. Every bit of the key affects every bit of |
| 63 | the return value. Every 1-bit and 2-bit delta achieves avalanche. |
| 64 | About 6len+35 instructions. |
| 65 | |
| 66 | The best hash table sizes are powers of 2. There is no need to do |
| 67 | mod a prime (mod is sooo slow!). If you need less than 32 bits, |
| 68 | use a bitmask. For example, if you need only 10 bits, do |
| 69 | h = (h & hashmask(10)); |
| 70 | In which case, the hash table should have hashsize(10) elements. |
| 71 | |
| 72 | If you are hashing n strings (ub1 **)k, do it like this: |
| 73 | for (i=0, h=0; i<n; ++i) h = lookup( k[i], len[i], h); |
| 74 | |
| 75 | By Bob Jenkins, 1996. bob_jenkins@burtleburtle.net. You may use this |
| 76 | code any way you wish, private, educational, or commercial. |
| 77 | |
| 78 | See http://burtleburtle.net/bob/hash/evahash.html |
| 79 | Use for hash table lookup, or anything where one collision in 2^32 is |
| 80 | acceptable. Do NOT use for cryptographic purposes. |
| 81 | -------------------------------------------------------------------- |
| 82 | */ |
| 83 | |
| 84 | ub4 lookup( k, length, level) |
| 85 | register ub1 *k; /* the key */ |
| 86 | register ub4 length; /* the length of the key */ |
| 87 | register ub4 level; /* the previous hash, or an arbitrary value */ |
| 88 | { |
| 89 | register ub4 a,b,c,len; |
| 90 | |
| 91 | /* Set up the internal state */ |
| 92 | len = length; |
| 93 | a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */ |
| 94 | c = level; /* the previous hash value */ |
| 95 | |
| 96 | /*---------------------------------------- handle most of the key */ |
| 97 | while (len >= 12) |
| 98 | { |
| 99 | a += (k[0] +((ub4)k[1]<<8) +((ub4)k[2]<<16) +((ub4)k[3]<<24)); |
| 100 | b += (k[4] +((ub4)k[5]<<8) +((ub4)k[6]<<16) +((ub4)k[7]<<24)); |
| 101 | c += (k[8] +((ub4)k[9]<<8) +((ub4)k[10]<<16)+((ub4)k[11]<<24)); |
| 102 | mix(a,b,c); |
| 103 | k += 12; len -= 12; |
| 104 | } |
| 105 | |
| 106 | /*------------------------------------- handle the last 11 bytes */ |
| 107 | c += length; |
| 108 | switch(len) /* all the case statements fall through */ |
| 109 | { |
| 110 | case 11: c+=((ub4)k[10]<<24); |
| 111 | case 10: c+=((ub4)k[9]<<16); |
| 112 | case 9 : c+=((ub4)k[8]<<8); |
| 113 | /* the first byte of c is reserved for the length */ |
| 114 | case 8 : b+=((ub4)k[7]<<24); |
| 115 | case 7 : b+=((ub4)k[6]<<16); |
| 116 | case 6 : b+=((ub4)k[5]<<8); |
| 117 | case 5 : b+=k[4]; |
| 118 | case 4 : a+=((ub4)k[3]<<24); |
| 119 | case 3 : a+=((ub4)k[2]<<16); |
| 120 | case 2 : a+=((ub4)k[1]<<8); |
| 121 | case 1 : a+=k[0]; |
| 122 | /* case 0: nothing left to add */ |
| 123 | } |
| 124 | mix(a,b,c); |
| 125 | /*-------------------------------------------- report the result */ |
| 126 | return c; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | /* |
| 131 | -------------------------------------------------------------------- |
| 132 | mixc -- mixc 8 4-bit values as quickly and thoroughly as possible. |
| 133 | Repeating mix() three times achieves avalanche. |
| 134 | Repeating mix() four times eliminates all funnels and all |
| 135 | characteristics stronger than 2^{-11}. |
| 136 | -------------------------------------------------------------------- |
| 137 | */ |
| 138 | #define mixc(a,b,c,d,e,f,g,h) \ |
| 139 | { \ |
| 140 | a^=b<<11; d+=a; b+=c; \ |
| 141 | b^=c>>2; e+=b; c+=d; \ |
| 142 | c^=d<<8; f+=c; d+=e; \ |
| 143 | d^=e>>16; g+=d; e+=f; \ |
| 144 | e^=f<<10; h+=e; f+=g; \ |
| 145 | f^=g>>4; a+=f; g+=h; \ |
| 146 | g^=h<<8; b+=g; h+=a; \ |
| 147 | h^=a>>9; c+=h; a+=b; \ |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | -------------------------------------------------------------------- |
| 152 | checksum() -- hash a variable-length key into a 256-bit value |
| 153 | k : the key (the unaligned variable-length array of bytes) |
| 154 | len : the length of the key, counting by bytes |
| 155 | state : an array of CHECKSTATE 4-byte values (256 bits) |
| 156 | The state is the checksum. Every bit of the key affects every bit of |
| 157 | the state. There are no funnels. About 112+6.875len instructions. |
| 158 | |
| 159 | If you are hashing n strings (ub1 **)k, do it like this: |
| 160 | for (i=0; i<8; ++i) state[i] = 0x9e3779b9; |
| 161 | for (i=0, h=0; i<n; ++i) checksum( k[i], len[i], state); |
| 162 | |
| 163 | (c) Bob Jenkins, 1996. bob_jenkins@burtleburtle.net. You may use this |
| 164 | code any way you wish, private, educational, or commercial, as long |
| 165 | as this whole comment accompanies it. |
| 166 | |
| 167 | See http://burtleburtle.net/bob/hash/evahash.html |
| 168 | Use to detect changes between revisions of documents, assuming nobody |
| 169 | is trying to cause collisions. Do NOT use for cryptography. |
| 170 | -------------------------------------------------------------------- |
| 171 | */ |
| 172 | void checksum( k, len, state) |
| 173 | register ub1 *k; |
| 174 | register ub4 len; |
| 175 | register ub4 *state; |
| 176 | { |
| 177 | register ub4 a,b,c,d,e,f,g,h,length; |
| 178 | |
| 179 | /* Use the length and level; add in the golden ratio. */ |
| 180 | length = len; |
| 181 | a=state[0]; b=state[1]; c=state[2]; d=state[3]; |
| 182 | e=state[4]; f=state[5]; g=state[6]; h=state[7]; |
| 183 | |
| 184 | /*---------------------------------------- handle most of the key */ |
| 185 | while (len >= 32) |
| 186 | { |
| 187 | a += (k[0] +(k[1]<<8) +(k[2]<<16) +(k[3]<<24)); |
| 188 | b += (k[4] +(k[5]<<8) +(k[6]<<16) +(k[7]<<24)); |
| 189 | c += (k[8] +(k[9]<<8) +(k[10]<<16)+(k[11]<<24)); |
| 190 | d += (k[12]+(k[13]<<8)+(k[14]<<16)+(k[15]<<24)); |
| 191 | e += (k[16]+(k[17]<<8)+(k[18]<<16)+(k[19]<<24)); |
| 192 | f += (k[20]+(k[21]<<8)+(k[22]<<16)+(k[23]<<24)); |
| 193 | g += (k[24]+(k[25]<<8)+(k[26]<<16)+(k[27]<<24)); |
| 194 | h += (k[28]+(k[29]<<8)+(k[30]<<16)+(k[31]<<24)); |
| 195 | mixc(a,b,c,d,e,f,g,h); |
| 196 | mixc(a,b,c,d,e,f,g,h); |
| 197 | mixc(a,b,c,d,e,f,g,h); |
| 198 | mixc(a,b,c,d,e,f,g,h); |
| 199 | k += 32; len -= 32; |
| 200 | } |
| 201 | |
| 202 | /*------------------------------------- handle the last 31 bytes */ |
| 203 | h += length; |
| 204 | switch(len) |
| 205 | { |
| 206 | case 31: h+=(k[30]<<24); |
| 207 | case 30: h+=(k[29]<<16); |
| 208 | case 29: h+=(k[28]<<8); |
| 209 | case 28: g+=(k[27]<<24); |
| 210 | case 27: g+=(k[26]<<16); |
| 211 | case 26: g+=(k[25]<<8); |
| 212 | case 25: g+=k[24]; |
| 213 | case 24: f+=(k[23]<<24); |
| 214 | case 23: f+=(k[22]<<16); |
| 215 | case 22: f+=(k[21]<<8); |
| 216 | case 21: f+=k[20]; |
| 217 | case 20: e+=(k[19]<<24); |
| 218 | case 19: e+=(k[18]<<16); |
| 219 | case 18: e+=(k[17]<<8); |
| 220 | case 17: e+=k[16]; |
| 221 | case 16: d+=(k[15]<<24); |
| 222 | case 15: d+=(k[14]<<16); |
| 223 | case 14: d+=(k[13]<<8); |
| 224 | case 13: d+=k[12]; |
| 225 | case 12: c+=(k[11]<<24); |
| 226 | case 11: c+=(k[10]<<16); |
| 227 | case 10: c+=(k[9]<<8); |
| 228 | case 9 : c+=k[8]; |
| 229 | case 8 : b+=(k[7]<<24); |
| 230 | case 7 : b+=(k[6]<<16); |
| 231 | case 6 : b+=(k[5]<<8); |
| 232 | case 5 : b+=k[4]; |
| 233 | case 4 : a+=(k[3]<<24); |
| 234 | case 3 : a+=(k[2]<<16); |
| 235 | case 2 : a+=(k[1]<<8); |
| 236 | case 1 : a+=k[0]; |
| 237 | } |
| 238 | mixc(a,b,c,d,e,f,g,h); |
| 239 | mixc(a,b,c,d,e,f,g,h); |
| 240 | mixc(a,b,c,d,e,f,g,h); |
| 241 | mixc(a,b,c,d,e,f,g,h); |
| 242 | |
| 243 | /*-------------------------------------------- report the result */ |
| 244 | state[0]=a; state[1]=b; state[2]=c; state[3]=d; |
| 245 | state[4]=e; state[5]=f; state[6]=g; state[7]=h; |
| 246 | } |