Skip to content

Montgomery multiplication for the jumps of the MWCX generators - #176

Merged
mborland merged 1 commit into
boostorg:developfrom
vigna:mwcx-montgomery
Dec 1, 2026
Merged

mborland merged 1 commit into
boostorg:developfrom
vigna:mwcx-montgomery

Conversation

@vigna

@vigna vigna commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Following up on #171, discard() and jump() of the MWCX generators now multiply modulo m with Montgomery's method on 32-bit limbs, instead of shift-and-add. The idea comes from the implementation of these generators in NVIDIA's CCCL (NVIDIA/cccl#12590), for which Rory Mitchell suggested it; the reference implementation now uses it, too. The results are unchanged.

On an x86-64 machine (Xeon E-2388G) with GCC 16.2 and Clang 23.1, jump(1, 64) of mwc128x goes from 47–68 µs to 1.4–1.6 µs, and discard(1 << 40) from 13 µs to 0.9–1.0 µs; overall, jumps are from 6 to 50 times faster, depending on the generator, the distance and the compiler. As jumping is now much cheaper, the threshold below which discard() steps the generator goes down from 4096, 16384 and 65536 steps (for one, two and three 64-bit words of state) to 128, 512 and 1152, which is close to the measured break-even point.

@codecov

codecov Bot commented Dec 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.20%. Comparing base (9ad4e64) to head (2f89478).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff            @@
##           develop     #176   +/-   ##
========================================
  Coverage    96.20%   96.20%           
========================================
  Files          130      130           
  Lines         8488     8490    +2     
========================================
+ Hits          8166     8168    +2     
  Misses         322      322           
Files with missing lines Coverage Δ
include/boost/random/detail/mwcx_arithmetic.hpp 100.00% <100.00%> (ø)
include/boost/random/detail/mwcx_base.hpp 100.00% <100.00%> (ø)
include/boost/random/mwcx.hpp 100.00% <ø> (ø)
test/test_comp_mwcx.hpp 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 9ad4e64...2f89478. Read the comment docs.

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

@vigna

vigna commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

The failures of the clang 3.6 and clang 3.7 jobs are unrelated to this PR: all the errors are in boost/math/special_functions/digamma.hpp (changed by boostorg/math#1677), so they affect only the distribution tests that use Boost.Math, and the MWCX tests pass in both jobs.

@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.

Looks good to me! Thanks as always. I will handle the failing clang jobs post merge.

@mborland
mborland merged commit 16f955a into boostorg:develop Dec 1, 2026
64 of 66 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