diff options
Diffstat (limited to '')
| -rw-r--r-- | src/core/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/core/hle/kernel/memory/system_control.cpp | 41 | ||||
| -rw-r--r-- | src/core/hle/kernel/memory/system_control.h | 18 |
3 files changed, 61 insertions, 0 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 504ee2777..92ed3bdcf 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt | |||
| @@ -159,6 +159,8 @@ add_library(core STATIC | |||
| 159 | hle/kernel/memory/memory_block.h | 159 | hle/kernel/memory/memory_block.h |
| 160 | hle/kernel/memory/memory_types.h | 160 | hle/kernel/memory/memory_types.h |
| 161 | hle/kernel/memory/slab_heap.h | 161 | hle/kernel/memory/slab_heap.h |
| 162 | hle/kernel/memory/system_control.cpp | ||
| 163 | hle/kernel/memory/system_control.h | ||
| 162 | hle/kernel/mutex.cpp | 164 | hle/kernel/mutex.cpp |
| 163 | hle/kernel/mutex.h | 165 | hle/kernel/mutex.h |
| 164 | hle/kernel/object.cpp | 166 | hle/kernel/object.cpp |
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 @@ | |||
| 1 | // Copyright 2020 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <random> | ||
| 8 | |||
| 9 | #include "core/hle/kernel/memory/system_control.h" | ||
| 10 | |||
| 11 | namespace Kernel::Memory::SystemControl { | ||
| 12 | |||
| 13 | u64 GenerateRandomU64ForInit() { | ||
| 14 | std::random_device device; | ||
| 15 | std::mt19937 gen(device()); | ||
| 16 | std::uniform_int_distribution<u64> distribution(1, std::numeric_limits<u64>::max()); | ||
| 17 | return distribution(gen); | ||
| 18 | } | ||
| 19 | |||
| 20 | template <typename F> | ||
| 21 | u64 GenerateUniformRange(u64 min, u64 max, F f) { | ||
| 22 | /* Handle the case where the difference is too large to represent. */ | ||
| 23 | if (max == std::numeric_limits<u64>::max() && min == std::numeric_limits<u64>::min()) { | ||
| 24 | return f(); | ||
| 25 | } | ||
| 26 | |||
| 27 | /* Iterate until we get a value in range. */ | ||
| 28 | const u64 range_size = ((max + 1) - min); | ||
| 29 | const u64 effective_max = (std::numeric_limits<u64>::max() / range_size) * range_size; | ||
| 30 | while (true) { | ||
| 31 | if (const u64 rnd = f(); rnd < effective_max) { | ||
| 32 | return min + (rnd % range_size); | ||
| 33 | } | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | u64 GenerateRandomRange(u64 min, u64 max) { | ||
| 38 | return GenerateUniformRange(min, max, GenerateRandomU64ForInit); | ||
| 39 | } | ||
| 40 | |||
| 41 | } // namespace Kernel::Memory::SystemControl | ||
diff --git a/src/core/hle/kernel/memory/system_control.h b/src/core/hle/kernel/memory/system_control.h new file mode 100644 index 000000000..3fa93111d --- /dev/null +++ b/src/core/hle/kernel/memory/system_control.h | |||
| @@ -0,0 +1,18 @@ | |||
| 1 | // Copyright 2020 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "common/common_types.h" | ||
| 8 | |||
| 9 | namespace Kernel::Memory::SystemControl { | ||
| 10 | |||
| 11 | u64 GenerateRandomU64ForInit(); | ||
| 12 | |||
| 13 | template <typename F> | ||
| 14 | u64 GenerateUniformRange(u64 min, u64 max, F f); | ||
| 15 | |||
| 16 | u64 GenerateRandomRange(u64 min, u64 max); | ||
| 17 | |||
| 18 | } // namespace Kernel::Memory::SystemControl | ||