include/terraingen/random/stream.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief `RandomStream`, a counter-based random number stream, derived seeds and 64-bit IDs. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "terraingen/random/philox.h" | ||
| 8 | #include "terraingen/random/streams.h" | ||
| 9 | |||
| 10 | #include <cstdint> | ||
| 11 | |||
| 12 | namespace terraingen { | ||
| 13 | |||
| 14 | /// A counter-based stream of random numbers built on `Philox4x32`. | ||
| 15 | /// | ||
| 16 | /// The key is (`seed`, `stream`), `stream` a tag from random/streams.h; the words `a`, `b` and `c` | ||
| 17 | /// address an independent substream, so the numbers for one cell never depend on any other cell. | ||
| 18 | /// Each substream gives 2^34 draws before it repeats. | ||
| 19 | class RandomStream { | ||
| 20 | public: | ||
| 21 | /// Opens a substream. | ||
| 22 | /// @param stream Separates purposes, such as a layer or a feature kind. | ||
| 23 | /// @param a First substream word, e.g. a cell's x. | ||
| 24 | /// @param b Second substream word, e.g. a cell's y. | ||
| 25 | /// @param c Third substream word, e.g. a size band. | ||
| 26 | RandomStream(std::uint32_t seed, std::uint32_t stream, std::uint32_t a = 0, std::uint32_t b = 0, | ||
| 27 | std::uint32_t c = 0); | ||
| 28 | |||
| 29 | /// The next uniformly distributed 32-bit word. | ||
| 30 | std::uint32_t NextU32(); | ||
| 31 | |||
| 32 | /// A uniform double in [0, 1) with 53 random bits from two draws. | ||
| 33 | double NextDouble(); | ||
| 34 | |||
| 35 | private: | ||
| 36 | U32x2 m_key; | ||
| 37 | U32x4 m_counter; | ||
| 38 | U32x4 m_block; | ||
| 39 | int m_used = 4; | ||
| 40 | }; | ||
| 41 | |||
| 42 | /// A 64-bit ID as two 32-bit words, for keying `RandomStream` and `Philox4x32` by it. | ||
| 43 | struct IdWords { | ||
| 44 | std::uint32_t low = 0; ///< Bits 0 to 31. | ||
| 45 | std::uint32_t high = 0; ///< Bits 32 to 63. | ||
| 46 | }; | ||
| 47 | |||
| 48 | /// Splits a 64-bit ID into its low and high 32-bit words. | ||
| 49 | 4601892 | constexpr IdWords SplitId(std::uint64_t id) { | |
| 50 | 4568987 | return {static_cast<std::uint32_t>(id), static_cast<std::uint32_t>(id >> 32)}; | |
| 51 | } | ||
| 52 | |||
| 53 | /// The 64-bit word with `high` above `low`: an ID from two draws or hash words. | ||
| 54 | 2827459 | constexpr std::uint64_t JoinWords(std::uint32_t high, std::uint32_t low) { | |
| 55 | 2827459 | return (static_cast<std::uint64_t>(high) << 32) | low; | |
| 56 | } | ||
| 57 | |||
| 58 | /// A seed for a generator keyed by a seed alone, such as a crater population or a noise field, | ||
| 59 | /// drawn from substream (`seed`, `stream`, `a`) apart from the draws of every `RandomStream`. | ||
| 60 | /// @param stream A tag from random/streams.h. | ||
| 61 | 3361127 | constexpr std::uint32_t SubstreamSeed(std::uint32_t seed, std::uint32_t stream, | |
| 62 | std::uint32_t a = 0) { | ||
| 63 | 3361127 | return Philox4x32({a, 0u, 0u, 0xFFFFFFFFu}, {seed, stream}).x; | |
| 64 | } | ||
| 65 | |||
| 66 | /// The 32-bit seed for seeded field `field` of a part reseeded with `seed` | ||
| 67 | /// (`Layer::Reseeded`, `Source::Reseeded`), so one 64-bit seed varies every seed of a part. | ||
| 68 | 741 | constexpr std::uint32_t ReseedWord(std::uint64_t seed, std::uint32_t field) { | |
| 69 | 741 | const IdWords words = SplitId(seed); | |
| 70 | 741 | return Philox4x32({field, 0u, 0u, 0xFFFFFFFEu}, {words.low, words.high}).x; | |
| 71 | } | ||
| 72 | |||
| 73 | } // namespace terraingen | ||
| 74 |