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/mersenne64.cpp | |
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/mersenne64.cpp')
-rw-r--r-- | util/random/mersenne64.cpp | 190 |
1 files changed, 95 insertions, 95 deletions
diff --git a/util/random/mersenne64.cpp b/util/random/mersenne64.cpp index 4ede2c6dca..9ebf24242f 100644 --- a/util/random/mersenne64.cpp +++ b/util/random/mersenne64.cpp @@ -1,100 +1,100 @@ -#include "mersenne64.h" - -#include <util/generic/array_size.h> -#include <util/stream/input.h> - -#define MM 156 -#define MATRIX_A ULL(0xB5026F5AA96619E9) -#define UM ULL(0xFFFFFFFF80000000) -#define LM ULL(0x7FFFFFFF) - -using namespace NPrivate; - +#include "mersenne64.h" + +#include <util/generic/array_size.h> +#include <util/stream/input.h> + +#define MM 156 +#define MATRIX_A ULL(0xB5026F5AA96619E9) +#define UM ULL(0xFFFFFFFF80000000) +#define LM ULL(0x7FFFFFFF) + +using namespace NPrivate; + void TMersenne64::InitGenRand(ui64 seed) noexcept { - mt[0] = seed; - - for (mti = 1; mti < NN; ++mti) { - mt[mti] = (ULL(6364136223846793005) * (mt[mti - 1] ^ (mt[mti - 1] >> 62)) + mti); - } -} - + mt[0] = seed; + + for (mti = 1; mti < NN; ++mti) { + mt[mti] = (ULL(6364136223846793005) * (mt[mti - 1] ^ (mt[mti - 1] >> 62)) + mti); + } +} + void TMersenne64::InitByArray(const ui64* init_key, size_t key_length) noexcept { - ui64 i = 1; - ui64 j = 0; - ui64 k; - - InitGenRand(ULL(19650218)); - - k = NN > key_length ? NN : key_length; - - for (; k; --k) { - mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >> 62)) * ULL(3935559000370003845))) + init_key[j] + j; - - ++i; - ++j; - - if (i >= NN) { - mt[0] = mt[NN - 1]; - i = 1; - } - - if (j >= key_length) { - j = 0; - } - } - - for (k = NN - 1; k; --k) { - mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >> 62)) * ULL(2862933555777941757))) - i; - - ++i; - - if (i >= NN) { - mt[0] = mt[NN - 1]; - i = 1; - } - } - - mt[0] = ULL(1) << 63; -} - + ui64 i = 1; + ui64 j = 0; + ui64 k; + + InitGenRand(ULL(19650218)); + + k = NN > key_length ? NN : key_length; + + for (; k; --k) { + mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >> 62)) * ULL(3935559000370003845))) + init_key[j] + j; + + ++i; + ++j; + + if (i >= NN) { + mt[0] = mt[NN - 1]; + i = 1; + } + + if (j >= key_length) { + j = 0; + } + } + + for (k = NN - 1; k; --k) { + mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >> 62)) * ULL(2862933555777941757))) - i; + + ++i; + + if (i >= NN) { + mt[0] = mt[NN - 1]; + i = 1; + } + } + + mt[0] = ULL(1) << 63; +} + void TMersenne64::InitNext() noexcept { - int i; - ui64 x; - ui64 mag01[2] = { - ULL(0), - MATRIX_A, - }; - - if (mti == NN + 1) { - InitGenRand(ULL(5489)); - } - - for (i = 0; i < NN - MM; ++i) { - x = (mt[i] & UM) | (mt[i + 1] & LM); - mt[i] = mt[i + MM] ^ (x >> 1) ^ mag01[(int)(x & ULL(1))]; - } - - for (; i < NN - 1; ++i) { - x = (mt[i] & UM) | (mt[i + 1] & LM); - mt[i] = mt[i + (MM - NN)] ^ (x >> 1) ^ mag01[(int)(x & ULL(1))]; - } - - x = (mt[NN - 1] & UM) | (mt[0] & LM); - mt[NN - 1] = mt[MM - 1] ^ (x >> 1) ^ mag01[(int)(x & ULL(1))]; - - mti = 0; -} - + int i; + ui64 x; + ui64 mag01[2] = { + ULL(0), + MATRIX_A, + }; + + if (mti == NN + 1) { + InitGenRand(ULL(5489)); + } + + for (i = 0; i < NN - MM; ++i) { + x = (mt[i] & UM) | (mt[i + 1] & LM); + mt[i] = mt[i + MM] ^ (x >> 1) ^ mag01[(int)(x & ULL(1))]; + } + + for (; i < NN - 1; ++i) { + x = (mt[i] & UM) | (mt[i + 1] & LM); + mt[i] = mt[i + (MM - NN)] ^ (x >> 1) ^ mag01[(int)(x & ULL(1))]; + } + + x = (mt[NN - 1] & UM) | (mt[0] & LM); + mt[NN - 1] = mt[MM - 1] ^ (x >> 1) ^ mag01[(int)(x & ULL(1))]; + + mti = 0; +} + TMersenne64::TMersenne64(IInputStream& input) - : mti(NN + 1) -{ - ui64 buf[128]; - - input.LoadOrFail(buf, sizeof(buf)); + : mti(NN + 1) +{ + ui64 buf[128]; + + input.LoadOrFail(buf, sizeof(buf)); InitByArray(buf, Y_ARRAY_SIZE(buf)); -} - -#undef MM -#undef MATRIX_A -#undef UM -#undef LM +} + +#undef MM +#undef MATRIX_A +#undef UM +#undef LM |