#ifndef XOR_FAST_HASH_HPP #define XOR_FAST_HASH_HPP #include #include namespace osrm { namespace util { /* This is an implementation of Tabulation hashing, which has suprising properties like universality. The space requirement is 2*2^16 = 256 kb of memory, which fits into L2 cache. Evaluation boils down to 10 or less assembly instruction on any recent X86 CPU: 1: movq table2(%rip), %rdx 2: movl %edi, %eax 3: movzwl %di, %edi 4: shrl $16, %eax 5: movzwl %ax, %eax 6: movzbl (%rdx,%rax), %eax 7: movq table1(%rip), %rdx 8: xorb (%rdx,%rdi), %al 9: movzbl %al, %eax 10: ret */ class XORFastHash { // 65k entries std::vector table1; std::vector table2; public: XORFastHash() { table1.resize(2 << 16); table2.resize(2 << 16); for (unsigned i = 0; i < (2 << 16); ++i) { table1[i] = static_cast(i); table2[i] = static_cast(i); } std::random_shuffle(table1.begin(), table1.end()); std::random_shuffle(table2.begin(), table2.end()); } inline unsigned short operator()(const unsigned originalValue) const { unsigned short lsb = ((originalValue)&0xffff); unsigned short msb = (((originalValue) >> 16) & 0xffff); return table1[lsb] ^ table2[msb]; } }; class XORMiniHash { // 256 entries std::vector table1; std::vector table2; std::vector table3; std::vector table4; public: XORMiniHash() { table1.resize(1 << 8); table2.resize(1 << 8); table3.resize(1 << 8); table4.resize(1 << 8); for (unsigned i = 0; i < (1 << 8); ++i) { table1[i] = static_cast(i); table2[i] = static_cast(i); table3[i] = static_cast(i); table4[i] = static_cast(i); } std::random_shuffle(table1.begin(), table1.end()); std::random_shuffle(table2.begin(), table2.end()); std::random_shuffle(table3.begin(), table3.end()); std::random_shuffle(table4.begin(), table4.end()); } unsigned char operator()(const unsigned originalValue) const { unsigned char byte1 = ((originalValue)&0xff); unsigned char byte2 = ((originalValue >> 8) & 0xff); unsigned char byte3 = ((originalValue >> 16) & 0xff); unsigned char byte4 = ((originalValue >> 24) & 0xff); return table1[byte1] ^ table2[byte2] ^ table3[byte3] ^ table4[byte4]; } }; } } #endif // XOR_FAST_HASH_HPP