OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

Hasher.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Description
00026     Hashing functions, mostly from Bob Jenkins
00027 \*---------------------------------------------------------------------------*/
00028 
00029 #include "Hasher.H"
00030 #include "HasherInt.H"
00031 
00032 #if defined (__GLIBC__)
00033 #  include <endian.h>
00034 #endif
00035 
00036 // Left-rotate a 32-bit value and carry by nBits
00037 #define bitRotateLeft(x, nBits)  (((x) << (nBits)) | ((x) >> (32 - (nBits))))
00038 
00039 
00040 // ----------------------------------------------------------------------------
00041 // lookup3.c, by Bob Jenkins, May 2006, Public Domain.
00042 //
00043 // These are functions for producing 32-bit hashes for hash table lookup.
00044 // hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
00045 // are externally useful functions.  Routines to test the hash are included
00046 // if SELF_TEST is defined.  You can use this free for any purpose.  It's in
00047 // the public domain.  It has no warranty.
00048 //
00049 // You probably want to use hashlittle().  hashlittle() and hashbig()
00050 // hash byte arrays.  hashlittle() is is faster than hashbig() on
00051 // little-endian machines.  Intel and AMD are little-endian machines.
00052 // On second thought, you probably want hashlittle2(), which is identical to
00053 // hashlittle() except it returns two 32-bit hashes for the price of one.
00054 // You could implement hashbig2() if you wanted but I haven't bothered here.
00055 //
00056 // If you want to find a hash of, say, exactly 7 integers, do
00057 //   a = i1;  b = i2;  c = i3;
00058 //   mix(a,b,c);
00059 //   a += i4; b += i5; c += i6;
00060 //   mix(a,b,c);
00061 //   a += i7;
00062 //   final(a,b,c);
00063 // then use c as the hash value.  If you have a variable length array of
00064 // 4-byte integers to hash, use hashword().  If you have a byte array (like
00065 // a character string), use hashlittle().  If you have several byte arrays, or
00066 // a mix of things, see the comments above hashlittle().
00067 //
00068 // Why is this so big?  I read 12 bytes at a time into 3 4-byte integers,
00069 // then mix those integers.  This is fast (you can do a lot more thorough
00070 // mixing with 12*3 instructions on 3 integers than you can with 3 instructions
00071 // on 1 byte), but shoehorning those bytes into integers efficiently is messy.
00072 // ----------------------------------------------------------------------------
00073 
00074 // ----------------------------------------------------------------------------
00075 // mix -- mix 3 32-bit values reversibly.
00076 //
00077 // This is reversible, so any information in (a,b,c) before mix_hash() is
00078 // still in (a,b,c) after mix_hash().
00079 //
00080 // If four pairs of (a,b,c) inputs are run through mix_hash(), or through
00081 // mix_hash() in reverse, there are at least 32 bits of the output that
00082 // are sometimes the same for one pair and different for another pair.
00083 // This was tested for:
00084 // * pairs that differed by one bit, by two bits, in any combination
00085 //   of top bits of (a,b,c), or in any combination of bottom bits of
00086 //   (a,b,c).
00087 // * "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
00088 //   the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
00089 //   is commonly produced by subtraction) look like a single 1-bit
00090 //   difference.
00091 // * the base values were pseudorandom, all zero but one bit set, or
00092 //   all zero plus a counter that starts at zero.
00093 //
00094 // Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
00095 // satisfy this are
00096 //     4  6  8 16 19  4
00097 //     9 15  3 18 27 15
00098 //    14  9  3  7 17  3
00099 // Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
00100 // for "differ" defined as + with a one-bit base and a two-bit delta.  I
00101 // used http://burtleburtle.net/bob/hash/avalanche.html to choose
00102 // the operations, constants, and arrangements of the variables.
00103 //
00104 // This does not achieve avalanche.  There are input bits of (a,b,c)
00105 // that fail to affect some output bits of (a,b,c), especially of a.  The
00106 // most thoroughly mixed value is c, but it doesn't really even achieve
00107 // avalanche in c.
00108 //
00109 // This allows some parallelism.  Read-after-writes are good at doubling
00110 // the number of bits affected, so the goal of mixing pulls in the opposite
00111 // direction as the goal of parallelism.  I did what I could.  Rotates
00112 // seem to cost as much as shifts on every machine I could lay my hands
00113 // on, and rotates are much kinder to the top and bottom bits, so I used
00114 // rotates.
00115 // ----------------------------------------------------------------------------
00116 
00117 #define bitMixer(a, b, c)                                                     \
00118     {                                                                         \
00119         a -= c; a ^= bitRotateLeft(c, 4); c += b;                             \
00120         b -= a; b ^= bitRotateLeft(a, 6); a += c;                             \
00121         c -= b; c ^= bitRotateLeft(b, 8); b += a;                             \
00122         a -= c; a ^= bitRotateLeft(c,16); c += b;                             \
00123         b -= a; b ^= bitRotateLeft(a,19); a += c;                             \
00124         c -= b; c ^= bitRotateLeft(b, 4); b += a;                             \
00125     }
00126 
00127 
00128 // ----------------------------------------------------------------------------
00129 // final -- final mixing of 3 32-bit values (a,b,c) into c
00130 //
00131 // Pairs of (a,b,c) values differing in only a few bits will usually
00132 // produce values of c that look totally different.  This was tested for
00133 // * pairs that differed by one bit, by two bits, in any combination
00134 //   of top bits of (a,b,c), or in any combination of bottom bits of
00135 //   (a,b,c).
00136 // * "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
00137 //   the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
00138 //   is commonly produced by subtraction) look like a single 1-bit
00139 //   difference.
00140 // * the base values were pseudorandom, all zero but one bit set, or
00141 //   all zero plus a counter that starts at zero.
00142 //
00143 // These constants passed:
00144 //  14 11 25 16 4 14 24
00145 //  12 14 25 16 4 14 24
00146 // and these came close:
00147 //   4  8 15 26 3 22 24
00148 //  10  8 15 26 3 22 24
00149 //  11  8 15 26 3 22 24
00150 // ----------------------------------------------------------------------------
00151 
00152 #define bitMixerFinal(a, b, c)                                                \
00153     {                                                                         \
00154         c ^= b; c -= bitRotateLeft(b, 14);                                    \
00155         a ^= c; a -= bitRotateLeft(c, 11);                                    \
00156         b ^= a; b -= bitRotateLeft(a, 25);                                    \
00157         c ^= b; c -= bitRotateLeft(b, 16);                                    \
00158         a ^= c; a -= bitRotateLeft(c, 4);                                     \
00159         b ^= a; b -= bitRotateLeft(a, 14);                                    \
00160         c ^= b; c -= bitRotateLeft(b, 24);                                    \
00161     }
00162 
00163 
00164 // * * * * * * * * * * * * * * Static Functions  * * * * * * * * * * * * * * //
00165 
00166 // ----------------------------------------------------------------------------
00167 // hashlittle() -- hash a variable-length key into a 32-bit value
00168 //   k       : the key (the unaligned variable-length array of bytes)
00169 //   length  : the length of the key, counting by bytes
00170 //   initval : can be any 4-byte value
00171 // Returns a 32-bit value.  Every bit of the key affects every bit of
00172 // the return value.  Two keys differing by one or two bits will have
00173 // totally different hash values.
00174 //
00175 // The best hash table sizes are powers of 2.  There is no need to do
00176 // mod a prime (mod is sooo slow!).  If you need less than 32 bits,
00177 // use a bitmask.  For example, if you need only 10 bits, do
00178 //   h = (h & hashmask(10));
00179 // In which case, the hash table should have hashsize(10) elements.
00180 //
00181 // If you are hashing n strings (uint8_t **)k, do it like this:
00182 //   for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);
00183 //
00184 // By Bob Jenkins, 2006.  bob_jenkins@burtleburtle.net.  You may use this
00185 // code any way you wish, private, educational, or commercial.  It's free.
00186 //
00187 // Use for hash table lookup, or anything where one collision in 2^^32 is
00188 // acceptable.  Do NOT use for cryptographic purposes.
00189 // ----------------------------------------------------------------------------
00190 
00191 //- specialized little-endian code
00192 #if !defined (__BYTE_ORDER) || (__BYTE_ORDER == __LITTLE_ENDIAN)
00193 static unsigned jenkins_hashlittle
00194 (
00195     const void *key,
00196     size_t length,
00197     unsigned initval
00198 )
00199 {
00200     uint32_t a, b, c;
00201     union { const void *ptr; size_t i; } u; // to cast key to (size_t) happily
00202 
00203     // Set up the internal state
00204     a = b = c = 0xdeadbeef + static_cast<uint32_t>(length) + initval;
00205 
00206     u.ptr = key;
00207     if ((u.i & 0x3) == 0)
00208     {
00209         // 32-bit chunks
00210         const uint32_t *k = reinterpret_cast<const uint32_t*>(key);
00211 
00212         // all but last block: aligned reads and affect 32 bits of (a,b,c)
00213         while (length > 12)
00214         {
00215             a += k[0];
00216             b += k[1];
00217             c += k[2];
00218             bitMixer(a,b,c);
00219             length -= 12;
00220             k += 3;
00221         }
00222 
00223         // handle the last (probably partial) block byte-wise
00224         const uint8_t *k8 = reinterpret_cast<const uint8_t*>(k);
00225         switch (length)
00226         {
00227             case 12: c += k[2]; b += k[1]; a += k[0]; break;
00228             case 11: c += static_cast<uint32_t>(k8[10]) << 16; // fall through
00229             case 10: c += static_cast<uint32_t>(k8[9])  << 8;  // fall through
00230             case 9 : c += k8[8];                               // fall through
00231             case 8 : b += k[1]; a += k[0]; break;
00232             case 7 : b += static_cast<uint32_t>(k8[6]) << 16;  // fall through
00233             case 6 : b += static_cast<uint32_t>(k8[5]) << 8;   // fall through
00234             case 5 : b += k8[4];                               // fall through
00235             case 4 : a += k[0]; break;
00236             case 3 : a += static_cast<uint32_t>(k8[2]) << 16;  // fall through
00237             case 2 : a += static_cast<uint32_t>(k8[1]) << 8;   // fall through
00238             case 1 : a += k8[0]; break;
00239             case 0 : return c;  // zero-length requires no mixing
00240         }
00241     }
00242     else if ((u.i & 0x1) == 0)
00243     {
00244         // 16-bit chunks
00245         const uint16_t *k = reinterpret_cast<const uint16_t*>(key);
00246 
00247         // all but last block: aligned reads and different mixing
00248         while (length > 12)
00249         {
00250             a += k[0] + (static_cast<uint32_t>(k[1]) << 16);
00251             b += k[2] + (static_cast<uint32_t>(k[3]) << 16);
00252             c += k[4] + (static_cast<uint32_t>(k[5]) << 16);
00253             bitMixer(a,b,c);
00254             length -= 12;
00255             k += 6;
00256         }
00257 
00258         // handle the last (probably partial) block
00259         const uint8_t *k8 = reinterpret_cast<const uint8_t*>(k);
00260         switch (length)
00261         {
00262             case 12:
00263                 c += k[4] + (static_cast<uint32_t>(k[5]) << 16);
00264                 b += k[2] + (static_cast<uint32_t>(k[3]) << 16);
00265                 a += k[0] + (static_cast<uint32_t>(k[1]) << 16);
00266                 break;
00267             case 11:
00268                 c += static_cast<uint32_t>(k8[10]) << 16;
00269                 // fall through
00270             case 10:
00271                 c += k[4];
00272                 b += k[2] + (static_cast<uint32_t>(k[3]) << 16);
00273                 a += k[0] + (static_cast<uint32_t>(k[1]) << 16);
00274                 break;
00275             case 9 :
00276                 c += k8[8];
00277                 // fall through
00278             case 8 :
00279                 b += k[2] + (static_cast<uint32_t>(k[3]) << 16);
00280                 a += k[0] + (static_cast<uint32_t>(k[1]) << 16);
00281                 break;
00282             case 7 :
00283                 b += static_cast<uint32_t>(k8[6]) << 16;
00284                 // fall through
00285             case 6 :
00286                 b += k[2];
00287                 a += k[0] + (static_cast<uint32_t>(k[1]) << 16);
00288                 break;
00289             case 5 :
00290                 b += k8[4];
00291                 // fall through
00292             case 4 :
00293                 a += k[0] + (static_cast<uint32_t>(k[1]) << 16);
00294                 break;
00295             case 3 :
00296                 a += static_cast<uint32_t>(k8[2]) << 16;
00297                 // fall through
00298             case 2 :
00299                 a += k[0];
00300                 break;
00301             case 1 :
00302                 a += k8[0];
00303                 break;
00304             case 0 : return c;     // zero-length requires no mixing
00305         }
00306     }
00307     else
00308     {
00309         const uint8_t *k = reinterpret_cast<const uint8_t*>(key);
00310 
00311         // all but the last block: affect some 32 bits of (a,b,c)
00312         while (length > 12)
00313         {
00314             a += k[0];
00315             a += static_cast<uint32_t>(k[1]) << 8;
00316             a += static_cast<uint32_t>(k[2]) << 16;
00317             a += static_cast<uint32_t>(k[3]) << 24;
00318             b += k[4];
00319             b += static_cast<uint32_t>(k[5]) << 8;
00320             b += static_cast<uint32_t>(k[6]) << 16;
00321             b += static_cast<uint32_t>(k[7]) << 24;
00322             c += k[8];
00323             c += static_cast<uint32_t>(k[9])  << 8;
00324             c += static_cast<uint32_t>(k[10]) << 16;
00325             c += static_cast<uint32_t>(k[11]) << 24;
00326 
00327             bitMixer(a,b,c);
00328             length -= 12;
00329             k += 12;
00330         }
00331 
00332         // last block: affect all 32 bits of (c)
00333         switch (length) // most case statements fall through
00334         {
00335             case 12: c += static_cast<uint32_t>(k[11]) << 24;
00336             case 11: c += static_cast<uint32_t>(k[10]) << 16;
00337             case 10: c += static_cast<uint32_t>(k[9]) << 8;
00338             case 9 : c += k[8];
00339 
00340             case 8 : b += static_cast<uint32_t>(k[7]) << 24;
00341             case 7 : b += static_cast<uint32_t>(k[6]) << 16;
00342             case 6 : b += static_cast<uint32_t>(k[5]) << 8;
00343             case 5 : b += k[4];
00344 
00345             case 4 : a += static_cast<uint32_t>(k[3]) << 24;
00346             case 3 : a += static_cast<uint32_t>(k[2]) << 16;
00347             case 2 : a += static_cast<uint32_t>(k[1]) << 8;
00348             case 1 : a += k[0];
00349                 break;
00350 
00351             case 0 : return c;
00352         }
00353     }
00354 
00355     bitMixerFinal(a,b,c);
00356     return c;
00357 }
00358 #endif
00359 
00360 
00361 
00362 
00363 // ----------------------------------------------------------------------------
00364 // hashbig():
00365 // This is the same as hashword() on big-endian machines.  It is different
00366 // from hashlittle() on all machines.  hashbig() takes advantage of
00367 // big-endian byte ordering.
00368 // ----------------------------------------------------------------------------
00369 // specialized big-endian code
00370 #if !defined (__BYTE_ORDER) || (__BYTE_ORDER == __BIG_ENDIAN)
00371 static unsigned jenkins_hashbig
00372 (
00373     const void *key,
00374     size_t length,
00375     unsigned initval
00376 )
00377 {
00378     uint32_t a, b, c;
00379     union { const void *ptr; size_t i; } u; // to cast key to (size_t) happily
00380 
00381     // Set up the internal state
00382     a = b = c = 0xdeadbeef + static_cast<uint32_t>(length) + initval;
00383 
00384     u.ptr = key;
00385     if ((u.i & 0x3) == 0)
00386     {
00387         // 32-bit chunks
00388         const uint32_t *k = reinterpret_cast<const uint32_t*>(key);
00389 
00390         // all but last block: aligned reads and affect 32 bits of (a,b,c)
00391         while (length > 12)
00392         {
00393             a += k[0];
00394             b += k[1];
00395             c += k[2];
00396             bitMixer(a,b,c);
00397             length -= 12;
00398             k += 3;
00399         }
00400 
00401         // handle the last (probably partial) block byte-wise
00402         const uint8_t *k8 = reinterpret_cast<const uint8_t*>(k);
00403 
00404         switch (length) // most the case statements fall through
00405         {
00406             case 12: c += k[2]; b += k[1]; a += k[0]; break;
00407             case 11: c += static_cast<uint32_t>(k8[10]) << 8; // fall through
00408             case 10: c += static_cast<uint32_t>(k8[9]) << 16; // fall through
00409             case 9 : c += static_cast<uint32_t>(k8[8]) << 24; // fall through
00410             case 8 : b += k[1]; a += k[0]; break;
00411             case 7 : b += static_cast<uint32_t>(k8[6]) << 8;  // fall through
00412             case 6 : b += static_cast<uint32_t>(k8[5]) << 16; // fall through
00413             case 5 : b += static_cast<uint32_t>(k8[4]) << 24; // fall through
00414             case 4 : a += k[0]; break;
00415             case 3 : a += static_cast<uint32_t>(k8[2]) << 8;  // fall through
00416             case 2 : a += static_cast<uint32_t>(k8[1]) << 16; // fall through
00417             case 1 : a += static_cast<uint32_t>(k8[0]) << 24; break;
00418             case 0 : return c;
00419         }
00420     }
00421     else
00422     {
00423         // need to read the key one byte at a time
00424         const uint8_t *k = reinterpret_cast<const uint8_t*>(key);
00425 
00426         // all but the last block: affect some 32 bits of (a,b,c)
00427         while (length > 12)
00428         {
00429             a += static_cast<uint32_t>(k[0]) << 24;
00430             a += static_cast<uint32_t>(k[1]) << 16;
00431             a += static_cast<uint32_t>(k[2]) << 8;
00432             a += static_cast<uint32_t>(k[3]);
00433             b += static_cast<uint32_t>(k[4]) << 24;
00434             b += static_cast<uint32_t>(k[5]) << 16;
00435             b += static_cast<uint32_t>(k[6]) << 8;
00436             b += static_cast<uint32_t>(k[7]);
00437             c += static_cast<uint32_t>(k[8]) << 24;
00438             c += static_cast<uint32_t>(k[9]) << 16;
00439             c += static_cast<uint32_t>(k[10]) << 8;
00440             c += static_cast<uint32_t>(k[11]);
00441 
00442             bitMixer(a,b,c);
00443             length -= 12;
00444             k += 12;
00445         }
00446 
00447         // last block: affect all 32 bits of (c)
00448         switch (length) // the case statements fall through
00449         {
00450             case 12: c += k[11];
00451             case 11: c += static_cast<uint32_t>(k[10]) << 8;
00452             case 10: c += static_cast<uint32_t>(k[9]) << 16;
00453             case 9 : c += static_cast<uint32_t>(k[8]) << 24;
00454             case 8 : b += k[7];
00455             case 7 : b += static_cast<uint32_t>(k[6]) << 8;
00456             case 6 : b += static_cast<uint32_t>(k[5]) << 16;
00457             case 5 : b += static_cast<uint32_t>(k[4]) << 24;
00458             case 4 : a += k[3];
00459             case 3 : a += static_cast<uint32_t>(k[2]) << 8;
00460             case 2 : a += static_cast<uint32_t>(k[1]) << 16;
00461             case 1 : a += static_cast<uint32_t>(k[0]) << 24;
00462                 break;
00463             case 0 : return c;
00464         }
00465     }
00466 
00467     bitMixerFinal(a,b,c);
00468     return c;
00469 }
00470 #endif
00471 
00472 
00473 // * * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * //
00474 
00475 
00476 unsigned Foam::Hasher
00477 (
00478     const void *key,
00479     size_t length,
00480     unsigned initval
00481 )
00482 {
00483 #ifdef __BYTE_ORDER
00484 # if (__BYTE_ORDER == __BIG_ENDIAN)
00485     return jenkins_hashbig(key, length, initval);
00486 # else
00487     return jenkins_hashlittle(key, length, initval);
00488 # endif
00489 #else
00490     // endian-ness not known at compile-time: runtime endian test
00491     const short endianTest = 0x0100;
00492 
00493     // yields 0x01 for big endian
00494     if (*(reinterpret_cast<const char *>(&endianTest)))
00495     {
00496         return jenkins_hashbig(key, length, initval);
00497     }
00498     else
00499     {
00500         return jenkins_hashlittle(key, length, initval);
00501     }
00502 #endif
00503 }
00504 
00505 
00506 // ----------------------------------------------------------------------------
00507 //  This works on all machines.  To be useful, it requires
00508 //  -- that the key be an array of uint32_t's, and
00509 //  -- that the length be the number of uint32_t's in the key
00510 //
00511 //  The function hashword() is identical to hashlittle() on little-endian
00512 //  machines, and identical to hashbig() on big-endian machines,
00513 //  except that the length has to be measured in uint32_ts rather than in
00514 //  bytes.  hashlittle() is more complicated than hashword() only because
00515 //  hashlittle() has to dance around fitting the key bytes into registers.
00516 // ----------------------------------------------------------------------------
00517 unsigned Foam::HasherInt
00518 (
00519     const uint32_t *k,
00520     size_t length,
00521     unsigned seed
00522 )
00523 {
00524     uint32_t a, b, c;
00525 
00526     // Set up the internal state
00527     a = b = c = 0xdeadbeef + (static_cast<uint32_t>(length) << 2) + seed;
00528 
00529     // handle most of the key
00530     while (length > 3)
00531     {
00532         a += k[0];
00533         b += k[1];
00534         c += k[2];
00535         bitMixer(a,b,c);
00536         length -= 3;
00537         k += 3;
00538     }
00539 
00540     // handle the last 3 uint32_t's
00541     switch (length)  // all case statements fall through
00542     {
00543         case 3 : c += k[2];
00544         case 2 : b += k[1];
00545         case 1 : a += k[0];
00546             bitMixerFinal(a,b,c);
00547         case 0 :  // case 0: nothing left to add
00548             break;
00549     }
00550 
00551     return c;
00552 }
00553 
00554 
00555 // ----------------------------------------------------------------------------
00556 // hashword2() -- same as hashword(), but take two seeds and return two
00557 // 32-bit values.  pc and pb must both be non-null, and *pc and *pb must
00558 // both be initialized with seeds.  If you pass in (*pb)==0, the output
00559 // (*pc) will be the same as the return value from hashword().
00560 // ----------------------------------------------------------------------------
00561 unsigned Foam::HasherDual
00562 (
00563     const uint32_t *k,
00564     size_t length,
00565     unsigned& hash1,  // IN: seed OUT: primary hash value
00566     unsigned& hash2   // IN: more seed OUT: secondary hash value
00567 )
00568 {
00569     uint32_t a, b, c;
00570 
00571     // Set up the internal state
00572     a = b = c = 0xdeadbeef + (static_cast<uint32_t>(length) << 2) + hash1;
00573     c += hash2;
00574 
00575     // handle most of the key
00576     while (length > 3)
00577     {
00578         a += k[0];
00579         b += k[1];
00580         c += k[2];
00581         bitMixer(a,b,c);
00582         length -= 3;
00583         k += 3;
00584     }
00585 
00586     // handle the last 3 uint32_t's
00587     switch (length)  // all case statements fall through
00588     {
00589         case 3 : c += k[2];
00590         case 2 : b += k[1];
00591         case 1 : a += k[0];
00592             bitMixerFinal(a,b,c);
00593         case 0 :  // case 0: nothing left to add
00594             break;
00595     }
00596 
00597     // report the result
00598     hash1 = c;
00599     hash2 = b;
00600 
00601     // return primary hash value
00602     return c;
00603 }
00604 
00605 
00606 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd