Skip to content

Commit 0458959

Browse files
philnik777kcloudy0717
authored andcommitted
[libc++][NFC] Inline mersenne_twister_engine functions into the class 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.
1 parent 552d527 commit 0458959

File tree

1 file changed

+50
-154
lines changed

1 file changed

+50
-154
lines changed

libcxx/include/__random/mersenne_twister_engine.h

Lines changed: 50 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -62,24 +62,6 @@ _LIBCPP_HIDE_FROM_ABI bool
6262
operator==(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
6363
const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
6464

65-
template <class _UInt,
66-
size_t _Wp,
67-
size_t _Np,
68-
size_t _Mp,
69-
size_t _Rp,
70-
_UInt _Ap,
71-
size_t _Up,
72-
_UInt _Dp,
73-
size_t _Sp,
74-
_UInt _Bp,
75-
size_t _Tp,
76-
_UInt _Cp,
77-
size_t _Lp,
78-
_UInt _Fp>
79-
_LIBCPP_HIDE_FROM_ABI bool
80-
operator!=(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
81-
const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
82-
8365
template <class _CharT,
8466
class _Traits,
8567
class _UInt,
@@ -194,14 +176,31 @@ class mersenne_twister_engine {
194176
_LIBCPP_HIDE_FROM_ABI explicit mersenne_twister_engine(_Sseq& __q) {
195177
seed(__q);
196178
}
197-
_LIBCPP_HIDE_FROM_ABI void seed(result_type __sd = default_seed);
179+
_LIBCPP_HIDE_FROM_ABI void seed(result_type __sd = default_seed) _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK {
180+
__x_[0] = __sd & _Max;
181+
for (size_t __i = 1; __i < __n; ++__i)
182+
__x_[__i] = (__f * (__x_[__i - 1] ^ __rshift<__w - 2>(__x_[__i - 1])) + __i) & _Max;
183+
__i_ = 0;
184+
}
198185
template <class _Sseq, __enable_if_t<__is_seed_sequence<_Sseq, mersenne_twister_engine>::value, int> = 0>
199186
_LIBCPP_HIDE_FROM_ABI void seed(_Sseq& __q) {
200187
__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());
201188
}
202189

203190
// generating functions
204-
_LIBCPP_HIDE_FROM_ABI result_type operator()();
191+
_LIBCPP_HIDE_FROM_ABI result_type operator()() {
192+
const size_t __j = (__i_ + 1) % __n;
193+
const result_type __mask = __r == _Dt ? result_type(~0) : (result_type(1) << __r) - result_type(1);
194+
const result_type __yp = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask);
195+
const size_t __k = (__i_ + __m) % __n;
196+
__x_[__i_] = __x_[__k] ^ __rshift<1>(__yp) ^ (__a * (__yp & 1));
197+
result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d);
198+
__i_ = __j;
199+
__z ^= __lshift<__s>(__z) & __b;
200+
__z ^= __lshift<__t>(__z) & __c;
201+
return __z ^ __rshift<__l>(__z);
202+
}
203+
205204
_LIBCPP_HIDE_FROM_ABI void discard(unsigned long long __z) {
206205
for (; __z; --__z)
207206
operator()();
@@ -225,24 +224,6 @@ class mersenne_twister_engine {
225224
const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
226225
const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
227226

228-
template <class _UInt,
229-
size_t _Wp,
230-
size_t _Np,
231-
size_t _Mp,
232-
size_t _Rp,
233-
_UInt _Ap,
234-
size_t _Up,
235-
_UInt _Dp,
236-
size_t _Sp,
237-
_UInt _Bp,
238-
size_t _Tp,
239-
_UInt _Cp,
240-
size_t _Lp,
241-
_UInt _Fp>
242-
friend bool operator!=(
243-
const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
244-
const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
245-
246227
template <class _CharT,
247228
class _Traits,
248229
class _UInt,
@@ -285,9 +266,38 @@ class mersenne_twister_engine {
285266

286267
private:
287268
template <class _Sseq>
288-
_LIBCPP_HIDE_FROM_ABI void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
269+
_LIBCPP_HIDE_FROM_ABI void __seed(_Sseq& __q, integral_constant<unsigned, 1>) {
270+
const unsigned __k = 1;
271+
uint32_t __ar[__n * __k];
272+
__q.generate(__ar, __ar + __n * __k);
273+
for (size_t __i = 0; __i < __n; ++__i)
274+
__x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
275+
const result_type __mask = __r == _Dt ? result_type(~0) : (result_type(1) << __r) - result_type(1);
276+
__i_ = 0;
277+
if ((__x_[0] & ~__mask) == 0) {
278+
for (size_t __i = 1; __i < __n; ++__i)
279+
if (__x_[__i] != 0)
280+
return;
281+
__x_[0] = result_type(1) << (__w - 1);
282+
}
283+
}
284+
289285
template <class _Sseq>
290-
_LIBCPP_HIDE_FROM_ABI void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
286+
_LIBCPP_HIDE_FROM_ABI void __seed(_Sseq& __q, integral_constant<unsigned, 2>) {
287+
const unsigned __k = 2;
288+
uint32_t __ar[__n * __k];
289+
__q.generate(__ar, __ar + __n * __k);
290+
for (size_t __i = 0; __i < __n; ++__i)
291+
__x_[__i] = static_cast<result_type>((__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
292+
const result_type __mask = __r == _Dt ? result_type(~0) : (result_type(1) << __r) - result_type(1);
293+
__i_ = 0;
294+
if ((__x_[0] & ~__mask) == 0) {
295+
for (size_t __i = 1; __i < __n; ++__i)
296+
if (__x_[__i] != 0)
297+
return;
298+
__x_[0] = result_type(1) << (__w - 1);
299+
}
300+
}
291301

292302
template <size_t __count,
293303
__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 {
310320
}
311321
};
312322

313-
template <class _UIntType,
314-
size_t __w,
315-
size_t __n,
316-
size_t __m,
317-
size_t __r,
318-
_UIntType __a,
319-
size_t __u,
320-
_UIntType __d,
321-
size_t __s,
322-
_UIntType __b,
323-
size_t __t,
324-
_UIntType __c,
325-
size_t __l,
326-
_UIntType __f>
327-
void mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::seed(
328-
result_type __sd) _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK { // __w >= 2
329-
__x_[0] = __sd & _Max;
330-
for (size_t __i = 1; __i < __n; ++__i)
331-
__x_[__i] = (__f * (__x_[__i - 1] ^ __rshift<__w - 2>(__x_[__i - 1])) + __i) & _Max;
332-
__i_ = 0;
333-
}
334-
335-
template <class _UIntType,
336-
size_t __w,
337-
size_t __n,
338-
size_t __m,
339-
size_t __r,
340-
_UIntType __a,
341-
size_t __u,
342-
_UIntType __d,
343-
size_t __s,
344-
_UIntType __b,
345-
size_t __t,
346-
_UIntType __c,
347-
size_t __l,
348-
_UIntType __f>
349-
template <class _Sseq>
350-
void mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::__seed(
351-
_Sseq& __q, integral_constant<unsigned, 1>) {
352-
const unsigned __k = 1;
353-
uint32_t __ar[__n * __k];
354-
__q.generate(__ar, __ar + __n * __k);
355-
for (size_t __i = 0; __i < __n; ++__i)
356-
__x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
357-
const result_type __mask = __r == _Dt ? result_type(~0) : (result_type(1) << __r) - result_type(1);
358-
__i_ = 0;
359-
if ((__x_[0] & ~__mask) == 0) {
360-
for (size_t __i = 1; __i < __n; ++__i)
361-
if (__x_[__i] != 0)
362-
return;
363-
__x_[0] = result_type(1) << (__w - 1);
364-
}
365-
}
366-
367-
template <class _UIntType,
368-
size_t __w,
369-
size_t __n,
370-
size_t __m,
371-
size_t __r,
372-
_UIntType __a,
373-
size_t __u,
374-
_UIntType __d,
375-
size_t __s,
376-
_UIntType __b,
377-
size_t __t,
378-
_UIntType __c,
379-
size_t __l,
380-
_UIntType __f>
381-
template <class _Sseq>
382-
void mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::__seed(
383-
_Sseq& __q, integral_constant<unsigned, 2>) {
384-
const unsigned __k = 2;
385-
uint32_t __ar[__n * __k];
386-
__q.generate(__ar, __ar + __n * __k);
387-
for (size_t __i = 0; __i < __n; ++__i)
388-
__x_[__i] = static_cast<result_type>((__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
389-
const result_type __mask = __r == _Dt ? result_type(~0) : (result_type(1) << __r) - result_type(1);
390-
__i_ = 0;
391-
if ((__x_[0] & ~__mask) == 0) {
392-
for (size_t __i = 1; __i < __n; ++__i)
393-
if (__x_[__i] != 0)
394-
return;
395-
__x_[0] = result_type(1) << (__w - 1);
396-
}
397-
}
398-
399-
template <class _UIntType,
400-
size_t __w,
401-
size_t __n,
402-
size_t __m,
403-
size_t __r,
404-
_UIntType __a,
405-
size_t __u,
406-
_UIntType __d,
407-
size_t __s,
408-
_UIntType __b,
409-
size_t __t,
410-
_UIntType __c,
411-
size_t __l,
412-
_UIntType __f>
413-
_UIntType
414-
mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::operator()() {
415-
const size_t __j = (__i_ + 1) % __n;
416-
const result_type __mask = __r == _Dt ? result_type(~0) : (result_type(1) << __r) - result_type(1);
417-
const result_type __yp = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask);
418-
const size_t __k = (__i_ + __m) % __n;
419-
__x_[__i_] = __x_[__k] ^ __rshift<1>(__yp) ^ (__a * (__yp & 1));
420-
result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d);
421-
__i_ = __j;
422-
__z ^= __lshift<__s>(__z) & __b;
423-
__z ^= __lshift<__t>(__z) & __c;
424-
return __z ^ __rshift<__l>(__z);
425-
}
426-
427323
template <class _UInt,
428324
size_t _Wp,
429325
size_t _Np,

0 commit comments

Comments
 (0)