1 /* boost random/tausworthe.hpp header file
3 * Copyright Jens Maurer 2002
4 * Distributed under the Boost Software License, Version 1.0. (See
5 * accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
8 * See http://www.boost.org for most recent version including documentation.
10 * $Id: linear_feedback_shift.hpp,v 1.12 2005/05/21 15:57:00 dgregor Exp $
14 #ifndef BOOST_RANDOM_LINEAR_FEEDBACK_SHIFT_HPP
15 #define BOOST_RANDOM_LINEAR_FEEDBACK_SHIFT_HPP
20 #include <boost/config.hpp>
21 #include <boost/static_assert.hpp>
22 #include <boost/limits.hpp>
28 template<class UIntType, int w, int k, int q, int s, UIntType val>
29 class linear_feedback_shift
32 typedef UIntType result_type;
33 // avoid the warning trouble when using (1<<w) on 32 bit machines
34 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
35 BOOST_STATIC_CONSTANT(int, word_size = w);
36 BOOST_STATIC_CONSTANT(int, exponent1 = k);
37 BOOST_STATIC_CONSTANT(int, exponent2 = q);
38 BOOST_STATIC_CONSTANT(int, step_size = s);
40 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return 0; }
41 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return wordmask; }
43 // MSVC 6 and possibly others crash when encountering complicated integral
44 // constant expressions. Avoid the checks for now.
45 // BOOST_STATIC_ASSERT(w > 0);
46 // BOOST_STATIC_ASSERT(q > 0);
47 // BOOST_STATIC_ASSERT(k < w);
48 // BOOST_STATIC_ASSERT(0 < 2*q && 2*q < k);
49 // BOOST_STATIC_ASSERT(0 < s && s <= k-q);
51 explicit linear_feedback_shift(UIntType s0 = 341) : wordmask(0)
53 // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
54 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
55 BOOST_STATIC_ASSERT(std::numeric_limits<UIntType>::is_integer);
56 BOOST_STATIC_ASSERT(!std::numeric_limits<UIntType>::is_signed);
59 // avoid "left shift count >= with of type" warning
60 for(int i = 0; i < w; ++i)
61 wordmask |= (1u << i);
65 template<class It> linear_feedback_shift(It& first, It last) : wordmask(0)
67 // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
68 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
69 BOOST_STATIC_ASSERT(std::numeric_limits<UIntType>::is_integer);
70 BOOST_STATIC_ASSERT(!std::numeric_limits<UIntType>::is_signed);
73 // avoid "left shift count >= with of type" warning
74 for(int i = 0; i < w; ++i)
75 wordmask |= (1u << i);
79 void seed(UIntType s0 = 341) { assert(s0 >= (1 << (w-k))); value = s0; }
80 template<class It> void seed(It& first, It last)
83 throw std::invalid_argument("linear_feedback_shift::seed");
85 assert(value >= (1 << (w-k)));
88 result_type operator()()
90 const UIntType b = (((value << q) ^ value) & wordmask) >> (k-s);
91 const UIntType mask = ( (~static_cast<UIntType>(0)) << (w-k) ) & wordmask;
92 value = ((value & mask) << s) ^ b;
95 bool validation(result_type x) const { return val == x; }
97 #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
99 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
100 template<class CharT, class Traits>
101 friend std::basic_ostream<CharT,Traits>&
102 operator<<(std::basic_ostream<CharT,Traits>& os, linear_feedback_shift x)
103 { os << x.value; return os; }
105 template<class CharT, class Traits>
106 friend std::basic_istream<CharT,Traits>&
107 operator>>(std::basic_istream<CharT,Traits>& is, linear_feedback_shift& x)
108 { is >> x.value; return is; }
111 friend bool operator==(linear_feedback_shift x, linear_feedback_shift y)
112 { return x.value == y.value; }
113 friend bool operator!=(linear_feedback_shift x, linear_feedback_shift y)
114 { return !(x == y); }
116 // Use a member function; Streamable concept not supported.
117 bool operator==(linear_feedback_shift rhs) const
118 { return value == rhs.value; }
119 bool operator!=(linear_feedback_shift rhs) const
120 { return !(*this == rhs); }
124 UIntType wordmask; // avoid "left shift count >= width of type" warnings
128 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
129 // A definition is required even for integral static constants
130 template<class UIntType, int w, int k, int q, int s, UIntType val>
131 const bool linear_feedback_shift<UIntType, w, k, q, s, val>::has_fixed_range;
132 template<class UIntType, int w, int k, int q, int s, UIntType val>
133 const int linear_feedback_shift<UIntType, w, k, q, s, val>::word_size;
134 template<class UIntType, int w, int k, int q, int s, UIntType val>
135 const int linear_feedback_shift<UIntType, w, k, q, s, val>::exponent1;
136 template<class UIntType, int w, int k, int q, int s, UIntType val>
137 const int linear_feedback_shift<UIntType, w, k, q, s, val>::exponent2;
138 template<class UIntType, int w, int k, int q, int s, UIntType val>
139 const int linear_feedback_shift<UIntType, w, k, q, s, val>::step_size;
142 } // namespace random
145 #endif // BOOST_RANDOM_LINEAR_FEEDBACK_SHIFT_HPP