Make mt-constants private to FX_Random. Avoid a copy of a std::array<> in the process by inlining init in the constructor itself. Change-Id: I561ffc3a66ace36f0a839bd7c8830add2dc75739 Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/149270 Reviewed-by: Lei Zhang <thestig@chromium.org> Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxcrt/fx_random.cpp b/core/fxcrt/fx_random.cpp index cc3ef27..6e9db07 100644 --- a/core/fxcrt/fx_random.cpp +++ b/core/fxcrt/fx_random.cpp
@@ -74,20 +74,15 @@ return ++global_seed; } -std::array<uint32_t, FX_Random::kStateSize> InitState(uint32_t seed) { - std::array<uint32_t, FX_Random::kStateSize> state; - state[0] = seed; - for (uint32_t i = 1; i < FX_Random::kStateSize; i++) { - const uint32_t prev = state[i - 1]; - state[i] = (1812433253UL * (prev ^ (prev >> 30)) + i); - } - return state; -} - } // namespace -FX_Random::FX_Random(uint32_t seed) - : next_index_(kStateSize), state_(InitState(seed)) {} +FX_Random::FX_Random(uint32_t seed) { + state_[0] = seed; + for (uint32_t i = 1; i < kStateSize; i++) { + const uint32_t prev = state_[i - 1]; + state_[i] = (1812433253UL * (prev ^ (prev >> 30)) + i); + } +} FX_Random::~FX_Random() = default;
diff --git a/core/fxcrt/fx_random.h b/core/fxcrt/fx_random.h index 0625f17..ed0da24 100644 --- a/core/fxcrt/fx_random.h +++ b/core/fxcrt/fx_random.h
@@ -17,14 +17,14 @@ // A Mersenne Twister (MT) pseudo-random number generator. class FX_Random { public: - static constexpr size_t kStateSize = 624; - static constexpr size_t kTwistOffset = 397; - // Using a temporary MT generator, fills `buffer` with random 32-bit unsigned // integers. static void Fill(pdfium::span<uint32_t> buffer); private: + static constexpr size_t kStateSize = 624; + static constexpr size_t kTwistOffset = 397; + explicit FX_Random(uint32_t seed); FX_Random(const FX_Random&) = delete; @@ -35,7 +35,7 @@ // Returns a single random 32-bit unsigned integer. uint32_t Generate(); - uint32_t next_index_; + uint32_t next_index_ = kStateSize; std::array<uint32_t, kStateSize> state_; };