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