1 /*
2 * Utilities for working with hash values.
3 *
4 * Portions Copyright (c) 2017-2018, PostgreSQL Global Development Group
5 */
6
7 #ifndef HASHUTILS_H
8 #define HASHUTILS_H
9
10 /*
11 * Combine two 32-bit hash values, resulting in another hash value, with
12 * decent bit mixing.
13 *
14 * Similar to boost's hash_combine().
15 */
16 static inline uint32
hash_combine(uint32 a,uint32 b)17 hash_combine(uint32 a, uint32 b)
18 {
19 a ^= b + 0x9e3779b9 + (a << 6) + (a >> 2);
20 return a;
21 }
22
23 /*
24 * Combine two 64-bit hash values, resulting in another hash value, using the
25 * same kind of technique as hash_combine(). Testing shows that this also
26 * produces good bit mixing.
27 */
28 static inline uint64
hash_combine64(uint64 a,uint64 b)29 hash_combine64(uint64 a, uint64 b)
30 {
31 /* 0x49a0f4dd15e5a8e3 is 64bit random data */
32 a ^= b + UINT64CONST(0x49a0f4dd15e5a8e3) + (a << 54) + (a >> 7);
33 return a;
34 }
35
36 /*
37 * Simple inline murmur hash implementation hashing a 32 bit integer, for
38 * performance.
39 */
40 static inline uint32
murmurhash32(uint32 data)41 murmurhash32(uint32 data)
42 {
43 uint32 h = data;
44
45 h ^= h >> 16;
46 h *= 0x85ebca6b;
47 h ^= h >> 13;
48 h *= 0xc2b2ae35;
49 h ^= h >> 16;
50 return h;
51 }
52
53 #endif /* HASHUTILS_H */
54