2014-12-16 19:57:19 +01:00
|
|
|
#include "hash.hh"
|
|
|
|
|
|
|
|
#include <cstdint>
|
2017-06-26 12:27:18 +02:00
|
|
|
#include <cstring>
|
2014-12-16 19:57:19 +01:00
|
|
|
|
2021-04-17 22:19:35 +02:00
|
|
|
#include "unit_tests.hh"
|
|
|
|
#include "assert.hh"
|
|
|
|
|
2014-12-16 19:57:19 +01:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
static inline uint32_t rotl(uint32_t x, int8_t r)
|
|
|
|
{
|
|
|
|
return (x << r) | (x >> (32 - r));
|
|
|
|
}
|
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
static inline uint32_t fmix(uint32_t h)
|
|
|
|
{
|
|
|
|
h ^= h >> 16;
|
|
|
|
h *= 0x85ebca6b;
|
|
|
|
h ^= h >> 13;
|
|
|
|
h *= 0xc2b2ae35;
|
|
|
|
h ^= h >> 16;
|
|
|
|
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
// murmur3 hash, based on https://github.com/PeterScott/murmur3
|
|
|
|
size_t hash_data(const char* input, size_t len)
|
|
|
|
{
|
|
|
|
const uint8_t* data = reinterpret_cast<const uint8_t*>(input);
|
|
|
|
uint32_t hash = 0x1235678;
|
|
|
|
constexpr uint32_t c1 = 0xcc9e2d51;
|
|
|
|
constexpr uint32_t c2 = 0x1b873593;
|
|
|
|
|
2021-05-28 09:03:06 +02:00
|
|
|
const ptrdiff_t nblocks = len / 4;
|
2017-06-26 12:27:18 +02:00
|
|
|
const uint8_t* blocks = data + nblocks*4;
|
2014-12-16 19:57:19 +01:00
|
|
|
|
2021-05-28 09:03:06 +02:00
|
|
|
for (ptrdiff_t i = -nblocks; i; ++i)
|
2014-12-16 19:57:19 +01:00
|
|
|
{
|
2017-06-26 12:27:18 +02:00
|
|
|
uint32_t key;
|
|
|
|
memcpy(&key, blocks + 4*i, 4);
|
2014-12-16 19:57:19 +01:00
|
|
|
key *= c1;
|
|
|
|
key = rotl(key, 15);
|
|
|
|
key *= c2;
|
|
|
|
|
|
|
|
hash ^= key;
|
|
|
|
hash = rotl(hash, 13);
|
|
|
|
hash = hash * 5 + 0xe6546b64;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t* tail = data + nblocks * 4;
|
|
|
|
uint32_t key = 0;
|
2021-04-17 22:19:35 +02:00
|
|
|
switch (len & 0b11)
|
2014-12-16 19:57:19 +01:00
|
|
|
{
|
2021-04-19 01:06:05 +02:00
|
|
|
case 3: key ^= tail[2] << 16; [[fallthrough]];
|
|
|
|
case 2: key ^= tail[1] << 8; [[fallthrough]];
|
2014-12-16 19:57:19 +01:00
|
|
|
case 1: key ^= tail[0];
|
|
|
|
key *= c1;
|
|
|
|
key = rotl(key,15);
|
|
|
|
key *= c2;
|
|
|
|
hash ^= key;
|
|
|
|
}
|
|
|
|
|
|
|
|
hash ^= len;
|
|
|
|
hash = fmix(hash);
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2021-04-19 01:06:05 +02:00
|
|
|
UnitTest test_murmur_hash{[] {
|
2021-04-17 22:19:35 +02:00
|
|
|
{
|
|
|
|
constexpr char data[] = "Hello, World!";
|
|
|
|
kak_assert(hash_data(data, strlen(data)) == 0xf816f95b);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
constexpr char data[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
|
|
|
|
kak_assert(hash_data(data, strlen(data)) == 3551113186);
|
|
|
|
}
|
|
|
|
kak_assert(hash_data("", 0) == 2572747774);
|
|
|
|
}};
|
|
|
|
|
2014-12-16 19:57:19 +01:00
|
|
|
}
|