I've wrote an implementation of Murmur3 hash and have defined the 128-bit keys as hash128_t
typedef struct
{
uint64_t p1;
uint64_t p2;
} hash128_t;
I'm trying to write my own hashmap using these keys, but I'm not exactly sure how to do arithmetic with a struct nor a number this large. I need help specifically with modulo operations, but help on how to treat these two blocks of memory as one integer would be greatly appreciated.
My modulo formula is
Dividend - ((Dividend/Divisor) * Divisor)
The easiest thing you can do is to simply discard half of the hash and do modulo (simply using
%
) on the other half.The next simplest thing to do is to use an existing bignum library.
If you want to use the whole hash, though, you need to do a long division here. There are a number of ways to actually do this; here's one (completely untested) method:
Note that this will break if divisor is larger than
2^63 - 1
. Fixing this is left as an exercise to the reader, as are various optimizations that are possible if you can constrain the divisor to a 32-bit space.