vSMC
vSMC: 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>
49 {
50  static_assert(std::is_unsigned<UIntType>::value,
51  "**UniformBits** USED WITH UIntType OTHER THAN UNSIGNED INTEGER "
52  "TYPES");
53 
54  public:
55  template <typename RNGType>
56  static UIntType eval(RNGType &rng)
57  {
58  static constexpr int w = std::numeric_limits<UIntType>::digits;
59  static constexpr int p = RNGBits<RNGType>::value;
60 
61  return eval(rng, std::integral_constant<bool, w <= p>());
62  }
63 
64  private:
65  template <typename RNGType>
66  static UIntType eval(RNGType &rng, std::true_type)
67  {
68  static constexpr int r = RNGMinBits<RNGType>::value;
69 
70  return static_cast<UIntType>(rng() >> r);
71  }
72 
73  template <typename RNGType>
74  static UIntType eval(RNGType &rng, std::false_type)
75  {
76  return patch<0>(rng, std::true_type());
77  }
78 
79  template <int, typename RNGType>
80  static UIntType patch(RNGType &, std::false_type)
81  {
82  return 0;
83  }
84 
85  template <int N, typename RNGType>
86  static UIntType patch(RNGType &rng, std::true_type)
87  {
88  static constexpr int w = std::numeric_limits<UIntType>::digits;
89  static constexpr int v =
90  std::numeric_limits<typename RNGType::result_type>::digits;
91  static constexpr int l = v - RNGMaxBits<RNGType>::value;
92  static constexpr int r = l + RNGMinBits<RNGType>::value;
93  static constexpr int p = N * RNGBits<RNGType>::value;
94  static constexpr int q = p + RNGBits<RNGType>::value;
95 
96  UIntType u = static_cast<UIntType>((rng() << l) >> r);
97 
98  return (u << p) +
99  patch<N + 1>(rng, std::integral_constant<bool, (q < w)>());
100  }
101 }; // class UniformBits
102 
110 template <typename UIntType>
112 {
114  UniformBits, uniform_bits, UIntType, unsigned, UNSIGNED)
115 
116  public:
117  result_type min() const { return std::numeric_limits<result_type>::min(); }
118 
119  result_type max() const { return std::numeric_limits<result_type>::max(); }
120 
121  void reset() {}
122 
123  private:
124  template <typename RNGType>
125  result_type generate(RNGType &rng, const param_type &)
126  {
127  return UniformBits<UIntType>::eval(rng);
128  }
129 }; // class UniformBitsDistribution
130 
131 namespace internal
132 {
133 
134 template <typename UIntType, typename RNGType>
136  RNGType &rng, std::size_t n, UIntType *r, std::false_type)
137 {
138  for (std::size_t i = 0; i != n; ++i)
139  r[i] = UniformBits<UIntType>::eval(rng);
140 }
141 
142 template <typename UIntType, typename RNGType>
144  RNGType &rng, std::size_t n, UIntType *r, std::true_type)
145 {
146  rng_rand(rng, n, reinterpret_cast<typename RNGType::result_type *>(r));
147 }
148 
149 } // namespace vsmc::internal
150 
151 template <typename UIntType, typename RNGType>
152 inline void uniform_bits_distribution(RNGType &rng, std::size_t n, UIntType *r)
153 {
154  static_assert(std::is_unsigned<UIntType>::value,
155  "**uniform_bits_distribution** USED WITH UIntType OTHER THAN UNSIGNED "
156  "TYPES");
157 
158  static constexpr bool zero_min = RNGMinBits<RNGType>::value == 0;
159  static constexpr bool eq_bits =
160  RNGBits<RNGType>::value == std::numeric_limits<UIntType>::digits;
161  static constexpr bool eq_size =
162  sizeof(typename RNGType::result_type) == sizeof(UIntType);
163  static constexpr bool eq_align =
164  alignof(typename RNGType::result_type) == alignof(UIntType);
165 
167  rng, n, r, std::integral_constant<bool,
168  (zero_min && eq_bits && eq_size && eq_align)>());
169 }
170 
172 
173 } // namespace vsmc
174 
175 #endif // VSMC_RNG_UNIFORM_BITS_DISTRIBUTION_HPP
Definition: monitor.hpp:49
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)
Definition: common.hpp:379
#define VSMC_DEFINE_RNG_DISTRIBUTION_0(Name, name, T, t, Type)
Definition: common.hpp:364
Find the smallest N such that (RNGType::min() >> N) == 0
Definition: common.hpp:455
Find the largest N such that RNGType::max() >= (M >> (W - N)) where M = std::numeric_limits<typename ...
Definition: common.hpp:491
void rng_rand(RNGType &rng, BetaDistribution< RealType > &dist, std::size_t n, RealType *r)
static UIntType eval(RNGType &rng)
void uniform_bits_distribution_impl(RNGType &rng, std::size_t n, UIntType *r, std::true_type)
void uniform_bits_distribution(RNGType &, std::size_t, UIntType *)
The value of RNGMaxBits<RNGType>::value - RNGMinBits<RNGType>::value.
Definition: common.hpp:508
Uniform bits distribution.
Definition: common.hpp:603
Generate uniform bits of given type.