Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / include / __random / independent_bits_engine.h
blob376524722d997a0013e2dc92609cca0e03a4822d
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 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___RANDOM_INDEPENDENT_BITS_ENGINE_H
10 #define _LIBCPP___RANDOM_INDEPENDENT_BITS_ENGINE_H
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>
24 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
25 # pragma GCC system_header
26 #endif
28 _LIBCPP_PUSH_MACROS
29 #include <__undef_macros>
31 _LIBCPP_BEGIN_NAMESPACE_STD
33 template<class _Engine, size_t __w, class _UIntType>
34 class _LIBCPP_TEMPLATE_VIS independent_bits_engine
36 template <class _UInt, _UInt _R0, size_t _Wp, size_t _Mp>
37 class __get_n
39 static _LIBCPP_CONSTEXPR const size_t _Dt = numeric_limits<_UInt>::digits;
40 static _LIBCPP_CONSTEXPR const size_t _Np = _Wp / _Mp + (_Wp % _Mp != 0);
41 static _LIBCPP_CONSTEXPR const size_t _W0 = _Wp / _Np;
42 static _LIBCPP_CONSTEXPR const _UInt _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0;
43 public:
44 static _LIBCPP_CONSTEXPR const size_t value = _R0 - _Y0 > _Y0 / _Np ? _Np + 1 : _Np;
46 public:
47 // types
48 typedef _UIntType result_type;
50 private:
51 _Engine __e_;
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");
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
62 + _Working_result_type(1);
63 #else
64 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
65 + _Working_result_type(1);
66 #endif
67 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
68 static _LIBCPP_CONSTEXPR const size_t __n = __get_n<_Working_result_type, _Rp, __w, __m>::value;
69 static _LIBCPP_CONSTEXPR const size_t __w0 = __w / __n;
70 static _LIBCPP_CONSTEXPR const size_t __n0 = __n - __w % __n;
71 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
72 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
73 static _LIBCPP_CONSTEXPR const _Working_result_type __y0 = __w0 >= _WDt ? 0 :
74 (_Rp >> __w0) << __w0;
75 static _LIBCPP_CONSTEXPR const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 :
76 (_Rp >> (__w0+1)) << (__w0+1);
77 static _LIBCPP_CONSTEXPR const _Engine_result_type __mask0 = __w0 > 0 ?
78 _Engine_result_type(~0) >> (_EDt - __w0) :
79 _Engine_result_type(0);
80 static _LIBCPP_CONSTEXPR const _Engine_result_type __mask1 = __w0 < _EDt - 1 ?
81 _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) :
82 _Engine_result_type(~0);
83 public:
84 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
85 static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
86 (result_type(1) << __w) - result_type(1);
87 static_assert(_Min < _Max, "independent_bits_engine invalid parameters");
89 // engine characteristics
90 _LIBCPP_INLINE_VISIBILITY
91 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
92 _LIBCPP_INLINE_VISIBILITY
93 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
95 // constructors and seeding functions
96 _LIBCPP_INLINE_VISIBILITY
97 independent_bits_engine() {}
98 _LIBCPP_INLINE_VISIBILITY
99 explicit independent_bits_engine(const _Engine& __e)
100 : __e_(__e) {}
101 #ifndef _LIBCPP_CXX03_LANG
102 _LIBCPP_INLINE_VISIBILITY
103 explicit independent_bits_engine(_Engine&& __e)
104 : __e_(_VSTD::move(__e)) {}
105 #endif // _LIBCPP_CXX03_LANG
106 _LIBCPP_INLINE_VISIBILITY
107 explicit independent_bits_engine(result_type __sd) : __e_(__sd) {}
108 template<class _Sseq, __enable_if_t<__is_seed_sequence<_Sseq, independent_bits_engine>::value &&
109 !is_convertible<_Sseq, _Engine>::value, int> = 0>
110 _LIBCPP_INLINE_VISIBILITY
111 explicit independent_bits_engine(_Sseq& __q)
112 : __e_(__q) {}
113 _LIBCPP_INLINE_VISIBILITY
114 void seed() {__e_.seed();}
115 _LIBCPP_INLINE_VISIBILITY
116 void seed(result_type __sd) {__e_.seed(__sd);}
117 template<class _Sseq, __enable_if_t<__is_seed_sequence<_Sseq, independent_bits_engine>::value, int> = 0>
118 _LIBCPP_INLINE_VISIBILITY
119 void
120 seed(_Sseq& __q) {__e_.seed(__q);}
122 // generating functions
123 _LIBCPP_INLINE_VISIBILITY
124 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
125 _LIBCPP_INLINE_VISIBILITY
126 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
128 // property functions
129 _LIBCPP_INLINE_VISIBILITY
130 const _Engine& base() const _NOEXCEPT {return __e_;}
132 template<class _Eng, size_t _Wp, class _UInt>
133 friend
134 bool
135 operator==(
136 const independent_bits_engine<_Eng, _Wp, _UInt>& __x,
137 const independent_bits_engine<_Eng, _Wp, _UInt>& __y);
139 template<class _Eng, size_t _Wp, class _UInt>
140 friend
141 bool
142 operator!=(
143 const independent_bits_engine<_Eng, _Wp, _UInt>& __x,
144 const independent_bits_engine<_Eng, _Wp, _UInt>& __y);
146 template <class _CharT, class _Traits,
147 class _Eng, size_t _Wp, class _UInt>
148 friend
149 basic_ostream<_CharT, _Traits>&
150 operator<<(basic_ostream<_CharT, _Traits>& __os,
151 const independent_bits_engine<_Eng, _Wp, _UInt>& __x);
153 template <class _CharT, class _Traits,
154 class _Eng, size_t _Wp, class _UInt>
155 friend
156 basic_istream<_CharT, _Traits>&
157 operator>>(basic_istream<_CharT, _Traits>& __is,
158 independent_bits_engine<_Eng, _Wp, _UInt>& __x);
160 private:
161 _LIBCPP_INLINE_VISIBILITY
162 result_type __eval(false_type);
163 _LIBCPP_HIDE_FROM_ABI result_type __eval(true_type);
165 template <size_t __count, __enable_if_t<__count < _Dt, int> = 0>
166 _LIBCPP_INLINE_VISIBILITY
167 static
168 result_type
169 __lshift(result_type __x) {return __x << __count;}
171 template <size_t __count, __enable_if_t<(__count >= _Dt), int> = 0>
172 _LIBCPP_INLINE_VISIBILITY
173 static
174 result_type
175 __lshift(result_type) {return result_type(0);}
178 template<class _Engine, size_t __w, class _UIntType>
179 inline
180 _UIntType
181 independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type)
183 return static_cast<result_type>(__e_() & __mask0);
186 template<class _Engine, size_t __w, class _UIntType>
187 _UIntType
188 independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type)
190 result_type __sp = 0;
191 for (size_t __k = 0; __k < __n0; ++__k)
193 _Engine_result_type __u;
196 __u = __e_() - _Engine::min();
197 } while (__u >= __y0);
198 __sp = static_cast<result_type>(__lshift<__w0>(__sp) + (__u & __mask0));
200 for (size_t __k = __n0; __k < __n; ++__k)
202 _Engine_result_type __u;
205 __u = __e_() - _Engine::min();
206 } while (__u >= __y1);
207 __sp = static_cast<result_type>(__lshift<__w0+1>(__sp) + (__u & __mask1));
209 return __sp;
212 template<class _Eng, size_t _Wp, class _UInt>
213 inline _LIBCPP_INLINE_VISIBILITY
214 bool
215 operator==(
216 const independent_bits_engine<_Eng, _Wp, _UInt>& __x,
217 const independent_bits_engine<_Eng, _Wp, _UInt>& __y)
219 return __x.base() == __y.base();
222 template<class _Eng, size_t _Wp, class _UInt>
223 inline _LIBCPP_INLINE_VISIBILITY
224 bool
225 operator!=(
226 const independent_bits_engine<_Eng, _Wp, _UInt>& __x,
227 const independent_bits_engine<_Eng, _Wp, _UInt>& __y)
229 return !(__x == __y);
232 template <class _CharT, class _Traits,
233 class _Eng, size_t _Wp, class _UInt>
234 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
235 operator<<(basic_ostream<_CharT, _Traits>& __os,
236 const independent_bits_engine<_Eng, _Wp, _UInt>& __x)
238 return __os << __x.base();
241 template <class _CharT, class _Traits,
242 class _Eng, size_t _Wp, class _UInt>
243 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
244 operator>>(basic_istream<_CharT, _Traits>& __is,
245 independent_bits_engine<_Eng, _Wp, _UInt>& __x)
247 _Eng __e;
248 __is >> __e;
249 if (!__is.fail())
250 __x.__e_ = __e;
251 return __is;
254 _LIBCPP_END_NAMESPACE_STD
256 _LIBCPP_POP_MACROS
258 #endif // _LIBCPP___RANDOM_INDEPENDENT_BITS_ENGINE_H