diff options
| author | bunnei <bunneidev@gmail.com> | 2021-02-11 18:55:22 -0800 |
|---|---|---|
| committer | bunnei <bunneidev@gmail.com> | 2021-02-18 16:16:24 -0800 |
| commit | f7a008d77f8aa47baf4c874c508b38af4965a145 (patch) | |
| tree | 3a11b865d5f49be8fa0588ee5187f1b6c12022fb /src/core/hle/kernel/k_system_control.cpp | |
| parent | 6a19086001b5d7229728199d074b72c1d0dd34af (diff) | |
hle: kernel: KSystemControl does not belong in Memory namespace.
Diffstat (limited to 'src/core/hle/kernel/k_system_control.cpp')
| -rw-r--r-- | src/core/hle/kernel/k_system_control.cpp | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/core/hle/kernel/k_system_control.cpp b/src/core/hle/kernel/k_system_control.cpp new file mode 100644 index 000000000..aa1682f69 --- /dev/null +++ b/src/core/hle/kernel/k_system_control.cpp @@ -0,0 +1,42 @@ +// Copyright 2021 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include <random> + +#include "core/hle/kernel/k_system_control.h" + +namespace Kernel { + +namespace { +template <typename F> +u64 GenerateUniformRange(u64 min, u64 max, F f) { + // Handle the case where the difference is too large to represent. + if (max == std::numeric_limits<u64>::max() && min == std::numeric_limits<u64>::min()) { + return f(); + } + + // Iterate until we get a value in range. + const u64 range_size = ((max + 1) - min); + const u64 effective_max = (std::numeric_limits<u64>::max() / range_size) * range_size; + while (true) { + if (const u64 rnd = f(); rnd < effective_max) { + return min + (rnd % range_size); + } + } +} + +} // Anonymous namespace + +u64 KSystemControl::GenerateRandomU64() { + static std::random_device device; + static std::mt19937 gen(device()); + static std::uniform_int_distribution<u64> distribution(1, std::numeric_limits<u64>::max()); + return distribution(gen); +} + +u64 KSystemControl::GenerateRandomRange(u64 min, u64 max) { + return GenerateUniformRange(min, max, GenerateRandomU64); +} + +} // namespace Kernel |
