• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef COMMON_HASHUTILS_H_
16 #define COMMON_HASHUTILS_H_
17 
18 #include "common/Platform.h"
19 
20 #include <bitset>
21 #include <functional>
22 
23 // Wrapper around std::hash to make it a templated function instead of a functor. It is marginally
24 // nicer, and avoids adding to the std namespace to add hashing of other types.
25 template <typename T>
Hash(const T & value)26 size_t Hash(const T& value) {
27     return std::hash<T>()(value);
28 }
29 
30 // When hashing sparse structures we want to iteratively build a hash value with only parts of the
31 // data. HashCombine "hashes" together an existing hash and hashable values.
32 //
33 // Example usage to compute the hash of a mask and values corresponding to the mask:
34 //
35 //    size_t hash = Hash(mask):
36 //    for (uint32_t i : IterateBitSet(mask)) { HashCombine(&hash, hashables[i]); }
37 //    return hash;
38 template <typename T>
HashCombine(size_t * hash,const T & value)39 void HashCombine(size_t* hash, const T& value) {
40 #if defined(DAWN_PLATFORM_64_BIT)
41     const size_t offset = 0x9e3779b97f4a7c16;
42 #elif defined(DAWN_PLATFORM_32_BIT)
43     const size_t offset = 0x9e3779b9;
44 #else
45 #    error "Unsupported platform"
46 #endif
47     *hash ^= Hash(value) + offset + (*hash << 6) + (*hash >> 2);
48 }
49 
50 template <typename T, typename... Args>
HashCombine(size_t * hash,const T & value,const Args &...args)51 void HashCombine(size_t* hash, const T& value, const Args&... args) {
52     HashCombine(hash, value);
53     HashCombine(hash, args...);
54 }
55 
56 // Workaround a bug between clang++ and libstdlibc++ by defining our own hashing for bitsets.
57 // When _GLIBCXX_DEBUG is enabled libstdc++ wraps containers into debug containers. For bitset this
58 // means what is normally std::bitset is defined as std::__cxx1988::bitset and is replaced by the
59 // debug version of bitset.
60 // When hashing, std::hash<std::bitset> proxies the call to std::hash<std::__cxx1998::bitset> and
61 // fails on clang because the latter tries to access the private _M_getdata member of the bitset.
62 // It looks like it should work because the non-debug bitset declares
63 //
64 //     friend struct std::hash<bitset> // bitset is the name of the class itself
65 //
66 // which should friend std::hash<std::__cxx1998::bitset> but somehow doesn't work on clang.
67 #if defined(_GLIBCXX_DEBUG)
68 template <size_t N>
Hash(const std::bitset<N> & value)69 size_t Hash(const std::bitset<N>& value) {
70     constexpr size_t kWindowSize = sizeof(unsigned long long);
71 
72     std::bitset<N> bits = value;
73     size_t hash = 0;
74     for (size_t processedBits = 0; processedBits < N; processedBits += kWindowSize) {
75         HashCombine(&hash, bits.to_ullong());
76         bits >>= kWindowSize;
77     }
78 
79     return hash;
80 }
81 #endif
82 
83 #endif  // COMMON_HASHUTILS_H_
84