summaryrefslogtreecommitdiffhomepage
path: root/compat/hash.cpp
blob: 3cb3cc35428f497899f2283a3ac84e1c63c4f8fc (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "compat/defs.hpp"
#include "hash.hpp"
#include <Corrade/Containers/StringView.h>
#include <bit>
#include <utility>

namespace floormat {

namespace {

using namespace floormat::Hash;

[[maybe_unused]]
CORRADE_ALWAYS_INLINE size_t fnvhash_uint_32(uint32_t x)
{
    constexpr auto params = fnvhash_params<sizeof(size_t)*8>{};
    constexpr auto a = params.a, b = params.b;
    auto hash = a;
    const auto* str = (const char*)&x;

    hash *= b; hash ^= (uint8_t)*str++; // 0
    hash *= b; hash ^= (uint8_t)*str++; // 1
    hash *= b; hash ^= (uint8_t)*str++; // 2
    hash *= b; hash ^= (uint8_t)*str++; // 3

    return hash;
}

CORRADE_ALWAYS_INLINE size_t fnvhash_uint_64(uint64_t x)
{
    constexpr auto params = fnvhash_params<sizeof(std::common_type_t<size_t>)*8>{};
    constexpr auto a = params.a, b = params.b;
    auto hash = a;
    const auto* str = (const char*)&x;

    hash *= b; hash ^= (uint8_t)*str++; // 0
    hash *= b; hash ^= (uint8_t)*str++; // 1
    hash *= b; hash ^= (uint8_t)*str++; // 2
    hash *= b; hash ^= (uint8_t)*str++; // 3
    hash *= b; hash ^= (uint8_t)*str++; // 4
    hash *= b; hash ^= (uint8_t)*str++; // 5
    hash *= b; hash ^= (uint8_t)*str++; // 6
    hash *= b; hash ^= (uint8_t)*str++; // 7

    return hash;
}

} // namespace

uint32_t hash_32(const void* buf, size_t size) noexcept
{
    constexpr auto params = fnvhash_params<32>{};
    constexpr auto a = params.a, b = params.b;
    auto hash = a;
    const auto *str = (const char*)buf, *const end = str + size;

fm_UNROLL_4
    for (; str != end; ++str)
    {
        hash *= b;
        hash ^= (uint8_t)*str;
    }
    return hash;
}

uint64_t hash_64(const void* buf, size_t size) noexcept
{
    constexpr auto params = fnvhash_params<sizeof(size_t)*8>{};
    constexpr auto a = params.a, b = params.b;
    auto hash = a;
    const auto *str = (const char*)buf, *const end = str + size;

fm_UNROLL_8
    for (; str != end; ++str)
    {
        hash *= b;
        hash ^= (uint8_t)*str;
    }
    return hash;
}

size_t hash_buf(const void* buf, size_t size) noexcept
{
#if 1
    if constexpr(sizeof nullptr > 4)
        return hash_64(buf, size);
    else
#endif
        return hash_32(buf, size);
}

size_t hash_int(uint32_t x) noexcept
{
#if 1
    if constexpr(sizeof nullptr > 4)
        return fnvhash_uint_64(x);
    else
#endif
        return fnvhash_uint_32(x);
}

size_t hash_int(uint64_t x) noexcept
{
    return fnvhash_uint_64(x);
}

size_t hash_string_view::operator()(StringView s) const noexcept { return Hash::fnvhash_buf(s.data(), s.size()); }

} // namespace floormat

namespace floormat::Hash {

size_t fnvhash_buf(const void* __restrict buf, size_t size, size_t seed) noexcept
{
    constexpr size_t b{fnvhash_params<sizeof nullptr*8>::b};
    size_t full_rounds = size / 8, rest = size % 8;
    size_t hash = seed;
    const char* str = (const char*)buf;

    while (full_rounds--)
    {
        hash *= b; hash ^= (uint8_t)*str++; // 0
        hash *= b; hash ^= (uint8_t)*str++; // 1
        hash *= b; hash ^= (uint8_t)*str++; // 2
        hash *= b; hash ^= (uint8_t)*str++; // 3
        hash *= b; hash ^= (uint8_t)*str++; // 4
        hash *= b; hash ^= (uint8_t)*str++; // 5
        hash *= b; hash ^= (uint8_t)*str++; // 6
        hash *= b; hash ^= (uint8_t)*str++; // 7
    }
    switch (rest)
    {
    case 7: hash *= b; hash ^= (uint8_t)*str++; [[fallthrough]];
    case 6: hash *= b; hash ^= (uint8_t)*str++; [[fallthrough]];
    case 5: hash *= b; hash ^= (uint8_t)*str++; [[fallthrough]];
    case 4: hash *= b; hash ^= (uint8_t)*str++; [[fallthrough]];
    case 3: hash *= b; hash ^= (uint8_t)*str++; [[fallthrough]];
    case 2: hash *= b; hash ^= (uint8_t)*str++; [[fallthrough]];
    case 1: hash *= b; hash ^= (uint8_t)*str++; [[fallthrough]];
    case 0: break;
    default: std::unreachable();
    }
    return hash;
}

} // namespace floormat::Hash