~ubuntu-branches/ubuntu/trusty/libthrust/trusty

« back to all changes in this revision

Viewing changes to random/detail/linear_congruential_engine.inl

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Beckmann
  • Date: 2011-05-28 09:32:48 UTC
  • Revision ID: james.westby@ubuntu.com-20110528093248-np3euv5sj7fw3nyv
Tags: upstream-1.4.0
ImportĀ upstreamĀ versionĀ 1.4.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright 2008-2011 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_congruential_engine.h>
 
18
#include <thrust/random/detail/mod.h>
 
19
#include <thrust/random/detail/random_core_access.h>
 
20
 
 
21
namespace thrust
 
22
{
 
23
 
 
24
namespace random
 
25
{
 
26
 
 
27
 
 
28
template<typename UIntType, UIntType a, UIntType c, UIntType m>
 
29
  linear_congruential_engine<UIntType,a,c,m>
 
30
    ::linear_congruential_engine(result_type s)
 
31
{
 
32
  seed(s);
 
33
} // end linear_congruential_engine::linear_congruential_engine()
 
34
 
 
35
 
 
36
template<typename UIntType, UIntType a, UIntType c, UIntType m>
 
37
  void linear_congruential_engine<UIntType,a,c,m>
 
38
    ::seed(result_type s)
 
39
{
 
40
  if((detail::mod<UIntType, 1, 0, m>(c) == 0) &&
 
41
     (detail::mod<UIntType, 1, 0, m>(s) == 0))
 
42
    m_x = detail::mod<UIntType, 1, 0, m>(1);
 
43
  else
 
44
    m_x = detail::mod<UIntType, 1, 0, m>(s);
 
45
} // end linear_congruential_engine::seed()
 
46
 
 
47
 
 
48
template<typename UIntType, UIntType a, UIntType c, UIntType m>
 
49
  typename linear_congruential_engine<UIntType,a,c,m>::result_type
 
50
    linear_congruential_engine<UIntType,a,c,m>
 
51
      ::operator()(void)
 
52
{
 
53
  m_x = detail::mod<UIntType,a,c,m>(m_x);
 
54
  return m_x;
 
55
} // end linear_congruential_engine::operator()()
 
56
 
 
57
 
 
58
template<typename UIntType, UIntType a, UIntType c, UIntType m>
 
59
  void linear_congruential_engine<UIntType,a,c,m>
 
60
    ::discard(unsigned long long z)
 
61
{
 
62
  thrust::random::detail::linear_congruential_engine_discard::discard(*this,z);
 
63
} // end linear_congruential_engine::discard()
 
64
 
 
65
 
 
66
template<typename UIntType, UIntType a, UIntType c, UIntType m>
 
67
  template<typename CharT, typename Traits>
 
68
    std::basic_ostream<CharT,Traits>& linear_congruential_engine<UIntType,a,c,m>
 
69
      ::stream_out(std::basic_ostream<CharT,Traits> &os) const
 
70
{
 
71
  typedef std::basic_ostream<CharT,Traits> ostream_type;
 
72
  typedef typename ostream_type::ios_base  ios_base;
 
73
 
 
74
  // save old flags & fill character
 
75
  const typename ios_base::fmtflags flags = os.flags();
 
76
  const CharT fill = os.fill();
 
77
 
 
78
  os.flags(ios_base::dec | ios_base::fixed | ios_base::left);
 
79
  os.fill(os.widen(' '));
 
80
 
 
81
  // output one word of state
 
82
  os << m_x;
 
83
 
 
84
  // restore flags & fill character
 
85
  os.flags(flags);
 
86
  os.fill(fill);
 
87
 
 
88
  return os;
 
89
}
 
90
 
 
91
 
 
92
template<typename UIntType, UIntType a, UIntType c, UIntType m>
 
93
  template<typename CharT, typename Traits>
 
94
    std::basic_istream<CharT,Traits>& linear_congruential_engine<UIntType,a,c,m>
 
95
      ::stream_in(std::basic_istream<CharT,Traits> &is)
 
96
{
 
97
  typedef std::basic_istream<CharT,Traits> istream_type;
 
98
  typedef typename istream_type::ios_base     ios_base;
 
99
 
 
100
  // save old flags
 
101
  const typename ios_base::fmtflags flags = is.flags();
 
102
 
 
103
  is.flags(ios_base::dec);
 
104
 
 
105
  // input one word of state
 
106
  is >> m_x;
 
107
 
 
108
  // restore flags
 
109
  is.flags(flags);
 
110
 
 
111
  return is;
 
112
}
 
113
 
 
114
 
 
115
template<typename UIntType, UIntType a, UIntType c, UIntType m>
 
116
bool linear_congruential_engine<UIntType,a,c,m>
 
117
  ::equal(const linear_congruential_engine<UIntType,a,c,m> &rhs) const
 
118
{
 
119
  return m_x == rhs.m_x;
 
120
}
 
121
 
 
122
 
 
123
template<typename UIntType_, UIntType_ a_, UIntType_ c_, UIntType_ m_>
 
124
__host__ __device__
 
125
bool operator==(const linear_congruential_engine<UIntType_,a_,c_,m_> &lhs,
 
126
                const linear_congruential_engine<UIntType_,a_,c_,m_> &rhs)
 
127
{
 
128
  return detail::random_core_access::equal(lhs,rhs);
 
129
}
 
130
 
 
131
 
 
132
template<typename UIntType, UIntType a, UIntType c, UIntType m>
 
133
bool operator!=(const linear_congruential_engine<UIntType,a,c,m> &lhs,
 
134
                const linear_congruential_engine<UIntType,a,c,m> &rhs)
 
135
{
 
136
  return !(lhs == rhs);
 
137
}
 
138
 
 
139
 
 
140
template<typename UIntType_, UIntType_ a_, UIntType_ c_, UIntType_ m_,
 
141
         typename CharT, typename Traits>
 
142
std::basic_ostream<CharT,Traits>&
 
143
operator<<(std::basic_ostream<CharT,Traits> &os,
 
144
           const linear_congruential_engine<UIntType_,a_,c_,m_> &e)
 
145
{
 
146
  return detail::random_core_access::stream_out(os,e);
 
147
}
 
148
 
 
149
 
 
150
template<typename UIntType_, UIntType_ a_, UIntType_ c_, UIntType_ m_,
 
151
         typename CharT, typename Traits>
 
152
std::basic_istream<CharT,Traits>&
 
153
operator>>(std::basic_istream<CharT,Traits> &is,
 
154
           linear_congruential_engine<UIntType_,a_,c_,m_> &e)
 
155
{
 
156
  return detail::random_core_access::stream_in(is,e);
 
157
}
 
158
 
 
159
 
 
160
} // end random
 
161
 
 
162
} // end thrust
 
163