xref: /freebsd/contrib/llvm-project/libcxx/include/__random/independent_bits_engine.h (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___RANDOM_INDEPENDENT_BITS_ENGINE_H
10 #define _LIBCPP___RANDOM_INDEPENDENT_BITS_ENGINE_H
11 
12 #include <__config>
13 #include <__fwd/istream.h>
14 #include <__fwd/ostream.h>
15 #include <__random/is_seed_sequence.h>
16 #include <__random/log2.h>
17 #include <__type_traits/conditional.h>
18 #include <__type_traits/enable_if.h>
19 #include <__type_traits/is_convertible.h>
20 #include <__utility/move.h>
21 #include <cstddef>
22 #include <limits>
23 
24 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
25 #  pragma GCC system_header
26 #endif
27 
28 _LIBCPP_PUSH_MACROS
29 #include <__undef_macros>
30 
31 _LIBCPP_BEGIN_NAMESPACE_STD
32 
33 template <class _Engine, size_t __w, class _UIntType>
34 class _LIBCPP_TEMPLATE_VIS independent_bits_engine {
35   template <class _UInt, _UInt _R0, size_t _Wp, size_t _Mp>
36   class __get_n {
37     static _LIBCPP_CONSTEXPR const size_t _Dt = numeric_limits<_UInt>::digits;
38     static _LIBCPP_CONSTEXPR const size_t _Np = _Wp / _Mp + (_Wp % _Mp != 0);
39     static _LIBCPP_CONSTEXPR const size_t _W0 = _Wp / _Np;
40     static _LIBCPP_CONSTEXPR const _UInt _Y0  = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0;
41 
42   public:
43     static _LIBCPP_CONSTEXPR const size_t value = _R0 - _Y0 > _Y0 / _Np ? _Np + 1 : _Np;
44   };
45 
46 public:
47   // types
48   typedef _UIntType result_type;
49 
50 private:
51   _Engine __e_;
52 
53   static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
54   static_assert(0 < __w, "independent_bits_engine invalid parameters");
55   static_assert(__w <= _Dt, "independent_bits_engine invalid parameters");
56 
57   typedef typename _Engine::result_type _Engine_result_type;
58   typedef __conditional_t<sizeof(_Engine_result_type) <= sizeof(result_type), result_type, _Engine_result_type>
59       _Working_result_type;
60 #ifdef _LIBCPP_CXX03_LANG
61   static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min + _Working_result_type(1);
62 #else
63   static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min() + _Working_result_type(1);
64 #endif
65   static _LIBCPP_CONSTEXPR const size_t __m                = __log2<_Working_result_type, _Rp>::value;
66   static _LIBCPP_CONSTEXPR const size_t __n                = __get_n<_Working_result_type, _Rp, __w, __m>::value;
67   static _LIBCPP_CONSTEXPR const size_t __w0               = __w / __n;
68   static _LIBCPP_CONSTEXPR const size_t __n0               = __n - __w % __n;
69   static _LIBCPP_CONSTEXPR const size_t _WDt               = numeric_limits<_Working_result_type>::digits;
70   static _LIBCPP_CONSTEXPR const size_t _EDt               = numeric_limits<_Engine_result_type>::digits;
71   static _LIBCPP_CONSTEXPR const _Working_result_type __y0 = __w0 >= _WDt ? 0 : (_Rp >> __w0) << __w0;
72   static _LIBCPP_CONSTEXPR const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 : (_Rp >> (__w0 + 1)) << (__w0 + 1);
73   static _LIBCPP_CONSTEXPR const
74       _Engine_result_type __mask0 = __w0 > 0 ? _Engine_result_type(~0) >> (_EDt - __w0) : _Engine_result_type(0);
75   static _LIBCPP_CONSTEXPR const _Engine_result_type __mask1 =
76       __w0 < _EDt - 1 ? _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) : _Engine_result_type(~0);
77 
78 public:
79   static _LIBCPP_CONSTEXPR const result_type _Min = 0;
80   static _LIBCPP_CONSTEXPR const result_type _Max =
81       __w == _Dt ? result_type(~0) : (result_type(1) << __w) - result_type(1);
82   static_assert(_Min < _Max, "independent_bits_engine invalid parameters");
83 
84   // engine characteristics
85   _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
86   _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
87 
88   // constructors and seeding functions
89   _LIBCPP_HIDE_FROM_ABI independent_bits_engine() {}
90   _LIBCPP_HIDE_FROM_ABI explicit independent_bits_engine(const _Engine& __e) : __e_(__e) {}
91 #ifndef _LIBCPP_CXX03_LANG
92   _LIBCPP_HIDE_FROM_ABI explicit independent_bits_engine(_Engine&& __e) : __e_(std::move(__e)) {}
93 #endif // _LIBCPP_CXX03_LANG
94   _LIBCPP_HIDE_FROM_ABI explicit independent_bits_engine(result_type __sd) : __e_(__sd) {}
95   template <
96       class _Sseq,
97       __enable_if_t<__is_seed_sequence<_Sseq, independent_bits_engine>::value && !is_convertible<_Sseq, _Engine>::value,
98                     int> = 0>
99   _LIBCPP_HIDE_FROM_ABI explicit independent_bits_engine(_Sseq& __q) : __e_(__q) {}
100   _LIBCPP_HIDE_FROM_ABI void seed() { __e_.seed(); }
101   _LIBCPP_HIDE_FROM_ABI void seed(result_type __sd) { __e_.seed(__sd); }
102   template <class _Sseq, __enable_if_t<__is_seed_sequence<_Sseq, independent_bits_engine>::value, int> = 0>
103   _LIBCPP_HIDE_FROM_ABI void seed(_Sseq& __q) {
104     __e_.seed(__q);
105   }
106 
107   // generating functions
108   _LIBCPP_HIDE_FROM_ABI result_type operator()() { return __eval(integral_constant<bool, _Rp != 0>()); }
109   _LIBCPP_HIDE_FROM_ABI void discard(unsigned long long __z) {
110     for (; __z; --__z)
111       operator()();
112   }
113 
114   // property functions
115   _LIBCPP_HIDE_FROM_ABI const _Engine& base() const _NOEXCEPT { return __e_; }
116 
117   template <class _Eng, size_t _Wp, class _UInt>
118   friend bool operator==(const independent_bits_engine<_Eng, _Wp, _UInt>& __x,
119                          const independent_bits_engine<_Eng, _Wp, _UInt>& __y);
120 
121   template <class _Eng, size_t _Wp, class _UInt>
122   friend bool operator!=(const independent_bits_engine<_Eng, _Wp, _UInt>& __x,
123                          const independent_bits_engine<_Eng, _Wp, _UInt>& __y);
124 
125   template <class _CharT, class _Traits, class _Eng, size_t _Wp, class _UInt>
126   friend basic_ostream<_CharT, _Traits>&
127   operator<<(basic_ostream<_CharT, _Traits>& __os, const independent_bits_engine<_Eng, _Wp, _UInt>& __x);
128 
129   template <class _CharT, class _Traits, class _Eng, size_t _Wp, class _UInt>
130   friend basic_istream<_CharT, _Traits>&
131   operator>>(basic_istream<_CharT, _Traits>& __is, independent_bits_engine<_Eng, _Wp, _UInt>& __x);
132 
133 private:
134   _LIBCPP_HIDE_FROM_ABI result_type __eval(false_type);
135   _LIBCPP_HIDE_FROM_ABI result_type __eval(true_type);
136 
137   template <size_t __count,
138             __enable_if_t<__count< _Dt, int> = 0> _LIBCPP_HIDE_FROM_ABI static result_type __lshift(result_type __x) {
139     return __x << __count;
140   }
141 
142   template <size_t __count, __enable_if_t<(__count >= _Dt), int> = 0>
143   _LIBCPP_HIDE_FROM_ABI static result_type __lshift(result_type) {
144     return result_type(0);
145   }
146 };
147 
148 template <class _Engine, size_t __w, class _UIntType>
149 inline _UIntType independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type) {
150   return static_cast<result_type>(__e_() & __mask0);
151 }
152 
153 template <class _Engine, size_t __w, class _UIntType>
154 _UIntType independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type) {
155   result_type __sp = 0;
156   for (size_t __k = 0; __k < __n0; ++__k) {
157     _Engine_result_type __u;
158     do {
159       __u = __e_() - _Engine::min();
160     } while (__u >= __y0);
161     __sp = static_cast<result_type>(__lshift<__w0>(__sp) + (__u & __mask0));
162   }
163   for (size_t __k = __n0; __k < __n; ++__k) {
164     _Engine_result_type __u;
165     do {
166       __u = __e_() - _Engine::min();
167     } while (__u >= __y1);
168     __sp = static_cast<result_type>(__lshift<__w0 + 1>(__sp) + (__u & __mask1));
169   }
170   return __sp;
171 }
172 
173 template <class _Eng, size_t _Wp, class _UInt>
174 inline _LIBCPP_HIDE_FROM_ABI bool
175 operator==(const independent_bits_engine<_Eng, _Wp, _UInt>& __x, const independent_bits_engine<_Eng, _Wp, _UInt>& __y) {
176   return __x.base() == __y.base();
177 }
178 
179 template <class _Eng, size_t _Wp, class _UInt>
180 inline _LIBCPP_HIDE_FROM_ABI bool
181 operator!=(const independent_bits_engine<_Eng, _Wp, _UInt>& __x, const independent_bits_engine<_Eng, _Wp, _UInt>& __y) {
182   return !(__x == __y);
183 }
184 
185 template <class _CharT, class _Traits, class _Eng, size_t _Wp, class _UInt>
186 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
187 operator<<(basic_ostream<_CharT, _Traits>& __os, const independent_bits_engine<_Eng, _Wp, _UInt>& __x) {
188   return __os << __x.base();
189 }
190 
191 template <class _CharT, class _Traits, class _Eng, size_t _Wp, class _UInt>
192 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
193 operator>>(basic_istream<_CharT, _Traits>& __is, independent_bits_engine<_Eng, _Wp, _UInt>& __x) {
194   _Eng __e;
195   __is >> __e;
196   if (!__is.fail())
197     __x.__e_ = __e;
198   return __is;
199 }
200 
201 _LIBCPP_END_NAMESPACE_STD
202 
203 _LIBCPP_POP_MACROS
204 
205 #endif // _LIBCPP___RANDOM_INDEPENDENT_BITS_ENGINE_H
206