Modernize FX_Random further [pdfium : main]

0 views
Skip to first unread message

Lei Zhang (Gerrit)

unread,
Feb 20, 2026, 2:33:32 PM (2 days ago) Feb 20
to Andy Phan, Lei Zhang, Pdfium LUCI CQ, pdfium-...@googlegroups.com
Attention needed from Andy Phan

Lei Zhang added 2 comments

File core/fxcrt/fx_random.cpp
Line 67, Patchset 1 (Latest): if (!g_global_seed_initialized) {
Lei Zhang . unresolved

Can get rid of `g_global_seed_initialized` with:

`static uint32_t global_seed = []() -> uint32_t { lambda_for_lines_68_to_74 }();`

Line 102, Patchset 1 (Latest): uint32_t bits;
Lei Zhang . unresolved

Maybe "result"?

Open in Gerrit

Related details

Attention is currently required from:
  • Andy Phan
Submit Requirements:
  • requirement is not satisfiedCode-Owners
  • requirement is not satisfiedCode-Review
  • requirement is not satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: pdfium
Gerrit-Branch: main
Gerrit-Change-Id: I23bddecd739a980e2ec3101de85f2fded8c73721
Gerrit-Change-Number: 143531
Gerrit-PatchSet: 1
Gerrit-Owner: Andy Phan <andy...@chromium.org>
Gerrit-Reviewer: Andy Phan <andy...@chromium.org>
Gerrit-CC: Lei Zhang <the...@chromium.org>
Gerrit-Attention: Andy Phan <andy...@chromium.org>
Gerrit-Comment-Date: Fri, 20 Feb 2026 19:33:26 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
unsatisfied_requirement
open
diffy

Lei Zhang (Gerrit)

unread,
Feb 20, 2026, 2:33:34 PM (2 days ago) Feb 20
to Andy Phan, Lei Zhang, Pdfium LUCI CQ, pdfium-...@googlegroups.com
Attention needed from Andy Phan

Lei Zhang voted Code-Review+1

Code-Review+1
Open in Gerrit

Related details

Attention is currently required from:
  • Andy Phan
Submit Requirements:
  • requirement satisfiedCode-Owners
  • requirement satisfiedCode-Review
  • requirement is not satisfiedNo-Unresolved-Comments
  • requirement satisfiedReview-Enforcement
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: pdfium
Gerrit-Branch: main
Gerrit-Change-Id: I23bddecd739a980e2ec3101de85f2fded8c73721
Gerrit-Change-Number: 143531
Gerrit-PatchSet: 1
Gerrit-Owner: Andy Phan <andy...@chromium.org>
Gerrit-Reviewer: Andy Phan <andy...@chromium.org>
Gerrit-Reviewer: Lei Zhang <the...@chromium.org>
Gerrit-Attention: Andy Phan <andy...@chromium.org>
Gerrit-Comment-Date: Fri, 20 Feb 2026 19:33:31 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
satisfied_requirement
unsatisfied_requirement
open
diffy

Andy Phan (Gerrit)

unread,
Feb 20, 2026, 2:51:50 PM (2 days ago) Feb 20
to Lei Zhang, Pdfium LUCI CQ, pdfium-...@googlegroups.com

Andy Phan added 2 comments

File core/fxcrt/fx_random.cpp
Line 67, Patchset 1: if (!g_global_seed_initialized) {
Lei Zhang . resolved

Can get rid of `g_global_seed_initialized` with:

`static uint32_t global_seed = []() -> uint32_t { lambda_for_lines_68_to_74 }();`

Andy Phan

Done

Line 102, Patchset 1: uint32_t bits;
Lei Zhang . resolved

Maybe "result"?

Andy Phan

Done

Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
    • requirement satisfiedCode-Owners
    • requirement satisfiedCode-Review
    • requirement satisfiedReview-Enforcement
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: pdfium
    Gerrit-Branch: main
    Gerrit-Change-Id: I23bddecd739a980e2ec3101de85f2fded8c73721
    Gerrit-Change-Number: 143531
    Gerrit-PatchSet: 3
    Gerrit-Owner: Andy Phan <andy...@chromium.org>
    Gerrit-Reviewer: Andy Phan <andy...@chromium.org>
    Gerrit-Reviewer: Lei Zhang <the...@chromium.org>
    Gerrit-Comment-Date: Fri, 20 Feb 2026 19:51:47 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Comment-In-Reply-To: Lei Zhang <the...@chromium.org>
    satisfied_requirement
    open
    diffy

    Andy Phan (Gerrit)

    unread,
    Feb 20, 2026, 5:20:51 PM (2 days ago) Feb 20
    to Lei Zhang, Pdfium LUCI CQ, pdfium-...@googlegroups.com

    Andy Phan voted Commit-Queue+2

    Commit-Queue+2
    Open in Gerrit

    Related details

    Attention set is empty
    Submit Requirements:
    • requirement satisfiedCode-Owners
    • requirement satisfiedCode-Review
    • requirement satisfiedReview-Enforcement
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: pdfium
    Gerrit-Branch: main
    Gerrit-Change-Id: I23bddecd739a980e2ec3101de85f2fded8c73721
    Gerrit-Change-Number: 143531
    Gerrit-PatchSet: 4
    Gerrit-Owner: Andy Phan <andy...@chromium.org>
    Gerrit-Reviewer: Andy Phan <andy...@chromium.org>
    Gerrit-Reviewer: Lei Zhang <the...@chromium.org>
    Gerrit-Comment-Date: Fri, 20 Feb 2026 22:20:49 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    satisfied_requirement
    open
    diffy

    Pdfium LUCI CQ (Gerrit)

    unread,
    Feb 20, 2026, 5:24:32 PM (2 days ago) Feb 20
    to Andy Phan, Lei Zhang, pdfium-...@googlegroups.com

    Pdfium LUCI CQ submitted the change with unreviewed changes

    Unreviewed changes

    1 is the latest approved patch-set.
    The change was submitted with unreviewed changes in the following files:

    ```
    The name of the file: core/fxcrt/fx_random.h
    Insertions: 2, Deletions: 2.

    @@ -17,6 +17,8 @@
    // A Mersenne Twister (MT) pseudo-random number generator.
    class FX_Random {
    public:
    + static constexpr size_t kStateSize = 848;
    +
    explicit FX_Random(uint32_t seed);

    FX_Random(const FX_Random&) = delete;
    @@ -32,8 +34,6 @@
    uint32_t Generate();

    private:
    - static constexpr size_t kStateSize = 848;
    -
    uint32_t next_index_;
    std::array<uint32_t, kStateSize> state_;
    };
    ```
    ```
    The name of the file: core/fxcrt/fx_random.cpp
    Insertions: 34, Deletions: 32.

    @@ -27,9 +27,6 @@
    constexpr uint32_t kUpperMask = 0x80000000;
    constexpr uint32_t kLowerMask = 0x7fffffff;

    -bool g_global_seed_initialized = false;
    -uint32_t g_global_seed = 0;
    -
    #if BUILDFLAG(IS_WIN)
    bool GenerateSeedFromCryptoRandom(uint32_t* seed) {
    HCRYPTPROV hCP = 0;
    @@ -64,29 +61,34 @@
    }

    uint32_t GetNextGlobalSeed() {
    - if (!g_global_seed_initialized) {
    + static uint32_t global_seed = []() -> uint32_t {
    + uint32_t initial_seed;
    #if BUILDFLAG(IS_WIN)
    - if (!GenerateSeedFromCryptoRandom(&g_global_seed)) {
    - g_global_seed = GenerateSeedFromEnvironment();
    + if (!GenerateSeedFromCryptoRandom(&initial_seed)) {
    + initial_seed = GenerateSeedFromEnvironment();
    }
    #else
    - g_global_seed = GenerateSeedFromEnvironment();
    + initial_seed = GenerateSeedFromEnvironment();
    #endif
    - g_global_seed_initialized = true;
    + return initial_seed;
    + }();
    + 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 ++g_global_seed;
    + return state;
    }

    } // namespace

    -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);
    - }
    - next_index_ = kStateSize;
    -}
    +FX_Random::FX_Random(uint32_t seed)
    + : next_index_(kStateSize), state_(InitState(seed)) {}

    FX_Random::~FX_Random() = default;

    @@ -99,31 +101,31 @@
    }

    uint32_t FX_Random::Generate() {
    - uint32_t bits;
    + uint32_t result;
    if (next_index_ >= kStateSize) {
    static constexpr std::array<uint32_t, 2> kMatrixATable = {{0, 0x9908b0df}};
    uint32_t i;
    for (i = 0; i < kStateSize - kTwistOffset; ++i) {
    - bits = (state_[i] & kUpperMask) | (state_[i + 1] & kLowerMask);
    + result = (state_[i] & kUpperMask) | (state_[i + 1] & kLowerMask);
    state_[i] =
    - state_[i + kTwistOffset] ^ (bits >> 1) ^ kMatrixATable[bits & 1];
    + state_[i + kTwistOffset] ^ (result >> 1) ^ kMatrixATable[result & 1];
    }
    for (; i < kStateSize - 1; ++i) {
    - bits = (state_[i] & kUpperMask) | (state_[i + 1] & kLowerMask);
    - // `kTwistOffset` - `kStateSize` underflows, but this is safe because
    + result = (state_[i] & kUpperMask) | (state_[i + 1] & kLowerMask);
    + // `kTwistOffset - kStateSize` underflows, but this is safe because
    // unsigned underflow is well-defined.
    - state_[i] = state_[i + (kTwistOffset - kStateSize)] ^ (bits >> 1) ^
    - kMatrixATable[bits & 1];
    + state_[i] = state_[i + (kTwistOffset - kStateSize)] ^ (result >> 1) ^
    + kMatrixATable[result & 1];
    }
    - bits = (state_[kStateSize - 1] & kUpperMask) | (state_[0] & kLowerMask);
    + result = (state_[kStateSize - 1] & kUpperMask) | (state_[0] & kLowerMask);
    state_[kStateSize - 1] =
    - state_[kTwistOffset - 1] ^ (bits >> 1) ^ kMatrixATable[bits & 1];
    + state_[kTwistOffset - 1] ^ (result >> 1) ^ kMatrixATable[result & 1];
    next_index_ = 0;
    }
    - bits = state_[next_index_++];
    - bits ^= (bits >> 11);
    - bits ^= (bits << 7) & 0x9d2c5680UL;
    - bits ^= (bits << 15) & 0xefc60000UL;
    - bits ^= (bits >> 18);
    - return bits;
    + result = state_[next_index_++];
    + result ^= (result >> 11);
    + result ^= (result << 7) & 0x9d2c5680UL;
    + result ^= (result << 15) & 0xefc60000UL;
    + result ^= (result >> 18);
    + return result;
    }
    ```

    Change information

    Commit message:
    Modernize FX_Random further

    Modernize FX_Random by:
    1. Renaming all abbreviated variables to more human-readable names.
    2. Updating naming style to modern convention.
    3. Changing macros to constants.
    4. Replace global variables by using static variables and lambdas.
    Change-Id: I23bddecd739a980e2ec3101de85f2fded8c73721
    Reviewed-by: Lei Zhang <the...@chromium.org>
    Commit-Queue: Andy Phan <andy...@chromium.org>
    Files:
    • M core/fxcrt/fx_random.cpp
    • M core/fxcrt/fx_random.h
    Change size: M
    Delta: 2 files changed, 56 insertions(+), 55 deletions(-)
    Branch: refs/heads/main
    Submit Requirements:
    • requirement satisfiedCode-Review: +1 by Lei Zhang
    Open in Gerrit
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: merged
    Gerrit-Project: pdfium
    Gerrit-Branch: main
    Gerrit-Change-Id: I23bddecd739a980e2ec3101de85f2fded8c73721
    Gerrit-Change-Number: 143531
    Gerrit-PatchSet: 5
    Gerrit-Owner: Andy Phan <andy...@chromium.org>
    Gerrit-Reviewer: Andy Phan <andy...@chromium.org>
    Gerrit-Reviewer: Lei Zhang <the...@chromium.org>
    open
    diffy
    satisfied_requirement
    Reply all
    Reply to author
    Forward
    0 new messages