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.
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>
23 #include <boost/random/detail/config.hpp>
29 template<class UIntType
, int w
, int k
, int q
, int s
, UIntType val
>
30 class linear_feedback_shift
33 typedef UIntType result_type
;
34 // avoid the warning trouble when using (1<<w) on 32 bit machines
35 BOOST_STATIC_CONSTANT(bool, has_fixed_range
= false);
36 BOOST_STATIC_CONSTANT(int, word_size
= w
);
37 BOOST_STATIC_CONSTANT(int, exponent1
= k
);
38 BOOST_STATIC_CONSTANT(int, exponent2
= q
);
39 BOOST_STATIC_CONSTANT(int, step_size
= s
);
41 result_type min
BOOST_PREVENT_MACRO_SUBSTITUTION () const { return 0; }
42 result_type max
BOOST_PREVENT_MACRO_SUBSTITUTION () const { return wordmask
; }
44 // MSVC 6 and possibly others crash when encountering complicated integral
45 // constant expressions. Avoid the checks for now.
46 // BOOST_STATIC_ASSERT(w > 0);
47 // BOOST_STATIC_ASSERT(q > 0);
48 // BOOST_STATIC_ASSERT(k < w);
49 // BOOST_STATIC_ASSERT(0 < 2*q && 2*q < k);
50 // BOOST_STATIC_ASSERT(0 < s && s <= k-q);
52 explicit linear_feedback_shift(UIntType s0
= 341) : wordmask(0)
54 // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
55 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
56 BOOST_STATIC_ASSERT(std::numeric_limits
<UIntType
>::is_integer
);
57 BOOST_STATIC_ASSERT(!std::numeric_limits
<UIntType
>::is_signed
);
60 // avoid "left shift count >= with of type" warning
61 for(int i
= 0; i
< w
; ++i
)
62 wordmask
|= (1u << i
);
66 template<class It
> linear_feedback_shift(It
& first
, It last
) : wordmask(0)
68 // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
69 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
70 BOOST_STATIC_ASSERT(std::numeric_limits
<UIntType
>::is_integer
);
71 BOOST_STATIC_ASSERT(!std::numeric_limits
<UIntType
>::is_signed
);
74 // avoid "left shift count >= with of type" warning
75 for(int i
= 0; i
< w
; ++i
)
76 wordmask
|= (1u << i
);
80 void seed(UIntType s0
= 341) { assert(s0
>= (1 << (w
-k
))); value
= s0
; }
81 template<class It
> void seed(It
& first
, It last
)
84 throw std::invalid_argument("linear_feedback_shift::seed");
86 assert(value
>= (1 << (w
-k
)));
89 result_type
operator()()
91 const UIntType b
= (((value
<< q
) ^ value
) & wordmask
) >> (k
-s
);
92 const UIntType mask
= ( (~static_cast<UIntType
>(0)) << (w
-k
) ) & wordmask
;
93 value
= ((value
& mask
) << s
) ^ b
;
96 static bool validation(result_type x
) { return val
== x
; }
98 #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
100 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
101 template<class CharT
, class Traits
>
102 friend std::basic_ostream
<CharT
,Traits
>&
103 operator<<(std::basic_ostream
<CharT
,Traits
>& os
, linear_feedback_shift x
)
104 { os
<< x
.value
; return os
; }
106 template<class CharT
, class Traits
>
107 friend std::basic_istream
<CharT
,Traits
>&
108 operator>>(std::basic_istream
<CharT
,Traits
>& is
, linear_feedback_shift
& x
)
109 { is
>> x
.value
; return is
; }
112 friend bool operator==(linear_feedback_shift x
, linear_feedback_shift y
)
113 { return x
.value
== y
.value
; }
114 friend bool operator!=(linear_feedback_shift x
, linear_feedback_shift y
)
115 { return !(x
== y
); }
117 // Use a member function; Streamable concept not supported.
118 bool operator==(linear_feedback_shift rhs
) const
119 { return value
== rhs
.value
; }
120 bool operator!=(linear_feedback_shift rhs
) const
121 { return !(*this == rhs
); }
125 UIntType wordmask
; // avoid "left shift count >= width of type" warnings
129 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
130 // A definition is required even for integral static constants
131 template<class UIntType
, int w
, int k
, int q
, int s
, UIntType val
>
132 const bool linear_feedback_shift
<UIntType
, w
, k
, q
, s
, val
>::has_fixed_range
;
133 template<class UIntType
, int w
, int k
, int q
, int s
, UIntType val
>
134 const int linear_feedback_shift
<UIntType
, w
, k
, q
, s
, val
>::word_size
;
135 template<class UIntType
, int w
, int k
, int q
, int s
, UIntType val
>
136 const int linear_feedback_shift
<UIntType
, w
, k
, q
, s
, val
>::exponent1
;
137 template<class UIntType
, int w
, int k
, int q
, int s
, UIntType val
>
138 const int linear_feedback_shift
<UIntType
, w
, k
, q
, s
, val
>::exponent2
;
139 template<class UIntType
, int w
, int k
, int q
, int s
, UIntType val
>
140 const int linear_feedback_shift
<UIntType
, w
, k
, q
, s
, val
>::step_size
;
143 } // namespace random
146 #endif // BOOST_RANDOM_LINEAR_FEEDBACK_SHIFT_HPP