vSMC
vSMC: Scalable Monte Carlo
u01.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/rng/u01.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_U01_HPP
33 #define VSMC_RNG_U01_HPP
34 
36 
37 namespace vsmc
38 {
39 
40 namespace internal
41 {
42 
43 template <int P,
44  int Q = (std::numeric_limits<unsigned long long>::digits <
45  std::numeric_limits<long double>::digits ?
46  std::numeric_limits<unsigned long long>::digits :
47  std::numeric_limits<long double>::digits) -
48  1,
49  bool = (Q < P)>
51 {
52  public:
53  static constexpr long double value =
54  static_cast<long double>(1ULL << Q) * U01ImplPow2L<P - Q>::value;
55 }; // class U01ImplPow2L
56 
57 template <int P, int Q>
59 {
60  public:
61  static constexpr long double value = static_cast<long double>(1ULL << P);
62 }; // class U01ImplPow2L
63 
64 template <int P>
66 {
67  public:
68  static constexpr long double value = 1.0L / U01ImplPow2L<P>::value;
69 }; // class U01ImplPow2InvL
70 
71 template <typename RealType, int P>
73 {
74  public:
75  static constexpr RealType value =
76  static_cast<RealType>(U01ImplPow2InvL<P>::value);
77 }; // class U01ImplPow2Inv
78 
79 } // namespace vsmc::internal
80 
81 namespace internal
82 {
83 
84 template <typename, typename, typename, typename>
85 class U01LRImpl;
86 
87 template <typename UIntType, typename RealType>
89 {
90  static constexpr int W = std::numeric_limits<UIntType>::digits;
91  static constexpr int M = std::numeric_limits<RealType>::digits;
92  static constexpr int P = W - 1 < M ? W - 1 : M;
93  static constexpr int V = P + 1;
94  static constexpr int L = V < W ? 1 : 0;
95  static constexpr int R = V < W ? W - 1 - V : 0;
96 
97  public:
98  static RealType eval(UIntType u) noexcept
99  {
100  return trans((u << L) >> (R + L),
101  std::integral_constant<bool, (V < W)>()) *
103  }
104 
105  static void eval(std::size_t n, const UIntType *u, RealType *r) noexcept
106  {
107  for (std::size_t i = 0; i != n; ++i) {
108  r[i] = trans((u[i] << L) >> (R + L),
109  std::integral_constant<bool, (V < W)>());
110  }
112  }
113 
114  private:
115  static RealType trans(UIntType u, std::true_type) noexcept
116  {
117  return static_cast<RealType>((u & 1) + u);
118  }
119 
120  static RealType trans(UIntType u, std::false_type) noexcept
121  {
122  return static_cast<RealType>(u & 1) + static_cast<RealType>(u);
123  }
124 }; // class U01LRImpl
125 
126 template <typename UIntType, typename RealType>
128 {
129  static constexpr int W = std::numeric_limits<UIntType>::digits;
130  static constexpr int M = std::numeric_limits<RealType>::digits;
131  static constexpr int P = W < M ? W : M;
132  static constexpr int R = W - P;
133 
134  public:
135  static RealType eval(UIntType u) noexcept
136  {
137  return static_cast<RealType>(u >> R) *
139  }
140 
141  static void eval(std::size_t n, const UIntType *u, RealType *r) noexcept
142  {
143  for (std::size_t i = 0; i != n; ++i)
144  r[i] = u[i] >> R;
146  }
147 }; // class U01LRImpl
148 
149 template <typename UIntType, typename RealType>
150 class U01LRImpl<UIntType, RealType, Open, Closed>
151 {
152  static constexpr int W = std::numeric_limits<UIntType>::digits;
153  static constexpr int M = std::numeric_limits<RealType>::digits;
154  static constexpr int P = W < M ? W : M;
155  static constexpr int R = W - P;
156 
157  public:
158  static RealType eval(UIntType u) noexcept
159  {
160  return static_cast<RealType>(u >> R) *
163  }
164 
165  static void eval(std::size_t n, const UIntType *u, RealType *r) noexcept
166  {
167  for (std::size_t i = 0; i != n; ++i)
168  r[i] = u[i] >> R;
171  }
172 }; // class U01LRImpl
173 
174 template <typename UIntType, typename RealType>
175 class U01LRImpl<UIntType, RealType, Open, Open>
176 {
177  static constexpr int W = std::numeric_limits<UIntType>::digits;
178  static constexpr int M = std::numeric_limits<RealType>::digits;
179  static constexpr int P = W + 1 < M ? W + 1 : M;
180  static constexpr int R = W + 1 - P;
181 
182  public:
183  static RealType eval(UIntType u) noexcept
184  {
185  return static_cast<RealType>(u >> R) *
188  }
189 
190  static void eval(std::size_t n, const UIntType *u, RealType *r) noexcept
191  {
192  for (std::size_t i = 0; i != n; ++i)
193  r[i] = u[i] >> R;
196  }
197 }; // class U01LRImpl
198 
199 } // namespace vsmc::internal
200 
211 template <typename UIntType, typename RealType, typename Left, typename Right>
212 RealType u01_lr(UIntType u) noexcept
213 {
214  static_assert(std::is_unsigned<UIntType>::value,
215  "**u01_lr** USED WITH UIntType OTHER THAN UNSIGNED INTEGER "
216  "TYPES");
217  static_assert(std::is_floating_point<RealType>::value,
218  "**u01_lr** USED WITH RealType OTHER THAN FLOATING POINT "
219  "TYPES");
220 
222 }
223 
226 template <typename UIntType, typename RealType, typename Left, typename Right>
227 void u01_lr(std::size_t n, const UIntType *u, RealType *r) noexcept
228 {
229  static_assert(std::is_unsigned<UIntType>::value,
230  "**u01_lr** USED WITH UIntType OTHER THAN UNSIGNED INTEGER "
231  "TYPES");
232  static_assert(std::is_floating_point<RealType>::value,
233  "**u01_lr** USED WITH RealType OTHER THAN FLOATING POINT "
234  "TYPES");
235 
237 }
238 
241 template <typename UIntType, typename RealType>
242 RealType u01_cc(UIntType u) noexcept
243 {
244  return u01_lr<UIntType, RealType, Closed, Closed>(u);
245 }
246 
249 template <typename UIntType, typename RealType>
250 RealType u01_co(UIntType u) noexcept
251 {
252  return u01_lr<UIntType, RealType, Closed, Open>(u);
253 }
254 
257 template <typename UIntType, typename RealType>
258 RealType u01_oc(UIntType u) noexcept
259 {
260  return u01_lr<UIntType, RealType, Open, Closed>(u);
261 }
262 
265 template <typename UIntType, typename RealType>
266 RealType u01_oo(UIntType u) noexcept
267 {
268  return u01_lr<UIntType, RealType, Open, Open>(u);
269 }
270 
273 template <typename UIntType, typename RealType>
274 void u01_cc(std::size_t n, const UIntType *u, RealType *r) noexcept
275 {
276  u01_lr<UIntType, RealType, Closed, Closed>(n, u, r);
277 }
278 
281 template <typename UIntType, typename RealType>
282 void u01_co(std::size_t n, const UIntType *u, RealType *r) noexcept
283 {
284  u01_lr<UIntType, RealType, Closed, Open>(n, u, r);
285 }
286 
289 template <typename UIntType, typename RealType>
290 void u01_oc(std::size_t n, const UIntType *u, RealType *r) noexcept
291 {
292  u01_lr<UIntType, RealType, Open, Closed>(n, u, r);
293 }
294 
297 template <typename UIntType, typename RealType>
298 void u01_oo(std::size_t n, const UIntType *u, RealType *r) noexcept
299 {
300  u01_lr<UIntType, RealType, Open, Open>(n, u, r);
301 }
302 
303 } // namespace vsmc
304 
305 #endif // VSMC_RNG_U01_HPP
Definition: monitor.hpp:49
RealType u01_oo(UIntType u) noexcept
Convert uniform unsigned integers to floating points on (0, 1)
Definition: u01.hpp:266
void mul(std::size_t n, const float *a, const float *b, float *y)
Definition: vmath.hpp:112
static void eval(std::size_t n, const UIntType *u, RealType *r) noexcept
Definition: u01.hpp:105
static RealType eval(UIntType u) noexcept
Definition: u01.hpp:158
RealType u01_co(UIntType u) noexcept
Convert uniform unsigned integers to floating points on [0, 1)
Definition: u01.hpp:250
RealType u01_lr(UIntType u) noexcept
Convert uniform unsigned integers to floating points within [0, 1].
Definition: u01.hpp:212
RealType u01_oc(UIntType u) noexcept
Convert uniform unsigned integers to floating points on (0, 1].
Definition: u01.hpp:258
static RealType eval(UIntType u) noexcept
Definition: u01.hpp:135
RealType u01_cc(UIntType u) noexcept
Convert uniform unsigned integers to floating points on [0, 1].
Definition: u01.hpp:242
static RealType eval(UIntType u) noexcept
Definition: u01.hpp:98
void fma(std::size_t n, const T *a, const T *b, const T *c, T *y)
For , compute .
Definition: vmath.hpp:257
static void eval(std::size_t n, const UIntType *u, RealType *r) noexcept
Definition: u01.hpp:190
static void eval(std::size_t n, const UIntType *u, RealType *r) noexcept
Definition: u01.hpp:141
static RealType eval(UIntType u) noexcept
Definition: u01.hpp:183
static void eval(std::size_t n, const UIntType *u, RealType *r) noexcept
Definition: u01.hpp:165