Created
November 6, 2017 20:44
-
-
Save adiog/6d932629af9b0988585d72ab810aee95 to your computer and use it in GitHub Desktop.
hash-constexpr-fnv1a
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdint> | |
// post: https://notes.underscorediscovery.com/constexpr-fnv1a/ | |
constexpr uint32_t val_32_const = 0x811c9dc5; | |
constexpr uint32_t prime_32_const = 0x1000193; | |
constexpr uint64_t val_64_const = 0xcbf29ce484222325; | |
constexpr uint64_t prime_64_const = 0x100000001b3; | |
inline constexpr uint32_t hash_32_fnv1a_const(const char* const str, const uint32_t value = val_32_const) noexcept { | |
return (str[0] == '\0') ? value : hash_32_fnv1a_const(&str[1], (value ^ uint32_t(str[0])) * prime_32_const); | |
} | |
inline constexpr uint64_t hash_64_fnv1a_const(const char* const str, const uint64_t value = val_64_const) noexcept { | |
return (str[0] == '\0') ? value : hash_64_fnv1a_const(&str[1], (value ^ uint64_t(str[0])) * prime_64_const); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment