rand/xorshift: simplify result operator specialisations

This commit is contained in:
Danny Robson 2016-07-01 16:23:55 +10:00
parent 325a7405b5
commit f741986c3f

View File

@ -34,31 +34,44 @@ xorshift<T>::xorshift (T seed):
///////////////////////////////////////////////////////////////////////////////
namespace util { namespace rand {
template <>
uint32_t
xorshift<uint32_t>::operator() (void)
{
m_state ^= m_state << 13u;
m_state ^= m_state >> 17u;
m_state ^= m_state << 5u;
return m_state;
}
// setup the constants a, b, and c. we use the values from the example
// generators provided in Marsaglia's paper.
template <typename T>
struct constants;
//-----------------------------------------------------------------------------
template <>
uint64_t
xorshift<uint64_t>::operator() (void)
{
m_state ^= m_state << 13u;
m_state ^= m_state >> 7u;
m_state ^= m_state << 17u;
//-----------------------------------------------------------------------------
template <>
struct constants<uint32_t>
{
static constexpr uint32_t a = 13u;
static constexpr uint32_t b = 17u;
static constexpr uint32_t c = 5u;
};
return m_state;
}
} }
//-----------------------------------------------------------------------------
template <>
struct constants<uint64_t>
{
static constexpr uint64_t a = 13u;
static constexpr uint64_t b = 7u;
static constexpr uint64_t c = 17u;
};
///////////////////////////////////////////////////////////////////////////////
template <typename T>
typename xorshift<T>::result_type
xorshift<T>::operator() (void)
{
m_state ^= m_state >> constants<T>::a;
m_state ^= m_state << constants<T>::b;
m_state ^= m_state >> constants<T>::c;
CHECK_NEZ (m_state);
return m_state;
};
///////////////////////////////////////////////////////////////////////////////