### Compute FillFactor metric M from k, n, r Source: https://github.com/aappleby/smhasher/wiki/FillFactor Calculates the FillFactor metric M, which measures hash table efficiency relative to a partially filled distribution. Requires the number of keys (k), number of buckets (n), and root-mean-square bucket count (r). Returns a value starting at 1, where higher values indicate more wasted table space. ```c double f = (k*k - 1) / (n*r*r - k); return 1 - (f / n); ``` -------------------------------- ### CMakeLists.txt for SMHasher project Source: https://github.com/aappleby/smhasher/blob/master/src/CMakeLists.txt Defines the SMHasher project, sets build type to Release, creates the SMHasherSupport static library from source files, and links it to the SMHasher executable. Requires CMake 2.4 or higher. ```cmake project(SMHasher) cmake_minimum_required(VERSION 2.4) set(CMAKE_BUILD_TYPE Release) add_library( SMHasherSupport AvalancheTest.cpp Bitslice.cpp Bitvec.cpp CityTest.cpp City.cpp crc.cpp DifferentialTest.cpp Hashes.cpp KeysetTest.cpp lookup3.cpp md5.cpp MurmurHash1.cpp MurmurHash2.cpp MurmurHash3.cpp Platform.cpp Random.cpp sha1.cpp SpeedTest.cpp Spooky.cpp SpookyTest.cpp Stats.cpp SuperFastHash.cpp Types.cpp PMurHash.c ) add_executable( SMHasher main.cpp ) target_link_libraries( SMHasher SMHasherSupport ) ``` -------------------------------- ### Simplified loop when m == 1 Source: https://github.com/aappleby/smhasher/wiki/MurmurHash2Flaw Shows the loop after setting m to 1, where x cancels out completely, causing all keys to hash to the same value. ```c x ^= x >> r; h ^= x; h ^= x; ``` -------------------------------- ### MurmurHash3 64-bit Finalizer Source: https://github.com/aappleby/smhasher/wiki/MurmurHash3 64-bit finalizer that avalanches all bits of `h` to within 0.25% bias. Constants were generated by a simulated-annealing algorithm. ```plaintext h ^= h >> 33; h *= 0xff51afd7ed558ccd; h ^= h >> 33; h *= 0xc4ceb9fe1a85ec53; h ^= h >> 33; ``` -------------------------------- ### MurmurHash3 32-bit Finalizer Source: https://github.com/aappleby/smhasher/wiki/MurmurHash3 32-bit finalizer that avalanches all bits of `h` to within 0.25% bias. Constants were generated by a simulated-annealing algorithm. ```plaintext h ^= h >> 16; h *= 0x85ebca6b; h ^= h >> 13; h *= 0xc2b2ae35; h ^= h >> 16; ``` -------------------------------- ### Unrolled loop for identical key values Source: https://github.com/aappleby/smhasher/wiki/MurmurHash2Flaw Unrolled version of the inner loop when both uint32_t values are the same x. Shows the cancellation effect when m equals 1. ```c x *= m; x ^= x >> r; x *= m; h *= m; h ^= x; h *= m; h ^= x; ``` -------------------------------- ### MurmurHash2 inner loop with bad keys Source: https://github.com/aappleby/smhasher/wiki/MurmurHash2Flaw Shows the inner loop of MurmurHash2 processing keys with repeated 4-byte values. The pre-mix and merge steps are labeled. This loop is the context for the flaw analysis. ```c while(len >= 4) { uint32_t k = *(uint32_t*)data; // we'll call this the 'pre-mix' step k *= m; k ^= k >> r; k *= m; // and this the 'merge' step h *= m; h ^= k; data += 4; len -= 4; } ``` -------------------------------- ### MurmurHash2 mix function Source: https://github.com/aappleby/smhasher/wiki/MurmurHash2 Core mixing step applied to each key block and the hash state. Uses constants m and r; the flaw in this version motivated the later MurmurHash2A fix. ```plaintext k *= m; k ^= k >> r; k *= m; h *= m; h ^= k; ``` -------------------------------- ### MurmurHash3 64-bit 2-Block Inter-Mix Source: https://github.com/aappleby/smhasher/wiki/MurmurHash3 Inter-mix operation for the 64-bit 128-bit variant that mixes two blocks of key data in parallel, ensuring intermediate hash blocks affect each other. ```plaintext h1 += h2; h2 = _rotl64(h2,41); h2 += h1; ``` -------------------------------- ### MurmurHash1 mix function Source: https://github.com/aappleby/smhasher/wiki/MurmurHash1 The core mixing step of MurmurHash1, applied to each block of the key. It combines the block with the hash state, multiplies by a constant, and XORs with a right-shifted version of itself. ```c h += k; h *= m; h ^= h >> r; ``` -------------------------------- ### MurmurHash3 Mix Function Core Operations Source: https://github.com/aappleby/smhasher/wiki/MurmurHash3 Core block mixing operations used in MurmurHash3. `k` is a block of the key, `h` is a block of the hash state, and `rN`/`mN`/`cN` are constants. Each key block is pre-mixed with two constants and a rotate, XORed into the hash block, then the hash block is mixed with a rotate and multiply-add. ```plaintext k *= c1; k = rotl(k,r1); k *= c2; h ^= k; h = rotl(h,r1); h h = h*m1+n1; ``` -------------------------------- ### Test function for cancellation evaluation Source: https://github.com/aappleby/smhasher/wiki/MurmurHash2Flaw Evaluates the hash for all possible x values with m = 0x5bd1e995 and r = 24. Returns only 172,013,942 unique results instead of 2^32, indicating ~4.6 bits cancelled on average. ```c uint32_t test ( uint32_t x ) { const uint32_t m = 0x5bd1e995; const int r = 24; uint32_t h = 0; x *= m; x ^= x >> r; x *= m; h *= m h ^= x; h *= m h ^= x; return h; } ``` -------------------------------- ### MurmurHash3 32-bit 4-Block Inter-Mix Source: https://github.com/aappleby/smhasher/wiki/MurmurHash3 Inter-mix operation for the 32-bit 128-bit variant that mixes four blocks of key data in parallel, ensuring intermediate hash blocks affect each other. `hN` is one block of the hash value. ```plaintext h1 += h2; h1 += h3; h1 += h4; h1 = _rotl(h1,17); h2 += h1; h3 += h1; h4 += h1; ```