diff options
author | Ulrich Drepper <drepper@gmail.com> | 2012-08-27 12:08:16 +0000 |
---|---|---|
committer | Ulrich Drepper <drepper@gcc.gnu.org> | 2012-08-27 12:08:16 +0000 |
commit | b668e41af6d282351a28a46fa8a37c1a7b416687 (patch) | |
tree | a1538e6e07a651393a5e0dec0446b30934a8e66f | |
parent | 6f79f4d1d6c6e3dcb0231404969882726d31a50c (diff) | |
download | gcc-b668e41af6d282351a28a46fa8a37c1a7b416687.zip gcc-b668e41af6d282351a28a46fa8a37c1a7b416687.tar.gz gcc-b668e41af6d282351a28a46fa8a37c1a7b416687.tar.bz2 |
random.h (mersenne_twister_engine): Don't inline discard here.
* include/bits/random.h (mersenne_twister_engine): Don't inline
discard here. New member function _M_gen_rand.
* include/bits/random.tcc (mersenne_twister_engine<>::_M_gen_rand):
New function. Extracted from operator().
(mersenne_twister_engine<>::discard): New implementation which
skips in large steps.
(mersenne_twister_engine<>::operator()): Use _M_gen_rand.
From-SVN: r190711
-rw-r--r-- | libstdc++-v3/ChangeLog | 10 | ||||
-rw-r--r-- | libstdc++-v3/include/bits/random.h | 8 | ||||
-rw-r--r-- | libstdc++-v3/include/bits/random.tcc | 81 |
3 files changed, 68 insertions, 31 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 057d435..bc088e8 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,13 @@ +012-08-22 Ulrich Drepper <drepper@gmail.com> + + * include/bits/random.h (mersenne_twister_engine): Don't inline + discard here. New member function _M_gen_rand. + * include/bits/random.tcc (mersenne_twister_engine<>::_M_gen_rand): + New function. Extracted from operator(). + (mersenne_twister_engine<>::discard): New implementation which + skips in large steps. + (mersenne_twister_engine<>::operator()): Use _M_gen_rand. + 2012-08-26 Marc Glisse <marc.glisse@inria.fr> Paolo Carlini <paolo.carlini@oracle.com> diff --git a/libstdc++-v3/include/bits/random.h b/libstdc++-v3/include/bits/random.h index b43b5f4..35aceea 100644 --- a/libstdc++-v3/include/bits/random.h +++ b/libstdc++-v3/include/bits/random.h @@ -530,11 +530,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * @brief Discard a sequence of random numbers. */ void - discard(unsigned long long __z) - { - for (; __z != 0ULL; --__z) - (*this)(); - } + discard(unsigned long long __z); result_type operator()(); @@ -610,6 +606,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __l1, __f1>& __x); private: + void _M_gen_rand(); + _UIntType _M_x[state_size]; size_t _M_p; }; diff --git a/libstdc++-v3/include/bits/random.tcc b/libstdc++-v3/include/bits/random.tcc index a3924be..6f501b1 100644 --- a/libstdc++-v3/include/bits/random.tcc +++ b/libstdc++-v3/include/bits/random.tcc @@ -392,6 +392,60 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _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>:: + _M_gen_rand(void) + { + const _UIntType __upper_mask = (~_UIntType()) << __r; + const _UIntType __lower_mask = ~__upper_mask; + + for (size_t __k = 0; __k < (__n - __m); ++__k) + { + _UIntType __y = ((_M_x[__k] & __upper_mask) + | (_M_x[__k + 1] & __lower_mask)); + _M_x[__k] = (_M_x[__k + __m] ^ (__y >> 1) + ^ ((__y & 0x01) ? __a : 0)); + } + + for (size_t __k = (__n - __m); __k < (__n - 1); ++__k) + { + _UIntType __y = ((_M_x[__k] & __upper_mask) + | (_M_x[__k + 1] & __lower_mask)); + _M_x[__k] = (_M_x[__k + (__m - __n)] ^ (__y >> 1) + ^ ((__y & 0x01) ? __a : 0)); + } + + _UIntType __y = ((_M_x[__n - 1] & __upper_mask) + | (_M_x[0] & __lower_mask)); + _M_x[__n - 1] = (_M_x[__m - 1] ^ (__y >> 1) + ^ ((__y & 0x01) ? __a : 0)); + _M_p = 0; + } + + template<typename _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>:: + discard(unsigned long long __z) + { + while (__z > state_size - _M_p) + { + __z -= state_size - _M_p; + _M_gen_rand(); + } + _M_p += __z; + } + + template<typename _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> typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type @@ -401,32 +455,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { // Reload the vector - cost is O(n) amortized over n calls. if (_M_p >= state_size) - { - const _UIntType __upper_mask = (~_UIntType()) << __r; - const _UIntType __lower_mask = ~__upper_mask; - - for (size_t __k = 0; __k < (__n - __m); ++__k) - { - _UIntType __y = ((_M_x[__k] & __upper_mask) - | (_M_x[__k + 1] & __lower_mask)); - _M_x[__k] = (_M_x[__k + __m] ^ (__y >> 1) - ^ ((__y & 0x01) ? __a : 0)); - } - - for (size_t __k = (__n - __m); __k < (__n - 1); ++__k) - { - _UIntType __y = ((_M_x[__k] & __upper_mask) - | (_M_x[__k + 1] & __lower_mask)); - _M_x[__k] = (_M_x[__k + (__m - __n)] ^ (__y >> 1) - ^ ((__y & 0x01) ? __a : 0)); - } - - _UIntType __y = ((_M_x[__n - 1] & __upper_mask) - | (_M_x[0] & __lower_mask)); - _M_x[__n - 1] = (_M_x[__m - 1] ^ (__y >> 1) - ^ ((__y & 0x01) ? __a : 0)); - _M_p = 0; - } + _M_gen_rand(); // Calculate o(x(i)). result_type __z = _M_x[_M_p++]; |