./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
40 / 40
100.0%
Branches:
18 / 22
81.8%
Functions:
3 / 3, 2 excl
100.0%

include/terraingen/random/philox.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief The Philox4x32-10 counter-based generator for the CPU and GPU backends.
5 #pragma once
6
7 #include "terraingen/util/portable.h"
8
9 namespace terraingen {
10
11 /// Four 32-bit words: a Philox counter or output block.
12 40490364 struct U32x4 {
13 uint32_t x = 0; ///< First word.
14 uint32_t y = 0; ///< Second word.
15 uint32_t z = 0; ///< Third word.
16 uint32_t w = 0; ///< Fourth word.
17
18 #ifndef __SLANG__
19 /// Componentwise equality.
20 14 bool operator==(const U32x4& other) const {
21
8/8
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 7 taken 4 times.
14 return x == other.x && y == other.y && z == other.z && w == other.w;
22 }
23 10 bool operator!=(const U32x4& other) const {
24
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 return !(*this == other);
25 }
26 #endif
27 };
28
29 /// Two 32-bit words: a Philox key.
30 struct U32x2 {
31 uint32_t x = 0; ///< First word.
32 uint32_t y = 0; ///< Second word.
33
34 #ifndef __SLANG__
35 /// Componentwise equality.
36 3 bool operator==(const U32x2& other) const {
37
3/6
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
2 return x == other.x && y == other.y;
38 }
39 2 bool operator!=(const U32x2& other) const {
40
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
3 return !(*this == other);
41 }
42 #endif
43 };
44
45 namespace detail {
46
47 /// The high (`x`) and low (`y`) words of the 64-bit product `a * b`, built from 16-bit halves on
48 /// GPUs without 64-bit integers, with the same result.
49 2093339110 constexpr U32x2 MulHiLo32(uint32_t a, uint32_t b) {
50 2093339110 U32x2 result = {};
51 #ifdef __SLANG__
52 const uint32_t aLow = a & 0xFFFFu;
53 const uint32_t aHigh = a >> 16u;
54 const uint32_t bLow = b & 0xFFFFu;
55 const uint32_t bHigh = b >> 16u;
56 const uint32_t lowHigh = aLow * bHigh;
57 const uint32_t highLow = aHigh * bLow;
58 const uint32_t middle = ((aLow * bLow) >> 16u) + (lowHigh & 0xFFFFu) + (highLow & 0xFFFFu);
59 result.x = aHigh * bHigh + (lowHigh >> 16u) + (highLow >> 16u) + (middle >> 16u);
60 result.y = a * b;
61 #else
62 2093339110 const std::uint64_t product = std::uint64_t(a) * b;
63 2093339110 result.x = uint32_t(product >> 32u);
64 2093339110 result.y = uint32_t(product);
65 #endif
66 2093339110 return result;
67 }
68
69 /// One Philox4x32 round of a block under a round key.
70 2093339110 constexpr U32x4 PhiloxRound(U32x4 counter, U32x2 key) {
71 2093339110 const U32x2 first = MulHiLo32(0xD2511F53u, counter.x);
72 2093339110 const U32x2 second = MulHiLo32(0xCD9E8D57u, counter.z);
73 2093339110 const U32x4 result = {second.x ^ counter.y ^ key.x, second.y, first.x ^ counter.w ^ key.y,
74 2093339110 first.y};
75 2093339110 return result;
76 }
77
78 } // namespace detail
79
80 /// Philox4x32-10: 128 pseudo-random bits, a bijection of the counter under the key, so any
81 /// element of any stream can be computed directly. Bit-identical on every platform and GPU.
82 209975352 constexpr U32x4 Philox4x32(U32x4 counter, U32x2 key) {
83
2/2
✓ Branch 0 taken 2093237703 times.
✓ Branch 1 taken 209975352 times.
2303213055 for (int round = 0; round < 10; ++round) {
84
2/2
✓ Branch 0 taken 1883992104 times.
✓ Branch 1 taken 209245599 times.
2093237703 if (round > 0) {
85 1883992104 key.x += 0x9E3779B9u;
86 1883992104 key.y += 0xBB67AE85u;
87 }
88 2093237703 counter = detail::PhiloxRound(counter, key);
89 }
90 209975352 return counter;
91 }
92
93 } // namespace terraingen
94