yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f65d756bf
master
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{ 15uint64_t hash ; 16explicit operator uint64_t ()const {return hash ; } 17bool operator == (StableHashCode64 other )const {return other .hash == hash ; }; 18bool operator != (StableHashCode64 other )const {return other .hash != hash ; }; 19}; 20 21struct StableHashCode32 22{ 23uint32_t hash ; 24explicit operator uint32_t ()const {return hash ; } 25bool operator == (StableHashCode32 other )const {return other .hash == hash ; }; 26bool 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{ 41uint64_t hash = 0 ; 42for (size_t i = 0 ;i < numChars ;++ i ) 43 { 44hash = uint64_t (buffer [i ])+ (hash <<6 )+ (hash <<16 )- hash ; 45 } 46return StableHashCode64 {hash }; 47} 48 49template < typename T > 50inline StableHashCode64 getStableHashCode64 (const T & t ) 51{ 52static_assert (std ::has_unique_object_representations_v < T > ); 53return getStableHashCode64 (reinterpret_cast < const char *> (& t ),sizeof (T )); 54} 55 56inline StableHashCode32 getStableHashCode32 (const char * buffer ,size_t numChars ) 57{ 58uint32_t hash = 0 ; 59for (size_t i = 0 ;i < numChars ;++ i ) 60 { 61hash = uint32_t (buffer [i ])+ (hash <<6 )+ (hash <<16 )- hash ; 62 } 63return StableHashCode32 {hash }; 64} 65 66template < typename T > 67inline StableHashCode32 getStableHashCode32 (const T & t ) 68{ 69static_assert (std ::has_unique_object_representations_v < T > ); 70return getStableHashCode32 (reinterpret_cast < const char *> (& t ),sizeof (T )); 71} 72 73inline StableHashCode64 combineStableHash (StableHashCode64 h ) 74{ 75return h ; 76} 77 78inline StableHashCode32 combineStableHash (StableHashCode32 h ) 79{ 80return 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{ 87return 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//