-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[libc++][NFC] Inline mersenne_twister_engine functions into the class body #170454
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
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Member
|
@llvm/pr-subscribers-libcxx Author: Nikolas Klauser (philnik777) ChangesDefining the functions outside the class makes things way harder to read here, since the list of template arguments is incredibly long. Full diff: https://github.com/llvm/llvm-project/pull/170454.diff 1 Files Affected:
diff --git a/libcxx/include/__random/mersenne_twister_engine.h b/libcxx/include/__random/mersenne_twister_engine.h
index c60fe1529bf57..332e830e731dc 100644
--- a/libcxx/include/__random/mersenne_twister_engine.h
+++ b/libcxx/include/__random/mersenne_twister_engine.h
@@ -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 @@ class mersenne_twister_engine {
_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 @@ class mersenne_twister_engine {
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 @@ class mersenne_twister_engine {
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 @@ class mersenne_twister_engine {
}
};
-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,
|
kcloudy0717
pushed a commit
to kcloudy0717/llvm-project
that referenced
this pull request
Dec 4, 2025
… body (llvm#170454) Defining the functions outside the class makes things way harder to read here, since the list of template arguments is incredibly long.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Defining the functions outside the class makes things way harder to read here, since the list of template arguments is incredibly long.