32 #ifndef VSMC_RNG_RDRAND_HPP
33 #define VSMC_RNG_RDRAND_HPP
37 #include <immintrin.h>
39 #ifndef VSMC_RDRAND_NTRIAL_MAX
40 #define VSMC_RDRAND_NTRIAL_MAX 10
43 #define VSMC_STATIC_ASSERT_RNG_RDRAND_GENERATOR_RESULT_TYPE(ResultType) \
44 VSMC_STATIC_ASSERT(( \
45 cxx11::is_same<ResultType, uint16_t>::value || \
46 cxx11::is_same<ResultType, uint32_t>::value || \
47 cxx11::is_same<ResultType, uint64_t>::value), \
48 USE_RDRANDGenerator_WITH_RESULT_TYPE_OTHER_THAN_uint16_t_OR_uint32_t_OR_uint64_t)
50 #define VSMC_STATIC_ASSERT_RNG_RDRAND_GENERATOR \
51 VSMC_STATIC_ASSERT_RNG_RDRAND_GENERATOR_RESULT_TYPE(ResultType);
53 #define VSMC_RUNTIME_WARNING_RNG_RDRAND_GENERATOR_NTRIAL(ntrial, NTrialMax) \
54 VSMC_RUNTIME_WARNING((ntrial < NTrialMax), \
55 ("**RDRAND::generate** MAXIMUM NUMBER OF TRIALS EXCEEDED"))
61 template <
typename UIntType>
inline bool rdrand (UIntType *);
68 int cf = _rdrand16_step(&r);
69 *rand =
static_cast<uint16_t
>(r);
79 int cf = _rdrand32_step(&r);
80 *rand =
static_cast<uint32_t
>(r);
90 int cf = _rdrand64_step(&r);
91 *rand =
static_cast<uint64_t
>(r);
98 template <
typename ResultType, std::
size_t NTrialMax = VSMC_RDRAND_NTRIAL_MAX>
110 std::size_t ntrial = 0;
111 while (!rdrand<result_type>(&r) && ntrial != NTrialMax)
133 #endif// VSMC_RNG_RDRAND_HPP
GeneratorWrapper< uint64_t, RDRANDGenerator< uint64_t > > RDRAND64
C++11 Engine using 64-bits RDRAND instruction.
bool rdrand< uint64_t >(uint64_t *rand)
Invoke the 64-bits RDRAND instruction and return the carry flag.
bool rdrand< uint32_t >(uint32_t *rand)
Invoke the 32-bits RDRAND instruction and return the carry flag.
#define VSMC_STATIC_ASSERT_RNG_RDRAND_GENERATOR
bool rdrand< uint16_t >(uint16_t *rand)
Invoke the 16-bits RDRAND instruction and return the carry flag.
GeneratorWrapper< uint32_t, RDRANDGenerator< uint32_t > > RDRAND32
C++11 Engine using 32-bits RDRAND instruction.
A thin wrapper over any RNG Generator for use with C++11 API.
GeneratorWrapper< uint16_t, RDRANDGenerator< uint16_t > > RDRAND16
C++11 Engine using 16-bits RDRAND instruction.
#define VSMC_RUNTIME_WARNING_RNG_RDRAND_GENERATOR_NTRIAL(ntrial, NTrialMax)
bool rdrand(UIntType *)
Invoke the RDRAND instruction and return the carry flag.
static result_type generate()