Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
hash.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_HASH_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_HASH_HPP_
7
8// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
10#include "arene/base/span.hpp"
22// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
23
24// The iwyu-private pragma below will direct users to include '<functional>' for use
25// of 'std::hash'
26//
27// Components that provide a specializtion of 'std::hash', should also provide
28// an iwyu-export pragma so that an include of <functional> is not needlessly
29// recommended.
30
31// IWYU pragma: private, include <functional>
32// IWYU pragma: friend "stdlib_detail/.*"
33
34// parasoft-begin-suppress CERT_CPP-DCL58-a-2 "Part of a standard library implementation"
35// parasoft-begin-suppress AUTOSAR-A17_6_1-a-2 "Part of a standard library implementation"
36
37namespace std {
38
39namespace hash_detail {
40
41/// @brief The FNV prime to use depending on bit variant.
42/// @tparam Scalar the scalar type
43/// @tparam Dummy a dummy type to allow partial specialization of variable template
44///
45/// The value of this constant depends on the bit length of the hash being used, determined by the type of @c Scalar.
46/// The value of this constant for common bit-lengths is provided by the paper "The FNV Non-Cryptographic Hash
47/// Algorithm".
48template <class Scalar, class Dummy = nullptr_t>
49extern constexpr Scalar fnv_prime_v{0};
50
51/// @brief The FNV 64-bit prime
52/// @tparam Dummy a dummy type to allow partial specialization of variable template
53template <class Dummy>
54extern constexpr uint64_t fnv_prime_v<uint64_t, Dummy>{1099511628211ULL};
55
56/// @brief The FNV 32-bit prime
57/// @tparam Dummy a dummy type to allow partial specialization of variable template
58template <class Dummy>
59extern constexpr uint32_t fnv_prime_v<uint32_t, Dummy>{16777619UL};
60
61/// @brief The FNV offset basis to use depending on bit variant.
62/// @tparam Scalar the scalar type
63/// @tparam Dummy a dummy type to allow partial specialization of variable template
64///
65/// The value of this constant depends on the bit length of the hash being used, determined by the type of @c Scalar.
66/// The value of this constant for common bit-lengths is provided by the paper "The FNV Non-Cryptographic Hash
67/// Algorithm".
68template <class Scalar, class Dummy = nullptr_t>
69extern constexpr Scalar fnv_offset_basis_v{0};
70
71/// @brief The FNV 64-bit offset basis
72/// @tparam Dummy a dummy type to allow partial specialization of variable template
73template <class Dummy>
74constexpr uint64_t fnv_offset_basis_v<uint64_t, Dummy>{14695981039346656037ULL};
75
76/// @brief The FNV 32-bit offset basis
77/// @tparam Dummy a dummy type to allow partial specialization of variable template
78template <class Dummy>
80
81/// @brief Canonicalize any special values of the given type that compare equal, so their hashes will be equal too
82/// @tparam T Type of the value to canonicalize; SFINAE overload for floating point
83/// @param value Value to canonicalize
84/// @return The unique canonical representative of <c>value</c>'s equivalence class
85template <class T, arene::base::constraints<std::enable_if_t<std::is_floating_point_v<T>>> = nullptr>
86constexpr auto canonicalize_special_values(T value) noexcept -> T {
87 // Canonicalize both +0.0 and -0.0 to +0.0
88 // parasoft-begin-suppress AUTOSAR-M6_2_2-a "std::hash is required to reflect exact equality so we must check for it"
90 ARENE_IGNORE_ALL("-Wfloat-equal", "Checking specifically for exact 0.0 so it can be specially handled");
91 if (value == T{}) {
92 return T{};
93 }
95 // parasoft-end-suppress AUTOSAR-M6_2_2-a
96 return value;
97}
98
99/// @brief Canonicalize any special values of the given type that compare equal, so their hashes will be equal too
100/// @tparam T Type of the value to canonicalize; SFINAE overload for non-floating point
101/// The value to canonicalize is passed as @c value.
102/// @cond CPPTEST_PARAMETER_DOCUMENTATION
103/// @param value The value to canonicalize
104/// @endcond
105/// @return The unique canonical representative of <c>value</c>'s equivalence class
106template <class T, arene::base::constraints<std::enable_if_t<!std::is_floating_point_v<T>>> = nullptr>
107constexpr auto canonicalize_special_values(T value) noexcept -> T {
108 return value;
109}
110
111/// @brief FNV-1a hash algorithm
112/// @tparam T the type to hash
113///
114/// Implement the Fowler-Noll-Vo hashing algorithm with the 1a variant. This hashing algorithm uses two constants that
115/// depend on the bit-length of @c std::size_t for the current platform.
116///
117/// The algorithm starts with an initial hash value of FNV offset basis. For each byte in the input, XOR the hash with
118/// the byte from the input, then multiply it by the FNV prime:
119/// @code
120/// hash := FNV_offset_basis
121/// for each byte_of_data to be hashed do
122/// hash := hash XOR byte_of_data
123/// hash := hash * FNV_prime
124/// return hash
125/// @endcode
126template <typename T>
128 // parasoft-begin-suppress AUTOSAR-A0_4_2-a "This assertion exists to prevent users from using long double"
129 static_assert(
130 !is_same_v<remove_cv_t<T>, long double>,
131 "long double is not supported in the Arene Base Standard Library"
132 );
133 // parasoft-end-suppress AUTOSAR-A0_4_2-a
134
135 public:
136 /// @brief The type of the result of the function call operator
138 /// @brief The type of the argument of the function call operator
139 using argument_type = T;
140
141 /// @brief Calculate the hash of a value
142 /// @param value The value to hash
143 /// @return The hash of the value
144 auto operator()(T value) const noexcept -> size_t {
146
147 using scalar_type = conditional_t<sizeof(size_t) == sizeof(uint32_t), uint32_t, uint64_t>;
148
150
151 for (auto const elem : arene::base::as_bytes(arene::base::span<T const, 1U>(&value, 1U))) {
152 hash ^= static_cast<scalar_type>(arene::base::to_underlying(elem));
154 }
155 return hash;
156 }
157};
158
159/// @brief Default implementation of @c std::hash implemented only for fundamental types, enumerations and pointers
160/// @tparam T type to hash
161template <class T, typename = arene::base::constraints<>>
163
164/// @brief Default implementation of @c std::hash
165///
166/// Specialization for fundamental types, enumerations and pointers
167/// @tparam T type to hash
168template <class T>
169class default_hash<T, arene::base::constraints<enable_if_t<is_fundamental_v<T> || is_enum_v<T> || is_pointer_v<T>>>>
170 : public fnv1a_hash<T> {};
171
172} // namespace hash_detail
173
174// parasoft-begin-suppress AUTOSAR-A11_0_2-a "Specified as struct in C++ Standard"
175/// @brief hash function primary template
176/// @tparam T type to hash
177///
178template <class T>
180// parasoft-end-suppress AUTOSAR-A11_0_2-a
181
182} // namespace std
183
184#endif // INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_HASH_HPP_
A backport of std::span for C++14. Provides a view over a contiguous range of elements of type T.
Definition span.hpp:315
Default implementation of std::hash implemented only for fundamental types, enumerations and pointers...
Definition hash.hpp:162
FNV-1a hash algorithm.
Definition hash.hpp:127
size_t result_type
The type of the result of the function call operator.
Definition hash.hpp:137
T argument_type
The type of the argument of the function call operator.
Definition hash.hpp:139
auto operator()(T value) const noexcept -> size_t
Calculate the hash of a value.
Definition hash.hpp:144
Public export header for the constraints subpackage.
#define ARENE_IGNORE_END()
Ends a region of a suppressed diagnostic.
Definition diagnostics.hpp:136
#define ARENE_IGNORE_START()
Begins a region of a suppressed diagnostic.
Definition diagnostics.hpp:131
#define ARENE_IGNORE_ALL(s, comment)
Pushes an ignore for a diagnostic into the compiler for any compiler.
Definition diagnostics.hpp:122
constexpr auto to_underlying(T value) noexcept -> std::underlying_type_t< T >
This replicates a function that went into C++23 called std::to_underlying (https://wg21....
Definition to_underlying.hpp:26
auto as_bytes(span< T, N > spn) noexcept -> span< byte const, span_detail::is_dynamic_extent(N) ? dynamic_extent :(N *sizeof(T))>
Obtain a const view to the object representation of the elements of the span spn.
Definition span.hpp:967
Definition accumulate.hpp:36
Definition hash.hpp:39
constexpr Scalar fnv_prime_v
The FNV prime to use depending on bit variant.
constexpr auto canonicalize_special_values(T value) noexcept -> T
Canonicalize any special values of the given type that compare equal, so their hashes will be equal t...
Definition hash.hpp:86
constexpr Scalar fnv_offset_basis_v
The FNV offset basis to use depending on bit variant.
STL namespace.
constexpr Type integral_constant< Type, Value >::value
Out-of-line definition for the value constant.
Definition integral_constant.hpp:64
constexpr bool is_same_v
Trait to detect if the two supplied types are the same or not. The value is true if the types are the...
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
::uint32_t uint32_t
A 32-bit unsigned integer type.
Definition cstdint.hpp:41
typename conditional< Condition, TrueType, FalseType >::type conditional_t
Helper template alias for selecting types based on a condition. If the supplied Condition is true the...
Definition conditional.hpp:57
::uint64_t uint64_t
A 64-bit unsigned integer type.
Definition cstdint.hpp:47
Public export header for span.hpp.
hash function primary template
Definition hash.hpp:179
Public export header for content from arene/base/utility.