./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
45 / 47
95.7%
Branches:
29 / 42
69.0%
Functions:
5 / 5, 3 excl
100.0%

include/terraingen/noise/hash.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief Stateless PCG hashes for the CPU and GPU backends (`portable.h`).
5 #pragma once
6
7 #include "terraingen/util/portable.h"
8
9 #ifndef __SLANG__
10 #include <initializer_list>
11 #endif
12
13 namespace terraingen {
14
15 /// Three 32-bit words.
16 struct UVec3 {
17 uint32_t x = 0; ///< First word.
18 uint32_t y = 0; ///< Second word.
19 uint32_t z = 0; ///< Third word.
20
21 #ifndef __SLANG__
22 /// Componentwise equality.
23 9 bool operator==(const UVec3& other) const {
24
6/10
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✓ Branch 7 taken 1 time.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 time.
8 return x == other.x && y == other.y && z == other.z;
25 }
26 3 bool operator!=(const UVec3& other) const {
27
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
5 return !(*this == other);
28 }
29 #endif
30 };
31
32 /// PCG hash (PCG-RXS-M-XS over one LCG step) of one 32-bit word; bit-identical everywhere.
33 275097882 constexpr uint32_t PcgHash(uint32_t v) {
34 275097882 const uint32_t state = v * 747796405u + 2891336453u;
35 275097882 const uint32_t word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;
36
4/4
<unknown function 23>:
✓ Branch 1 taken 100000 times.
<unknown function 33>:
✓ Branch 0 taken 1609 times.
✓ Branch 1 taken 615763 times.
✓ Branch 4 taken 20000 times.
11227030 return (word >> 22u) ^ word;
37 }
38
39 /// The `pcg3d` hash of three 32-bit words to three well-mixed words.
40 1218878805 constexpr UVec3 Pcg3dHash(UVec3 v) {
41 1218878805 v.x = v.x * 1664525u + 1013904223u;
42 1218878805 v.y = v.y * 1664525u + 1013904223u;
43 1218878805 v.z = v.z * 1664525u + 1013904223u;
44
45 1218878805 v.x += v.y * v.z;
46 1218878805 v.y += v.z * v.x;
47 1218878805 v.z += v.x * v.y;
48
49 1218878805 v.x ^= v.x >> 16u;
50 1218878805 v.y ^= v.y >> 16u;
51 1218878805 v.z ^= v.z >> 16u;
52
53 1218878805 v.x += v.y * v.z;
54 1218878805 v.y += v.z * v.x;
55 1218878805 v.z += v.x * v.y;
56 1218878805 return v;
57 }
58
59 /// Maps a hash to a float in [0, 1) from its top 24 bits, which a `float` represents exactly.
60 265153998 constexpr float HashToUnitFloat(uint32_t hash) {
61
15/24
<unknown function 33>:
✗ Branch 0 not taken.
✗ Branch 1 not taken.
terraingen::HashToUnitFloat(unsigned int):
✓ Branch 2 taken 91 times.
✓ Branch 3 taken 476 times.
✓ Branch 4 taken 815224 times.
✓ Branch 5 taken 2385326 times.
terraingen::Pcg3dHash(terraingen::UVec3):
✓ Branch 0 taken 6447151 times.
✗ Branch 0 not taken.
✓ Branch 1 taken 17840400 times.
✓ Branch 1 taken 3952910 times.
✓ Branch 2 taken 12877739 times.
✓ Branch 2 taken 81045 times.
✓ Branch 3 taken 4114424 times.
✓ Branch 3 taken 378679 times.
✓ Branch 4 taken 598775 times.
✓ Branch 4 taken 1598007 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1756425 times.
✓ Branch 5 taken 4706868 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 7 not taken.
262025873 return float(hash >> 8u) * 0x1p-24f;
62 }
63
64 #ifndef __SLANG__
65
66 /// A 32-bit `PcgHash` of a 64-bit word, mixed with `seed`: for keying a stream or noise by an ID.
67 1343439 constexpr uint32_t HashWord64(std::uint64_t word, uint32_t seed = 0) {
68 1343439 return PcgHash(uint32_t(word) ^ PcgHash(uint32_t(word >> 32u) ^ seed));
69 }
70
71 /// A hash for hashed containers of a key made of 64-bit `words` (integers, pointers or double
72 /// bits): the words chained through `Pcg3dHash` and the first two output words joined.
73 9257833 inline std::size_t HashWords(std::initializer_list<std::uint64_t> words) {
74 9257833 UVec3 state = {};
75
2/2
✓ Branch 0 taken 27764037 times.
✓ Branch 1 taken 9257833 times.
37021870 for (const std::uint64_t word : words) {
76 27764037 const UVec3 mixed = {state.x ^ uint32_t(word), state.y ^ uint32_t(word >> 32u),
77 27764037 state.z + 1u};
78 27764037 state = Pcg3dHash(mixed);
79 }
80 9257833 return static_cast<std::size_t>((std::uint64_t(state.x) << 32u) ^ state.y);
81 }
82
83 #endif
84
85 } // namespace terraingen
86