summaryrefslogtreecommitdiffhomepage
path: root/compat/int-hash.hpp
blob: ee2dbf166d55d463194eb07a294356f0521da033 (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
#pragma once
#include "integer-types.hpp"

namespace floormat {

constexpr inline std::size_t int_hash(std::size_t x) noexcept
{
    if constexpr(sizeof(std::size_t) == 4)
    {
        // by Chris Wellons <https://nullprogram.com/blog/2018/07/31/>
        x ^= x >> 15;
        x *= 0x2c1b3c6dU;
        x ^= x >> 12;
        x *= 0x297a2d39U;
        x ^= x >> 15;
    }
    else if constexpr(sizeof(std::size_t) == 8)
    {
        // splitmix64 by George Marsaglia
        x ^= x >> 30;
        x *= 0xbf58476d1ce4e5b9U;
        x ^= x >> 27;
        x *= 0x94d049bb133111ebU;
        x ^= x >> 31;
    }

    return x;
}

} // namespace floormat