diff options
author | bunnei <bunneidev@gmail.com> | 2020-04-05 20:48:50 +0200 |
---|---|---|
committer | bunnei <bunneidev@gmail.com> | 2020-04-17 06:59:30 +0200 |
commit | 81cb4d3c7f5f959ad6ed3710171517334af23e22 (patch) | |
tree | d31da03a6246dd6656cfd5e95bc88e0a75bbcea3 /src/core/hle/kernel/memory/system_control.cpp | |
parent | physical_memory: Add missing include for <vector>. (diff) | |
download | yuzu-81cb4d3c7f5f959ad6ed3710171517334af23e22.tar yuzu-81cb4d3c7f5f959ad6ed3710171517334af23e22.tar.gz yuzu-81cb4d3c7f5f959ad6ed3710171517334af23e22.tar.bz2 yuzu-81cb4d3c7f5f959ad6ed3710171517334af23e22.tar.lz yuzu-81cb4d3c7f5f959ad6ed3710171517334af23e22.tar.xz yuzu-81cb4d3c7f5f959ad6ed3710171517334af23e22.tar.zst yuzu-81cb4d3c7f5f959ad6ed3710171517334af23e22.zip |
Diffstat (limited to '')
-rw-r--r-- | src/core/hle/kernel/memory/system_control.cpp | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/core/hle/kernel/memory/system_control.cpp b/src/core/hle/kernel/memory/system_control.cpp new file mode 100644 index 000000000..e61522dc0 --- /dev/null +++ b/src/core/hle/kernel/memory/system_control.cpp @@ -0,0 +1,41 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include <random> + +#include "core/hle/kernel/memory/system_control.h" + +namespace Kernel::Memory::SystemControl { + +u64 GenerateRandomU64ForInit() { + std::random_device device; + std::mt19937 gen(device()); + std::uniform_int_distribution<u64> distribution(1, std::numeric_limits<u64>::max()); + return distribution(gen); +} + +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); + } + } +} + +u64 GenerateRandomRange(u64 min, u64 max) { + return GenerateUniformRange(min, max, GenerateRandomU64ForInit); +} + +} // namespace Kernel::Memory::SystemControl |