• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2018 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // hash_utils.h: Hashing based helper functions.
7 
8 #ifndef COMMON_HASHUTILS_H_
9 #define COMMON_HASHUTILS_H_
10 
11 #include "common/debug.h"
12 #include "xxhash.h"
13 
14 namespace angle
15 {
16 // Computes a hash of "key". Any data passed to this function must be multiples of
17 // 4 bytes, since the PMurHash32 method can only operate increments of 4-byte words.
ComputeGenericHash(const void * key,size_t keySize)18 inline size_t ComputeGenericHash(const void *key, size_t keySize)
19 {
20     constexpr unsigned int kSeed = 0xABCDEF98;
21 
22     // We can't support "odd" alignments.  ComputeGenericHash requires aligned types
23     ASSERT(keySize % 4 == 0);
24 #if defined(ANGLE_IS_64_BIT_CPU)
25     return XXH64(key, keySize, kSeed);
26 #else
27     return XXH32(key, keySize, kSeed);
28 #endif  // defined(ANGLE_IS_64_BIT_CPU)
29 }
30 
31 template <typename T>
ComputeGenericHash(const T & key)32 size_t ComputeGenericHash(const T &key)
33 {
34     static_assert(sizeof(key) % 4 == 0, "ComputeGenericHash requires aligned types");
35     return ComputeGenericHash(&key, sizeof(key));
36 }
37 
HashCombine(size_t & seed)38 inline void HashCombine(size_t &seed) {}
39 
40 template <typename T, typename... Rest>
HashCombine(std::size_t & seed,const T & hashableObject,Rest...rest)41 inline void HashCombine(std::size_t &seed, const T &hashableObject, Rest... rest)
42 {
43     std::hash<T> hasher;
44     seed ^= hasher(hashableObject) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
45     HashCombine(seed, rest...);
46 }
47 
48 template <typename T, typename... Rest>
HashMultiple(const T & hashableObject,Rest...rest)49 inline size_t HashMultiple(const T &hashableObject, Rest... rest)
50 {
51     size_t seed = 0;
52     HashCombine(seed, hashableObject, rest...);
53     return seed;
54 }
55 
56 }  // namespace angle
57 
58 #endif  // COMMON_HASHUTILS_H_
59