nfx-hashing 0.4.0
Modern C++20 header-only hashing library with hardware acceleration
Loading...
Searching...
No Matches
Algorithms.h
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025 nfx
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
31
32#pragma once
33
34#include <cstdint>
35
36#include "Concepts.h"
37#include "Constants.h"
38
39namespace nfx::hashing
40{
41 //=====================================================================
42 // Low - level hash building blocks
43 //=====================================================================
44
45 //----------------------------------------------
46 // Hash algorithm primitives
47 //----------------------------------------------
48
59 template <Hash32or64 HashType = uint32_t>
60 [[nodiscard]] inline constexpr HashType larson( HashType hash, uint8_t ch ) noexcept;
61
72 template <
73 Hash32or64 HashType = uint32_t,
74 uint64_t FnvPrime = ( sizeof( HashType ) == 4 ? constants::FNV_PRIME_32 : constants::FNV_PRIME_64 )>
75 [[nodiscard]] inline constexpr HashType fnv1a( HashType hash, uint8_t ch ) noexcept;
76
95 [[nodiscard]] inline uint32_t crc32c( uint32_t hash, uint8_t ch ) noexcept;
96
108 [[nodiscard]] inline constexpr uint32_t crc32cSoft( uint32_t hash, uint8_t ch ) noexcept;
109
110 //----------------------------------------------
111 // Seed and bit mixing
112 //----------------------------------------------
113
128 template <Hash32or64 HashType = uint32_t, uint64_t MixConstant = constants::SEED_MIX_MULTIPLIER_64>
129 [[nodiscard]] inline constexpr HashType seedMix( HashType seed, HashType hash, uint64_t size ) noexcept;
130
131 //----------------------------------------------
132 // Hash combination
133 //----------------------------------------------
134
145 template <Hash32or64 HashType = uint32_t>
146 [[nodiscard]] inline constexpr HashType combine( HashType existingHash, HashType newHash, HashType prime ) noexcept;
147
170 template <Hash32or64 HashType = uint32_t>
171 [[nodiscard]] inline constexpr HashType combine( HashType existingHash, HashType newHash ) noexcept;
172} // namespace nfx::hashing
173
174#include "nfx/detail/hashing/Algorithms.inl"
HashType hash(const T &value) noexcept
Hash a value with explicit type and hash size specification.
Mathematical constants for hash algorithms.
constexpr uint32_t FNV_PRIME_32
FNV-1a 32-bit prime constant.
Definition Constants.h:50
constexpr uint64_t FNV_PRIME_64
FNV-1a 64-bit prime constant.
Definition Constants.h:56
constexpr HashType larson(HashType hash, uint8_t ch) noexcept
Larson multiplicative hash function: 37 * hash + ch.
constexpr HashType combine(HashType existingHash, HashType newHash, HashType prime) noexcept
Combines two hash values using FNV-1a mixing.
uint32_t crc32c(uint32_t hash, uint8_t ch) noexcept
Computes one step of the CRC32-C hash function with runtime hardware acceleration.
constexpr HashType fnv1a(HashType hash, uint8_t ch) noexcept
Computes one step of the FNV-1a hash function.
constexpr uint32_t crc32cSoft(uint32_t hash, uint8_t ch) noexcept
Software implementation of CRC32-C (Castagnoli) hash function.
constexpr HashType seedMix(HashType seed, HashType hash, uint64_t size) noexcept
Mixes a seed value with a hash using bit-mixing and multiplicative hashing.
C++20 concepts and type traits for nfx-hashing library.
Concept requiring type to be either uint32_t or uint64_t.
Definition Concepts.h:46