[flang][runtime] Ensure that 0. <= RANDOM_NUMBER() < 1.

It was possible for RANDOM_NUMBER() to return 1.0.

Differential Revision: https://reviews.llvm.org/D127020
This commit is contained in:
Peter Klausler 2022-05-30 16:13:48 -07:00
parent 025b309631
commit f3278e0f3c
1 changed files with 15 additions and 9 deletions

View File

@ -54,17 +54,23 @@ inline void Generate(const Descriptor &harvest) {
{
CriticalSection critical{lock};
for (std::size_t j{0}; j < elements; ++j) {
Int fraction{generator()};
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};
fraction = (fraction << rangeBits) | word;
while (true) {
Int fraction{generator()};
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};
fraction = (fraction << rangeBits) | word;
}
}
fraction >>= words * rangeBits - PREC;
REAL next{std::ldexp(static_cast<REAL>(fraction), -(PREC + 1))};
if (next >= 0.0 && next < 1.0) {
*harvest.Element<REAL>(at) = next;
break;
}
}
fraction >>= words * rangeBits - PREC;
*harvest.Element<REAL>(at) =
std::ldexp(static_cast<REAL>(fraction), -(PREC + 1));
harvest.IncrementSubscripts(at);
}
}