2
* Copyright 2008-2011 NVIDIA Corporation
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
#include <thrust/random/linear_congruential_engine.h>
18
#include <thrust/random/subtract_with_carry_engine.h>
19
#include <thrust/random/detail/mod.h>
20
#include <thrust/random/detail/random_core_access.h>
29
template<typename UIntType, size_t w, size_t s, size_t r>
30
subtract_with_carry_engine<UIntType,w,s,r>
31
::subtract_with_carry_engine(result_type value)
34
} // end subtract_with_carry_engine::subtract_with_carry_engine()
37
template<typename UIntType, size_t w, size_t s, size_t r>
38
void subtract_with_carry_engine<UIntType,w,s,r>
39
::seed(result_type value)
41
thrust::random::linear_congruential_engine<result_type,
42
40014u, 0u, 2147483563u> e(value == 0u ? default_seed : value);
45
for(size_t i = 0; i < long_lag; ++i)
47
m_x[i] = detail::mod<UIntType, 1, 0, modulus>(e());
50
m_carry = (m_x[long_lag-1] == 0);
52
} // end subtract_with_carry_engine::seed()
55
template<typename UIntType, size_t w, size_t s, size_t r>
56
typename subtract_with_carry_engine<UIntType,w,s,r>::result_type
57
subtract_with_carry_engine<UIntType,w,s,r>
60
// XXX we probably need to cache these m_x[m_k] in a register
61
// maybe we need to cache the use of all member variables
62
int short_index = m_k - short_lag;
64
short_index += long_lag;
66
if (m_x[short_index] >= m_x[m_k] + m_carry)
69
xi = m_x[short_index] - m_x[m_k] - m_carry;
75
xi = modulus - m_x[m_k] - m_carry + m_x[short_index];
83
} // end subtract_with_carry_engine::operator()()
86
template<typename UIntType, size_t w, size_t s, size_t r>
87
void subtract_with_carry_engine<UIntType,w,s,r>
88
::discard(unsigned long long z)
94
} // end subtract_with_carry_engine::discard()
97
template<typename UIntType, size_t w, size_t s, size_t r>
98
template<typename CharT, typename Traits>
99
std::basic_ostream<CharT,Traits>& subtract_with_carry_engine<UIntType,w,s,r>
100
::stream_out(std::basic_ostream<CharT,Traits> &os) const
102
typedef std::basic_ostream<CharT,Traits> ostream_type;
103
typedef typename ostream_type::ios_base ios_base;
105
const typename ios_base::fmtflags flags = os.flags();
106
const CharT fill = os.fill();
107
const CharT space = os.widen(' ');
108
os.flags(ios_base::dec | ios_base::fixed | ios_base::left);
111
const UIntType long_lag = r;
113
for(size_t i = 0; i < r; ++i)
114
os << m_x[(i + m_k) % long_lag] << space;
123
template<typename UIntType, size_t w, size_t s, size_t r>
124
template<typename CharType, typename Traits>
125
std::basic_istream<CharType,Traits>& subtract_with_carry_engine<UIntType,w,s,r>
126
::stream_in(std::basic_istream<CharType,Traits> &is)
128
typedef std::basic_istream<CharType,Traits> istream_type;
129
typedef typename istream_type::ios_base ios_base;
131
const typename ios_base::fmtflags flags = is.flags();
132
is.flags(ios_base::dec | ios_base::skipws);
134
for(size_t i = 0; i < r; ++i)
145
template<typename UIntType, size_t w, size_t s, size_t r>
146
bool subtract_with_carry_engine<UIntType,w,s,r>
147
::equal(const subtract_with_carry_engine<UIntType,w,s,r> &rhs) const
149
const UIntType long_lag = r;
152
for(size_t i = 0; i < r; ++i)
154
result &= (m_x[(i + m_k) % long_lag] == rhs.m_x[(i + rhs.m_k) % long_lag]);
157
// XXX not sure if this last check is necessary
158
result &= (m_carry == rhs.m_carry);
164
template<typename UIntType, size_t w, size_t s, size_t r,
165
typename CharT, typename Traits>
166
std::basic_ostream<CharT,Traits>&
167
operator<<(std::basic_ostream<CharT,Traits> &os,
168
const subtract_with_carry_engine<UIntType,w,s,r> &e)
170
return thrust::random::detail::random_core_access::stream_out(os,e);
174
template<typename UIntType, size_t w, size_t s, size_t r,
175
typename CharType, typename Traits>
176
std::basic_istream<CharType,Traits>&
177
operator>>(std::basic_istream<CharType,Traits> &is,
178
subtract_with_carry_engine<UIntType,w,s,r> &e)
180
return thrust::random::detail::random_core_access::stream_in(is,e);
184
template<typename UIntType, size_t w, size_t s, size_t r>
185
bool operator==(const subtract_with_carry_engine<UIntType,w,s,r> &lhs,
186
const subtract_with_carry_engine<UIntType,w,s,r> &rhs)
188
return thrust::random::detail::random_core_access::equal(lhs,rhs);
192
template<typename UIntType, size_t w, size_t s, size_t r>
193
bool operator!=(const subtract_with_carry_engine<UIntType,w,s,r> &lhs,
194
const subtract_with_carry_engine<UIntType,w,s,r> &rhs)
196
return !(lhs == rhs);