random/stream.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "terraingen/random/stream.h" | ||
| 2 | |||
| 3 | namespace terraingen { | ||
| 4 | |||
| 5 | 209173606 | RandomStream::RandomStream(std::uint32_t seed, std::uint32_t stream, std::uint32_t a, | |
| 6 | 209173606 | std::uint32_t b, std::uint32_t c) | |
| 7 | 209173606 | : key_{seed, stream}, counter_{a, b, c, 0u} {} | |
| 8 | |||
| 9 | 2744698592 | std::uint32_t RandomStream::NextU32() { | |
| 10 | 2744698592 | if (used_ == 4) { | |
| 11 | 751017886 | block_ = Philox4x32(counter_, key_); | |
| 12 | 751017886 | ++counter_.w; | |
| 13 | 751017886 | used_ = 0; | |
| 14 | } | ||
| 15 | 2744698592 | const std::uint32_t words[4] = {block_.x, block_.y, block_.z, block_.w}; | |
| 16 | 2744698592 | return words[used_++]; | |
| 17 | } | ||
| 18 | |||
| 19 | 65097 | float RandomStream::NextFloat() { | |
| 20 | 65097 | return static_cast<float>(NextU32() >> 8u) * 0x1p-24f; | |
| 21 | } | ||
| 22 | |||
| 23 | 1287987905 | double RandomStream::NextDouble() { | |
| 24 | 1287987905 | const std::uint64_t high = NextU32() >> 5u; | |
| 25 | 1287987905 | const std::uint64_t low = NextU32() >> 6u; | |
| 26 | 1287987905 | return static_cast<double>((high << 26u) | low) * 0x1p-53; | |
| 27 | } | ||
| 28 | |||
| 29 | 111006 | double RandomStream::NextDoubleOpenBelow() { | |
| 30 | 111006 | return 1.0 - NextDouble(); | |
| 31 | } | ||
| 32 | |||
| 33 | } // namespace terraingen | ||
| 34 |