32 #ifndef MCKL_RANDOM_INTERNAL_U01_GENERIC_HPP 33 #define MCKL_RANDOM_INTERNAL_U01_GENERIC_HPP 41 template <
int W,
typename UIntType>
43 std::conditional_t<W <= 64, std::uint64_t, UIntType>>;
45 template <
typename UIntType,
typename,
typename,
typename>
48 template <
typename UIntType,
typename RealType>
52 static RealType
eval(UIntType u)
54 constexpr
int W = std::numeric_limits<UIntType>::digits;
55 constexpr
int M = std::numeric_limits<RealType>::digits;
56 constexpr
int P = W - 1 < M ? W - 1 : M;
57 constexpr
int V = P + 1;
58 constexpr
int L = V < W ? 1 : 0;
59 constexpr
int R = V < W ? W - 1 - V : 0;
61 using UIntLeastType = U01UIntLeastType<W - R, UIntType>;
63 return trans(static_cast<UIntLeastType>((u << L) >> (R + L)),
64 std::integral_constant<
bool, (V < W)>()) *
65 Pow2<RealType, -(P + 1)>::value;
68 static void eval(std::size_t n,
const UIntType *u, RealType *r)
70 for (std::size_t i = 0; i != n; ++i) {
76 template <
typename UIntLeastType>
77 static RealType trans(UIntLeastType u, std::true_type)
79 return static_cast<RealType
>((u & 1) + u);
82 template <
typename UIntLeastType>
83 static RealType trans(UIntLeastType u, std::false_type)
85 return static_cast<RealType
>(u & 1) + static_cast<RealType>(u);
89 template <
typename UIntType,
typename RealType>
93 static RealType
eval(UIntType u)
95 constexpr
int W = std::numeric_limits<UIntType>::digits;
96 constexpr
int M = std::numeric_limits<RealType>::digits;
97 constexpr
int P = W < M ? W : M;
98 constexpr
int R = W - P;
100 using UIntLeastType = U01UIntLeastType<W - R, UIntType>;
102 return static_cast<RealType
>(
static_cast<UIntLeastType
>(u >> R)) *
103 Pow2<RealType, -P>::value;
106 static void eval(std::size_t n,
const UIntType *u, RealType *r)
108 for (std::size_t i = 0; i != n; ++i) {
114 template <
typename UIntType,
typename RealType>
118 static RealType
eval(UIntType u)
120 constexpr
int W = std::numeric_limits<UIntType>::digits;
121 constexpr
int M = std::numeric_limits<RealType>::digits;
122 constexpr
int P = W < M ? W : M;
123 constexpr
int R = W - P;
125 using UIntLeastType = U01UIntLeastType<W - R, UIntType>;
127 return static_cast<RealType
>(
static_cast<UIntLeastType
>(u >> R)) *
128 Pow2<RealType, -P>::value +
129 Pow2<RealType, -P>::value;
132 static void eval(std::size_t n,
const UIntType *u, RealType *r)
134 for (std::size_t i = 0; i != n; ++i) {
140 template <
typename UIntType,
typename RealType>
144 static RealType
eval(UIntType u)
146 constexpr
int W = std::numeric_limits<UIntType>::digits;
147 constexpr
int M = std::numeric_limits<RealType>::digits;
148 constexpr
int P = W + 1 < M ? W + 1 : M;
149 constexpr
int R = W + 1 - P;
151 using UIntLeastType = U01UIntLeastType<W - R, UIntType>;
153 return static_cast<RealType
>(
static_cast<UIntLeastType
>(u >> R)) *
154 Pow2<RealType, -(P - 1)>::value +
158 static void eval(std::size_t n,
const UIntType *u, RealType *r)
160 for (std::size_t i = 0; i != n; ++i) {
166 template <
typename UIntType,
typename RealType,
int Q>
170 static RealType
eval(
const UIntType *u)
172 return eval<0>(u, std::true_type());
175 static void eval(std::size_t n,
const UIntType *u, RealType *r)
177 for (std::size_t i = 0; i != n; ++i, u += Q) {
183 template <std::
size_t>
184 static RealType eval(
const UIntType *, std::false_type)
189 template <std::
size_t N>
190 static RealType eval(
const UIntType *u, std::true_type)
192 constexpr
int W = std::numeric_limits<UIntType>::digits;
194 return static_cast<RealType
>(u[N]) *
195 Pow2<RealType, -static_cast<int>((Q - N) * W)>::value +
196 eval<N + 1>(u, std::integral_constant<
bool, N + 1 < Q>());
204 #endif // MCKL_RANDOM_INTERNAL_U01_GENERIC_HPP
static RealType eval(UIntType u)
static RealType eval(const UIntType *u)
static void eval(std::size_t n, const UIntType *u, RealType *r)
std::conditional_t< W<=32, std::uint32_t, std::conditional_t< W<=64, std::uint64_t, UIntType > > U01UIntLeastType
static RealType eval(UIntType u)
static void eval(std::size_t n, const UIntType *u, RealType *r)
static RealType eval(UIntType u)
static RealType eval(UIntType u)
static void eval(std::size_t n, const UIntType *u, RealType *r)
Pow2Impl< RealType, P > Pow2
static void eval(std::size_t n, const UIntType *u, RealType *r)
static void eval(std::size_t n, const UIntType *u, RealType *r)