1 /* boost random/tausworthe.hpp header file
\r
3 * Copyright Jens Maurer 2002
\r
4 * Distributed under the Boost Software License, Version 1.0. (See
\r
5 * accompanying file LICENSE_1_0.txt or copy at
\r
6 * http://www.boost.org/LICENSE_1_0.txt)
\r
8 * See http://www.boost.org for most recent version including documentation.
\r
10 * $Id: linear_feedback_shift.hpp 58649 2010-01-02 21:23:17Z steven_watanabe $
\r
14 #ifndef BOOST_RANDOM_LINEAR_FEEDBACK_SHIFT_HPP
\r
15 #define BOOST_RANDOM_LINEAR_FEEDBACK_SHIFT_HPP
\r
19 #include <stdexcept>
\r
20 #include <boost/config.hpp>
\r
21 #include <boost/static_assert.hpp>
\r
22 #include <boost/limits.hpp>
\r
23 #include <boost/random/detail/config.hpp>
\r
29 template<class UIntType, int w, int k, int q, int s, UIntType val>
\r
30 class linear_feedback_shift
\r
33 typedef UIntType result_type;
\r
34 // avoid the warning trouble when using (1<<w) on 32 bit machines
\r
35 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
\r
36 BOOST_STATIC_CONSTANT(int, word_size = w);
\r
37 BOOST_STATIC_CONSTANT(int, exponent1 = k);
\r
38 BOOST_STATIC_CONSTANT(int, exponent2 = q);
\r
39 BOOST_STATIC_CONSTANT(int, step_size = s);
\r
41 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return 0; }
\r
42 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return wordmask; }
\r
44 // MSVC 6 and possibly others crash when encountering complicated integral
\r
45 // constant expressions. Avoid the checks for now.
\r
46 // BOOST_STATIC_ASSERT(w > 0);
\r
47 // BOOST_STATIC_ASSERT(q > 0);
\r
48 // BOOST_STATIC_ASSERT(k < w);
\r
49 // BOOST_STATIC_ASSERT(0 < 2*q && 2*q < k);
\r
50 // BOOST_STATIC_ASSERT(0 < s && s <= k-q);
\r
52 explicit linear_feedback_shift(UIntType s0 = 341) : wordmask(0)
\r
54 // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
\r
55 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
\r
56 BOOST_STATIC_ASSERT(std::numeric_limits<UIntType>::is_integer);
\r
57 BOOST_STATIC_ASSERT(!std::numeric_limits<UIntType>::is_signed);
\r
60 // avoid "left shift count >= with of type" warning
\r
61 for(int i = 0; i < w; ++i)
\r
62 wordmask |= (1u << i);
\r
66 template<class It> linear_feedback_shift(It& first, It last) : wordmask(0)
\r
68 // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
\r
69 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
\r
70 BOOST_STATIC_ASSERT(std::numeric_limits<UIntType>::is_integer);
\r
71 BOOST_STATIC_ASSERT(!std::numeric_limits<UIntType>::is_signed);
\r
74 // avoid "left shift count >= with of type" warning
\r
75 for(int i = 0; i < w; ++i)
\r
76 wordmask |= (1u << i);
\r
80 void seed(UIntType s0 = 341) {
\r
81 if(s0 < (1 << (w-k))) {
\r
86 template<class It> void seed(It& first, It last)
\r
89 throw std::invalid_argument("linear_feedback_shift::seed");
\r
91 assert(value >= (1 << (w-k)));
\r
94 result_type operator()()
\r
96 const UIntType b = (((value << q) ^ value) & wordmask) >> (k-s);
\r
97 const UIntType mask = ( (~static_cast<UIntType>(0)) << (w-k) ) & wordmask;
\r
98 value = ((value & mask) << s) ^ b;
\r
101 static bool validation(result_type x) { return val == x; }
\r
103 #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
\r
105 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
\r
106 template<class CharT, class Traits>
\r
107 friend std::basic_ostream<CharT,Traits>&
\r
108 operator<<(std::basic_ostream<CharT,Traits>& os, linear_feedback_shift x)
\r
109 { os << x.value; return os; }
\r
111 template<class CharT, class Traits>
\r
112 friend std::basic_istream<CharT,Traits>&
\r
113 operator>>(std::basic_istream<CharT,Traits>& is, linear_feedback_shift& x)
\r
114 { is >> x.value; return is; }
\r
117 friend bool operator==(linear_feedback_shift x, linear_feedback_shift y)
\r
118 { return x.value == y.value; }
\r
119 friend bool operator!=(linear_feedback_shift x, linear_feedback_shift y)
\r
120 { return !(x == y); }
\r
122 // Use a member function; Streamable concept not supported.
\r
123 bool operator==(linear_feedback_shift rhs) const
\r
124 { return value == rhs.value; }
\r
125 bool operator!=(linear_feedback_shift rhs) const
\r
126 { return !(*this == rhs); }
\r
130 UIntType wordmask; // avoid "left shift count >= width of type" warnings
\r
134 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
\r
135 // A definition is required even for integral static constants
\r
136 template<class UIntType, int w, int k, int q, int s, UIntType val>
\r
137 const bool linear_feedback_shift<UIntType, w, k, q, s, val>::has_fixed_range;
\r
138 template<class UIntType, int w, int k, int q, int s, UIntType val>
\r
139 const int linear_feedback_shift<UIntType, w, k, q, s, val>::word_size;
\r
140 template<class UIntType, int w, int k, int q, int s, UIntType val>
\r
141 const int linear_feedback_shift<UIntType, w, k, q, s, val>::exponent1;
\r
142 template<class UIntType, int w, int k, int q, int s, UIntType val>
\r
143 const int linear_feedback_shift<UIntType, w, k, q, s, val>::exponent2;
\r
144 template<class UIntType, int w, int k, int q, int s, UIntType val>
\r
145 const int linear_feedback_shift<UIntType, w, k, q, s, val>::step_size;
\r
148 } // namespace random
\r
149 } // namespace boost
\r
151 #endif // BOOST_RANDOM_LINEAR_FEEDBACK_SHIFT_HPP
\r