nfx-hashing 0.1.2
Modern C++20 header-only hashing library with hardware acceleration
Loading...
Searching...
No Matches
Hash.h File Reference

Unified template-based hash function API. More...

#include "nfx/hashing/Concepts.h"
#include "nfx/hashing/Hasher.h"
#include "nfx/detail/hashing/Hash.inl"
Include dependency graph for Hash.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

template<typename T, Hash32or64 HashType = uint32_t, HashType Seed = ( sizeof( HashType ) == 4 ? constants::FNV_OFFSET_BASIS_32 : constants::FNV_OFFSET_BASIS_64 )>
HashType nfx::hashing::hash (const T &value) noexcept
 Hash a value with explicit type and hash size specification.

Detailed Description

Unified template-based hash function API.

Provides a clean, STL-style hash<T>() interface that delegates to Hasher<HashType, Seed>. Supports explicit type and hash size specification with optional custom seeding.

Definition in file Hash.h.

Function Documentation

◆ hash()

template<typename T, Hash32or64 HashType = uint32_t, HashType Seed = ( sizeof( HashType ) == 4 ? constants::FNV_OFFSET_BASIS_32 : constants::FNV_OFFSET_BASIS_64 )>
HashType nfx::hashing::hash ( const T & value)
inlinenodiscardnoexcept

Hash a value with explicit type and hash size specification.

Template Parameters
TThe type of value to hash
HashTypeThe hash output type (uint32_t or uint64_t)
SeedThe seed value for hash initialization (default: FNV_OFFSET_BASIS_32 for 32-bit, FNV_OFFSET_BASIS_64 for 64-bit)
Parameters
valueThe value to hash
Returns
Hash value of type HashType
Note
This function is marked [[nodiscard]] - the return value should not be ignored

Unified template-based hash function that provides a clean, STL-style API for hashing values. The function automatically selects appropriate algorithms based on input type:

  • Strings: CRC32-C (SSE4.2 hardware-accelerated when available, compiler flags required)
  • Integers: Multiplicative hashing (Knuth for 32-bit, Wang for 64-bit)
  • Floating-point: Bit-representation hashing with normalization
  • Complex types: Recursive element hashing with combining

Hardware Acceleration: String hashing uses SSE4.2 CRC32-C instructions when:

  1. CPU supports SSE4.2 (runtime detection)
  2. Code compiled with proper flags: -march=native or -msse4.2 (GCC/Clang), /arch:AVX (MSVC)

Without compiler flags, software fallback is used even if CPU supports SSE4.2.

Default Seed: FNV offset basis (0x811C9DC5 for 32-bit, 0xCBF29CE484222325 for 64-bit) provides consistent initialization across the library. For zero-seed behavior, explicitly specify: hash<T, HashType, 0>(value)

Internally delegates to Hasher<HashType, Seed> functor for type-specific implementations.

// Basic usage with default FNV seed
auto h1 = hash<int>(42); // uint32_t, seed=0x811C9DC5
auto h2 = hash<std::string>("hello"); // uint32_t, seed=0x811C9DC5
// 64-bit hash output
auto h3 = hash<std::string, uint64_t>("hello"); // uint64_t, seed=0xCBF29CE484222325
// Custom seeds
auto h4 = hash<int, uint32_t, 0>(42); // Zero seed (no mixing)
auto h5 = hash<int, uint32_t, 0xDEADBEEF>(42); // Custom seed