OSDN Git Service

CUDA
[eos/hostdependX86LINUX64.git] / util / X86LINUX64 / cuda-6.5 / include / thrust / random / detail / linear_feedback_shift_engine.inl
1 /*
2  *  Copyright 2008-2013 NVIDIA Corporation
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  */
16
17 #include <thrust/random/linear_feedback_shift_engine.h>
18
19 namespace thrust
20 {
21
22 namespace random
23 {
24
25 template<typename UIntType, size_t w, size_t k, size_t q, size_t s>
26   linear_feedback_shift_engine<UIntType,w,k,q,s>
27     ::linear_feedback_shift_engine(result_type value)
28 {
29   seed(value);
30 } // end linear_feedback_shift_engine::linear_feedback_shift_engine()
31
32 template<typename UIntType, size_t w, size_t k, size_t q, size_t s>
33   void linear_feedback_shift_engine<UIntType,w,k,q,s>
34     ::seed(result_type value)
35 {
36   m_value = value;
37 } // end linear_feedback_shift_engine::seed()
38
39 template<typename UIntType, size_t w, size_t k, size_t q, size_t s>
40   typename linear_feedback_shift_engine<UIntType,w,k,q,s>::result_type
41     linear_feedback_shift_engine<UIntType,w,k,q,s>
42       ::operator()(void)
43 {
44   const UIntType b = (((m_value << q) ^ m_value) & wordmask) >> (k-s);
45   const UIntType mask = ( (~static_cast<UIntType>(0)) << (w-k) ) & wordmask;
46   m_value = ((m_value & mask) << s) ^ b;
47   return m_value;
48 } // end linear_feedback_shift_engine::operator()()
49
50
51 template<typename UIntType, size_t w, size_t k, size_t q, size_t s>
52   void linear_feedback_shift_engine<UIntType,w,k,q,s>
53     ::discard(unsigned long long z)
54 {
55   for(; z > 0; --z)
56   {
57     this->operator()();
58   } // end for
59 } // end linear_feedback_shift_engine::discard()
60
61
62 template<typename UIntType, size_t w, size_t k, size_t q, size_t s>
63   template<typename CharT, typename Traits>
64     std::basic_ostream<CharT,Traits>& linear_feedback_shift_engine<UIntType,w,k,q,s>
65       ::stream_out(std::basic_ostream<CharT,Traits> &os) const
66 {
67   typedef std::basic_ostream<CharT,Traits> ostream_type;
68   typedef typename ostream_type::ios_base  ios_base;
69
70   // save old flags & fill character
71   const typename ios_base::fmtflags flags = os.flags();
72   const CharT fill = os.fill();
73
74   os.flags(ios_base::dec | ios_base::fixed | ios_base::left);
75   os.fill(os.widen(' '));
76
77   // output one word of state
78   os << m_value;
79
80   // restore flags & fill character
81   os.flags(flags);
82   os.fill(fill);
83
84   return os;
85 }
86
87
88 template<typename UIntType, size_t w, size_t k, size_t q, size_t s>
89   template<typename CharT, typename Traits>
90     std::basic_istream<CharT,Traits>& linear_feedback_shift_engine<UIntType,w,k,q,s>
91       ::stream_in(std::basic_istream<CharT,Traits> &is)
92 {
93   typedef std::basic_istream<CharT,Traits> istream_type;
94   typedef typename istream_type::ios_base     ios_base;
95
96   // save old flags
97   const typename ios_base::fmtflags flags = is.flags();
98
99   is.flags(ios_base::skipws);
100
101   // input one word of state
102   is >> m_value;
103
104   // restore flags
105   is.flags(flags);
106
107   return is;
108 }
109
110
111 template<typename UIntType, size_t w, size_t k, size_t q, size_t s>
112   bool linear_feedback_shift_engine<UIntType,w,k,q,s>
113     ::equal(const linear_feedback_shift_engine<UIntType,w,k,q,s> &rhs) const
114 {
115   return m_value == rhs.m_value;
116 }
117
118
119 template<typename UIntType, size_t w, size_t k, size_t q, size_t s>
120 bool operator==(const linear_feedback_shift_engine<UIntType,w,k,q,s> &lhs,
121                 const linear_feedback_shift_engine<UIntType,w,k,q,s> &rhs)
122 {
123   return thrust::random::detail::random_core_access::equal(lhs,rhs);
124 }
125
126
127 template<typename UIntType, size_t w, size_t k, size_t q, size_t s>
128 bool operator!=(const linear_feedback_shift_engine<UIntType,w,k,q,s> &lhs,
129                 const linear_feedback_shift_engine<UIntType,w,k,q,s> &rhs)
130 {
131   return !(lhs == rhs);
132 }
133
134
135 template<typename UIntType_, size_t w_, size_t k_, size_t q_, size_t s_,
136          typename CharT, typename Traits>
137 std::basic_ostream<CharT,Traits>&
138 operator<<(std::basic_ostream<CharT,Traits> &os,
139            const linear_feedback_shift_engine<UIntType_,w_,k_,q_,s_> &e)
140 {
141   return thrust::random::detail::random_core_access::stream_out(os,e);
142 }
143
144
145 template<typename UIntType_, size_t w_, size_t k_, size_t q_, size_t s_,
146          typename CharT, typename Traits>
147 std::basic_istream<CharT,Traits>&
148 operator>>(std::basic_istream<CharT,Traits> &is,
149            linear_feedback_shift_engine<UIntType_,w_,k_,q_,s_> &e)
150 {
151   return thrust::random::detail::random_core_access::stream_in(is,e);
152 }
153
154
155 } // end random
156
157 } // end thrust
158