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 =
79 template <
typename RealType,
int P>
83 static constexpr RealType value =
87 template <
typename,
typename,
typename,
typename>
90 template <
typename UIntType,
typename RealType>
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;
101 static RealType
eval(UIntType u) noexcept
103 return trans((u << L) >> (R + L),
104 std::integral_constant<
bool, (V < W)>()) *
108 static void eval(std::size_t n,
const UIntType *u, RealType *r) noexcept
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)>());
118 static RealType trans(UIntType u, std::true_type) noexcept
120 return static_cast<RealType
>((u & 1) + u);
123 static RealType trans(UIntType u, std::false_type) noexcept
125 return static_cast<RealType
>(u & 1) + static_cast<RealType>(u);
129 template <
typename UIntType,
typename RealType>
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;
138 static RealType
eval(UIntType u) noexcept
143 static void eval(std::size_t n,
const UIntType *u, RealType *r) noexcept
145 for (std::size_t i = 0; i != n; ++i)
146 r[i] = static_cast<RealType>(u[i] >> R);
151 template <
typename UIntType,
typename RealType>
152 class U01Impl<UIntType, RealType, Open, Closed>
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;
160 static RealType
eval(UIntType u) noexcept
166 static void eval(std::size_t n,
const UIntType *u, RealType *r) noexcept
168 for (std::size_t i = 0; i != n; ++i)
169 r[i] = static_cast<RealType>(u[i] >> R);
175 template <
typename UIntType,
typename RealType>
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;
184 static RealType
eval(UIntType u) noexcept
186 return static_cast<RealType
>(u >> R) *
191 static void eval(std::size_t n,
const UIntType *u, RealType *r) noexcept
193 for (std::size_t i = 0; i != n; ++i)
194 r[i] = static_cast<RealType>(u[i] >> R);
212 template <
typename UIntType,
typename RealType,
typename Lower,
typename Upper>
213 inline RealType
u01(UIntType u) noexcept
215 static_assert(std::is_unsigned<UIntType>::value,
216 "**u01** USED WITH UIntType OTHER THAN UNSIGNED INTEGER " 218 static_assert(std::is_floating_point<RealType>::value,
219 "**u01** USED WITH RealType OTHER THAN FLOATING POINT " 227 template <
typename UIntType,
typename RealType,
typename Lower,
typename Upper>
228 inline void u01(std::size_t n,
const UIntType *u, RealType *r) noexcept
230 static_assert(std::is_unsigned<UIntType>::value,
231 "**u01** USED WITH UIntType OTHER THAN UNSIGNED INTEGER " 233 static_assert(std::is_floating_point<RealType>::value,
234 "**u01** USED WITH RealType OTHER THAN FLOATING POINT " 242 template <
typename UIntType,
typename RealType>
243 inline RealType
u01_cc(UIntType u) noexcept
245 return u01<UIntType, RealType, Closed, Closed>(u);
250 template <
typename UIntType,
typename RealType>
251 inline RealType
u01_co(UIntType u) noexcept
253 return u01<UIntType, RealType, Closed, Open>(u);
258 template <
typename UIntType,
typename RealType>
259 inline RealType
u01_oc(UIntType u) noexcept
261 return u01<UIntType, RealType, Open, Closed>(u);
266 template <
typename UIntType,
typename RealType>
267 inline RealType
u01_oo(UIntType u) noexcept
269 return u01<UIntType, RealType, Open, Open>(u);
274 template <
typename UIntType,
typename RealType>
275 inline void u01_cc(std::size_t n,
const UIntType *u, RealType *r) noexcept
277 u01<UIntType, RealType, Closed, Closed>(n, u, r);
282 template <
typename UIntType,
typename RealType>
283 inline void u01_co(std::size_t n,
const UIntType *u, RealType *r) noexcept
285 u01<UIntType, RealType, Closed, Open>(n, u, r);
290 template <
typename UIntType,
typename RealType>
291 inline void u01_oc(std::size_t n,
const UIntType *u, RealType *r) noexcept
293 u01<UIntType, RealType, Open, Closed>(n, u, r);
298 template <
typename UIntType,
typename RealType>
299 inline void u01_oo(std::size_t n,
const UIntType *u, RealType *r) noexcept
301 u01<UIntType, RealType, Open, Open>(n, u, r);
306 #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 RealType eval(UIntType u) noexcept
RealType u01_co(UIntType u) noexcept
Convert uniform unsigned integers to floating points on [0, 1)
static RealType eval(UIntType u) noexcept
static void eval(std::size_t n, const UIntType *u, RealType *r) noexcept
RealType u01(UIntType u) noexcept
Convert uniform unsigned integers to floating points within [0, 1].
static void eval(std::size_t n, const UIntType *u, RealType *r) noexcept
RealType u01_oc(UIntType u) noexcept
Convert uniform unsigned integers to floating points on (0, 1].
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
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 RealType eval(UIntType u) noexcept