JPL Spatial
Sound spatialization and propagation library
Loading...
Searching...
No Matches
Hash.h
Go to the documentation of this file.
1//
2// ██╗██████╗ ██╗ ██╗██████╗ ███████╗
3// ██║██╔══██╗ ██║ ██║██╔══██╗██╔════╝ ** JPLSpatial **
4// ██║██████╔╝ ██║ ██║██████╔╝███████╗
5// ██ ██║██╔═══╝ ██║ ██║██╔══██╗╚════██║ https://github.com/Jaytheway/JPLSpatial
6// ╚█████╔╝██║ ███████╗██║██████╔╝███████║
7// ╚════╝ ╚═╝ ╚══════╝╚═╝╚═════╝ ╚══════╝
8//
9// Copyright Jaroslav Pevno, JPLSpatial is offered under the terms of the ISC license:
10//
11// Permission to use, copy, modify, and/or distribute this software for any purpose with or
12// without fee is hereby granted, provided that the above copyright notice and this permission
13// notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
14// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
16// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
17// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
18// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20#pragma once
21
22#include <cstdint>
23#include <span>
24#include <concepts>
25
26namespace JPL
27{
28 // Simple utility helper to combine hash
29 struct Hash
30 {
31 static constexpr uint32_t cStartSeed = 0x811c9dc5u;
32
34 [[nodiscard]] JPL_INLINE constexpr uint32_t Finalize() const;
35 [[nodiscard]] JPL_INLINE constexpr uint32_t GetCurrent() const { return mSeed; }
36
37 protected:
39 };
40
41 // Start uint32_t seed = 0x811C9DC5u (Hash::cStartSeed)
43 {
44 seed ^= id32 + 0x9e3779b9u + (seed << 6) + (seed >> 2);
45
46 // FNV-1a prime - extra diffusion
47 // (optional, if observing collisions)
48 //seed *= 16777619u;
49 }
50
51 // Start uint32_t seed = 0x811C9DC5u (Hash::cStartSeed)
53 {
54 // fold 64 -> 32 (XOR upper & lower halves)
55 const uint32_t v = uint32_t(id64) ^ uint32_t(id64 >> 32);
57 }
58
60 {
61 hash ^= hash >> 13;
62 hash *= 0x85ebca6bu;
63 hash ^= hash >> 16;
64 return hash;
65 }
66
67 // 32-bit hash of an ordered sequence of 64-bit IDs
68 template<std::integral T> requires (sizeof(T) == 8)
69 [[nodiscard]] constexpr inline uint32_t HashIntSequence(std::span<const T> ids)
70 {
71 uint32_t h = Hash::cStartSeed; // FNV offset basis
72
73 for (T id : ids)
74 {
75 // fold 64 -> 32 (XOR upper & lower halves)
76 uint32_t v = uint32_t(id) ^ uint32_t(id >> 32);
77
78 // (good enough for a few hundreds of elements,
79 // switch to HasCombine32 if not enough)
80 h ^= v; // FNV-1a step
81 h *= 16777619u; // 32-bit prime
82 }
83
84 return Murmur3Finalize(h);
85 }
86
87 // 32-bit hash of an ordered sequence of 32-bit IDs
88 template<std::integral T> requires (sizeof(T) == 4)
89 [[nodiscard]] constexpr inline uint32_t HashIntSequence32(std::span<const T> ids)
90 {
92 for (T v : ids)
93 HashCombine32(h, static_cast<uint32_t>(v));
94
95 return Murmur3Finalize(h);
96 }
97} // namespace JPL
98
99//==============================================================================
100//
101// Code beyond this point is implementation detail...
102//
103//==============================================================================
104
105namespace JPL
106{
109} // namespace JPL
Definition AcousticMaterial.h:36
JPL_INLINE constexpr void HashCombine32(uint32_t &seed, uint32_t id32)
Definition Hash.h:42
constexpr uint32_t HashIntSequence32(std::span< const T > ids)
Definition Hash.h:89
JPL_INLINE constexpr uint32_t Murmur3Finalize(uint32_t hash)
Definition Hash.h:59
JPL_INLINE simd min(const simd &a, const simd &b) noexcept
Element-wise min.
Definition SIMD.h:1813
constexpr uint32_t HashIntSequence(std::span< const T > ids)
Definition Hash.h:69
Definition Hash.h:30
JPL_INLINE constexpr uint32_t GetCurrent() const
Definition Hash.h:35
JPL_INLINE constexpr uint32_t Finalize() const
Definition Hash.h:108
static constexpr uint32_t cStartSeed
Definition Hash.h:31
JPL_INLINE constexpr uint32_t Combine(uint32_t value)
Definition Hash.h:107
uint32_t mSeed
Definition Hash.h:38