1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
11 OpenFOAM is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 Hashing functions, mostly from Bob Jenkins
26 \*---------------------------------------------------------------------------*/
29 #include "HasherInt.H"
31 #if defined (__GLIBC__)
35 // Left-rotate a 32-bit value and carry by nBits
36 #define bitRotateLeft(x, nBits) (((x) << (nBits)) | ((x) >> (32 - (nBits))))
39 // ----------------------------------------------------------------------------
40 // lookup3.c, by Bob Jenkins, May 2006, Public Domain.
42 // These are functions for producing 32-bit hashes for hash table lookup.
43 // hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
44 // are externally useful functions. Routines to test the hash are included
45 // if SELF_TEST is defined. You can use this free for any purpose. It's in
46 // the public domain. It has no warranty.
48 // You probably want to use hashlittle(). hashlittle() and hashbig()
49 // hash byte arrays. hashlittle() is is faster than hashbig() on
50 // little-endian machines. Intel and AMD are little-endian machines.
51 // On second thought, you probably want hashlittle2(), which is identical to
52 // hashlittle() except it returns two 32-bit hashes for the price of one.
53 // You could implement hashbig2() if you wanted but I haven't bothered here.
55 // If you want to find a hash of, say, exactly 7 integers, do
56 // a = i1; b = i2; c = i3;
58 // a += i4; b += i5; c += i6;
62 // then use c as the hash value. If you have a variable length array of
63 // 4-byte integers to hash, use hashword(). If you have a byte array (like
64 // a character string), use hashlittle(). If you have several byte arrays, or
65 // a mix of things, see the comments above hashlittle().
67 // Why is this so big? I read 12 bytes at a time into 3 4-byte integers,
68 // then mix those integers. This is fast (you can do a lot more thorough
69 // mixing with 12*3 instructions on 3 integers than you can with 3 instructions
70 // on 1 byte), but shoehorning those bytes into integers efficiently is messy.
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
74 // mix -- mix 3 32-bit values reversibly.
76 // This is reversible, so any information in (a,b,c) before mix_hash() is
77 // still in (a,b,c) after mix_hash().
79 // If four pairs of (a,b,c) inputs are run through mix_hash(), or through
80 // mix_hash() in reverse, there are at least 32 bits of the output that
81 // are sometimes the same for one pair and different for another pair.
82 // This was tested for:
83 // * pairs that differed by one bit, by two bits, in any combination
84 // of top bits of (a,b,c), or in any combination of bottom bits of
86 // * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
87 // the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
88 // is commonly produced by subtraction) look like a single 1-bit
90 // * the base values were pseudorandom, all zero but one bit set, or
91 // all zero plus a counter that starts at zero.
93 // Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
98 // Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
99 // for "differ" defined as + with a one-bit base and a two-bit delta. I
100 // used http://burtleburtle.net/bob/hash/avalanche.html to choose
101 // the operations, constants, and arrangements of the variables.
103 // This does not achieve avalanche. There are input bits of (a,b,c)
104 // that fail to affect some output bits of (a,b,c), especially of a. The
105 // most thoroughly mixed value is c, but it doesn't really even achieve
108 // This allows some parallelism. Read-after-writes are good at doubling
109 // the number of bits affected, so the goal of mixing pulls in the opposite
110 // direction as the goal of parallelism. I did what I could. Rotates
111 // seem to cost as much as shifts on every machine I could lay my hands
112 // on, and rotates are much kinder to the top and bottom bits, so I used
114 // ----------------------------------------------------------------------------
116 #define bitMixer(a, b, c) \
118 a -= c; a ^= bitRotateLeft(c, 4); c += b; \
119 b -= a; b ^= bitRotateLeft(a, 6); a += c; \
120 c -= b; c ^= bitRotateLeft(b, 8); b += a; \
121 a -= c; a ^= bitRotateLeft(c,16); c += b; \
122 b -= a; b ^= bitRotateLeft(a,19); a += c; \
123 c -= b; c ^= bitRotateLeft(b, 4); b += a; \
127 // ----------------------------------------------------------------------------
128 // final -- final mixing of 3 32-bit values (a,b,c) into c
130 // Pairs of (a,b,c) values differing in only a few bits will usually
131 // produce values of c that look totally different. This was tested for
132 // * pairs that differed by one bit, by two bits, in any combination
133 // of top bits of (a,b,c), or in any combination of bottom bits of
135 // * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
136 // the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
137 // is commonly produced by subtraction) look like a single 1-bit
139 // * the base values were pseudorandom, all zero but one bit set, or
140 // all zero plus a counter that starts at zero.
142 // These constants passed:
143 // 14 11 25 16 4 14 24
144 // 12 14 25 16 4 14 24
145 // and these came close:
147 // 10 8 15 26 3 22 24
148 // 11 8 15 26 3 22 24
149 // ----------------------------------------------------------------------------
151 #define bitMixerFinal(a, b, c) \
153 c ^= b; c -= bitRotateLeft(b, 14); \
154 a ^= c; a -= bitRotateLeft(c, 11); \
155 b ^= a; b -= bitRotateLeft(a, 25); \
156 c ^= b; c -= bitRotateLeft(b, 16); \
157 a ^= c; a -= bitRotateLeft(c, 4); \
158 b ^= a; b -= bitRotateLeft(a, 14); \
159 c ^= b; c -= bitRotateLeft(b, 24); \
163 // * * * * * * * * * * * * * * Static Functions * * * * * * * * * * * * * * //
165 // ----------------------------------------------------------------------------
166 // hashlittle() -- hash a variable-length key into a 32-bit value
167 // k : the key (the unaligned variable-length array of bytes)
168 // length : the length of the key, counting by bytes
169 // initval : can be any 4-byte value
170 // Returns a 32-bit value. Every bit of the key affects every bit of
171 // the return value. Two keys differing by one or two bits will have
172 // totally different hash values.
174 // The best hash table sizes are powers of 2. There is no need to do
175 // mod a prime (mod is sooo slow!). If you need less than 32 bits,
176 // use a bitmask. For example, if you need only 10 bits, do
177 // h = (h & hashmask(10));
178 // In which case, the hash table should have hashsize(10) elements.
180 // If you are hashing n strings (uint8_t **)k, do it like this:
181 // for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);
183 // By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this
184 // code any way you wish, private, educational, or commercial. It's free.
186 // Use for hash table lookup, or anything where one collision in 2^^32 is
187 // acceptable. Do NOT use for cryptographic purposes.
188 // ----------------------------------------------------------------------------
190 //- specialized little-endian code
191 #if !defined (__BYTE_ORDER) || (__BYTE_ORDER == __LITTLE_ENDIAN)
192 static unsigned jenkins_hashlittle
200 union { const void *ptr; size_t i; } u; // to cast key to (size_t) happily
202 // Set up the internal state
203 a = b = c = 0xdeadbeef + static_cast<uint32_t>(length) + initval;
206 if ((u.i & 0x3) == 0)
209 const uint32_t *k = reinterpret_cast<const uint32_t*>(key);
211 // all but last block: aligned reads and affect 32 bits of (a,b,c)
222 // handle the last (probably partial) block byte-wise
223 const uint8_t *k8 = reinterpret_cast<const uint8_t*>(k);
226 case 12: c += k[2]; b += k[1]; a += k[0]; break;
227 case 11: c += static_cast<uint32_t>(k8[10]) << 16; // fall through
228 case 10: c += static_cast<uint32_t>(k8[9]) << 8; // fall through
229 case 9 : c += k8[8]; // fall through
230 case 8 : b += k[1]; a += k[0]; break;
231 case 7 : b += static_cast<uint32_t>(k8[6]) << 16; // fall through
232 case 6 : b += static_cast<uint32_t>(k8[5]) << 8; // fall through
233 case 5 : b += k8[4]; // fall through
234 case 4 : a += k[0]; break;
235 case 3 : a += static_cast<uint32_t>(k8[2]) << 16; // fall through
236 case 2 : a += static_cast<uint32_t>(k8[1]) << 8; // fall through
237 case 1 : a += k8[0]; break;
238 case 0 : return c; // zero-length requires no mixing
241 else if ((u.i & 0x1) == 0)
244 const uint16_t *k = reinterpret_cast<const uint16_t*>(key);
246 // all but last block: aligned reads and different mixing
249 a += k[0] + (static_cast<uint32_t>(k[1]) << 16);
250 b += k[2] + (static_cast<uint32_t>(k[3]) << 16);
251 c += k[4] + (static_cast<uint32_t>(k[5]) << 16);
257 // handle the last (probably partial) block
258 const uint8_t *k8 = reinterpret_cast<const uint8_t*>(k);
262 c += k[4] + (static_cast<uint32_t>(k[5]) << 16);
263 b += k[2] + (static_cast<uint32_t>(k[3]) << 16);
264 a += k[0] + (static_cast<uint32_t>(k[1]) << 16);
267 c += static_cast<uint32_t>(k8[10]) << 16;
271 b += k[2] + (static_cast<uint32_t>(k[3]) << 16);
272 a += k[0] + (static_cast<uint32_t>(k[1]) << 16);
278 b += k[2] + (static_cast<uint32_t>(k[3]) << 16);
279 a += k[0] + (static_cast<uint32_t>(k[1]) << 16);
282 b += static_cast<uint32_t>(k8[6]) << 16;
286 a += k[0] + (static_cast<uint32_t>(k[1]) << 16);
292 a += k[0] + (static_cast<uint32_t>(k[1]) << 16);
295 a += static_cast<uint32_t>(k8[2]) << 16;
303 case 0 : return c; // zero-length requires no mixing
308 const uint8_t *k = reinterpret_cast<const uint8_t*>(key);
310 // all but the last block: affect some 32 bits of (a,b,c)
314 a += static_cast<uint32_t>(k[1]) << 8;
315 a += static_cast<uint32_t>(k[2]) << 16;
316 a += static_cast<uint32_t>(k[3]) << 24;
318 b += static_cast<uint32_t>(k[5]) << 8;
319 b += static_cast<uint32_t>(k[6]) << 16;
320 b += static_cast<uint32_t>(k[7]) << 24;
322 c += static_cast<uint32_t>(k[9]) << 8;
323 c += static_cast<uint32_t>(k[10]) << 16;
324 c += static_cast<uint32_t>(k[11]) << 24;
331 // last block: affect all 32 bits of (c)
332 switch (length) // most case statements fall through
334 case 12: c += static_cast<uint32_t>(k[11]) << 24;
335 case 11: c += static_cast<uint32_t>(k[10]) << 16;
336 case 10: c += static_cast<uint32_t>(k[9]) << 8;
339 case 8 : b += static_cast<uint32_t>(k[7]) << 24;
340 case 7 : b += static_cast<uint32_t>(k[6]) << 16;
341 case 6 : b += static_cast<uint32_t>(k[5]) << 8;
344 case 4 : a += static_cast<uint32_t>(k[3]) << 24;
345 case 3 : a += static_cast<uint32_t>(k[2]) << 16;
346 case 2 : a += static_cast<uint32_t>(k[1]) << 8;
354 bitMixerFinal(a,b,c);
362 // ----------------------------------------------------------------------------
364 // This is the same as hashword() on big-endian machines. It is different
365 // from hashlittle() on all machines. hashbig() takes advantage of
366 // big-endian byte ordering.
367 // ----------------------------------------------------------------------------
368 // specialized big-endian code
369 #if !defined (__BYTE_ORDER) || (__BYTE_ORDER == __BIG_ENDIAN)
370 static unsigned jenkins_hashbig
378 union { const void *ptr; size_t i; } u; // to cast key to (size_t) happily
380 // Set up the internal state
381 a = b = c = 0xdeadbeef + static_cast<uint32_t>(length) + initval;
384 if ((u.i & 0x3) == 0)
387 const uint32_t *k = reinterpret_cast<const uint32_t*>(key);
389 // all but last block: aligned reads and affect 32 bits of (a,b,c)
400 // handle the last (probably partial) block byte-wise
401 const uint8_t *k8 = reinterpret_cast<const uint8_t*>(k);
403 switch (length) // most the case statements fall through
405 case 12: c += k[2]; b += k[1]; a += k[0]; break;
406 case 11: c += static_cast<uint32_t>(k8[10]) << 8; // fall through
407 case 10: c += static_cast<uint32_t>(k8[9]) << 16; // fall through
408 case 9 : c += static_cast<uint32_t>(k8[8]) << 24; // fall through
409 case 8 : b += k[1]; a += k[0]; break;
410 case 7 : b += static_cast<uint32_t>(k8[6]) << 8; // fall through
411 case 6 : b += static_cast<uint32_t>(k8[5]) << 16; // fall through
412 case 5 : b += static_cast<uint32_t>(k8[4]) << 24; // fall through
413 case 4 : a += k[0]; break;
414 case 3 : a += static_cast<uint32_t>(k8[2]) << 8; // fall through
415 case 2 : a += static_cast<uint32_t>(k8[1]) << 16; // fall through
416 case 1 : a += static_cast<uint32_t>(k8[0]) << 24; break;
422 // need to read the key one byte at a time
423 const uint8_t *k = reinterpret_cast<const uint8_t*>(key);
425 // all but the last block: affect some 32 bits of (a,b,c)
428 a += static_cast<uint32_t>(k[0]) << 24;
429 a += static_cast<uint32_t>(k[1]) << 16;
430 a += static_cast<uint32_t>(k[2]) << 8;
431 a += static_cast<uint32_t>(k[3]);
432 b += static_cast<uint32_t>(k[4]) << 24;
433 b += static_cast<uint32_t>(k[5]) << 16;
434 b += static_cast<uint32_t>(k[6]) << 8;
435 b += static_cast<uint32_t>(k[7]);
436 c += static_cast<uint32_t>(k[8]) << 24;
437 c += static_cast<uint32_t>(k[9]) << 16;
438 c += static_cast<uint32_t>(k[10]) << 8;
439 c += static_cast<uint32_t>(k[11]);
446 // last block: affect all 32 bits of (c)
447 switch (length) // the case statements fall through
450 case 11: c += static_cast<uint32_t>(k[10]) << 8;
451 case 10: c += static_cast<uint32_t>(k[9]) << 16;
452 case 9 : c += static_cast<uint32_t>(k[8]) << 24;
454 case 7 : b += static_cast<uint32_t>(k[6]) << 8;
455 case 6 : b += static_cast<uint32_t>(k[5]) << 16;
456 case 5 : b += static_cast<uint32_t>(k[4]) << 24;
458 case 3 : a += static_cast<uint32_t>(k[2]) << 8;
459 case 2 : a += static_cast<uint32_t>(k[1]) << 16;
460 case 1 : a += static_cast<uint32_t>(k[0]) << 24;
466 bitMixerFinal(a,b,c);
472 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
475 unsigned Foam::Hasher
483 # if (__BYTE_ORDER == __BIG_ENDIAN)
484 return jenkins_hashbig(key, length, initval);
486 return jenkins_hashlittle(key, length, initval);
489 // endian-ness not known at compile-time: runtime endian test
490 const short endianTest = 0x0100;
492 // yields 0x01 for big endian
493 if (*(reinterpret_cast<const char*>(&endianTest)))
495 return jenkins_hashbig(key, length, initval);
499 return jenkins_hashlittle(key, length, initval);
505 // ----------------------------------------------------------------------------
506 // This works on all machines. To be useful, it requires
507 // -- that the key be an array of uint32_t's, and
508 // -- that the length be the number of uint32_t's in the key
510 // The function hashword() is identical to hashlittle() on little-endian
511 // machines, and identical to hashbig() on big-endian machines,
512 // except that the length has to be measured in uint32_ts rather than in
513 // bytes. hashlittle() is more complicated than hashword() only because
514 // hashlittle() has to dance around fitting the key bytes into registers.
515 // ----------------------------------------------------------------------------
516 unsigned Foam::HasherInt
525 // Set up the internal state
526 a = b = c = 0xdeadbeef + (static_cast<uint32_t>(length) << 2) + seed;
528 // handle most of the key
539 // handle the last 3 uint32_t's
540 switch (length) // all case statements fall through
545 bitMixerFinal(a,b,c);
546 case 0 : // case 0: nothing left to add
554 // ----------------------------------------------------------------------------
555 // hashword2() -- same as hashword(), but take two seeds and return two
556 // 32-bit values. pc and pb must both be non-null, and *pc and *pb must
557 // both be initialized with seeds. If you pass in (*pb)==0, the output
558 // (*pc) will be the same as the return value from hashword().
559 // ----------------------------------------------------------------------------
560 unsigned Foam::HasherDual
564 unsigned& hash1, // IN: seed OUT: primary hash value
565 unsigned& hash2 // IN: more seed OUT: secondary hash value
570 // Set up the internal state
571 a = b = c = 0xdeadbeef + (static_cast<uint32_t>(length) << 2) + hash1;
574 // handle most of the key
585 // handle the last 3 uint32_t's
586 switch (length) // all case statements fall through
591 bitMixerFinal(a,b,c);
592 case 0 : // case 0: nothing left to add
600 // return primary hash value
605 // ************************************************************************* //