Sen API
Sen Libraries
Loading...
Searching...
No Matches
hash32.h
Go to the documentation of this file.
1// === hash32.h ========================================================================================================
2// Sen Infrastructure
3// Released under the Apache License v2.0 (SPDX-License-Identifier Apache-2.0).
4// See the LICENSE.txt file for more information.
5// © Airbus SAS, Airbus Helicopters, and Airbus Defence and Space SAU/GmbH/SAS.
6// =====================================================================================================================
7
8#ifndef SEN_CORE_BASE_HASH32_H
9#define SEN_CORE_BASE_HASH32_H
10
14
15// std
16#include <array>
17#include <cstdint>
18#include <cstring>
19#include <filesystem>
20#include <memory>
21#include <numeric>
22#include <string>
23#include <string_view>
24
28
29namespace sen
30{
31
34
36constexpr u32 hashSeed = 23835769U;
37constexpr u32 propertyHashSeed = 19830715U;
38constexpr u32 methodHashSeed = 93580253U;
39constexpr u32 eventHashSeed = 12125807U;
40
42constexpr u32 nonPresentTypeHash = 18121997U;
43
46template <typename InputIterator>
47[[nodiscard]] u32 crc32(InputIterator first, InputIterator last) noexcept;
48
50[[nodiscard]] inline u32 crc32(std::string_view str) noexcept { return crc32(str.begin(), str.end()); }
51
53template <typename... Types>
54[[nodiscard]] u32 hashCombine(u32 seed, const Types&... args) noexcept;
55
59template <typename... Types>
60[[nodiscard]] uint_fast32_t platformDependentHashCombine(uint_fast32_t seed, const Types&... args) noexcept;
61
63[[nodiscard]] std::unique_ptr<unsigned char[]> decompressSymbol(const void* compressedData);
64
66[[nodiscard]] std::string decompressSymbolToString(const void* compressedData, unsigned int originalSize);
67
70[[nodiscard]] Result<void, std::string> fileToCompressedArray(const std::filesystem::path& inputFile,
71 std::string_view symbolName,
72 const std::filesystem::path& outputFile);
73
75// [[nodiscard]] leads: the macro expands to a GNU attribute on gcc and clang, and a standard
76// attribute list has to start the declaration.
77[[nodiscard]] SEN_DEPRECATED_FOR(0.7.0, fileToCompressedArray) bool fileToCompressedArrayFile(
78 const std::filesystem::path& inputFile,
79 std::string_view symbolName,
80 const std::filesystem::path& outputFile);
81
83
84//----------------------------------------------------------------------------------------------------------------------
85// Inline implementation
86//----------------------------------------------------------------------------------------------------------------------
87
88namespace impl
89{
90
91// FN-1a implementation of the hash
92constexpr u32 fnv1aOffsetBasis = 0x811c9dc5;
93constexpr u32 fnv1aPrime = 0x01000193;
94
96template <typename T>
97inline constexpr std::enable_if_t<std::is_integral_v<T>, u32> hashIntegral(T value) noexcept
98{
99 using UT = std::make_unsigned_t<T>;
100 auto u = static_cast<UT>(value);
101
102 auto hash = fnv1aOffsetBasis;
103
104 // process from most-significant byte to least-significant
105 for (int i = (sizeof(UT) - 1) * 8; i >= 0; i -= 8)
106 {
107 hash ^= static_cast<u8>((u >> i) & 0xFF); // NOLINT
108 hash *= fnv1aPrime;
109 }
110
111 return hash;
112}
113
115template <typename T>
116inline constexpr std::enable_if_t<std::is_floating_point_v<T>, u32> hashFloat(T value) noexcept
117{
118 using IntType = std::conditional_t<sizeof(T) == 4, u32, u64>;
119
120 IntType bits = 0;
121 std::memcpy(&bits, &value, sizeof(T));
122 return hashIntegral(bits);
123}
124
125template <typename T, typename Enable = void>
126struct hash;
127
128template <typename T>
129struct hash<T, typename std::enable_if_t<std::is_integral_v<T>>>
130{
131 inline u32 operator()(T value) const noexcept { return hashIntegral(value); }
132};
133
134template <typename T>
135struct hash<T, typename std::enable_if_t<std::is_floating_point_v<T>>>
136{
137 inline u32 operator()(T value) const noexcept { return hashFloat(value); }
138};
139
140template <>
141struct hash<bool>
142{
143 inline u32 operator()(bool value) const noexcept { return hashIntegral(static_cast<u8>(value)); }
144};
145
146template <>
147struct hash<i8>
148{
149 inline u32 operator()(i8 value) const noexcept { return hashIntegral(static_cast<u8>(value)); }
150};
151
152template <>
153struct hash<std::basic_string_view<char>>
154{
155 inline u32 operator()(const std::basic_string_view<char>& value) const noexcept
156 {
157 auto hash = fnv1aOffsetBasis;
158
159 for (unsigned char c: value)
160 {
161 hash ^= c;
162 hash *= fnv1aPrime;
163 }
164
165 return hash;
166 }
167};
168
169template <>
170struct hash<std::basic_string<char>>
171{
172 inline u32 operator()(const std::basic_string<char>& value) const noexcept
173 {
174 return hash<std::basic_string_view<char>>()(value);
175 }
176};
177
178template <typename T>
179struct hash<T, std::enable_if_t<std::is_enum_v<T>>>
180{
181 inline u32 operator()(T value) const noexcept { return hashIntegral(static_cast<std::underlying_type_t<T>>(value)); }
182};
183
184template <typename T>
185struct hash<T*>
186{
187 u32 operator()(T* value) const noexcept
188 {
189 return hashIntegral(reinterpret_cast<std::uintptr_t>(value)); // NOLINT
190 }
191};
192
193[[nodiscard]] std::array<u32, 256> generateCrc32LookupTable() noexcept; // NOLINT
194
195template <typename T>
196inline void hashCombineImpl(u32& seed, const T& val) noexcept
197{
198 seed ^= hash<T>()(val) + 0x9e3779b9U + (seed << 6) + (seed >> 2); // NOLINT
199}
200
201template <typename T>
202inline void platformDependentHashCombineImpl(std::uint_fast32_t& seed, const T& val) noexcept
203{
204 seed ^= std::hash<T>()(val) + 0x9e3779b9U + (seed << 6) + (seed >> 2); // NOLINT
205}
206
207} // namespace impl
208
209template <typename InputIterator>
210inline u32 crc32(InputIterator first, InputIterator last) noexcept
211{
212 // generate lookup table only on first use then cache it - this is thread-safe.
213 static auto const table = impl::generateCrc32LookupTable();
214
215 // calculate the checksum - make sure to clip to 32 bits, for systems that don't
216 // have a true (fast) 32-bit type.
217 return u32 {0xffffffffUL} & // NOLINT
218 ~std::accumulate(first,
219 last,
220 ~u32 {0} & u32 {0xffffffffUL}, // NOLINT
221 [](u32 checksum, u32 value)
222 { return table[(checksum ^ value) & 0xffU] ^ (checksum >> 8U); }); // NOLINT
223}
224
225template <typename... Types>
226inline u32 hashCombine(u32 seed, const Types&... args) noexcept
227{
228 (impl::hashCombineImpl(seed, args), ...);
229 return seed;
230}
231
232template <typename... Types>
233inline std::uint_fast32_t platformDependentHashCombine(std::uint_fast32_t seed, const Types&... args) noexcept
234{
235 (impl::platformDependentHashCombineImpl(seed, args), ...);
236 return seed;
237}
238
239} // namespace sen
240
241#endif // SEN_CORE_BASE_HASH32_H
Result<T, E> is a template type that can be used to return and propagate errors. The intent is to rep...
Definition result.h:135
Result< void, std::string > fileToCompressedArray(const std::filesystem::path &inputFile, std::string_view symbolName, const std::filesystem::path &outputFile)
Creates a C++ source file that contains an array representing the contents of another file....
constexpr u32 hashSeed
Initial seed for all hashes.
Definition hash32.h:36
u32 crc32(InputIterator first, InputIterator last) noexcept
Calculates the CRC32 for any sequence of values. (You could use type traits and a static assert to en...
Definition hash32.h:210
std::string decompressSymbolToString(const void *compressedData, unsigned int originalSize)
Decompresses a blob into a string.
constexpr u32 propertyHashSeed
Definition hash32.h:37
constexpr u32 methodHashSeed
Definition hash32.h:38
uint_fast32_t platformDependentHashCombine(uint_fast32_t seed, const Types &... args) noexcept
Old version of the hashCombine method. The current one replaced this implementation to allow sen proc...
constexpr u32 eventHashSeed
Definition hash32.h:39
u32 hashCombine(u32 seed, const Types &... args) noexcept
Combines the hash of different values into a single 32-bit hash.
Definition hash32.h:226
std::unique_ptr< unsigned char[]> decompressSymbol(const void *compressedData)
Decompresses a blob into its original shape.
constexpr u32 nonPresentTypeHash
This hash is combined when no Type is found in a certain spec.
Definition hash32.h:42
int8_t i8
Definition numbers.h:20
uint64_t u64
Definition numbers.h:27
uint32_t u32
Definition numbers.h:25
uint8_t u8
Definition numbers.h:21
Definition assert.h:19
STL namespace.