From 63c019c6d248c53b9715e897da0d23642dce5ad5 Mon Sep 17 00:00:00 2001 From: Yonghua Huang Date: Thu, 14 May 2020 09:56:50 +0800 Subject: [PATCH] hv: Add 64 bits hash function This patch adds hash function to hash 64bit value. Tracked-On: #4550 Signed-off-by: Yonghua Huang Acked-by: Eddie Dong --- hypervisor/include/lib/hash.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 hypervisor/include/lib/hash.h diff --git a/hypervisor/include/lib/hash.h b/hypervisor/include/lib/hash.h new file mode 100644 index 000000000..a014424f1 --- /dev/null +++ b/hypervisor/include/lib/hash.h @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2020 Intel Corporation. All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#ifndef HASH_H +#define HASH_H + +#include + +/* + * Hash factor is selected following below formula: + * - factor64 = 2^64 * ((sqrt(5) - 1)/2) + * here, ((sqrt(5) - 1)/2) is golden ratio. + */ +#define HASH_FACTOR64 0x9E3779B9486E555EUL + +/* + * Hash function multiplies 64bit key by 64bit hash + * factor and returns high bits. + * + * @pre (bits < 64) + */ +static inline uint64_t hash64(uint64_t key, uint32_t bits) +{ + return (key * HASH_FACTOR64) >> (64U - bits); +} + +#endif /* HASH_H */