diff options
| author | Ellie Hermaszewska <ellieh@nvidia.com> | 2024-10-29 14:49:26 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-29 14:49:26 +0800 |
| commit | f65d756bff8d4c5cbc15bd0322a2ae8e6b896a21 (patch) | |
| tree | ea1d61342cd29368e19135000ec2948813096205 /source/core/slang-random-generator.h | |
| parent | a729c15e9dce9f5116a38afc66329ab2ca4cea54 (diff) | |
format
* format
* Minor test fixes
* enable checking cpp format in ci
Diffstat (limited to 'source/core/slang-random-generator.h')
| -rw-r--r-- | source/core/slang-random-generator.h | 81 |
1 files changed, 44 insertions, 37 deletions
diff --git a/source/core/slang-random-generator.h b/source/core/slang-random-generator.h index fa83fec3b..02bf18af2 100644 --- a/source/core/slang-random-generator.h +++ b/source/core/slang-random-generator.h @@ -1,78 +1,86 @@ #ifndef SLANG_CORE_RANDOM_GENERATOR_H #define SLANG_CORE_RANDOM_GENERATOR_H +#include "slang-smart-pointer.h" #include "slang.h" #include <stdlib.h> #include <string.h> -#include "slang-smart-pointer.h" - -namespace Slang { - -class RandomGenerator: public RefObject +namespace Slang { - public: - /// Make a copy of the generator in the same state +class RandomGenerator : public RefObject +{ +public: + /// Make a copy of the generator in the same state virtual RandomGenerator* clone() = 0; - /// Reset with a seed + /// Reset with a seed virtual void reset(int32_t seed) = 0; - /// Next int32_t random number + /// Next int32_t random number virtual int32_t nextInt32() = 0; - /// Next int64_t random number + /// Next int64_t random number virtual int64_t nextInt64(); - /// Get a 0-1 range floating point + /// Get a 0-1 range floating point virtual float nextUnitFloat32(); - - /// Get the next bool + + /// Get the next bool virtual bool nextBool(); - /// Get multiple int32s + /// Get multiple int32s virtual void nextInt32s(int32_t* dst, size_t count) = 0; - /// Next uint32_t + /// Next uint32_t uint32_t nextUInt32() { return uint32_t(nextInt32()); } - /// Next Int32 which can only be positive + /// Next Int32 which can only be positive int32_t nextPositiveInt32() { return nextInt32() & 0x7fffffff; } - /// Next Int64 which can only be positive + /// Next Int64 which can only be positive int64_t nextPositiveInt64() { return nextInt64() & SLANG_INT64(0x7fffffffffffffff); } - /// Returns value up to BUT NOT INCLUDING maxValue. - int32_t nextInt32UpTo(int32_t maxValue) { assert(maxValue > 0); return (maxValue <= 1) ? 0 : (nextPositiveInt32() % maxValue); } + /// Returns value up to BUT NOT INCLUDING maxValue. + int32_t nextInt32UpTo(int32_t maxValue) + { + assert(maxValue > 0); + return (maxValue <= 1) ? 0 : (nextPositiveInt32() % maxValue); + } - /// Returns value from min up to BUT NOT INCLUDING max. + /// Returns value from min up to BUT NOT INCLUDING max. int32_t nextInt32InRange(int32_t min, int32_t max); /// Returns value from min up to BUT NOT INCLUDING max uint32_t nextUInt32InRange(uint32_t min, uint32_t max); - /// Returns value up to BUT NOT INCLUDING maxValue - int64_t nextInt64UpTo(int64_t maxValue) { assert(maxValue > 0); return (maxValue <= 1) ? 0 : (nextPositiveInt64() % maxValue); } + /// Returns value up to BUT NOT INCLUDING maxValue + int64_t nextInt64UpTo(int64_t maxValue) + { + assert(maxValue > 0); + return (maxValue <= 1) ? 0 : (nextPositiveInt64() % maxValue); + } - /// Returns value from min up to BUT NOT INCLUDING max + /// Returns value from min up to BUT NOT INCLUDING max int64_t nextInt64InRange(int64_t min, int64_t max); - /// Fill with random data. - /// NOTE! Output is only identical bytes if generator in same state *and* size_t(dst) & 3 is the same on calls. + /// Fill with random data. + /// NOTE! Output is only identical bytes if generator in same state *and* size_t(dst) & 3 is the + /// same on calls. void nextData(void* dst, size_t size); - /// Create a RandomGenerator with specified seed using default generator type + /// Create a RandomGenerator with specified seed using default generator type static RandomGenerator* create(int32_t seed); }; -/* Mersenne Twister random number generator +/* Mersenne Twister random number generator https://en.wikipedia.org/wiki/Mersenne_Twister */ -class Mt19937RandomGenerator: public RandomGenerator +class Mt19937RandomGenerator : public RandomGenerator { - public: +public: typedef Mt19937RandomGenerator ThisType; - enum + enum { kNumEntries = 624 }; @@ -82,24 +90,23 @@ class Mt19937RandomGenerator: public RandomGenerator int32_t nextInt32() SLANG_OVERRIDE; void nextInt32s(int32_t* dst, size_t count) SLANG_OVERRIDE; - /// Ctor + /// Ctor Mt19937RandomGenerator(); Mt19937RandomGenerator(const ThisType& rhs); explicit Mt19937RandomGenerator(int32_t seed); - /// Assignment + /// Assignment void operator=(const ThisType& rhs) { m_index = rhs.m_index; ::memcpy(m_mt, rhs.m_mt, sizeof(m_mt)); } - - protected: - void _generate(); - uint32_t m_mt[kNumEntries]; ///< The random state vector - int m_index; ///< If set to >= kMaxEntries it means reset +protected: + void _generate(); + uint32_t m_mt[kNumEntries]; ///< The random state vector + int m_index; ///< If set to >= kMaxEntries it means reset }; typedef Mt19937RandomGenerator DefaultRandomGenerator; |
