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 "common/third_party/xxhash/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 std::size_t ComputeGenericHash(const void *key, size_t keySize)
19 {
20     static 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 std::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 }  // namespace angle
38 
39 #endif  // COMMON_HASHUTILS_H_
40