return lcg();
}
#endif
+
+ enum Which {
+ rand_s = 1, rdseed = 2, rdrand = 4, device_file = 8, prng = 16,
+ any = 0xffff
+ };
+
+ inline Which
+ which_source(random_device::result_type (*func [[maybe_unused]])(void*),
+ void* file [[maybe_unused]])
+ {
+#ifdef _GLIBCXX_USE_CRT_RAND_S
+ if (func == &__winxp_rand_s)
+ return rand_s;
+#endif
+
+#ifdef USE_RDSEED
+#ifdef USE_RDRAND
+ if (func == &__x86_rdseed_rdrand)
+ return rdseed;
+#endif
+ if (func == &__x86_rdseed)
+ return rdseed;
+#endif
+
+#ifdef USE_RDRAND
+ if (func == &__x86_rdrand)
+ return rdrand;
+#endif
+
+#ifdef _GLIBCXX_USE_DEV_RANDOM
+ if (file != nullptr)
+ return device_file;
+#endif
+
+#ifdef USE_LCG
+ if (func == &__lcg)
+ return prng;
+#endif
+
+#ifdef USE_MT19937
+ return prng;
+#endif
+
+ return any; // should be unreachable
+ }
}
void
const char* fname [[gnu::unused]] = nullptr;
- enum {
- rand_s = 1, rdseed = 2, rdrand = 4, device_file = 8, prng = 16,
- any = 0xffff
- } which;
+ Which which;
if (token == "default")
{
double
random_device::_M_getentropy() const noexcept
{
+ const int max = sizeof(result_type) * __CHAR_BIT__;
+
+ switch(which_source(_M_func, _M_file))
+ {
+ case rdrand:
+ case rdseed:
+ return (double) max;
+ case rand_s:
+ case prng:
+ return 0.0;
+ case device_file:
+ // handled below
+ break;
+ default:
+ return 0.0;
+ }
+
#if defined _GLIBCXX_USE_DEV_RANDOM \
&& defined _GLIBCXX_HAVE_SYS_IOCTL_H && defined RNDGETENTCNT
- if (!_M_file)
- return 0.0;
#ifdef USE_POSIX_FILE_IO
const int fd = _M_fd;
if (ent < 0)
return 0.0;
- const int max = sizeof(result_type) * __CHAR_BIT__;
if (ent > max)
ent = max;
--- /dev/null
+// { dg-do run { target c++11 } }
+
+#include <random>
+#include <testsuite_hooks.h>
+#include <testsuite_random.h>
+
+void
+test01()
+{
+ for (auto token : { "mt19937", "prng", "rand_s" })
+ if (__gnu_test::random_device_available(token))
+ VERIFY( std::random_device(token).entropy() == 0.0 );
+
+ using result_type = std::random_device::result_type;
+ const double max = std::log2(std::numeric_limits<result_type>::max() + 1.0);
+
+ for (auto token : { "/dev/random", "/dev/urandom" })
+ if (__gnu_test::random_device_available(token))
+ {
+ const double entropy = std::random_device(token).entropy();
+ VERIFY( entropy >= 0.0 );
+ VERIFY( entropy <= max );
+ }
+
+ for (auto token : { "rdrand", "rdseed" })
+ if (__gnu_test::random_device_available(token))
+ {
+ const double entropy = std::random_device(token).entropy();
+ VERIFY( entropy == max );
+ }
+}
+
+int
+main()
+{
+ test01();
+}