[libc++][NFC] Inline mersenne_twister_engine functions into the class body (#170454)

Defining the functions outside the class makes things way harder to read
here, since the list of template arguments is incredibly long.
This commit is contained in:
Nikolas Klauser
2025-12-04 12:39:27 +01:00
committed by GitHub
parent adb7275995
commit d364c0eda9

View File

@@ -62,24 +62,6 @@ _LIBCPP_HIDE_FROM_ABI bool
operator==(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
template <class _UInt,
size_t _Wp,
size_t _Np,
size_t _Mp,
size_t _Rp,
_UInt _Ap,
size_t _Up,
_UInt _Dp,
size_t _Sp,
_UInt _Bp,
size_t _Tp,
_UInt _Cp,
size_t _Lp,
_UInt _Fp>
_LIBCPP_HIDE_FROM_ABI bool
operator!=(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
template <class _CharT,
class _Traits,
class _UInt,
@@ -194,14 +176,31 @@ public:
_LIBCPP_HIDE_FROM_ABI explicit mersenne_twister_engine(_Sseq& __q) {
seed(__q);
}
_LIBCPP_HIDE_FROM_ABI void seed(result_type __sd = default_seed);
_LIBCPP_HIDE_FROM_ABI void seed(result_type __sd = default_seed) _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK {
__x_[0] = __sd & _Max;
for (size_t __i = 1; __i < __n; ++__i)
__x_[__i] = (__f * (__x_[__i - 1] ^ __rshift<__w - 2>(__x_[__i - 1])) + __i) & _Max;
__i_ = 0;
}
template <class _Sseq, __enable_if_t<__is_seed_sequence<_Sseq, mersenne_twister_engine>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI void seed(_Sseq& __q) {
__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());
}
// generating functions
_LIBCPP_HIDE_FROM_ABI result_type operator()();
_LIBCPP_HIDE_FROM_ABI result_type operator()() {
const size_t __j = (__i_ + 1) % __n;
const result_type __mask = __r == _Dt ? result_type(~0) : (result_type(1) << __r) - result_type(1);
const result_type __yp = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask);
const size_t __k = (__i_ + __m) % __n;
__x_[__i_] = __x_[__k] ^ __rshift<1>(__yp) ^ (__a * (__yp & 1));
result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d);
__i_ = __j;
__z ^= __lshift<__s>(__z) & __b;
__z ^= __lshift<__t>(__z) & __c;
return __z ^ __rshift<__l>(__z);
}
_LIBCPP_HIDE_FROM_ABI void discard(unsigned long long __z) {
for (; __z; --__z)
operator()();
@@ -225,24 +224,6 @@ public:
const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
template <class _UInt,
size_t _Wp,
size_t _Np,
size_t _Mp,
size_t _Rp,
_UInt _Ap,
size_t _Up,
_UInt _Dp,
size_t _Sp,
_UInt _Bp,
size_t _Tp,
_UInt _Cp,
size_t _Lp,
_UInt _Fp>
friend bool operator!=(
const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
template <class _CharT,
class _Traits,
class _UInt,
@@ -285,9 +266,38 @@ public:
private:
template <class _Sseq>
_LIBCPP_HIDE_FROM_ABI void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
_LIBCPP_HIDE_FROM_ABI void __seed(_Sseq& __q, integral_constant<unsigned, 1>) {
const unsigned __k = 1;
uint32_t __ar[__n * __k];
__q.generate(__ar, __ar + __n * __k);
for (size_t __i = 0; __i < __n; ++__i)
__x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
const result_type __mask = __r == _Dt ? result_type(~0) : (result_type(1) << __r) - result_type(1);
__i_ = 0;
if ((__x_[0] & ~__mask) == 0) {
for (size_t __i = 1; __i < __n; ++__i)
if (__x_[__i] != 0)
return;
__x_[0] = result_type(1) << (__w - 1);
}
}
template <class _Sseq>
_LIBCPP_HIDE_FROM_ABI void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
_LIBCPP_HIDE_FROM_ABI void __seed(_Sseq& __q, integral_constant<unsigned, 2>) {
const unsigned __k = 2;
uint32_t __ar[__n * __k];
__q.generate(__ar, __ar + __n * __k);
for (size_t __i = 0; __i < __n; ++__i)
__x_[__i] = static_cast<result_type>((__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
const result_type __mask = __r == _Dt ? result_type(~0) : (result_type(1) << __r) - result_type(1);
__i_ = 0;
if ((__x_[0] & ~__mask) == 0) {
for (size_t __i = 1; __i < __n; ++__i)
if (__x_[__i] != 0)
return;
__x_[0] = result_type(1) << (__w - 1);
}
}
template <size_t __count,
__enable_if_t<__count< __w, int> = 0> _LIBCPP_HIDE_FROM_ABI static result_type __lshift(result_type __x) {
@@ -310,120 +320,6 @@ private:
}
};
template <class _UIntType,
size_t __w,
size_t __n,
size_t __m,
size_t __r,
_UIntType __a,
size_t __u,
_UIntType __d,
size_t __s,
_UIntType __b,
size_t __t,
_UIntType __c,
size_t __l,
_UIntType __f>
void mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::seed(
result_type __sd) _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK { // __w >= 2
__x_[0] = __sd & _Max;
for (size_t __i = 1; __i < __n; ++__i)
__x_[__i] = (__f * (__x_[__i - 1] ^ __rshift<__w - 2>(__x_[__i - 1])) + __i) & _Max;
__i_ = 0;
}
template <class _UIntType,
size_t __w,
size_t __n,
size_t __m,
size_t __r,
_UIntType __a,
size_t __u,
_UIntType __d,
size_t __s,
_UIntType __b,
size_t __t,
_UIntType __c,
size_t __l,
_UIntType __f>
template <class _Sseq>
void mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::__seed(
_Sseq& __q, integral_constant<unsigned, 1>) {
const unsigned __k = 1;
uint32_t __ar[__n * __k];
__q.generate(__ar, __ar + __n * __k);
for (size_t __i = 0; __i < __n; ++__i)
__x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
const result_type __mask = __r == _Dt ? result_type(~0) : (result_type(1) << __r) - result_type(1);
__i_ = 0;
if ((__x_[0] & ~__mask) == 0) {
for (size_t __i = 1; __i < __n; ++__i)
if (__x_[__i] != 0)
return;
__x_[0] = result_type(1) << (__w - 1);
}
}
template <class _UIntType,
size_t __w,
size_t __n,
size_t __m,
size_t __r,
_UIntType __a,
size_t __u,
_UIntType __d,
size_t __s,
_UIntType __b,
size_t __t,
_UIntType __c,
size_t __l,
_UIntType __f>
template <class _Sseq>
void mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::__seed(
_Sseq& __q, integral_constant<unsigned, 2>) {
const unsigned __k = 2;
uint32_t __ar[__n * __k];
__q.generate(__ar, __ar + __n * __k);
for (size_t __i = 0; __i < __n; ++__i)
__x_[__i] = static_cast<result_type>((__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
const result_type __mask = __r == _Dt ? result_type(~0) : (result_type(1) << __r) - result_type(1);
__i_ = 0;
if ((__x_[0] & ~__mask) == 0) {
for (size_t __i = 1; __i < __n; ++__i)
if (__x_[__i] != 0)
return;
__x_[0] = result_type(1) << (__w - 1);
}
}
template <class _UIntType,
size_t __w,
size_t __n,
size_t __m,
size_t __r,
_UIntType __a,
size_t __u,
_UIntType __d,
size_t __s,
_UIntType __b,
size_t __t,
_UIntType __c,
size_t __l,
_UIntType __f>
_UIntType
mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::operator()() {
const size_t __j = (__i_ + 1) % __n;
const result_type __mask = __r == _Dt ? result_type(~0) : (result_type(1) << __r) - result_type(1);
const result_type __yp = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask);
const size_t __k = (__i_ + __m) % __n;
__x_[__i_] = __x_[__k] ^ __rshift<1>(__yp) ^ (__a * (__yp & 1));
result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d);
__i_ = __j;
__z ^= __lshift<__s>(__z) & __b;
__z ^= __lshift<__t>(__z) & __c;
return __z ^ __rshift<__l>(__z);
}
template <class _UInt,
size_t _Wp,
size_t _Np,