Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimized divisionless implementation of minstd #5256

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions stl/inc/random
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,34 @@ _NODISCARD _Uint _Next_linear_congruential_value(_Uint _Prev) noexcept {
}
}

// Optimized specialization for minstd_rand0
mirounga marked this conversation as resolved.
Show resolved Hide resolved
template <>
_NODISCARD inline unsigned int _Next_linear_congruential_value<unsigned int, 16807, 0, 2147483647>(
mirounga marked this conversation as resolved.
Show resolved Hide resolved
unsigned int _Prev) noexcept {
constexpr unsigned long long _All = 16807ULL;
constexpr unsigned long long _Mll = 2147483647ULL;

auto _Next = static_cast<unsigned long long>(_Prev) * _All;
_Next = (_Next >> 31) + (_Next & _Mll);
_Next = _Next < _Mll ? _Next : _Next - _Mll;

return static_cast<unsigned int>(_Next);
}

// Optimized specialization for minstd_rand
template <>
_NODISCARD inline unsigned int _Next_linear_congruential_value<unsigned int, 48271, 0, 2147483647>(
unsigned int _Prev) noexcept {
constexpr unsigned long long _All = 48271ULL;
constexpr unsigned long long _Mll = 2147483647ULL;

auto _Next = static_cast<unsigned long long>(_Prev) * _All;
_Next = (_Next >> 31) + (_Next & _Mll);
_Next = _Next < _Mll ? _Next : _Next - _Mll;

return static_cast<unsigned int>(_Next);
}

template <class _Seed_seq>
_NODISCARD constexpr unsigned int _Seed_seq_to_uint(_Seed_seq& _Seq) {
unsigned int _Arr[4]{};
Expand Down