32 #ifndef MCKL_RANDOM_RDRAND_HPP 33 #define MCKL_RANDOM_RDRAND_HPP 37 #ifndef MCKL_RDRAND_NTRIAL_MAX 38 #define MCKL_RDRAND_NTRIAL_MAX 0 45 template <
typename UIntType, std::
size_t W>
46 inline bool rdrand(UIntType *, std::integral_constant<int, W>);
50 template <
typename UIntType>
51 inline bool rdrand(UIntType *
rand, std::integral_constant<int, 16>)
54 int cf = _rdrand16_step(&r);
55 *rand =
static_cast<UIntType
>(r);
62 template <
typename UIntType>
63 inline bool rdrand(UIntType *
rand, std::integral_constant<int, 32>)
66 int cf = _rdrand32_step(&r);
67 *rand =
static_cast<UIntType
>(r);
74 template <
typename UIntType>
75 inline bool rdrand(UIntType *
rand, std::integral_constant<int, 64>)
77 #if defined(MCKL_INTEL) && MCKL_INTEL_VERSION < 1600 82 int cf = _rdrand64_step(&r);
83 *rand =
static_cast<UIntType
>(r);
90 template <
typename ResultType, std::
size_t NTrialMax = MCKL_RDRAND_NTRIAL_MAX>
93 static_assert(std::is_unsigned<ResultType>::value,
94 "**RDRANDEngine** used with ResultType other than unsigned integer " 97 static_assert(std::numeric_limits<ResultType>::digits == 16 ||
98 std::numeric_limits<ResultType>::digits == 32 ||
99 std::numeric_limits<ResultType>::digits == 64,
100 "**RDRANDEngine** used with ResultType of size other than 16, 32 or " 108 template <
typename Seed>
113 template <
typename Seed>
120 return generate(std::integral_constant<bool, NTrialMax != 0>());
127 return std::numeric_limits<result_type>::min();
132 return std::numeric_limits<result_type>::max();
147 template <
typename CharT,
typename CharTraits>
149 std::basic_ostream<CharT, CharTraits> &os,
155 template <
typename CharT,
typename CharTraits>
157 std::basic_istream<CharT, CharTraits> &is,
167 std::size_t ntrial = 0;
170 bool success = rdrand<result_type>(&r,
171 std::integral_constant<int,
172 std::numeric_limits<result_type>::digits>());
173 if (success || ntrial > NTrialMax) {
178 "**RDRAND::generator** maximum number of trials exceeded",
true);
187 bool success = rdrand<result_type>(&r,
188 std::integral_constant<int,
189 std::numeric_limits<result_type>::digits>());
213 #endif // MCKL_RANDOM_RDRAND_HPP
bool rdrand(UIntType *, std::integral_constant< int, W >)
Invoke the RDRAND instruction and return the carry flag.
static constexpr result_type min()
static constexpr result_type max()
friend std::basic_istream< CharT, CharTraits > & operator>>(std::basic_istream< CharT, CharTraits > &is, RDRANDEngine< ResultType, NTrialMax > &)
friend std::basic_ostream< CharT, CharTraits > & operator<<(std::basic_ostream< CharT, CharTraits > &os, const RDRANDEngine< ResultType, NTrialMax > &)
RDRANDEngine(const Seed &)
void rand(RNGType &rng, ArcsineDistribution< RealType > &distribution, std::size_t N, RealType *r)
friend bool operator!=(const RDRANDEngine< ResultType, NTrialMax > &, const RDRANDEngine< ResultType, NTrialMax > &)
void discard(std::size_t)
RNG default seed generator.
void runtime_assert(bool cond, const char *msg, bool soft=false)
friend bool operator==(const RDRANDEngine< ResultType, NTrialMax > &, const RDRANDEngine< ResultType, NTrialMax > &)