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