Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
bitwise_uniform_distribution.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
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_
7
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"
21
22namespace arene {
23namespace base {
24namespace testing {
25
26/// @brief A uniform distribution to apply on top of a bit generator. Primary template (deliberately undefined).
27/// @tparam T The value type to generate
28template <typename T, typename = arene::base::constraints<>>
30
31/// @brief A uniform distribution to apply on top of a bit generator. SFINAE overload for integral types.
32/// @tparam T The value type to generate
33template <typename T>
34class bitwise_uniform_distribution<T, arene::base::constraints<std::enable_if_t<std::is_integral<T>::value>>> {
35 public:
36 /// @brief The value type produced by this distribution
37 using result_type = T;
38
39 private:
40 /// @brief Assert that the PRNG being used covers its type's whole range, since that's all our code currently handles
41 /// @tparam BitGenerator A bit generator (PRNG) type satisfying UniformRandomBitGenerator
42 template <typename BitGenerator>
43 static constexpr auto assert_generator_is_ok() noexcept -> void {
44 using bit_generator_result_type = typename BitGenerator::result_type;
45 static_assert(
47 "BitGenerator::result_type must be unsigned integral for BitGenerator to satisfy UniformRandomBitGenerator"
48 );
49 static_assert(
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"
52 );
53 static_assert(
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"
56 );
57 }
58
59 /// @brief Use a PRNG to generate a requested number of bits; overload for when the PRNG @c result_type is big enough
60 /// @tparam ExpectedBitsType An unsigned integer type that's bit enough to be used for the subsequent type conversion
61 /// @tparam BitGenerator A bit generator (PRNG) type satisfying UniformRandomBitGenerator
62 /// @param prng A bit generator (PRNG) instance
63 /// @return A uniformly-distributed random sequence of bits which can be converted into @c T
64 template <
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>();
71
72 return static_cast<ExpectedBitsType>(prng());
73 }
74
75 /// @brief Use a PRNG to generate a requested number of bits; overload for when the PRNG @c result_type is too small
76 /// @tparam ExpectedBitsType An unsigned integer type that's bit enough to be used for the subsequent type conversion
77 /// @tparam BitGenerator A bit generator (PRNG) type satisfying UniformRandomBitGenerator
78 /// @param prng A bit generator (PRNG) instance
79 /// @return A uniformly-distributed random sequence of bits which can be converted into @c T
80 template <
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>();
87
88 using chunk_type = typename BitGenerator::result_type;
89 static_assert(
90 sizeof(ExpectedBitsType) % sizeof(chunk_type) == 0U,
91 "Implementation assumes that an integer number of chunks fits in ExpectedBitsType"
92 );
93
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))
99 );
100 }
101
102 return result_bits;
103 }
104
105 public:
106 /// @brief Generate a value of type <c>T</c>; overload for when T is integral
107 /// @tparam BitGenerator A bit generator (PRNG) type satisfying UniformRandomBitGenerator
108 /// @param prng A bit generator (PRNG) instance
109 /// @return A random, uniformly-distributed @c T value
110 template <
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));
115 }
116};
117
118/// @brief A uniform distribution to apply on top of a bit generator. SFINAE overload for floating-point types.
119/// @tparam T The value type to generate
120template <typename T>
122 T,
123 arene::base::constraints<std::enable_if_t<std::is_same<T, float>::value || std::is_same<T, double>::value>>> {
124 public:
125 /// @brief The value type produced by this distribution
126 using result_type = T;
127
128 /// @brief Generate a value of type @c T
129 /// @tparam BitGenerator A bit generator (PRNG) type satisfying UniformRandomBitGenerator
130 /// @param prng A bit generator (PRNG) instance
131 /// @return A random, bitwise-uniformly-distributed @c T value
132 template <
133 typename BitGenerator,
135 ARENE_BIT_CAST_CONSTEXPR auto operator()(BitGenerator& prng) const noexcept(noexcept(prng())) -> T {
138 return arene::base::bit_cast<T>(byte_dist(prng));
139 }
140};
141
142/// @brief A uniform distribution to apply on top of a bit generator. SFINAE overload for complex types.
143/// @tparam T The value type to generate
144template <typename T>
145class bitwise_uniform_distribution<T, arene::base::constraints<std::enable_if_t<arene::base::math::is_complex_v<T>>>> {
146 public:
147 /// @brief The value type produced by this distribution
148 using result_type = T;
149
150 /// @brief Generate a value of type @c T
151 /// @tparam BitGenerator A bit generator (PRNG) type satisfying UniformRandomBitGenerator
152 /// @param prng A bit generator (PRNG) instance
153 /// @return A random, bitwise-uniformly-distributed @c T value
154 template <
155 typename BitGenerator,
157 constexpr auto operator()(BitGenerator& prng) const noexcept(noexcept(prng())) -> T {
159
161 return T{component_dist(prng), component_dist(prng)};
162 }
163};
164
165} // namespace testing
166} // namespace base
167} // namespace arene
168
169#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TESTING_BITWISE_UNIFORM_DISTRIBUTION_HPP_
#define ARENE_BIT_CAST_CONSTEXPR
Definition bit_cast.hpp:31
constexpr auto operator()(BitGenerator &prng) const noexcept(noexcept(prng())) -> T
Generate a value of type T.
Definition bitwise_uniform_distribution.hpp:157
T result_type
The value type produced by this distribution.
Definition bitwise_uniform_distribution.hpp:148
ARENE_BIT_CAST_CONSTEXPR auto operator()(BitGenerator &prng) const noexcept(noexcept(prng())) -> T
Generate a value of type T.
Definition bitwise_uniform_distribution.hpp:135
constexpr auto operator()(BitGenerator &prng) const noexcept(noexcept(prng())) -> T
Generate a value of type T; overload for when T is integral.
Definition bitwise_uniform_distribution.hpp:113
T result_type
The value type produced by this distribution.
Definition bitwise_uniform_distribution.hpp:37
A uniform distribution to apply on top of a bit generator. Primary template (deliberately undefined).
Definition bitwise_uniform_distribution.hpp:29
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