Commit e1e0e0ed authored by Davis King's avatar Davis King

Added an overload of murmur_hash3_128bit() that takes 4 integers instead

of a block of memory.
parent 9aecb4c4
......@@ -326,6 +326,53 @@ namespace dlib
return std::make_pair(h1,h2);
}
// ----------------------------------------------------------------------------------------
inline std::pair<uint64,uint64> murmur_hash3_128bit (
const uint32& v1,
const uint32& v2,
const uint32& v3,
const uint32& v4
)
{
uint64 h1 = 0;
uint64 h2 = 0;
const uint64 c1 = DLIB_BIG_CONSTANT(0x87c37b91114253d5);
const uint64 c2 = DLIB_BIG_CONSTANT(0x4cf5ad432745937f);
//----------
// body
uint64 k1 = (static_cast<uint64>(v2)<<32)|v1;
uint64 k2 = (static_cast<uint64>(v4)<<32)|v3;
k1 *= c1; k1 = DLIB_ROTL64(k1,31); k1 *= c2;
h1 = DLIB_ROTL64(k1,27); h1 = h1*5+0x52dce729;
k2 *= c2; k2 = DLIB_ROTL64(k2,33); k2 *= c1;
h2 = DLIB_ROTL64(k2,31); h2 += h1; h2 = h2*5+0x38495ab5;
//----------
// finalization
h1 ^= 16; h2 ^= 16;
h1 += h2;
h2 += h1;
h1 = murmur_fmix(h1);
h2 = murmur_fmix(h2);
h1 += h2;
h2 += h1;
return std::make_pair(h1,h2);
}
// ----------------------------------------------------------------------------------------
}
......
......@@ -50,6 +50,24 @@ namespace dlib
See: http://code.google.com/p/smhasher/
!*/
// ----------------------------------------------------------------------------------------
inline std::pair<uint64,uint64> murmur_hash3_128bit (
const uint32& v1,
const uint32& v2,
const uint32& v3,
const uint32& v4
);
/*!
ensures
- returns a 128bit hash (as two 64bit numbers) of the 4 integers given to this
function.
- This function is machine architecture agnostic and should always give the
same hash value when presented with the same inputs.
- This hashing algorithm is Austin Appleby's excellent MurmurHash3_x64_128.
See: http://code.google.com/p/smhasher/
!*/
// ----------------------------------------------------------------------------------------
}
......
......@@ -6,6 +6,7 @@
#include <cstdlib>
#include <ctime>
#include <dlib/hash.h>
#include <dlib/rand.h>
#include <dlib/matrix.h>
#include <dlib/byte_orderer.h>
......@@ -107,6 +108,28 @@ namespace
DLIB_TEST(final == 0x6384BA69);
}
void test_murmur_hash_128_4()
{
dlib::rand rnd;
for (int i = 0; i < 100; ++i)
{
uint32 buf[4] = { rnd.get_random_32bit_number(),
rnd.get_random_32bit_number(),
rnd.get_random_32bit_number(),
rnd.get_random_32bit_number()
};
std::pair<uint64,uint64> temp1, temp2;
// Make sure the 4 integer version of murmur hash does the same thing
// as the memory block version.
temp1 = murmur_hash3_128bit(buf, sizeof(buf), 0);
temp2 = murmur_hash3_128bit(buf[0], buf[1], buf[2], buf[3]);
DLIB_TEST( temp1.first == temp2.first);
DLIB_TEST( temp1.second == temp2.second);
}
}
class test_hash : public tester
{
public:
......@@ -183,6 +206,7 @@ namespace
DLIB_TEST(dlib::hash(mat2,6) == 0xb8aa7714);
DLIB_TEST(murmur_hash3(&str1[0], str1.size(), 1) == 0xb17cea93);
test_murmur_hash_128_4();
}
} a;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment