nfx-hashing 0.1.2
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 <Hash32or64 HashType = uint32_t, uint64_t FnvPrime = ( sizeof( HashType ) == 4 ? constants::FNV_PRIME_32 : constants::FNV_PRIME_64 )>
73 [[nodiscard]] inline constexpr HashType fnv1a( HashType hash, uint8_t ch ) noexcept;
74
93 [[nodiscard]] inline uint32_t crc32c( uint32_t hash, uint8_t ch ) noexcept;
94
106 [[nodiscard]] inline constexpr uint32_t crc32cSoft( uint32_t hash, uint8_t ch ) noexcept;
107
108 //----------------------------------------------
109 // Seed and bit mixing
110 //----------------------------------------------
111
126 template <Hash32or64 HashType = uint32_t, uint64_t MixConstant = constants::SEED_MIX_MULTIPLIER_64>
127 [[nodiscard]] inline constexpr HashType seedMix( HashType seed, HashType hash, uint64_t size ) noexcept;
128
129 //----------------------------------------------
130 // Hash combination
131 //----------------------------------------------
132
143 template <Hash32or64 HashType = uint32_t>
144 [[nodiscard]] inline constexpr HashType combine( HashType existingHash, HashType newHash, HashType prime ) noexcept;
145
168 template <Hash32or64 HashType = uint32_t>
169 [[nodiscard]] inline constexpr HashType combine( HashType existingHash, HashType newHash ) noexcept;
170} // namespace nfx::hashing
171
172#include "nfx/detail/hashing/Algorithms.inl"
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.
Mathematical constants for hash algorithms.
HashType hash(const T &value) noexcept
Hash a value with explicit type and hash size specification.
C++20 concepts and type traits for nfx-hashing library.