/*1* Taken from http://burtleburtle.net/bob/c/lookup3.c2*/34#include <sys/hash.h>5#include <machine/endian.h>67/*8-------------------------------------------------------------------------------9lookup3.c, by Bob Jenkins, May 2006, Public Domain.1011These are functions for producing 32-bit hashes for hash table lookup.12hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()13are externally useful functions. Routines to test the hash are included14if SELF_TEST is defined. You can use this free for any purpose. It's in15the public domain. It has no warranty.1617You probably want to use hashlittle(). hashlittle() and hashbig()18hash byte arrays. hashlittle() is faster than hashbig() on19little-endian machines. Intel and AMD are little-endian machines.20On second thought, you probably want hashlittle2(), which is identical to21hashlittle() except it returns two 32-bit hashes for the price of one.22You could implement hashbig2() if you wanted but I haven't bothered here.2324If you want to find a hash of, say, exactly 7 integers, do25a = i1; b = i2; c = i3;26mix(a,b,c);27a += i4; b += i5; c += i6;28mix(a,b,c);29a += i7;30final(a,b,c);31then use c as the hash value. If you have a variable length array of324-byte integers to hash, use hashword(). If you have a byte array (like33a character string), use hashlittle(). If you have several byte arrays, or34a mix of things, see the comments above hashlittle().3536Why is this so big? I read 12 bytes at a time into 3 4-byte integers,37then mix those integers. This is fast (you can do a lot more thorough38mixing with 12*3 instructions on 3 integers than you can with 3 instructions39on 1 byte), but shoehorning those bytes into integers efficiently is messy.40-------------------------------------------------------------------------------41*/4243#define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))4445/*46-------------------------------------------------------------------------------47mix -- mix 3 32-bit values reversibly.4849This is reversible, so any information in (a,b,c) before mix() is50still in (a,b,c) after mix().5152If four pairs of (a,b,c) inputs are run through mix(), or through53mix() in reverse, there are at least 32 bits of the output that54are sometimes the same for one pair and different for another pair.55This was tested for:56* pairs that differed by one bit, by two bits, in any combination57of top bits of (a,b,c), or in any combination of bottom bits of58(a,b,c).59* "differ" is defined as +, -, ^, or ~^. For + and -, I transformed60the output delta to a Gray code (a^(a>>1)) so a string of 1's (as61is commonly produced by subtraction) look like a single 1-bit62difference.63* the base values were pseudorandom, all zero but one bit set, or64all zero plus a counter that starts at zero.6566Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that67satisfy this are684 6 8 16 19 4699 15 3 18 27 157014 9 3 7 17 371Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing72for "differ" defined as + with a one-bit base and a two-bit delta. I73used http://burtleburtle.net/bob/hash/avalanche.html to choose74the operations, constants, and arrangements of the variables.7576This does not achieve avalanche. There are input bits of (a,b,c)77that fail to affect some output bits of (a,b,c), especially of a. The78most thoroughly mixed value is c, but it doesn't really even achieve79avalanche in c.8081This allows some parallelism. Read-after-writes are good at doubling82the number of bits affected, so the goal of mixing pulls in the opposite83direction as the goal of parallelism. I did what I could. Rotates84seem to cost as much as shifts on every machine I could lay my hands85on, and rotates are much kinder to the top and bottom bits, so I used86rotates.87-------------------------------------------------------------------------------88*/89#define mix(a,b,c) \90{ \91a -= c; a ^= rot(c, 4); c += b; \92b -= a; b ^= rot(a, 6); a += c; \93c -= b; c ^= rot(b, 8); b += a; \94a -= c; a ^= rot(c,16); c += b; \95b -= a; b ^= rot(a,19); a += c; \96c -= b; c ^= rot(b, 4); b += a; \97}9899/*100-------------------------------------------------------------------------------101final -- final mixing of 3 32-bit values (a,b,c) into c102103Pairs of (a,b,c) values differing in only a few bits will usually104produce values of c that look totally different. This was tested for105* pairs that differed by one bit, by two bits, in any combination106of top bits of (a,b,c), or in any combination of bottom bits of107(a,b,c).108* "differ" is defined as +, -, ^, or ~^. For + and -, I transformed109the output delta to a Gray code (a^(a>>1)) so a string of 1's (as110is commonly produced by subtraction) look like a single 1-bit111difference.112* the base values were pseudorandom, all zero but one bit set, or113all zero plus a counter that starts at zero.114115These constants passed:11614 11 25 16 4 14 2411712 14 25 16 4 14 24118and these came close:1194 8 15 26 3 22 2412010 8 15 26 3 22 2412111 8 15 26 3 22 24122-------------------------------------------------------------------------------123*/124#define final(a,b,c) \125{ \126c ^= b; c -= rot(b,14); \127a ^= c; a -= rot(c,11); \128b ^= a; b -= rot(a,25); \129c ^= b; c -= rot(b,16); \130a ^= c; a -= rot(c,4); \131b ^= a; b -= rot(a,14); \132c ^= b; c -= rot(b,24); \133}134135/*136--------------------------------------------------------------------137This works on all machines. To be useful, it requires138-- that the key be an array of uint32_t's, and139-- that the length be the number of uint32_t's in the key140141The function hashword() is identical to hashlittle() on little-endian142machines, and identical to hashbig() on big-endian machines,143except that the length has to be measured in uint32_ts rather than in144bytes. hashlittle() is more complicated than hashword() only because145hashlittle() has to dance around fitting the key bytes into registers.146--------------------------------------------------------------------147*/148uint32_t jenkins_hash32(149const uint32_t *k, /* the key, an array of uint32_t values */150size_t length, /* the length of the key, in uint32_ts */151uint32_t initval) /* the previous hash, or an arbitrary value */152{153uint32_t a,b,c;154155/* Set up the internal state */156a = b = c = 0xdeadbeef + (((uint32_t)length)<<2) + initval;157158/*------------------------------------------------- handle most of the key */159while (length > 3)160{161a += k[0];162b += k[1];163c += k[2];164mix(a,b,c);165length -= 3;166k += 3;167}168169/*------------------------------------------- handle the last 3 uint32_t's */170switch(length) /* all the case statements fall through */171{172case 3 : c+=k[2];173case 2 : b+=k[1];174case 1 : a+=k[0];175final(a,b,c);176case 0: /* case 0: nothing left to add */177break;178}179/*------------------------------------------------------ report the result */180return c;181}182183#if BYTE_ORDER == LITTLE_ENDIAN184/*185-------------------------------------------------------------------------------186hashlittle() -- hash a variable-length key into a 32-bit value187k : the key (the unaligned variable-length array of bytes)188length : the length of the key, counting by bytes189initval : can be any 4-byte value190Returns a 32-bit value. Every bit of the key affects every bit of191the return value. Two keys differing by one or two bits will have192totally different hash values.193194The best hash table sizes are powers of 2. There is no need to do195mod a prime (mod is sooo slow!). If you need less than 32 bits,196use a bitmask. For example, if you need only 10 bits, do197h = (h & hashmask(10));198In which case, the hash table should have hashsize(10) elements.199200If you are hashing n strings (uint8_t **)k, do it like this:201for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);202203By Bob Jenkins, 2006. [email protected]. You may use this204code any way you wish, private, educational, or commercial. It's free.205206Use for hash table lookup, or anything where one collision in 2^^32 is207acceptable. Do NOT use for cryptographic purposes.208-------------------------------------------------------------------------------209*/210211uint32_t jenkins_hash( const void *key, size_t length, uint32_t initval)212{213uint32_t a,b,c; /* internal state */214union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */215216/* Set up the internal state */217a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;218219u.ptr = key;220if ((u.i & 0x3) == 0) {221const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */222223/*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */224while (length > 12)225{226a += k[0];227b += k[1];228c += k[2];229mix(a,b,c);230length -= 12;231k += 3;232}233234/*----------------------------- handle the last (probably partial) block */235/*236* "k[2]&0xffffff" actually reads beyond the end of the string, but237* then masks off the part it's not allowed to read. Because the238* string is aligned, the masked-off tail is in the same word as the239* rest of the string. Every machine with memory protection I've seen240* does it on word boundaries, so is OK with this. But VALGRIND will241* still catch it and complain. The masking trick does make the hash242* noticeably faster for short strings (like English words).243*/244245switch(length)246{247case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;248case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;249case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;250case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;251case 8 : b+=k[1]; a+=k[0]; break;252case 7 : b+=k[1]&0xffffff; a+=k[0]; break;253case 6 : b+=k[1]&0xffff; a+=k[0]; break;254case 5 : b+=k[1]&0xff; a+=k[0]; break;255case 4 : a+=k[0]; break;256case 3 : a+=k[0]&0xffffff; break;257case 2 : a+=k[0]&0xffff; break;258case 1 : a+=k[0]&0xff; break;259case 0 : return c; /* zero length strings require no mixing */260}261262} else if ((u.i & 0x1) == 0) {263const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */264const uint8_t *k8;265266/*--------------- all but last block: aligned reads and different mixing */267while (length > 12)268{269a += k[0] + (((uint32_t)k[1])<<16);270b += k[2] + (((uint32_t)k[3])<<16);271c += k[4] + (((uint32_t)k[5])<<16);272mix(a,b,c);273length -= 12;274k += 6;275}276277/*----------------------------- handle the last (probably partial) block */278k8 = (const uint8_t *)k;279switch(length)280{281case 12: c+=k[4]+(((uint32_t)k[5])<<16);282b+=k[2]+(((uint32_t)k[3])<<16);283a+=k[0]+(((uint32_t)k[1])<<16);284break;285case 11: c+=((uint32_t)k8[10])<<16; /* fall through */286case 10: c+=k[4];287b+=k[2]+(((uint32_t)k[3])<<16);288a+=k[0]+(((uint32_t)k[1])<<16);289break;290case 9 : c+=k8[8]; /* fall through */291case 8 : b+=k[2]+(((uint32_t)k[3])<<16);292a+=k[0]+(((uint32_t)k[1])<<16);293break;294case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */295case 6 : b+=k[2];296a+=k[0]+(((uint32_t)k[1])<<16);297break;298case 5 : b+=k8[4]; /* fall through */299case 4 : a+=k[0]+(((uint32_t)k[1])<<16);300break;301case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */302case 2 : a+=k[0];303break;304case 1 : a+=k8[0];305break;306case 0 : return c; /* zero length requires no mixing */307}308309} else { /* need to read the key one byte at a time */310const uint8_t *k = (const uint8_t *)key;311312/*--------------- all but the last block: affect some 32 bits of (a,b,c) */313while (length > 12)314{315a += k[0];316a += ((uint32_t)k[1])<<8;317a += ((uint32_t)k[2])<<16;318a += ((uint32_t)k[3])<<24;319b += k[4];320b += ((uint32_t)k[5])<<8;321b += ((uint32_t)k[6])<<16;322b += ((uint32_t)k[7])<<24;323c += k[8];324c += ((uint32_t)k[9])<<8;325c += ((uint32_t)k[10])<<16;326c += ((uint32_t)k[11])<<24;327mix(a,b,c);328length -= 12;329k += 12;330}331332/*-------------------------------- last block: affect all 32 bits of (c) */333switch(length) /* all the case statements fall through */334{335case 12: c+=((uint32_t)k[11])<<24;336case 11: c+=((uint32_t)k[10])<<16;337case 10: c+=((uint32_t)k[9])<<8;338case 9 : c+=k[8];339case 8 : b+=((uint32_t)k[7])<<24;340case 7 : b+=((uint32_t)k[6])<<16;341case 6 : b+=((uint32_t)k[5])<<8;342case 5 : b+=k[4];343case 4 : a+=((uint32_t)k[3])<<24;344case 3 : a+=((uint32_t)k[2])<<16;345case 2 : a+=((uint32_t)k[1])<<8;346case 1 : a+=k[0];347break;348case 0 : return c;349}350}351352final(a,b,c);353return c;354}355356#else /* !(BYTE_ORDER == LITTLE_ENDIAN) */357358/*359* hashbig():360* This is the same as hashword() on big-endian machines. It is different361* from hashlittle() on all machines. hashbig() takes advantage of362* big-endian byte ordering.363*/364uint32_t jenkins_hash( const void *key, size_t length, uint32_t initval)365{366uint32_t a,b,c;367union { const void *ptr; size_t i; } u; /* to cast key to (size_t) happily */368369/* Set up the internal state */370a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;371372u.ptr = key;373if ((u.i & 0x3) == 0) {374const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */375376/*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */377while (length > 12)378{379a += k[0];380b += k[1];381c += k[2];382mix(a,b,c);383length -= 12;384k += 3;385}386387/*----------------------------- handle the last (probably partial) block */388/*389* "k[2]<<8" actually reads beyond the end of the string, but390* then shifts out the part it's not allowed to read. Because the391* string is aligned, the illegal read is in the same word as the392* rest of the string. Every machine with memory protection I've seen393* does it on word boundaries, so is OK with this. But VALGRIND will394* still catch it and complain. The masking trick does make the hash395* noticeably faster for short strings (like English words).396*/397398switch(length)399{400case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;401case 11: c+=k[2]&0xffffff00; b+=k[1]; a+=k[0]; break;402case 10: c+=k[2]&0xffff0000; b+=k[1]; a+=k[0]; break;403case 9 : c+=k[2]&0xff000000; b+=k[1]; a+=k[0]; break;404case 8 : b+=k[1]; a+=k[0]; break;405case 7 : b+=k[1]&0xffffff00; a+=k[0]; break;406case 6 : b+=k[1]&0xffff0000; a+=k[0]; break;407case 5 : b+=k[1]&0xff000000; a+=k[0]; break;408case 4 : a+=k[0]; break;409case 3 : a+=k[0]&0xffffff00; break;410case 2 : a+=k[0]&0xffff0000; break;411case 1 : a+=k[0]&0xff000000; break;412case 0 : return c; /* zero length strings require no mixing */413}414415} else { /* need to read the key one byte at a time */416const uint8_t *k = (const uint8_t *)key;417418/*--------------- all but the last block: affect some 32 bits of (a,b,c) */419while (length > 12)420{421a += ((uint32_t)k[0])<<24;422a += ((uint32_t)k[1])<<16;423a += ((uint32_t)k[2])<<8;424a += ((uint32_t)k[3]);425b += ((uint32_t)k[4])<<24;426b += ((uint32_t)k[5])<<16;427b += ((uint32_t)k[6])<<8;428b += ((uint32_t)k[7]);429c += ((uint32_t)k[8])<<24;430c += ((uint32_t)k[9])<<16;431c += ((uint32_t)k[10])<<8;432c += ((uint32_t)k[11]);433mix(a,b,c);434length -= 12;435k += 12;436}437438/*-------------------------------- last block: affect all 32 bits of (c) */439switch(length) /* all the case statements fall through */440{441case 12: c+=k[11];442case 11: c+=((uint32_t)k[10])<<8;443case 10: c+=((uint32_t)k[9])<<16;444case 9 : c+=((uint32_t)k[8])<<24;445case 8 : b+=k[7];446case 7 : b+=((uint32_t)k[6])<<8;447case 6 : b+=((uint32_t)k[5])<<16;448case 5 : b+=((uint32_t)k[4])<<24;449case 4 : a+=k[3];450case 3 : a+=((uint32_t)k[2])<<8;451case 2 : a+=((uint32_t)k[1])<<16;452case 1 : a+=((uint32_t)k[0])<<24;453break;454case 0 : return c;455}456}457458final(a,b,c);459return c;460}461#endif462463464