Skip to content

Conversation

@philnik777
Copy link
Contributor

@philnik777 philnik777 commented Dec 3, 2025

Defining the functions outside the class makes things way harder to read here, since the list of template arguments is incredibly long.

@philnik777 philnik777 marked this pull request as ready for review December 4, 2025 11:39
@philnik777 philnik777 requested a review from a team as a code owner December 4, 2025 11:39
@philnik777 philnik777 merged commit d364c0e into llvm:main Dec 4, 2025
83 checks passed
@philnik777 philnik777 deleted the simplify_mt branch December 4, 2025 11:39
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Dec 4, 2025
@llvmbot
Copy link
Member

llvmbot commented Dec 4, 2025

@llvm/pr-subscribers-libcxx

Author: Nikolas Klauser (philnik777)

Changes

Defining 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:

  • (modified) libcxx/include/__random/mersenne_twister_engine.h (+50-154)
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

Labels

libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants