1 /*
2  * Copyright (C) 2020-2022 Intel Corporation.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef HASH_H
8 #define HASH_H
9 
10 #include <types.h>
11 
12 /*
13  * Hash factor is selected following below formula:
14  *  - factor64 = 2^64 * ((sqrt(5) - 1)/2)
15  * here, ((sqrt(5) - 1)/2) is golden ratio.
16  */
17 #define HASH_FACTOR64 0x9E3779B9486E555EUL
18 
19 /*
20  * Hash function multiplies 64bit key by 64bit hash
21  * factor and returns high bits.
22  *
23  * @pre (bits < 64)
24  */
hash64(uint64_t key,uint32_t bits)25 static inline uint64_t hash64(uint64_t key, uint32_t bits)
26 {
27 	return (key * HASH_FACTOR64) >> (64U - bits);
28 }
29 
30 #endif /* HASH_H */
31