Skip to content

Arbitrary logarithmic-time jumps for the xoshiro generators - #172

Merged
mborland merged 2 commits into
boostorg:developfrom
vigna:xoshiro-jump
Nov 3, 2026
Merged

mborland merged 2 commits into
boostorg:developfrom
vigna:xoshiro-jump

Conversation

@vigna

@vigna vigna commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

This PR makes discard(z) of the xoshiro generators logarithmic in z (it currently calls next() z times), and adds jump(c, e), which advances the generator by c · 2^e steps, so that non-overlapping subsequences can be obtained at any distance, not only at those of jump() and long_jump(). It is the same interface as the MWCX generators of #171, and the same functions are in the reference C code at https://prng.di.unimi.it (jump_ce()).

Advancing a linear generator by n steps is equivalent to multiplying its state by x^n modulo the characteristic polynomial of its transition map; x^n mod p is computed by square-and-multiply, and it is applied to the state with the same accumulate-and-step loop used by jump() and long_jump().

Notes:

  • Code. detail/f2x_arithmetic.hpp is a translation of f2x.c from https://prng.di.unimi.it: plain shift-and-xor arithmetic in F₂[x], no carryless-multiply instructions. xoshiro_base.hpp gets the characteristic polynomials of the three linear engines (xoshiro128, xoshiro256, xoshiro512: the output scramblers do not matter), jump(c, e), and the new discard(). jump() and long_jump() are unchanged, except that their comments now give the distances for each state size (they said 2^128 and 2^192 for all of them).
  • Cost. On an x86-64 machine with GCC 15.2 and Clang 21.1, a jump costs about 3 µs (xoshiro128), 5.5 µs (xoshiro256) and 17 µs (xoshiro512) at the distances where discard() starts jumping, and about 20, 75 and 200 µs for distances close to 2^64. Below a threshold (2048 steps per 64 bits of state) discard() still steps the generator, as it is faster: the threshold is close to the break-even point with GCC and Clang.
  • Tests. test/test_xoshiro_jump.cpp checks, for all nine generators, discard() and jump() against states computed with the reference jump_ce(), discard(d) and jump(d, 0) against d steps around the threshold, jump(1, 128) and jump(1, 192) against jump() and long_jump() (jump(1, 64)/jump(1, 96) and jump(1, 256)/jump(1, 384) for xoshiro128 and xoshiro512), jump(1, n) against a single step for n bits of state (the period is 2^n − 1), and the composition of jumps. Run with GCC 15.2 and Clang 21.1, C++11 and C++20, 64-bit and -m32; the existing xoshiro tests pass. I could not test MSVC.

discard() is now computed in time logarithmic in the distance, and the new
jump(c, e) advances the generator by c * 2^e steps.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vigna

vigna commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

The code is a port from the C code at https://prng.di.unimi.it/ authored by Claude Opus 5.5.

@codecov

codecov Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.18%. Comparing base (0fa1dd5) to head (cf262b2).

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #172      +/-   ##
===========================================
+ Coverage    96.11%   96.18%   +0.07%     
===========================================
  Files          127      129       +2     
  Lines         8261     8446     +185     
===========================================
+ Hits          7940     8124     +184     
- Misses         321      322       +1     
Files with missing lines Coverage Δ
include/boost/random/detail/f2x_arithmetic.hpp 100.00% <100.00%> (ø)
include/boost/random/detail/xoshiro_base.hpp 99.42% <100.00%> (-0.58%) ⬇️
test/test_xoshiro_jump.cpp 100.00% <100.00%> (ø)

Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0fa1dd5...cf262b2. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mborland mborland left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@mborland
mborland merged commit 7779326 into boostorg:develop Nov 3, 2026
64 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants