Hash function for 64 bit to 10 bits
c, gcc, hash, linux, x86-64
Solution
I would say somethig like this:
uint32_t hash(uint64_t x)
{
x >>= 3;
return (x ^ (x>>10) ^ (x>>20)) & 0x3FF;
}
The lest significant 3 bits are not very useful, as most variables are 4-byte or 8-byte aligned, so we remove them. Then we take the next 30 bits and mix them together (XOR) in blocks of 10 bits each.
Naturally, you could also take the `(x>>30)^(x>>40)^(x>>50)` but I'm not sure if they'll make any difference in practice.
Problem
I want a hash function that takes a long number (64 bits) and produces result of 10 bits. What is the best hash function for such purpose. Inputs are basically addresses of variables (Addresses are of 64 bits or 8 bytes on Linux), so my hash function should be optimized for that purpose.