From 188e478033b126938d4b45d88733c14515b11c74 Mon Sep 17 00:00:00 2001 From: Reece Date: Sun, 20 Feb 2022 20:43:37 +0000 Subject: [PATCH] [+] Improve RNG apis. TODO: more work is needed --- Include/Aurora/RNG/IRandomDevice.hpp | 28 ++++++++++++++++++++-------- Include/Aurora/RNG/RNG.hpp | 10 ++++++++-- Source/RNG/RNG.cpp | 2 +- Source/RNG/RandomDevice.cpp | 8 ++++---- 4 files changed, 33 insertions(+), 15 deletions(-) diff --git a/Include/Aurora/RNG/IRandomDevice.hpp b/Include/Aurora/RNG/IRandomDevice.hpp index 4dac7ad3..c4bf869a 100644 --- a/Include/Aurora/RNG/IRandomDevice.hpp +++ b/Include/Aurora/RNG/IRandomDevice.hpp @@ -28,25 +28,37 @@ namespace Aurora::RNG template - inline void NextArray(T(&array)[N]) + inline void NextFillArray(T(&array)[N]) { Read(array, N * sizeof(T)); } - + template - inline void NextArray(T *array, AuUInt32 length) + inline void NextFillArray(T *array, AuUInt32 length) { - Raed(array, length * sizeof(T)); + Read(array, length * sizeof(T)); + } + + template + static inline T &NextFillRange(const T &items) + { + NextFillArray(items.begin(), items.end() - items.begin()); } template - inline T NextTmpl() + inline T NextFillTmpl() { T ret {}; Read(&ret, sizeof(T)); return ret; } - + + template + inline T &NextIterator(T &begin, T &end) + { + return begin + NextIndex(AuUInt(end - begin)); + } + template inline T &NextArray(T *items, AuUInt32 count) { @@ -60,9 +72,9 @@ namespace Aurora::RNG } template - static inline T &RngRange(const T &items) + static inline T &NextRange(const T &items) { - return RngArray(items.begin(), items.end() - items.begin()); + return NextIterator(items.begin(), items.end()); } }; diff --git a/Include/Aurora/RNG/RNG.hpp b/Include/Aurora/RNG/RNG.hpp index af48d5ee..cbe01d8f 100644 --- a/Include/Aurora/RNG/RNG.hpp +++ b/Include/Aurora/RNG/RNG.hpp @@ -33,7 +33,7 @@ namespace Aurora::RNG // Note: it is conceivable that someone may want the following templates for some cryptographic purpose template - static inline void RngArray(T(&array)[N]) + static inline void RngFillArray(T(&array)[N]) { if constexpr (fast) { @@ -46,7 +46,7 @@ namespace Aurora::RNG } template - static inline void RngArray(T *array, AuUInt32 length) + static inline void RngFillArray(T *array, AuUInt32 length) { if constexpr (fast) { @@ -58,6 +58,12 @@ namespace Aurora::RNG } } + template + static inline T &RngFillRange(const T &items) + { + return RngFillArray(items.begin(), items.end() - items.begin()); + } + template static inline T RngTmpl() { diff --git a/Source/RNG/RNG.cpp b/Source/RNG/RNG.cpp index 0c5425e1..f3f2ade7 100644 --- a/Source/RNG/RNG.cpp +++ b/Source/RNG/RNG.cpp @@ -261,7 +261,7 @@ namespace Aurora::RNG static void InitFastRng() { AuArray maxEntropy; - RngArray(maxEntropy.data(), 64); + RngFillArray(maxEntropy.data(), 64); gWellRand = WELL_SeedRandBig64(maxEntropy); gFastDevice = RandomUnique(RandomDef {false}); } diff --git a/Source/RNG/RandomDevice.cpp b/Source/RNG/RandomDevice.cpp index e1ef521c..4b491761 100644 --- a/Source/RNG/RandomDevice.cpp +++ b/Source/RNG/RandomDevice.cpp @@ -55,7 +55,7 @@ namespace Aurora::RNG else { this->fast_ = {}; - RNG::RngArray(this->fast_.state); + RNG::RngFillArray(this->fast_.state); } } // secure rng requires no init -> we just passthrough to the global ReadSecureRNG function @@ -110,7 +110,7 @@ namespace Aurora::RNG AuUInt8 RandomDevice::NextByte() { - return NextTmpl(); + return NextFillTmpl(); } bool RandomDevice::NextBoolean() @@ -120,12 +120,12 @@ namespace Aurora::RNG AuUInt32 RandomDevice::NextU32() { - return NextTmpl(); + return NextFillTmpl(); } AuUInt64 RandomDevice::NextU64() { - return NextTmpl(); + return NextFillTmpl(); } AuInt32 RandomDevice::NextInt(AuInt32 min, AuInt32 max)