5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TESTING_BITWISE_UNIFORM_DISTRIBUTION_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TESTING_BITWISE_UNIFORM_DISTRIBUTION_HPP_
11#include "arene/base/stdlib_choice/climits.hpp"
12#include "arene/base/stdlib_choice/cstddef.hpp"
13#include "arene/base/stdlib_choice/declval.hpp"
14#include "arene/base/stdlib_choice/enable_if.hpp"
15#include "arene/base/stdlib_choice/is_integral.hpp"
16#include "arene/base/stdlib_choice/is_same.hpp"
17#include "arene/base/stdlib_choice/is_unsigned.hpp"
18#include "arene/base/stdlib_choice/make_unsigned.hpp"
28template <
typename T,
typename = arene::base::constra
ints<>>
42 template <
typename BitGenerator>
43 static constexpr auto assert_generator_is_ok() noexcept ->
void {
44 using bit_generator_result_type =
typename BitGenerator::result_type;
47 "BitGenerator::result_type must be unsigned integral for BitGenerator to satisfy UniformRandomBitGenerator"
50 BitGenerator::min() == bit_generator_result_type{},
51 "More sophisticated generation code is needed to handle cases where the real range is smaller than the type"
54 BitGenerator::max() ==
static_cast<bit_generator_result_type
>(~bit_generator_result_type{}),
55 "More sophisticated generation code is needed to handle cases where the real range is smaller than the type"
65 typename ExpectedBitsType,
66 typename BitGenerator,
68 std::enable_if_t<
sizeof(
typename BitGenerator::result_type) >=
sizeof(ExpectedBitsType)>> =
nullptr>
69 static constexpr auto generate_bits(BitGenerator& prng)
noexcept(
noexcept(prng())) -> ExpectedBitsType {
70 assert_generator_is_ok<BitGenerator>();
72 return static_cast<ExpectedBitsType
>(prng());
81 typename ExpectedBitsType,
82 typename BitGenerator,
84 std::enable_if_t<
sizeof(
typename BitGenerator::result_type) <
sizeof(ExpectedBitsType)>> =
nullptr>
85 static constexpr auto generate_bits(BitGenerator& prng)
noexcept(
noexcept(prng())) -> ExpectedBitsType {
86 assert_generator_is_ok<BitGenerator>();
88 using chunk_type =
typename BitGenerator::result_type;
90 sizeof(ExpectedBitsType) %
sizeof(chunk_type) == 0U,
91 "Implementation assumes that an integer number of chunks fits in ExpectedBitsType"
94 ExpectedBitsType result_bits{};
95 for (
std::size_t chunk_idx{}; chunk_idx < (
sizeof(ExpectedBitsType) /
sizeof(chunk_type)); ++chunk_idx) {
96 auto new_bits =
static_cast<ExpectedBitsType
>(prng());
97 result_bits =
static_cast<ExpectedBitsType
>(
98 result_bits |
static_cast<ExpectedBitsType
>(new_bits << (chunk_idx *
sizeof(chunk_type) * CHAR_BIT))
111 typename BitGenerator,
112 arene::base::constraints<std::enable_if_t<std::is_integral<typename BitGenerator::result_type>::value>> =
nullptr>
113 constexpr auto operator()(BitGenerator& prng)
const noexcept(
noexcept(prng())) -> T {
114 return static_cast<T
>(generate_bits<std::make_unsigned_t<T>>(prng));
123 arene::
base::constraints<std::enable_if_t<std::is_same<T, float>::value || std::is_same<T, double>::value>>> {
133 typename BitGenerator,
155 typename BitGenerator,
157 constexpr auto operator()(BitGenerator& prng)
const noexcept(
noexcept(prng())) -> T {
161 return T{component_dist(prng), component_dist(prng)};
#define ARENE_BIT_CAST_CONSTEXPR
Definition bit_cast.hpp:31
static constexpr value_type value
Definition integral_constant.hpp:34
Definition customization.hpp:31
Definition accumulate.hpp:37
constexpr bit_cast_detail::do_bit_cast< To > bit_cast
Reinterpret the object representation of one type as that of another.
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
remove_cv_t< remove_reference_t< T > > remove_cvref_t
Backport of std::remove_cvref_t.
Definition remove_cvref.hpp:35
smallest_unsigned_integer_for< 1ULL<<(sizeof(T) *CHAR_BIT - 1)> smallest_unsigned_integer_for_type
Calculates the smallest unsigned integer type that can be used to hold values of type T.
Definition smallest_integer_for.hpp:49
Definition accumulate.hpp:36
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41