Commit b94f4bef by Paolo Carlini Committed by Paolo Carlini

random.h (__mod(_Tp)): Use defaults.

2009-06-29  Paolo Carlini  <paolo.carlini@oracle.com>

	* include/bits/random.h (__mod(_Tp)): Use defaults.
	(struct _Mod): Adjust template parameters.
	(linear_congruential_engine<>::operator()()): Adjust.
	(typedef _UInt32Type): Remove.
	(mersenne_twister_engine<>::initialization_multiplier): Fix type.
	* include/bits/random.tcc (struct _Mod): Adjust template parameters.
	(linear_congruential_engine<>::seed(result_type)): Adjust __mod
	calls.
	(mersenne_twister_engine<>::seed(result_type)): Likewise.
	(mersenne_twister_engine<>::seed(seed_seq&)): Likewise.
	(subtract_with_carry_engine<>::seed(result_type)): Likewise.
	(subtract_with_carry_engine<>::seed(seed_seq&)): Likewise.
	(seed_seq::seed_seq(std::initializer_list<>)): Likewise.
	(seed_seq::seed_seq(_InputIterator, _InputIterator)): Likewise.
	(seed_seq::generate(_RandomAccessIterator, _RandomAccessIterator)):
	Likewise.

	* include/bits/random.tcc
	(mersenne_twister_engine<>::seed(result_type)): Fix i usage in the
	formula (i mod n).
	(mersenne_twister_engine<>::seed(seed_seq&)): Use uint_least32_t
	as array type.
	(subtract_with_carry_engine<>::seed(seed_seq&)): Likewise; fix array
	size.
	(subtract_with_carry_engine<>::seed(result_type)): Fix; also use
	uint_least32_t.

From-SVN: r149074
parent 15ab11cb
2009-06-29 Paolo Carlini <paolo.carlini@oracle.com>
* include/bits/random.h (__mod(_Tp)): Use defaults.
(struct _Mod): Adjust template parameters.
(linear_congruential_engine<>::operator()()): Adjust.
(typedef _UInt32Type): Remove.
(mersenne_twister_engine<>::initialization_multiplier): Fix type.
* include/bits/random.tcc (struct _Mod): Adjust template parameters.
(linear_congruential_engine<>::seed(result_type)): Adjust __mod
calls.
(mersenne_twister_engine<>::seed(result_type)): Likewise.
(mersenne_twister_engine<>::seed(seed_seq&)): Likewise.
(subtract_with_carry_engine<>::seed(result_type)): Likewise.
(subtract_with_carry_engine<>::seed(seed_seq&)): Likewise.
(seed_seq::seed_seq(std::initializer_list<>)): Likewise.
(seed_seq::seed_seq(_InputIterator, _InputIterator)): Likewise.
(seed_seq::generate(_RandomAccessIterator, _RandomAccessIterator)):
Likewise.
* include/bits/random.tcc
(mersenne_twister_engine<>::seed(result_type)): Fix i usage in the
formula (i mod n).
(mersenne_twister_engine<>::seed(seed_seq&)): Use uint_least32_t
as array type.
(subtract_with_carry_engine<>::seed(seed_seq&)): Likewise; fix array
size.
(subtract_with_carry_engine<>::seed(result_type)): Fix; also use
uint_least32_t.
2009-06-24 Jonathan Wakely <jwakely.gcc@gmail.com> 2009-06-24 Jonathan Wakely <jwakely.gcc@gmail.com>
* include/std/future (shared_future): Adjust doxygen comment. * include/std/future (shared_future): Adjust doxygen comment.
......
...@@ -68,18 +68,15 @@ namespace std ...@@ -68,18 +68,15 @@ namespace std
struct _Shift<_UIntType, __w, true> struct _Shift<_UIntType, __w, true>
{ static const _UIntType __value = _UIntType(1) << __w; }; { static const _UIntType __value = _UIntType(1) << __w; };
template<typename _Tp, _Tp __a, _Tp __c, _Tp __m, bool> template<typename _Tp, _Tp __m, _Tp __a, _Tp __c, bool>
struct _Mod; struct _Mod;
// Dispatch based on modulus value to prevent divide-by-zero compile-time // Dispatch based on modulus value to prevent divide-by-zero compile-time
// errors when m == 0. // errors when m == 0.
template<typename _Tp, _Tp __a, _Tp __c, _Tp __m> template<typename _Tp, _Tp __m, _Tp __a = 1, _Tp __c = 0>
inline _Tp inline _Tp
__mod(_Tp __x) __mod(_Tp __x)
{ return _Mod<_Tp, __a, __c, __m, __m == 0>::__calc(__x); } { return _Mod<_Tp, __m, __a, __c, __m == 0>::__calc(__x); }
typedef __gnu_cxx::__conditional_type<(sizeof(unsigned) == 4),
unsigned, unsigned long>::__type _UInt32Type;
/* /*
* An adaptor class for converting the output of any Generator into * An adaptor class for converting the output of any Generator into
...@@ -253,7 +250,7 @@ namespace std ...@@ -253,7 +250,7 @@ namespace std
result_type result_type
operator()() operator()()
{ {
_M_x = __detail::__mod<_UIntType, __a, __c, __m>(_M_x); _M_x = __detail::__mod<_UIntType, __m, __a, __c>(_M_x);
return _M_x; return _M_x;
} }
...@@ -281,8 +278,7 @@ namespace std ...@@ -281,8 +278,7 @@ namespace std
* @returns __os. * @returns __os.
*/ */
template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1, template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
_UIntType1 __m1, _UIntType1 __m1, typename _CharT, typename _Traits>
typename _CharT, typename _Traits>
friend std::basic_ostream<_CharT, _Traits>& friend std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>&, operator<<(std::basic_ostream<_CharT, _Traits>&,
const std::linear_congruential_engine<_UIntType1, const std::linear_congruential_engine<_UIntType1,
...@@ -302,8 +298,7 @@ namespace std ...@@ -302,8 +298,7 @@ namespace std
* @returns __is. * @returns __is.
*/ */
template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1, template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
_UIntType1 __m1, _UIntType1 __m1, typename _CharT, typename _Traits>
typename _CharT, typename _Traits>
friend std::basic_istream<_CharT, _Traits>& friend std::basic_istream<_CharT, _Traits>&
operator>>(std::basic_istream<_CharT, _Traits>&, operator>>(std::basic_istream<_CharT, _Traits>&,
std::linear_congruential_engine<_UIntType1, __a1, std::linear_congruential_engine<_UIntType1, __a1,
...@@ -371,6 +366,10 @@ namespace std ...@@ -371,6 +366,10 @@ namespace std
"mersenne_twister_engine template arguments out of bounds"); "mersenne_twister_engine template arguments out of bounds");
static_assert(__c <= (__detail::_Shift<_UIntType, __w>::__value - 1), static_assert(__c <= (__detail::_Shift<_UIntType, __w>::__value - 1),
"mersenne_twister_engine template arguments out of bounds"); "mersenne_twister_engine template arguments out of bounds");
static_assert(__d <= (__detail::_Shift<_UIntType, __w>::__value - 1),
"mersenne_twister_engine template arguments out of bounds");
static_assert(__f <= (__detail::_Shift<_UIntType, __w>::__value - 1),
"mersenne_twister_engine template arguments out of bounds");
public: public:
/** The type of the generated random value. */ /** The type of the generated random value. */
...@@ -389,7 +388,7 @@ namespace std ...@@ -389,7 +388,7 @@ namespace std
static const size_t tempering_t = __t; static const size_t tempering_t = __t;
static const result_type tempering_c = __c; static const result_type tempering_c = __c;
static const size_t tempering_l = __l; static const size_t tempering_l = __l;
static const size_t initialization_multiplier = __f; static const result_type initialization_multiplier = __f;
static const result_type default_seed = 5489u; static const result_type default_seed = 5489u;
// constructors and member function // constructors and member function
...@@ -1351,11 +1350,11 @@ namespace std ...@@ -1351,11 +1350,11 @@ namespace std
typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24>
ranlux24_base; ranlux24_base;
typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12>
ranlux48_base; ranlux48_base;
typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48; typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
/** /**
......
...@@ -45,7 +45,7 @@ namespace std ...@@ -45,7 +45,7 @@ namespace std
// //
// Preconditions: a > 0, m > 0. // Preconditions: a > 0, m > 0.
// //
template<typename _Tp, _Tp __a, _Tp __c, _Tp __m, bool> template<typename _Tp, _Tp __m, _Tp __a, _Tp __c, bool>
struct _Mod struct _Mod
{ {
static _Tp static _Tp
...@@ -80,8 +80,8 @@ namespace std ...@@ -80,8 +80,8 @@ namespace std
// Special case for m == 0 -- use unsigned integer overflow as modulo // Special case for m == 0 -- use unsigned integer overflow as modulo
// operator. // operator.
template<typename _Tp, _Tp __a, _Tp __c, _Tp __m> template<typename _Tp, _Tp __m, _Tp __a, _Tp __c>
struct _Mod<_Tp, __a, __c, __m, true> struct _Mod<_Tp, __m, __a, __c, true>
{ {
static _Tp static _Tp
__calc(_Tp __x) __calc(_Tp __x)
...@@ -98,11 +98,11 @@ namespace std ...@@ -98,11 +98,11 @@ namespace std
linear_congruential_engine<_UIntType, __a, __c, __m>:: linear_congruential_engine<_UIntType, __a, __c, __m>::
seed(result_type __s) seed(result_type __s)
{ {
if ((__detail::__mod<_UIntType, 1U, 0U, __m>(__c) == 0U) if ((__detail::__mod<_UIntType, __m>(__c) == 0)
&& (__detail::__mod<_UIntType, 1U, 0U, __m>(__s) == 0U)) && (__detail::__mod<_UIntType, __m>(__s) == 0))
_M_x = __detail::__mod<_UIntType, 1U, 0U, __m>(1U); _M_x = 1;
else else
_M_x = __detail::__mod<_UIntType, 1U, 0U, __m>(__s); _M_x = __detail::__mod<_UIntType, __m>(__s);
} }
/** /**
...@@ -118,8 +118,8 @@ namespace std ...@@ -118,8 +118,8 @@ namespace std
const _UIntType __k = (__k0 + 31) / 32; const _UIntType __k = (__k0 + 31) / 32;
_UIntType __arr[__k + 3]; _UIntType __arr[__k + 3];
__q.generate(__arr + 0, __arr + __k + 3); __q.generate(__arr + 0, __arr + __k + 3);
_UIntType __factor = 1U; _UIntType __factor = 1u;
_UIntType __sum = 0U; _UIntType __sum = 0u;
for (size_t __j = 0; __j < __k; ++__j) for (size_t __j = 0; __j < __k; ++__j)
{ {
__sum += __arr[__j + 3] * __factor; __sum += __arr[__j + 3] * __factor;
...@@ -179,7 +179,7 @@ namespace std ...@@ -179,7 +179,7 @@ namespace std
__s, __b, __t, __c, __l, __f>:: __s, __b, __t, __c, __l, __f>::
seed(result_type __sd) seed(result_type __sd)
{ {
_M_x[0] = __detail::__mod<_UIntType, 1, 0, _M_x[0] = __detail::__mod<_UIntType,
__detail::_Shift<_UIntType, __w>::__value>(__sd); __detail::_Shift<_UIntType, __w>::__value>(__sd);
for (size_t __i = 1; __i < state_size; ++__i) for (size_t __i = 1; __i < state_size; ++__i)
...@@ -187,8 +187,8 @@ namespace std ...@@ -187,8 +187,8 @@ namespace std
_UIntType __x = _M_x[__i - 1]; _UIntType __x = _M_x[__i - 1];
__x ^= __x >> (__w - 2); __x ^= __x >> (__w - 2);
__x *= __f; __x *= __f;
__x += __i; __x += __detail::__mod<_UIntType, __n>(__i);
_M_x[__i] = __detail::__mod<_UIntType, 1, 0, _M_x[__i] = __detail::__mod<_UIntType,
__detail::_Shift<_UIntType, __w>::__value>(__x); __detail::_Shift<_UIntType, __w>::__value>(__x);
} }
_M_p = state_size; _M_p = state_size;
...@@ -206,35 +206,35 @@ namespace std ...@@ -206,35 +206,35 @@ namespace std
{ {
const _UIntType __upper_mask = (~_UIntType()) << __r; const _UIntType __upper_mask = (~_UIntType()) << __r;
const size_t __k = (__w + 31) / 32; const size_t __k = (__w + 31) / 32;
_UIntType __arr[__k * __n]; uint_least32_t __arr[__n * __k];
__q.generate(__arr + 0, __arr + __k * __n); __q.generate(__arr + 0, __arr + __n * __k);
bool __zero = true; bool __zero = true;
for (size_t __i = 0; __i < state_size; ++__i) for (size_t __i = 0; __i < state_size; ++__i)
{ {
_UIntType __factor = 1U; _UIntType __factor = 1u;
_UIntType __sum = 0U; _UIntType __sum = 0u;
for (size_t __j = 0; __j < __k; ++__j) for (size_t __j = 0; __j < __k; ++__j)
{ {
__sum += __arr[__i * __k + __j] * __factor; __sum += __arr[__k * __i + __j] * __factor;
__factor *= __detail::_Shift<_UIntType, 32>::__value; __factor *= __detail::_Shift<_UIntType, 32>::__value;
} }
_M_x[__i] = __detail::__mod<_UIntType, 1U, 0U, _M_x[__i] = __detail::__mod<_UIntType,
__detail::_Shift<_UIntType, __w>::__value>(__sum); __detail::_Shift<_UIntType, __w>::__value>(__sum);
if (__zero) if (__zero)
{ {
if (__i == 0) if (__i == 0)
{ {
if ((_M_x[0] & __upper_mask) != 0U) if ((_M_x[0] & __upper_mask) != 0u)
__zero = false; __zero = false;
} }
else if (_M_x[__i] != 0U) else if (_M_x[__i] != 0u)
__zero = false; __zero = false;
} }
} }
if (__zero) if (__zero)
_M_x[0] = __detail::_Shift<_UIntType, __w - 1U>::__value; _M_x[0] = __detail::_Shift<_UIntType, __w - 1>::__value;
} }
template<typename _UIntType, size_t __w, template<typename _UIntType, size_t __w,
...@@ -345,26 +345,23 @@ namespace std ...@@ -345,26 +345,23 @@ namespace std
subtract_with_carry_engine<_UIntType, __w, __s, __r>:: subtract_with_carry_engine<_UIntType, __w, __s, __r>::
seed(result_type __value) seed(result_type __value)
{ {
if (__value == 0) std::linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
__value = default_seed; __lcg(__value == 0u ? default_seed : __value);
std::linear_congruential_engine<result_type, 40014U, 0U, 2147483563U>
__lcg(__value);
// I hope this is right. The "10000" tests work for the ranluxen. const size_t __n = (__w + 31) / 32;
const size_t __n = (word_size + 31) / 32;
for (size_t __i = 0; __i < long_lag; ++__i) for (size_t __i = 0; __i < long_lag; ++__i)
{ {
_UIntType __sum = 0U; _UIntType __sum = 0u;
_UIntType __factor = 1U; _UIntType __factor = 1u;
for (size_t __j = 0; __j < __n; ++__j) for (size_t __j = 0; __j < __n; ++__j)
{ {
__sum += __detail::__mod<__detail::_UInt32Type, 1, 0, 0> __sum += __detail::__mod<uint_least32_t,
__detail::_Shift<uint_least32_t, 32>::__value>
(__lcg()) * __factor; (__lcg()) * __factor;
__factor *= __detail::_Shift<_UIntType, 32>::__value; __factor *= __detail::_Shift<_UIntType, 32>::__value;
} }
_M_x[__i] = __detail::__mod<_UIntType, 1, 0, _M_x[__i] = __detail::__mod<_UIntType,
__detail::_Shift<_UIntType, __w>::__value>(__sum); __detail::_Shift<_UIntType, __w>::__value>(__sum);
} }
_M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0; _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
...@@ -376,21 +373,20 @@ namespace std ...@@ -376,21 +373,20 @@ namespace std
subtract_with_carry_engine<_UIntType, __w, __s, __r>:: subtract_with_carry_engine<_UIntType, __w, __s, __r>::
seed(seed_seq& __q) seed(seed_seq& __q)
{ {
const size_t __n = (word_size + 31) / 32; const size_t __k = (__w + 31) / 32;
_UIntType __arr[long_lag + __n]; uint_least32_t __arr[__r * __k];
__q.generate(__arr + 0, __arr + long_lag + __n); __q.generate(__arr + 0, __arr + __r * __k);
for (size_t __i = 0; __i < long_lag; ++__i) for (size_t __i = 0; __i < long_lag; ++__i)
{ {
_UIntType __sum = 0U; _UIntType __sum = 0u;
_UIntType __factor = 1U; _UIntType __factor = 1u;
for (size_t __j = 0; __j < __n; ++__j) for (size_t __j = 0; __j < __k; ++__j)
{ {
__sum += __detail::__mod<__detail::_UInt32Type, 1, 0, 0> __sum += __arr[__k * __i + __j] * __factor;
(__arr[__i * __n + __j]) * __factor;
__factor *= __detail::_Shift<_UIntType, 32>::__value; __factor *= __detail::_Shift<_UIntType, 32>::__value;
} }
_M_x[__i] = __detail::__mod<_UIntType, 1, 0, _M_x[__i] = __detail::__mod<_UIntType,
__detail::_Shift<_UIntType, __w>::__value>(__sum); __detail::_Shift<_UIntType, __w>::__value>(__sum);
} }
_M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0; _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
...@@ -2485,7 +2481,7 @@ namespace std ...@@ -2485,7 +2481,7 @@ namespace std
seed_seq::seed_seq(std::initializer_list<_IntType> __il) seed_seq::seed_seq(std::initializer_list<_IntType> __il)
{ {
for (auto __iter = __il.begin(); __iter != __il.end(); ++__iter) for (auto __iter = __il.begin(); __iter != __il.end(); ++__iter)
_M_v.push_back(__detail::__mod<result_type, 1, 0, _M_v.push_back(__detail::__mod<result_type,
__detail::_Shift<result_type, 32>::__value>(*__iter)); __detail::_Shift<result_type, 32>::__value>(*__iter));
} }
...@@ -2493,7 +2489,7 @@ namespace std ...@@ -2493,7 +2489,7 @@ namespace std
seed_seq::seed_seq(_InputIterator __begin, _InputIterator __end) seed_seq::seed_seq(_InputIterator __begin, _InputIterator __end)
{ {
for (_InputIterator __iter = __begin; __iter != __end; ++__iter) for (_InputIterator __iter = __begin; __iter != __end; ++__iter)
_M_v.push_back(__detail::__mod<result_type, 1, 0, _M_v.push_back(__detail::__mod<result_type,
__detail::_Shift<result_type, 32>::__value>(*__iter)); __detail::_Shift<result_type, 32>::__value>(*__iter));
} }
...@@ -2508,7 +2504,7 @@ namespace std ...@@ -2508,7 +2504,7 @@ namespace std
if (__begin == __end) if (__begin == __end)
return; return;
std::fill(__begin, __end, _Type(0x8b8b8b8bU)); std::fill(__begin, __end, _Type(0x8b8b8b8bu));
const size_t __n = __end - __begin; const size_t __n = __end - __begin;
const size_t __s = _M_v.size(); const size_t __s = _M_v.size();
...@@ -2527,8 +2523,8 @@ namespace std ...@@ -2527,8 +2523,8 @@ namespace std
^ __begin[(__k + __p) % __n] ^ __begin[(__k + __p) % __n]
^ __begin[(__k - 1) % __n]); ^ __begin[(__k - 1) % __n]);
_Type __r1 = __arg ^ (__arg << 27); _Type __r1 = __arg ^ (__arg << 27);
__r1 = __detail::__mod<_Type, 1664525U, 0U, __r1 = __detail::__mod<_Type, __detail::_Shift<_Type, 32>::__value,
__detail::_Shift<_Type, 32>::__value>(__r1); 1664525u, 0u>(__r1);
_Type __r2 = __r1; _Type __r2 = __r1;
if (__k == 0) if (__k == 0)
__r2 += __s; __r2 += __s;
...@@ -2536,8 +2532,8 @@ namespace std ...@@ -2536,8 +2532,8 @@ namespace std
__r2 += __k % __n + _M_v[__k - 1]; __r2 += __k % __n + _M_v[__k - 1];
else else
__r2 += __k % __n; __r2 += __k % __n;
__r2 = __detail::__mod<_Type, 1U, 0U, __r2 = __detail::__mod<_Type,
__detail::_Shift<_Type, 32>::__value>(__r2); __detail::_Shift<_Type, 32>::__value>(__r2);
__begin[(__k + __p) % __n] += __r1; __begin[(__k + __p) % __n] += __r1;
__begin[(__k + __q) % __n] += __r2; __begin[(__k + __q) % __n] += __r2;
__begin[__k % __n] = __r2; __begin[__k % __n] = __r2;
...@@ -2549,11 +2545,11 @@ namespace std ...@@ -2549,11 +2545,11 @@ namespace std
+ __begin[(__k + __p) % __n] + __begin[(__k + __p) % __n]
+ __begin[(__k - 1) % __n]); + __begin[(__k - 1) % __n]);
_Type __r3 = __arg ^ (__arg << 27); _Type __r3 = __arg ^ (__arg << 27);
__r3 = __detail::__mod<_Type, 1566083941U, 0U, __r3 = __detail::__mod<_Type, __detail::_Shift<_Type, 32>::__value,
__detail::_Shift<_Type, 32>::__value>(__r3); 1566083941u, 0u>(__r3);
_Type __r4 = __r3 - __k % __n; _Type __r4 = __r3 - __k % __n;
__r4 = __detail::__mod<_Type, 1U, 0U, __r4 = __detail::__mod<_Type,
__detail::_Shift<_Type, 32>::__value>(__r4); __detail::_Shift<_Type, 32>::__value>(__r4);
__begin[(__k + __p) % __n] ^= __r4; __begin[(__k + __p) % __n] ^= __r4;
__begin[(__k + __q) % __n] ^= __r3; __begin[(__k + __q) % __n] ^= __r3;
__begin[__k % __n] = __r4; __begin[__k % __n] = __r4;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment