[flang-commits] [PATCH] D127023: [flang][runtime] Don't let random seed queries change the sequence
Peter Klausler via Phabricator via flang-commits
flang-commits at lists.llvm.org
Sat Jun 4 08:47:07 PDT 2022
This revision was automatically updated to reflect the committed changes.
Closed by commit rGea1a69d66dc7: [flang][runtime] Don't let random seed queries change the sequence (authored by klausler).
Changed prior to commit:
https://reviews.llvm.org/D127023?vs=434220&id=434266#toc
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D127023/new/
https://reviews.llvm.org/D127023
Files:
flang/runtime/random.cpp
Index: flang/runtime/random.cpp
===================================================================
--- flang/runtime/random.cpp
+++ flang/runtime/random.cpp
@@ -40,6 +40,19 @@
static Lock lock;
static Generator generator;
+static std::optional<GeneratedWord> nextValue;
+
+// Call only with lock held
+static GeneratedWord GetNextValue() {
+ GeneratedWord result;
+ if (nextValue.has_value()) {
+ result = *nextValue;
+ nextValue.reset();
+ } else {
+ result = generator();
+ }
+ return result;
+}
template <typename REAL, int PREC>
inline void Generate(const Descriptor &harvest) {
@@ -55,12 +68,12 @@
CriticalSection critical{lock};
for (std::size_t j{0}; j < elements; ++j) {
while (true) {
- Int fraction{generator()};
+ Int fraction{GetNextValue()};
if constexpr (words > 1) {
for (std::size_t k{1}; k < words; ++k) {
static constexpr auto rangeMask{
(GeneratedWord{1} << rangeBits) - 1};
- GeneratedWord word{(generator() - generator.min()) & rangeMask};
+ GeneratedWord word{(GetNextValue() - generator.min()) & rangeMask};
fraction = (fraction << rangeBits) | word;
}
}
@@ -161,6 +174,7 @@
{
CriticalSection critical{lock};
generator.seed(seed);
+ nextValue = seed;
}
}
@@ -183,8 +197,8 @@
GeneratedWord seed;
{
CriticalSection critical{lock};
- seed = generator();
- generator.seed(seed);
+ seed = GetNextValue();
+ nextValue = seed;
}
switch (kind) {
case 4:
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127023.434266.patch
Type: text/x-patch
Size: 1582 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20220604/605c7e71/attachment.bin>
More information about the flang-commits
mailing list