From 7630df84ad5b8bb2b44097af2f76beae74661389 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Fri, 10 Feb 2023 02:59:30 +0200 Subject: [PATCH] BAN: Add basic hashing functionality --- BAN/include/BAN/Hash.h | 50 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 BAN/include/BAN/Hash.h diff --git a/BAN/include/BAN/Hash.h b/BAN/include/BAN/Hash.h new file mode 100644 index 00000000..b13e8250 --- /dev/null +++ b/BAN/include/BAN/Hash.h @@ -0,0 +1,50 @@ +#pragma once + +#include +#include +#include + +namespace BAN +{ + + template + struct hash; + + using hash_t = uint32_t; + + inline constexpr hash_t u32_hash(uint32_t val) + { + val = ((val >> 16) ^ val) * 0x119de1f3; + val = ((val >> 16) ^ val) * 0x119de1f3; + val = ((val >> 16) ^ val); + return val; + } + + inline constexpr hash_t u64_hash(uint64_t val) + { + hash_t low = u32_hash(val); + hash_t high = u32_hash(val >> 32); + return low ^ high; + } + + template + struct hash + { + constexpr hash_t operator()(T val) const + { + if constexpr(sizeof(val) <= sizeof(uint32_t)) + return u32_hash(val); + return u64_hash(val); + } + }; + + template + struct hash + { + constexpr hash_t operator()(T val) const + { + return hash()((uintptr_t)val); + } + }; + +} \ No newline at end of file