32 #ifndef VSMC_RNG_U01_HPP 33 #define VSMC_RNG_U01_HPP 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) -
53 static constexpr
long double value =
57 template <
int P,
int Q>
61 static constexpr
long double value =
static_cast<long double>(1ULL << P);
71 template <
typename RealType,
int P>
75 static constexpr RealType value =
84 template <
typename,
typename,
typename,
typename>
87 template <
typename UIntType,
typename RealType>
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;
98 static RealType
eval(UIntType u) noexcept
100 return trans((u << L) >> (R + L),
101 std::integral_constant<
bool, (V < W)>()) *
105 static void eval(std::size_t n,
const UIntType *u, RealType *r) noexcept
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)>());
115 static RealType trans(UIntType u, std::true_type) noexcept
117 return static_cast<RealType
>((u & 1) + u);
120 static RealType trans(UIntType u, std::false_type) noexcept
122 return static_cast<RealType
>(u & 1) + static_cast<RealType>(u);
126 template <
typename UIntType,
typename RealType>
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;
135 static RealType
eval(UIntType u) noexcept
137 return static_cast<RealType
>(u >> R) *
141 static void eval(std::size_t n,
const UIntType *u, RealType *r) noexcept
143 for (std::size_t i = 0; i != n; ++i)
149 template <
typename UIntType,
typename RealType>
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;
158 static RealType
eval(UIntType u) noexcept
160 return static_cast<RealType
>(u >> R) *
165 static void eval(std::size_t n,
const UIntType *u, RealType *r) noexcept
167 for (std::size_t i = 0; i != n; ++i)
174 template <
typename UIntType,
typename RealType>
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;
183 static RealType
eval(UIntType u) noexcept
185 return static_cast<RealType
>(u >> R) *
190 static void eval(std::size_t n,
const UIntType *u, RealType *r) noexcept
192 for (std::size_t i = 0; i != n; ++i)
211 template <
typename UIntType,
typename RealType,
typename Left,
typename Right>
214 static_assert(std::is_unsigned<UIntType>::value,
215 "**u01_lr** USED WITH UIntType OTHER THAN UNSIGNED INTEGER " 217 static_assert(std::is_floating_point<RealType>::value,
218 "**u01_lr** USED WITH RealType OTHER THAN FLOATING POINT " 226 template <
typename UIntType,
typename RealType,
typename Left,
typename Right>
227 void u01_lr(std::size_t n,
const UIntType *u, RealType *r) noexcept
229 static_assert(std::is_unsigned<UIntType>::value,
230 "**u01_lr** USED WITH UIntType OTHER THAN UNSIGNED INTEGER " 232 static_assert(std::is_floating_point<RealType>::value,
233 "**u01_lr** USED WITH RealType OTHER THAN FLOATING POINT " 241 template <
typename UIntType,
typename RealType>
244 return u01_lr<UIntType, RealType, Closed, Closed>(u);
249 template <
typename UIntType,
typename RealType>
252 return u01_lr<UIntType, RealType, Closed, Open>(u);
257 template <
typename UIntType,
typename RealType>
260 return u01_lr<UIntType, RealType, Open, Closed>(u);
265 template <
typename UIntType,
typename RealType>
268 return u01_lr<UIntType, RealType, Open, Open>(u);
273 template <
typename UIntType,
typename RealType>
274 void u01_cc(std::size_t n,
const UIntType *u, RealType *r) noexcept
276 u01_lr<UIntType, RealType, Closed, Closed>(n, u, r);
281 template <
typename UIntType,
typename RealType>
282 void u01_co(std::size_t n,
const UIntType *u, RealType *r) noexcept
284 u01_lr<UIntType, RealType, Closed, Open>(n, u, r);
289 template <
typename UIntType,
typename RealType>
290 void u01_oc(std::size_t n,
const UIntType *u, RealType *r) noexcept
292 u01_lr<UIntType, RealType, Open, Closed>(n, u, r);
297 template <
typename UIntType,
typename RealType>
298 void u01_oo(std::size_t n,
const UIntType *u, RealType *r) noexcept
300 u01_lr<UIntType, RealType, Open, Open>(n, u, r);
305 #endif // VSMC_RNG_U01_HPP
RealType u01_oo(UIntType u) noexcept
Convert uniform unsigned integers to floating points on (0, 1)
void mul(std::size_t n, const float *a, const float *b, float *y)
static void eval(std::size_t n, const UIntType *u, RealType *r) noexcept
static RealType eval(UIntType u) noexcept
RealType u01_co(UIntType u) noexcept
Convert uniform unsigned integers to floating points on [0, 1)
RealType u01_lr(UIntType u) noexcept
Convert uniform unsigned integers to floating points within [0, 1].
RealType u01_oc(UIntType u) noexcept
Convert uniform unsigned integers to floating points on (0, 1].
static RealType eval(UIntType u) noexcept
RealType u01_cc(UIntType u) noexcept
Convert uniform unsigned integers to floating points on [0, 1].
static RealType eval(UIntType u) noexcept
void fma(std::size_t n, const T *a, const T *b, const T *c, T *y)
For , compute .
static void eval(std::size_t n, const UIntType *u, RealType *r) noexcept
static void eval(std::size_t n, const UIntType *u, RealType *r) noexcept
static RealType eval(UIntType u) noexcept
static void eval(std::size_t n, const UIntType *u, RealType *r) noexcept