User Tools

Site Tools


cpp:hash
snippet.cpp
//fnv1a 32 and 64 bit hash functions
// key is the data to hash, len is the size of the data (or how much of it to hash against)
// code license: public domain or equivalent
// post: https://notes.underscorediscovery.com/constexpr-fnv1a/
 
constexpr const std::uint32_t hash_32_fnv1a(std::string_view str) 
{   
    std::uint32_t hash = 0x811c9dc5;
    constexpr std::uint32_t prime = 0x1000193;
 
    for (std::uint8_t value : str)
    {
        hash = hash ^ value;
        hash *= prime;
    }
 
    return hash;    
} //hash_32_fnv1a
 
constexpr const uint64_t hash_64_fnv1a(std::string_view str) 
{
    uint64_t hash = 0xcbf29ce484222325;
    constexpr uint64_t prime = 0x100000001b3;
 
    for (std::uint8_t value : str)
    {
        hash = hash ^ value;
        hash *= prime;
    }
 
    return hash;   
} //hash_64_fnv1a
cpp/hash.txt · Last modified: by allspark_ise