diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:15 +0300 |
commit | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch) | |
tree | da2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /util/random/fast.h | |
parent | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff) | |
download | ydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'util/random/fast.h')
-rw-r--r-- | util/random/fast.h | 168 |
1 files changed, 84 insertions, 84 deletions
diff --git a/util/random/fast.h b/util/random/fast.h index ddc5711641..69b1772dd8 100644 --- a/util/random/fast.h +++ b/util/random/fast.h @@ -1,101 +1,101 @@ -#pragma once - -#include "lcg_engine.h" -#include "common_ops.h" - +#pragma once + +#include "lcg_engine.h" +#include "common_ops.h" + #include <util/generic/bitops.h> -#include <util/system/platform.h> - -// based on http://www.pcg-random.org/. See T*FastRng* family below. - -struct TPCGMixer { +#include <util/system/platform.h> + +// based on http://www.pcg-random.org/. See T*FastRng* family below. + +struct TPCGMixer { static inline ui32 Mix(ui64 x) noexcept { - const ui32 xorshifted = ((x >> 18u) ^ x) >> 27u; - const ui32 rot = x >> 59u; - + const ui32 xorshifted = ((x >> 18u) ^ x) >> 27u; + const ui32 rot = x >> 59u; + return RotateBitsRight(xorshifted, rot); - } -}; - -using TFastRng32Base = TLcgRngBase<TLcgIterator<ui64, ULL(6364136223846793005)>, TPCGMixer>; -using TReallyFastRng32Base = TLcgRngBase<TFastLcgIterator<ui64, ULL(6364136223846793005), ULL(1)>, TPCGMixer>; - + } +}; + +using TFastRng32Base = TLcgRngBase<TLcgIterator<ui64, ULL(6364136223846793005)>, TPCGMixer>; +using TReallyFastRng32Base = TLcgRngBase<TFastLcgIterator<ui64, ULL(6364136223846793005), ULL(1)>, TPCGMixer>; + class IInputStream; - -struct TFastRng32: public TCommonRNG<ui32, TFastRng32>, public TFastRng32Base { - inline TFastRng32(ui64 seed, ui32 seq) - : TFastRng32Base(seed, seq) - { - } - + +struct TFastRng32: public TCommonRNG<ui32, TFastRng32>, public TFastRng32Base { + inline TFastRng32(ui64 seed, ui32 seq) + : TFastRng32Base(seed, seq) + { + } + TFastRng32(IInputStream& entropy); -}; - -// faster than TFastRng32, but have only one possible stream sequence -struct TReallyFastRng32: public TCommonRNG<ui32, TReallyFastRng32>, public TReallyFastRng32Base { - inline TReallyFastRng32(ui64 seed) - : TReallyFastRng32Base(seed) - { - } - +}; + +// faster than TFastRng32, but have only one possible stream sequence +struct TReallyFastRng32: public TCommonRNG<ui32, TReallyFastRng32>, public TReallyFastRng32Base { + inline TReallyFastRng32(ui64 seed) + : TReallyFastRng32Base(seed) + { + } + TReallyFastRng32(IInputStream& entropy); -}; - -class TFastRng64: public TCommonRNG<ui64, TFastRng64> { -public: - struct TArgs { +}; + +class TFastRng64: public TCommonRNG<ui64, TFastRng64> { +public: + struct TArgs { TArgs(ui64 seed) noexcept; TArgs(IInputStream& entropy); - - ui64 Seed1; - ui64 Seed2; - ui32 Seq1; - ui32 Seq2; - }; - + + ui64 Seed1; + ui64 Seed2; + ui32 Seq1; + ui32 Seq2; + }; + TFastRng64(ui64 seed1, ui32 seq1, ui64 seed2, ui32 seq2) noexcept; - - /* - * simplify constructions like - * TFastRng64 rng(17); + + /* + * simplify constructions like + * TFastRng64 rng(17); * TFastRng64 rng(Seek()); //from any IInputStream - */ + */ inline TFastRng64(const TArgs& args) noexcept - : TFastRng64(args.Seed1, args.Seq1, args.Seed2, args.Seq2) - { - } - + : TFastRng64(args.Seed1, args.Seq1, args.Seed2, args.Seq2) + { + } + inline ui64 GenRand() noexcept { - const ui64 x = R1_.GenRand(); - const ui64 y = R2_.GenRand(); - - return (x << 32) | y; - } - + const ui64 x = R1_.GenRand(); + const ui64 y = R2_.GenRand(); + + return (x << 32) | y; + } + inline void Advance(ui64 delta) noexcept { R1_.Advance(delta); R2_.Advance(delta); } -private: - TFastRng32Base R1_; - TFastRng32Base R2_; -}; - -namespace NPrivate { - template <typename T> - struct TFastRngTraits; - - template <> - struct TFastRngTraits<ui32> { - using TResult = TReallyFastRng32; - }; - - template <> - struct TFastRngTraits<ui64> { - using TResult = TFastRng64; - }; -} - -template <typename T> -using TFastRng = typename ::NPrivate::TFastRngTraits<T>::TResult; +private: + TFastRng32Base R1_; + TFastRng32Base R2_; +}; + +namespace NPrivate { + template <typename T> + struct TFastRngTraits; + + template <> + struct TFastRngTraits<ui32> { + using TResult = TReallyFastRng32; + }; + + template <> + struct TFastRngTraits<ui64> { + using TResult = TFastRng64; + }; +} + +template <typename T> +using TFastRng = typename ::NPrivate::TFastRngTraits<T>::TResult; |