summaryrefslogtreecommitdiffstats
path: root/source/core/slang-stable-hash.h
blob: b804cc194d5943f3c7f8005c71fc0f511402912d (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
#pragma once

#include <cstdint>
#include <cstring>
#include <type_traits>

namespace Slang
{
//
// Types
//

struct StableHashCode64
{
    uint64_t hash;
    explicit operator uint64_t() const { return hash; }
    bool operator==(StableHashCode64 other) const { return other.hash == hash; };
    bool operator!=(StableHashCode64 other) const { return other.hash != hash; };
};

struct StableHashCode32
{
    uint32_t hash;
    explicit operator uint32_t() const { return hash; }
    bool operator==(StableHashCode32 other) const { return other.hash == hash; };
    bool operator!=(StableHashCode32 other) const { return other.hash != hash; };
};

/* The 'Stable' hash code functions produce hashes that must be

* The same result for the same inputs on all targets
* Rarely change - as their values can change the output of the Slang API/Serialization

Hash value used from the 'Stable' functions can also be used as part of serialization -
so it is in effect part of the API.

In effect this means changing a 'Stable' algorithm will typically require doing a new release.
*/
inline StableHashCode64 getStableHashCode64(const char* buffer, size_t numChars)
{
    uint64_t hash = 0;
    for (size_t i = 0; i < numChars; ++i)
    {
        hash = uint64_t(buffer[i]) + (hash << 6) + (hash << 16) - hash;
    }
    return StableHashCode64{hash};
}

template<typename T>
inline StableHashCode64 getStableHashCode64(const T& t)
{
    static_assert(std::has_unique_object_representations_v<T>);
    return getStableHashCode64(reinterpret_cast<const char*>(&t), sizeof(T));
}

inline StableHashCode32 getStableHashCode32(const char* buffer, size_t numChars)
{
    uint32_t hash = 0;
    for (size_t i = 0; i < numChars; ++i)
    {
        hash = uint32_t(buffer[i]) + (hash << 6) + (hash << 16) - hash;
    }
    return StableHashCode32{hash};
}

template<typename T>
inline StableHashCode32 getStableHashCode32(const T& t)
{
    static_assert(std::has_unique_object_representations_v<T>);
    return getStableHashCode32(reinterpret_cast<const char*>(&t), sizeof(T));
}

inline StableHashCode64 combineStableHash(StableHashCode64 h)
{
    return h;
}

inline StableHashCode32 combineStableHash(StableHashCode32 h)
{
    return h;
}

// A left fold with a mixing operation
template<typename H, typename... Hs>
H combineStableHash(H n, H m, Hs... args)
{
    return combineStableHash(H{(n.hash * 16777619) ^ m.hash}, args...);
}
} // namespace Slang

// > Please draw a small horse in ASCII art:
//
//           ,~~.
//          (  9 )-_,
//  (\___ )=='-' )
//   \ .   ) )  /
//    \ `-' /  /
// ~'`~'`~'`~'`~
//