vSMC  v3.0.0
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)>
50 class U01Pow2L
51 {
52  public:
53  static constexpr long double value =
54  static_cast<long double>(1ULL << Q) * U01Pow2L<P - Q>::value;
55 }; // class U01Pow2L
56 
57 template <int P, int Q>
59 {
60  public:
61  static constexpr long double value = static_cast<long double>(1ULL << P);
62 }; // class U01Pow2L
63 
64 template <int P>
66 {
67  public:
68  static constexpr long double value = 1.0L / U01Pow2L<P>::value;
69 }; // class U01Pow2InvL
70 
71 template <typename RealType, int P>
72 class U01Pow2
73 {
74  public:
75  static constexpr RealType value =
76  static_cast<RealType>(U01Pow2L<P>::value);
77 }; // class U01Pow2
78 
79 template <typename RealType, int P>
81 {
82  public:
83  static constexpr RealType value =
84  static_cast<RealType>(U01Pow2InvL<P>::value);
85 }; // class U01Pow2Inv
86 
87 template <typename, typename, typename, typename>
88 class U01Impl;
89 
90 template <typename UIntType, typename RealType>
92 {
93  static constexpr int W = std::numeric_limits<UIntType>::digits;
94  static constexpr int M = std::numeric_limits<RealType>::digits;
95  static constexpr int P = W - 1 < M ? W - 1 : M;
96  static constexpr int V = P + 1;
97  static constexpr int L = V < W ? 1 : 0;
98  static constexpr int R = V < W ? W - 1 - V : 0;
99 
100  public:
101  static RealType eval(UIntType u) noexcept
102  {
103  return trans((u << L) >> (R + L),
104  std::integral_constant<bool, (V < W)>()) *
106  }
107 
108  static void eval(std::size_t n, const UIntType *u, RealType *r) noexcept
109  {
110  for (std::size_t i = 0; i != n; ++i) {
111  r[i] = trans((u[i] << L) >> (R + L),
112  std::integral_constant<bool, (V < W)>());
113  }
115  }
116 
117  private:
118  static RealType trans(UIntType u, std::true_type) noexcept
119  {
120  return static_cast<RealType>((u & 1) + u);
121  }
122 
123  static RealType trans(UIntType u, std::false_type) noexcept
124  {
125  return static_cast<RealType>(u & 1) + static_cast<RealType>(u);
126  }
127 }; // class U01Impl
128 
129 template <typename UIntType, typename RealType>
131 {
132  static constexpr int W = std::numeric_limits<UIntType>::digits;
133  static constexpr int M = std::numeric_limits<RealType>::digits;
134  static constexpr int P = W < M ? W : M;
135  static constexpr int R = W - P;
136 
137  public:
138  static RealType eval(UIntType u) noexcept
139  {
140  return static_cast<RealType>(u >> R) * U01Pow2Inv<RealType, P>::value;
141  }
142 
143  static void eval(std::size_t n, const UIntType *u, RealType *r) noexcept
144  {
145  for (std::size_t i = 0; i != n; ++i)
146  r[i] = static_cast<RealType>(u[i] >> R);
148  }
149 }; // class U01Impl
150 
151 template <typename UIntType, typename RealType>
152 class U01Impl<UIntType, RealType, Open, Closed>
153 {
154  static constexpr int W = std::numeric_limits<UIntType>::digits;
155  static constexpr int M = std::numeric_limits<RealType>::digits;
156  static constexpr int P = W < M ? W : M;
157  static constexpr int R = W - P;
158 
159  public:
160  static RealType eval(UIntType u) noexcept
161  {
162  return static_cast<RealType>(u >> R) * U01Pow2Inv<RealType, P>::value +
164  }
165 
166  static void eval(std::size_t n, const UIntType *u, RealType *r) noexcept
167  {
168  for (std::size_t i = 0; i != n; ++i)
169  r[i] = static_cast<RealType>(u[i] >> R);
172  }
173 }; // class U01Impl
174 
175 template <typename UIntType, typename RealType>
176 class U01Impl<UIntType, RealType, Open, Open>
177 {
178  static constexpr int W = std::numeric_limits<UIntType>::digits;
179  static constexpr int M = std::numeric_limits<RealType>::digits;
180  static constexpr int P = W + 1 < M ? W + 1 : M;
181  static constexpr int R = W + 1 - P;
182 
183  public:
184  static RealType eval(UIntType u) noexcept
185  {
186  return static_cast<RealType>(u >> R) *
189  }
190 
191  static void eval(std::size_t n, const UIntType *u, RealType *r) noexcept
192  {
193  for (std::size_t i = 0; i != n; ++i)
194  r[i] = static_cast<RealType>(u[i] >> R);
197  }
198 }; // class U01Impl
199 
200 } // namespace vsmc::internal
201 
212 template <typename UIntType, typename RealType, typename Lower, typename Upper>
213 inline RealType u01(UIntType u) noexcept
214 {
215  static_assert(std::is_unsigned<UIntType>::value,
216  "**u01** USED WITH UIntType OTHER THAN UNSIGNED INTEGER "
217  "TYPES");
218  static_assert(std::is_floating_point<RealType>::value,
219  "**u01** USED WITH RealType OTHER THAN FLOATING POINT "
220  "TYPES");
221 
223 }
224 
227 template <typename UIntType, typename RealType, typename Lower, typename Upper>
228 inline void u01(std::size_t n, const UIntType *u, RealType *r) noexcept
229 {
230  static_assert(std::is_unsigned<UIntType>::value,
231  "**u01** USED WITH UIntType OTHER THAN UNSIGNED INTEGER "
232  "TYPES");
233  static_assert(std::is_floating_point<RealType>::value,
234  "**u01** USED WITH RealType OTHER THAN FLOATING POINT "
235  "TYPES");
236 
238 }
239 
242 template <typename UIntType, typename RealType>
243 inline RealType u01_cc(UIntType u) noexcept
244 {
245  return u01<UIntType, RealType, Closed, Closed>(u);
246 }
247 
250 template <typename UIntType, typename RealType>
251 inline RealType u01_co(UIntType u) noexcept
252 {
253  return u01<UIntType, RealType, Closed, Open>(u);
254 }
255 
258 template <typename UIntType, typename RealType>
259 inline RealType u01_oc(UIntType u) noexcept
260 {
261  return u01<UIntType, RealType, Open, Closed>(u);
262 }
263 
266 template <typename UIntType, typename RealType>
267 inline RealType u01_oo(UIntType u) noexcept
268 {
269  return u01<UIntType, RealType, Open, Open>(u);
270 }
271 
274 template <typename UIntType, typename RealType>
275 inline void u01_cc(std::size_t n, const UIntType *u, RealType *r) noexcept
276 {
277  u01<UIntType, RealType, Closed, Closed>(n, u, r);
278 }
279 
282 template <typename UIntType, typename RealType>
283 inline void u01_co(std::size_t n, const UIntType *u, RealType *r) noexcept
284 {
285  u01<UIntType, RealType, Closed, Open>(n, u, r);
286 }
287 
290 template <typename UIntType, typename RealType>
291 inline void u01_oc(std::size_t n, const UIntType *u, RealType *r) noexcept
292 {
293  u01<UIntType, RealType, Open, Closed>(n, u, r);
294 }
295 
298 template <typename UIntType, typename RealType>
299 inline void u01_oo(std::size_t n, const UIntType *u, RealType *r) noexcept
300 {
301  u01<UIntType, RealType, Open, Open>(n, u, r);
302 }
303 
304 } // namespace vsmc
305 
306 #endif // VSMC_RNG_U01_HPP
Definition: monitor.hpp:48
RealType u01_oo(UIntType u) noexcept
Convert uniform unsigned integers to floating points on (0, 1)
Definition: u01.hpp:267
void mul(std::size_t n, const float *a, const float *b, float *y)
Definition: vmath.hpp:77
static RealType eval(UIntType u) noexcept
Definition: u01.hpp:138
RealType u01_co(UIntType u) noexcept
Convert uniform unsigned integers to floating points on [0, 1)
Definition: u01.hpp:251
static RealType eval(UIntType u) noexcept
Definition: u01.hpp:101
static void eval(std::size_t n, const UIntType *u, RealType *r) noexcept
Definition: u01.hpp:166
RealType u01(UIntType u) noexcept
Convert uniform unsigned integers to floating points within [0, 1].
Definition: u01.hpp:213
static void eval(std::size_t n, const UIntType *u, RealType *r) noexcept
Definition: u01.hpp:143
RealType u01_oc(UIntType u) noexcept
Convert uniform unsigned integers to floating points on (0, 1].
Definition: u01.hpp:259
static void eval(std::size_t n, const UIntType *u, RealType *r) noexcept
Definition: u01.hpp:191
static void eval(std::size_t n, const UIntType *u, RealType *r) noexcept
Definition: u01.hpp:108
RealType u01_cc(UIntType u) noexcept
Convert uniform unsigned integers to floating points on [0, 1].
Definition: u01.hpp:243
static RealType eval(UIntType u) noexcept
Definition: u01.hpp:160
void fma(std::size_t n, const T *a, const T *b, const T *c, T *y)
For , compute .
Definition: vmath.hpp:361
static RealType eval(UIntType u) noexcept
Definition: u01.hpp:184