rand/system: add system generator for Linux
This commit is contained in:
parent
ad916e61c9
commit
3a83851e90
@ -102,6 +102,8 @@ if (LINUX)
|
||||
list (APPEND UTIL_FILES
|
||||
thread/event_futex.cpp
|
||||
thread/flag_futex.cpp
|
||||
rand/system_linux.cpp
|
||||
rand/system_linux.hpp
|
||||
)
|
||||
endif ()
|
||||
|
||||
@ -144,6 +146,8 @@ if (WIN32)
|
||||
thread/event_win32.cpp
|
||||
library_win32.cpp
|
||||
library_win32.hpp
|
||||
rand/system_win32.cpp
|
||||
rand/system_win32.hpp
|
||||
time_win32.cpp
|
||||
win32/windows.hpp
|
||||
win32/except.cpp
|
||||
@ -436,6 +440,7 @@ list (
|
||||
rand/mwc64x.hpp
|
||||
rand/pcg.cpp
|
||||
rand/pcg.hpp
|
||||
rand/system.hpp
|
||||
random.cpp
|
||||
random.hpp
|
||||
range.cpp
|
||||
|
19
rand/system.hpp
Normal file
19
rand/system.hpp
Normal file
@ -0,0 +1,19 @@
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Copyright 2019 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../platform.hpp"
|
||||
|
||||
#if defined(PLATFORM_WIN32)
|
||||
#include "system_win32.hpp"
|
||||
#elif defined(PLATFORM_LINUX)
|
||||
#include "system_linux.hpp"
|
||||
#else
|
||||
#error "Unhandled platform"
|
||||
#endif
|
62
rand/system_linux.cpp
Normal file
62
rand/system_linux.cpp
Normal file
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Copyright 2019 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#include "system_linux.hpp"
|
||||
|
||||
#include "../posix/except.hpp"
|
||||
#include "../io.hpp"
|
||||
|
||||
#include <sys/ioctl.h>
|
||||
#include <linux/random.h>
|
||||
|
||||
|
||||
using cruft::rand::linux::device;
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
static std::string const& default_source (void)
|
||||
{
|
||||
static std::string const s_path = "/dev/urandom";
|
||||
return s_path;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
device::device ():
|
||||
device (default_source ())
|
||||
{ ; }
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
device::device (std::string const &token)
|
||||
: m_fd (token, O_RDONLY | O_BINARY)
|
||||
{ ; }
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
device::result_type
|
||||
device::operator() (void)
|
||||
{
|
||||
result_type res;
|
||||
cruft::read (m_fd, cruft::make_byte_view<u08> (res));
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
double
|
||||
device::entropy () const noexcept
|
||||
{
|
||||
int val;
|
||||
|
||||
cruft::posix::error::try_code (
|
||||
ioctl (m_fd.native (), RNDGETENTCNT, &val)
|
||||
);
|
||||
|
||||
return val < 0 ? 0. : double (val);
|
||||
}
|
40
rand/system_linux.hpp
Normal file
40
rand/system_linux.hpp
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Copyright 2019 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../posix/fd.hpp"
|
||||
|
||||
|
||||
namespace cruft::rand {
|
||||
namespace linux {
|
||||
/// Models std::random_device and is suitable as a drop-in replacement.
|
||||
class device {
|
||||
public:
|
||||
using result_type = unsigned;
|
||||
|
||||
device ();
|
||||
device (std::string const&);
|
||||
device (device const &) = delete;
|
||||
|
||||
device& operator= (device const&) = delete;
|
||||
|
||||
result_type operator() (void);
|
||||
|
||||
double entropy (void) const noexcept;
|
||||
|
||||
static constexpr result_type min (void) { return std::numeric_limits<result_type>::min (); }
|
||||
static constexpr result_type max (void) { return std::numeric_limits<result_type>::max (); }
|
||||
|
||||
private:
|
||||
cruft::posix::fd m_fd;
|
||||
};
|
||||
}
|
||||
|
||||
using system = linux::device;
|
||||
}
|
0
rand/system_win32.cpp
Normal file
0
rand/system_win32.cpp
Normal file
8
rand/system_win32.hpp
Normal file
8
rand/system_win32.hpp
Normal file
@ -0,0 +1,8 @@
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Copyright 2019 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
@ -2,37 +2,24 @@
|
||||
#include "rand/lcg.hpp"
|
||||
#include "rand/mwc64x.hpp"
|
||||
#include "rand/pcg.hpp"
|
||||
#include "rand/rdrand.hpp"
|
||||
#include "rand/system.hpp"
|
||||
|
||||
#include "tap.hpp"
|
||||
#include "maths.hpp"
|
||||
#include "types/string.hpp"
|
||||
#include "introspection.hpp"
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <>
|
||||
std::string
|
||||
type_to_string<cruft::rand::xorshift<uint32_t>> (void) { return "xorshift<uint32_t>"; }
|
||||
|
||||
template <>
|
||||
std::string
|
||||
type_to_string<cruft::rand::xorshift<uint64_t>> (void) { return "xorshift<uint64_t>"; }
|
||||
|
||||
template <> std::string type_to_string<cruft::rand::lcg_t> (void) { return "lcg_t"; }
|
||||
|
||||
template <> std::string type_to_string<cruft::rand::mwc64x> (void) { return "mwc64x"; }
|
||||
|
||||
template <> std::string type_to_string<cruft::rand::pcg_xsh_rr<u64,u32>> (void) { return "pcg_xsh_rr<64,32>"; }
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
/// check random outputs are roughly divisible between a number of fixed width
|
||||
/// buckets.
|
||||
/// //////////////////////////////////////////////////////////////////////////////
|
||||
/// Check that outputs from a UniformRandomBitGenerator are roughly divisible
|
||||
/// between a number of fixed width buckets.
|
||||
///
|
||||
/// this is not anything close to a strict statistical test. it is more aimed
|
||||
/// This is not anything close to a strict statistical test. It is more aimed
|
||||
/// at link testing and basic functionality verification within a small
|
||||
/// resource footprint (ie, fast unit testing).
|
||||
template <typename G, typename ...Args>
|
||||
void
|
||||
template <typename GeneratorT, typename ...Args>
|
||||
static void
|
||||
test_buckets (cruft::TAP::logger &tap, Args&& ...args)
|
||||
{
|
||||
constexpr unsigned BUCKET_BITS = 8u;
|
||||
@ -40,7 +27,7 @@ test_buckets (cruft::TAP::logger &tap, Args&& ...args)
|
||||
constexpr unsigned EXPECTED = 1024u;
|
||||
constexpr unsigned ITERATIONS = BUCKET_COUNT * EXPECTED;
|
||||
|
||||
G gen (std::forward<Args> (args)...);
|
||||
GeneratorT gen (std::forward<Args> (args)...);
|
||||
std::uniform_int_distribution<int> dist (0, BUCKET_COUNT - 1);
|
||||
|
||||
unsigned buckets[BUCKET_COUNT] = {};
|
||||
@ -51,7 +38,8 @@ test_buckets (cruft::TAP::logger &tap, Args&& ...args)
|
||||
std::find_if (std::cbegin (buckets),
|
||||
std::cend (buckets),
|
||||
[] (auto v) { return v < EXPECTED * 7 / 8; }) == std::cend (buckets),
|
||||
"bucket counts for %s", type_to_string<G> ());
|
||||
"bucket counts for %s", cruft::type_name<GeneratorT> ()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -66,6 +54,7 @@ main (int,char**)
|
||||
test_buckets<cruft::rand::lcg_t> (tap, 0x1234u);
|
||||
test_buckets<cruft::rand::mwc64x> (tap, 0x1234u);
|
||||
test_buckets<cruft::rand::pcg_xsh_rr<>> (tap, 0x1234u);
|
||||
test_buckets<cruft::rand::system> (tap);
|
||||
|
||||
return tap.status ();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user