]> git.donarmstrong.com Git - rsem.git/blob - boost/random/linear_feedback_shift.hpp
RSEM Source Codes
[rsem.git] / boost / random / linear_feedback_shift.hpp
1 /* boost random/tausworthe.hpp header file
2  *
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)
7  *
8  * See http://www.boost.org for most recent version including documentation.
9  *
10  * $Id: linear_feedback_shift.hpp 60755 2010-03-22 00:45:06Z steven_watanabe $
11  *
12  */
13
14 #ifndef BOOST_RANDOM_LINEAR_FEEDBACK_SHIFT_HPP
15 #define BOOST_RANDOM_LINEAR_FEEDBACK_SHIFT_HPP
16
17 #include <iostream>
18 #include <cassert>
19 #include <stdexcept>
20 #include <boost/config.hpp>
21 #include <boost/static_assert.hpp>
22 #include <boost/limits.hpp>
23 #include <boost/random/detail/config.hpp>
24
25 namespace boost {
26 namespace random {
27
28 /**
29  * Instatiation of @c linear_feedback_shift model a
30  * \pseudo_random_number_generator.  It was originally
31  * proposed in
32  *
33  *  @blockquote
34  *  "Random numbers generated by linear recurrence modulo two.",
35  *  Tausworthe, R. C.(1965), Mathematics of Computation 19, 201-209.
36  *  @endblockquote
37  */
38 template<class UIntType, int w, int k, int q, int s, UIntType val>
39 class linear_feedback_shift
40 {
41 public:
42   typedef UIntType result_type;
43   // avoid the warning trouble when using (1<<w) on 32 bit machines
44   BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
45   BOOST_STATIC_CONSTANT(int, word_size = w);
46   BOOST_STATIC_CONSTANT(int, exponent1 = k);
47   BOOST_STATIC_CONSTANT(int, exponent2 = q);
48   BOOST_STATIC_CONSTANT(int, step_size = s);
49
50   result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return 0; }
51   result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return wordmask; }
52
53   // MSVC 6 and possibly others crash when encountering complicated integral
54   // constant expressions.  Avoid the checks for now.
55   // BOOST_STATIC_ASSERT(w > 0);
56   // BOOST_STATIC_ASSERT(q > 0);
57   // BOOST_STATIC_ASSERT(k < w);
58   // BOOST_STATIC_ASSERT(0 < 2*q && 2*q < k);
59   // BOOST_STATIC_ASSERT(0 < s && s <= k-q);
60
61   explicit linear_feedback_shift(UIntType s0 = 341) : wordmask(0)
62   {
63     // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
64 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
65     BOOST_STATIC_ASSERT(std::numeric_limits<UIntType>::is_integer);
66     BOOST_STATIC_ASSERT(!std::numeric_limits<UIntType>::is_signed);
67 #endif
68
69     // avoid "left shift count >= with of type" warning
70     for(int i = 0; i < w; ++i)
71       wordmask |= (1u << i);
72     seed(s0);
73   }
74
75   template<class It> linear_feedback_shift(It& first, It last) : wordmask(0)
76   {
77     // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
78 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
79     BOOST_STATIC_ASSERT(std::numeric_limits<UIntType>::is_integer);
80     BOOST_STATIC_ASSERT(!std::numeric_limits<UIntType>::is_signed);
81 #endif
82
83     // avoid "left shift count >= with of type" warning
84     for(int i = 0; i < w; ++i)
85       wordmask |= (1u << i);
86     seed(first, last);
87   }
88
89   void seed(UIntType s0 = 341) {
90       if(s0 < (1 << (w-k))) {
91           s0 += 1 << (w-k);
92       }
93       value = s0;
94   }
95   template<class It> void seed(It& first, It last)
96   {
97     if(first == last)
98       throw std::invalid_argument("linear_feedback_shift::seed");
99     value = *first++;
100     assert(value >= (1 << (w-k)));
101   }
102
103   result_type operator()()
104   {
105     const UIntType b = (((value << q) ^ value) & wordmask) >> (k-s);
106     const UIntType mask = ( (~static_cast<UIntType>(0)) << (w-k) ) & wordmask;
107     value = ((value & mask) << s) ^ b;
108     return value;
109   }
110   static bool validation(result_type x) { return val == x; }
111
112 #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
113
114 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
115   template<class CharT, class Traits>
116   friend std::basic_ostream<CharT,Traits>&
117   operator<<(std::basic_ostream<CharT,Traits>& os, linear_feedback_shift x)
118   { os << x.value; return os; }
119
120   template<class CharT, class Traits>
121   friend std::basic_istream<CharT,Traits>&
122   operator>>(std::basic_istream<CharT,Traits>& is, linear_feedback_shift& x)
123   { is >> x.value; return is; }
124 #endif
125
126   friend bool operator==(linear_feedback_shift x, linear_feedback_shift y)
127   { return x.value == y.value; }
128   friend bool operator!=(linear_feedback_shift x, linear_feedback_shift y)
129   { return !(x == y); }
130 #else
131   // Use a member function; Streamable concept not supported.
132   bool operator==(linear_feedback_shift rhs) const
133   { return value == rhs.value; }
134   bool operator!=(linear_feedback_shift rhs) const
135   { return !(*this == rhs); }
136 #endif
137
138 private:
139   UIntType wordmask; // avoid "left shift count >= width of type" warnings
140   UIntType value;
141 };
142
143 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
144 //  A definition is required even for integral static constants
145 template<class UIntType, int w, int k, int q, int s, UIntType val>
146 const bool linear_feedback_shift<UIntType, w, k, q, s, val>::has_fixed_range;
147 template<class UIntType, int w, int k, int q, int s, UIntType val>
148 const int linear_feedback_shift<UIntType, w, k, q, s, val>::word_size;
149 template<class UIntType, int w, int k, int q, int s, UIntType val>
150 const int linear_feedback_shift<UIntType, w, k, q, s, val>::exponent1;
151 template<class UIntType, int w, int k, int q, int s, UIntType val>
152 const int linear_feedback_shift<UIntType, w, k, q, s, val>::exponent2;
153 template<class UIntType, int w, int k, int q, int s, UIntType val>
154 const int linear_feedback_shift<UIntType, w, k, q, s, val>::step_size;
155 #endif
156
157 } // namespace random
158 } // namespace boost
159
160 #endif // BOOST_RANDOM_LINEAR_FEEDBACK_SHIFT_HPP