summaryrefslogtreecommitdiffhomepage
path: root/compat/int-hash.cpp
blob: f9ae19c1a5ae61283b979a745c0823d332284b51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "compat/defs.hpp"
#include "int-hash.hpp"
#include <bit>

namespace floormat {

namespace {

[[maybe_unused]]
CORRADE_ALWAYS_INLINE uint32_t FNVHash_32(uint32_t x)
{
    const auto *str = (const char*)&x, *end = str + 4;
    uint32_t hash = 0x811c9dc5u;
fm_UNROLL_4
    for (; str != end; ++str)
    {
        hash *= 0x01000193u;
        hash ^= (uint8_t)*str;
    }
    return hash;
}

CORRADE_ALWAYS_INLINE uint64_t FNVHash_64(uint64_t x)
{
    const auto *str = (const char*)&x, *end = str + 8;
    uint64_t hash = 0xcbf29ce484222325u;
fm_UNROLL_8
    for (; str != end; ++str)
    {
        hash *= 0x100000001b3u;
        hash ^= (uint8_t)*str;
    }
    return hash;
}

} // namespace

uint64_t fnvhash_32(const void* buf, size_t size)
{
    const auto *str = (const char*)buf, *const end = str + size;
    uint32_t hash = 0x811c9dc5u;
fm_UNROLL_8
    for (; str != end; ++str)
    {
        hash *= 0x01000193u;
        hash ^= (uint8_t)*str;
    }
    return hash;
}

uint64_t fnvhash_64(const void* buf, size_t size)
{
    const auto *str = (const char*)buf, *const end = str + size;
    uint64_t hash = 0xcbf29ce484222325u;
fm_UNROLL_4
    for (; str != end; ++str)
    {
        hash *= 0x100000001b3u;
        hash ^= (uint8_t)*str;
    }
    return hash;
}

size_t int_hash(uint32_t x) noexcept
{
    if constexpr(sizeof(size_t) == 4)
        return FNVHash_32(x);
    else
        return FNVHash_64(x);
}

size_t int_hash(uint64_t x) noexcept
{
    return FNVHash_64(x);
}

} // namespace floormat