vSMC  v3.0.0
Scalable Monte Carlo
uniform_bits_distribution.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/rng/uniform_bits_distribution.hpp
3 //----------------------------------------------------------------------------
4 // vSMC: Scalable Monte Carlo
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013-2016, Yan Zhou
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 //============================================================================
31 
32 #ifndef VSMC_RNG_UNIFORM_BITS_DISTRIBUTION_HPP
33 #define VSMC_RNG_UNIFORM_BITS_DISTRIBUTION_HPP
34 
36 
37 namespace vsmc
38 {
39 
47 template <typename UIntType>
48 class UniformBitsDistribution
49 {
51  UniformBits, uniform_bits, UIntType, unsigned, UNSIGNED)
53 
54  public:
55  result_type min() const { return std::numeric_limits<result_type>::min(); }
56 
57  result_type max() const { return std::numeric_limits<result_type>::max(); }
58 
59  void reset() {}
60 
61  private:
62  template <typename RNGType>
63  result_type generate(RNGType &rng, const param_type &)
64  {
65  return generate(rng,
66  std::integral_constant<bool, RNGTraits<RNGType>::is_full_range>());
67  }
68 
69  template <typename RNGType>
70  static UIntType generate(RNGType &rng, std::false_type)
71  {
72  std::independent_bits_engine<RNGType,
73  std::numeric_limits<UIntType>::digits, UIntType>
74  eng(std::move(rng));
75  UIntType u = eng();
76  rng = std::move(eng.base());
77 
78  return u;
79  }
80 
81  template <typename RNGType>
82  static UIntType generate(RNGType &rng, std::true_type)
83  {
84  static constexpr int w = std::numeric_limits<UIntType>::digits;
85  static constexpr int r = RNGTraits<RNGType>::bits;
86 
87  return generate_patch(rng, std::integral_constant<bool, (w <= r)>());
88  }
89 
90  template <typename RNGType>
91  static UIntType generate_patch(RNGType &rng, std::true_type)
92  {
93  return static_cast<UIntType>(rng() - RNGType::min());
94  }
95 
96  template <typename RNGType>
97  static UIntType generate_patch(RNGType &rng, std::false_type)
98  {
99  return patch<0>(rng, std::true_type());
100  }
101 
102  template <int, typename RNGType>
103  static UIntType patch(RNGType &, std::false_type)
104  {
105  return 0;
106  }
107 
108  template <int N, typename RNGType>
109  static UIntType patch(RNGType &rng, std::true_type)
110  {
111  static constexpr int w = std::numeric_limits<UIntType>::digits;
112  static constexpr int r = RNGTraits<RNGType>::bits;
113  static constexpr int p = r * N;
114  static constexpr int q = r * N + r;
115 
116  UIntType u = static_cast<UIntType>(rng() - RNGType::min())
117  << static_cast<UIntType>(p);
118 
119  return u + patch<N + 1>(rng, std::integral_constant<bool, (q < w)>());
120  }
121 }; // class UniformBitsDistribution
122 
123 namespace internal
124 {
125 
126 template <typename UIntType, typename RNGType>
128  RNGType &rng, std::size_t n, UIntType *r, std::false_type)
129 {
131  for (std::size_t i = 0; i != n; ++i)
132  r[i] = ubits(rng);
133 }
134 
135 template <typename UIntType, typename RNGType>
137  RNGType &rng, std::size_t n, UIntType *r, std::true_type)
138 {
139  static constexpr int rbits = RNGTraits<RNGType>::bits;
140  static constexpr int ubits = std::numeric_limits<UIntType>::digits;
141  static constexpr std::size_t rate = ubits / rbits;
142  const std::size_t m = n * rate;
143  rand(rng, m, reinterpret_cast<typename RNGType::result_type *>(r));
144 }
145 
146 } // namespace vsmc::internal
147 
148 template <typename UIntType, typename RNGType>
149 inline void uniform_bits_distribution(RNGType &rng, std::size_t n, UIntType *r)
150 {
151  static_assert(std::is_unsigned<UIntType>::value,
152  "**uniform_bits_distribution** USED WITH UIntType OTHER THAN UNSIGNED "
153  "TYPES");
154 
155  static constexpr int rbits = RNGTraits<RNGType>::bits;
156  static constexpr int ubits = std::numeric_limits<UIntType>::digits;
157  static constexpr int tbits =
158  std::numeric_limits<typename RNGType::result_type>::digits;
159  static constexpr std::size_t ualign = alignof(UIntType);
160  static constexpr std::size_t talign = alignof(UIntType);
161 
163  rng, n, r,
164  std::integral_constant<
165  bool, (RNGType::min() == 0 && RNGTraits<RNGType>::is_full_range &&
166  rbits == tbits && ubits >= tbits && ubits % tbits == 0 &&
167  ualign >= talign && ualign % talign == 0)>());
168 }
169 
170 VSMC_DEFINE_RNG_DISTRIBUTION_RAND_0(UniformBits, uniform_bits, UIntType)
171 
172 } // namespace vsmc
173 
174 #endif // VSMC_RNG_UNIFORM_BITS_DISTRIBUTION_HPP
Definition: monitor.hpp:48
void uniform_bits_distribution_impl(RNGType &rng, std::size_t n, UIntType *r, std::false_type)
#define VSMC_DEFINE_RNG_DISTRIBUTION_RAND_0(Name, name, T)
#define VSMC_DEFINE_RNG_DISTRIBUTION_0(Name, name, T, t, Type)
void uniform_bits_distribution_impl(RNGType &rng, std::size_t n, UIntType *r, std::true_type)
Traits of RNG engines.
#define VSMC_DEFINE_RNG_DISTRIBUTION_MEMBER_0
void rand(RNGType &rng, ArcsineDistribution< RealType > &dist, std::size_t N, RealType *r)
void uniform_bits_distribution(RNGType &, std::size_t, UIntType *)
Uniform bits distribution.