osrm-backend/include/util/xor_fast_hash.hpp

96 lines
2.7 KiB
C++
Raw Normal View History

#ifndef XOR_FAST_HASH_HPP
#define XOR_FAST_HASH_HPP
2012-05-25 05:41:52 -04:00
#include <algorithm>
#include <vector>
2016-01-05 10:51:13 -05:00
namespace osrm
{
namespace util
{
2012-05-25 05:41:52 -04:00
/*
2014-05-07 12:39:16 -04:00
This is an implementation of Tabulation hashing, which has suprising properties like
universality.
2012-05-25 05:41:52 -04:00
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
*/
2014-05-07 12:39:16 -04:00
class XORFastHash
{ // 65k entries
std::vector<unsigned short> table1;
std::vector<unsigned short> table2;
2014-05-07 12:39:16 -04:00
public:
XORFastHash()
{
2013-02-26 03:57:29 -05:00
table1.resize(2 << 16);
table2.resize(2 << 16);
2014-05-07 12:39:16 -04:00
for (unsigned i = 0; i < (2 << 16); ++i)
{
table1[i] = static_cast<unsigned short>(i);
table2[i] = static_cast<unsigned short>(i);
2012-05-25 05:41:52 -04:00
}
std::random_shuffle(table1.begin(), table1.end());
std::random_shuffle(table2.begin(), table2.end());
}
inline unsigned short operator()(const unsigned originalValue) const
2014-05-07 12:39:16 -04:00
{
2014-07-15 05:42:27 -04:00
unsigned short lsb = ((originalValue)&0xffff);
2012-05-25 05:41:52 -04:00
unsigned short msb = (((originalValue) >> 16) & 0xffff);
return table1[lsb] ^ table2[msb];
}
};
2014-05-07 12:39:16 -04:00
class XORMiniHash
{ // 256 entries
std::vector<unsigned char> table1;
std::vector<unsigned char> table2;
std::vector<unsigned char> table3;
std::vector<unsigned char> table4;
2012-05-25 05:41:52 -04:00
2014-05-07 12:39:16 -04:00
public:
XORMiniHash()
{
table1.resize(1 << 8);
table2.resize(1 << 8);
table3.resize(1 << 8);
table4.resize(1 << 8);
2014-05-07 12:39:16 -04:00
for (unsigned i = 0; i < (1 << 8); ++i)
{
table1[i] = static_cast<unsigned char>(i);
table2[i] = static_cast<unsigned char>(i);
table3[i] = static_cast<unsigned char>(i);
table4[i] = static_cast<unsigned char>(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());
}
2014-05-07 12:39:16 -04:00
unsigned char operator()(const unsigned originalValue) const
{
2014-07-15 05:42:27 -04:00
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];
}
};
2016-01-05 10:51:13 -05:00
}
}
#endif // XOR_FAST_HASH_HPP