1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Based on lookup3.c, by Bob Jenkins, May 2006, Public Domain.
4 * http://www.burtleburtle.net/bob/c/lookup3.c
6 * These functions for producing 32-bit hashes for has table lookup.
7 * hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
8 * are externally useful functions. Routines to test the hash are included
9 * if SELF_TEST is defined. You can use this free for any purpose. It is in
10 * the public domain. It has no warranty.
16 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
18 /** -------------------------------------------------------------------------
19 * This is reversible, so any information in (a,b,c) before mix() is
20 * still in (a,b,c) after mix().
22 * If four pairs of (a,b,c) inputs are run through mix(), or through
23 * mix() in reverse, there are at least 32 bits of the output that
24 * are sometimes the same for one pair and different for another pair.
25 * This was tested for:
26 * pairs that differed by one bit, by two bits, in any combination
27 * of top bits of (a,b,c), or in any combination of bottom bits of
29 * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
30 * the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
31 * is commonly produced by subtraction) look like a single 1-bit
33 * the base values were pseudorandom, all zero but one bit set, or
34 * all zero plus a counter that starts at zero.
36 * Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
41 * Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
42 * for "differ" defined as + with a one-bit base and a two-bit delta. I
43 * used http://burtleburtle.net/bob/hash/avalanche.html to choose
44 * the operations, constants, and arrangements of the variables.
46 * This does not achieve avalanche. There are input bits of (a,b,c)
47 * that fail to affect some output bits of (a,b,c), especially of a. The
48 * most thoroughly mixed value is c, but it doesn't really even achieve
51 * This allows some parallelism. Read-after-writes are good at doubling
52 * the number of bits affected, so the goal of mixing pulls in the opposite
53 * direction as the goal of parallelism. I did what I could. Rotates
54 * seem to cost as much as shifts on every machine I could lay my hands
55 * on, and rotates are much kinder to the top and bottom bits, so I used
57 * --------------------------------------------------------------------------
59 #define mix(a, b, c) \
61 (a) -= (c); (a) ^= rot((c), 4); (c) += b; \
62 (b) -= (a); (b) ^= rot((a), 6); (a) += c; \
63 (c) -= (b); (c) ^= rot((b), 8); (b) += a; \
64 (a) -= (c); (a) ^= rot((c), 16); (c) += b; \
65 (b) -= (a); (b) ^= rot((a), 19); (a) += c; \
66 (c) -= (b); (c) ^= rot((b), 4); (b) += a; \
69 /** --------------------------------------------------------------------------
70 * final -- final mixing of 3 32-bit values (a,b,c) into c
72 * Pairs of (a,b,c) values differing in only a few bits will usually
73 * produce values of c that look totally different. This was tested for
74 * pairs that differed by one bit, by two bits, in any combination
75 * of top bits of (a,b,c), or in any combination of bottom bits of
77 * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
78 * the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
79 * is commonly produced by subtraction) look like a single 1-bit
81 * the base values were pseudorandom, all zero but one bit set, or
82 * all zero plus a counter that starts at zero.
84 * These constants passed:
87 * and these came close:
91 * --------------------------------------------------------------------------
93 #define final(a, b, c) \
95 (c) ^= (b); (c) -= rot((b), 14); \
96 (a) ^= (c); (a) -= rot((c), 11); \
97 (b) ^= (a); (b) -= rot((a), 25); \
98 (c) ^= (b); (c) -= rot((b), 16); \
99 (a) ^= (c); (a) -= rot((c), 4); \
100 (b) ^= (a); (b) -= rot((a), 14); \
101 (c) ^= (b); (c) -= rot((b), 24); \
104 /** --------------------------------------------------------------------
105 * This works on all machines. To be useful, it requires
106 * -- that the key be an array of uint32_t's, and
107 * -- that the length be the number of uint32_t's in the key
109 * The function hashword() is identical to hashlittle() on little-endian
110 * machines, and identical to hashbig() on big-endian machines,
111 * except that the length has to be measured in uint32_ts rather than in
112 * bytes. hashlittle() is more complicated than hashword() only because
113 * hashlittle() has to dance around fitting the key bytes into registers.
116 * key: an array of uint32_t values
117 * length: the length of the key, in uint32_ts
118 * initval: the previous hash, or an arbitrary value
119 * --------------------------------------------------------------------
121 static inline uint32_t hashword(const uint32_t *k,
125 int index = length - 1;
127 /* Set up the internal state */
128 a = 0xdeadbeef + (((uint32_t)length) << 2) + initval;
132 /*-------------------------------------------- handle most of the key */
142 /*-------------------------------------- handle the last 3 uint32_t's */
143 switch (length) { /* all the case statements fall through */
154 case 0: /* case 0: nothing left to add */
157 /*------------------------------------------------- report the result */
161 #endif /* _LOOKUP3_H_ */