yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie Hermaszewskaformatf65d756bf

master
2.5 KiB99 linesraw
1#pragma once
2
3#include <cstdint>
4#include <cstring>
5#include <type_traits>
6
7namespace Slang
8{
9//
10// Types
11//
12
13struct StableHashCode64
14{
15    uint64_t hash;
16    explicit operator uint64_t() const { return hash; }
17    bool operator==(StableHashCode64 other) const { return other.hash == hash; };
18    bool operator!=(StableHashCode64 other) const { return other.hash != hash; };
19};
20
21struct StableHashCode32
22{
23    uint32_t hash;
24    explicit operator uint32_t() const { return hash; }
25    bool operator==(StableHashCode32 other) const { return other.hash == hash; };
26    bool operator!=(StableHashCode32 other) const { return other.hash != hash; };
27};
28
29/* The 'Stable' hash code functions produce hashes that must be
30
31* The same result for the same inputs on all targets
32* Rarely change - as their values can change the output of the Slang API/Serialization
33
34Hash value used from the 'Stable' functions can also be used as part of serialization -
35so it is in effect part of the API.
36
37In effect this means changing a 'Stable' algorithm will typically require doing a new release.
38*/
39inline StableHashCode64 getStableHashCode64(const char* buffer, size_t numChars)
40{
41    uint64_t hash = 0;
42    for (size_t i = 0; i < numChars; ++i)
43    {
44        hash = uint64_t(buffer[i]) + (hash << 6) + (hash << 16) - hash;
45    }
46    return StableHashCode64{hash};
47}
48
49template<typename T>
50inline StableHashCode64 getStableHashCode64(const T& t)
51{
52    static_assert(std::has_unique_object_representations_v<T>);
53    return getStableHashCode64(reinterpret_cast<const char*>(&t), sizeof(T));
54}
55
56inline StableHashCode32 getStableHashCode32(const char* buffer, size_t numChars)
57{
58    uint32_t hash = 0;
59    for (size_t i = 0; i < numChars; ++i)
60    {
61        hash = uint32_t(buffer[i]) + (hash << 6) + (hash << 16) - hash;
62    }
63    return StableHashCode32{hash};
64}
65
66template<typename T>
67inline StableHashCode32 getStableHashCode32(const T& t)
68{
69    static_assert(std::has_unique_object_representations_v<T>);
70    return getStableHashCode32(reinterpret_cast<const char*>(&t), sizeof(T));
71}
72
73inline StableHashCode64 combineStableHash(StableHashCode64 h)
74{
75    return h;
76}
77
78inline StableHashCode32 combineStableHash(StableHashCode32 h)
79{
80    return h;
81}
82
83// A left fold with a mixing operation
84template<typename H, typename... Hs>
85H combineStableHash(H n, H m, Hs... args)
86{
87    return combineStableHash(H{(n.hash * 16777619) ^ m.hash}, args...);
88}
89} // namespace Slang
90
91// > Please draw a small horse in ASCII art:
92//
93//           ,~~.
94//          (  9 )-_,
95//  (\___ )=='-' )
96//   \ .   ) )  /
97//    \ `-' /  /
98// ~'`~'`~'`~'`~
99//