hash.h
Go to the documentation of this file.
/*
Copyright (c) 2026 - SD-Innovation S.A.S. - FRANCE
*/
/*
ver: 2.x.x - build: 2026-04-28
*/
/*
The zlib License Copyright (c) 2026 SD-Innovation S.A.S. This software is provided ‘as-is’, without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution.
*/
#pragma once
#include <cstdint>
#include <span>
#include <string_view>
namespace sdi_toolBox::common::utils::hash
{
//--------------------------------------------------------------
constexpr std::uint32_t fnv1a(std::span<const std::byte> data) noexcept;
constexpr std::uint32_t fnv1a(std::string_view sv) noexcept;
template<typename T>
requires std::is_trivially_copyable_v<T>
constexpr std::uint32_t fnv1a(const T &value) noexcept;
constexpr std::uint64_t fnv1a_64(std::span<const std::byte> data) noexcept;
constexpr std::uint64_t fnv1a_64(std::string_view sv) noexcept;
template<typename T>
requires std::is_trivially_copyable_v<T>
constexpr std::uint64_t fnv1a_64(const T &value) noexcept;
//--------------------------------------------------------------
//--------------------------------------------------------------
/* Compute the FNV-1a 32-bit hash for a span of bytes */
inline constexpr std::uint32_t fnv1a(const std::span<const std::byte> data) noexcept
{
constexpr std::uint32_t FNV_OFFSET_BASIS = 2166136261u;
constexpr std::uint32_t FNV_PRIME = 16777619u;
std::uint32_t hash = FNV_OFFSET_BASIS;
for (const auto b : data)
{
hash ^= static_cast<std::uint32_t>(std::to_integer<std::uint8_t>(b));
hash *= FNV_PRIME;
}
return hash;
}
//--------------------------------------------------------------
/* Compute the FNV-1a 32-bit hash for a std::string_view */
inline constexpr std::uint32_t fnv1a(const std::string_view sv) noexcept
{
return fnv1a({ reinterpret_cast<const std::byte *>(sv.data()), sv.size() });
}
//--------------------------------------------------------------
/* Compute the FNV-1a 32-bit hash for a trivially copyable POD object */
template<typename T>
requires std::is_trivially_copyable_v<T>
inline constexpr std::uint32_t fnv1a(const T &value) noexcept
{
return fnv1a({ reinterpret_cast<const std::byte *>(std::addressof(value)), sizeof(T) });
}
//--------------------------------------------------------------
/* Compute the FNV-1a 64-bit hash for a span of bytes */
inline constexpr std::uint64_t fnv1a_64(const std::span<const std::byte> data) noexcept
{
constexpr std::uint64_t FNV_OFFSET_BASIS = 14695981039346656037ull;
constexpr std::uint64_t FNV_PRIME = 1099511628211ull;
std::uint64_t hash = FNV_OFFSET_BASIS;
for (const auto b : data)
{
hash ^= static_cast<std::uint64_t>(std::to_integer<std::uint8_t>(b));
hash *= FNV_PRIME;
}
return hash;
}
//--------------------------------------------------------------
/* Compute the FNV-1a 64-bit hash for a std::string_view */
inline constexpr std::uint64_t fnv1a_64(const std::string_view sv) noexcept
{
return fnv1a_64({ reinterpret_cast<const std::byte *>(sv.data()), sv.size() });
}
//--------------------------------------------------------------
/* Compute the FNV-1a 64-bit hash for a trivially copyable POD object */
template<typename T>
requires std::is_trivially_copyable_v<T>
inline constexpr std::uint64_t fnv1a_64(const T &value) noexcept
{
return fnv1a_64(std::span<const std::byte>{ reinterpret_cast<const std::byte *>(std::addressof(value)), sizeof(T) });
}
//--------------------------------------------------------------
} // namespace sdi_toolBox::common::utils::hash
sdi_toolBox::common::utils::hash
Lightweight hashing utilities (FNV-1a implementations).
Definition hash.h:20
sdi_toolBox::common::utils::hash::fnv1a_64
constexpr std::uint64_t fnv1a_64(std::span< const std::byte > data) noexcept
Compute the FNV-1a 64-bit hash for a span of bytes.
Definition hash.h:124
sdi_toolBox::common::utils::hash::fnv1a
constexpr std::uint32_t fnv1a(std::span< const std::byte > data) noexcept
Compute the FNV-1a 32-bit hash for a span of bytes.
Definition hash.h:95