independent_bits_engine.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. #ifndef _LIBCPP___RANDOM_INDEPENDENT_BITS_ENGINE_H
  9. #define _LIBCPP___RANDOM_INDEPENDENT_BITS_ENGINE_H
  10. #include <__config>
  11. #include <__random/is_seed_sequence.h>
  12. #include <__random/log2.h>
  13. #include <__utility/move.h>
  14. #include <iosfwd>
  15. #include <limits>
  16. #include <type_traits>
  17. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  18. # pragma GCC system_header
  19. #endif
  20. _LIBCPP_PUSH_MACROS
  21. #include <__undef_macros>
  22. _LIBCPP_BEGIN_NAMESPACE_STD
  23. template<class _Engine, size_t __w, class _UIntType>
  24. class _LIBCPP_TEMPLATE_VIS independent_bits_engine
  25. {
  26. template <class _UInt, _UInt _R0, size_t _Wp, size_t _Mp>
  27. class __get_n
  28. {
  29. static _LIBCPP_CONSTEXPR const size_t _Dt = numeric_limits<_UInt>::digits;
  30. static _LIBCPP_CONSTEXPR const size_t _Np = _Wp / _Mp + (_Wp % _Mp != 0);
  31. static _LIBCPP_CONSTEXPR const size_t _W0 = _Wp / _Np;
  32. static _LIBCPP_CONSTEXPR const _UInt _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0;
  33. public:
  34. static _LIBCPP_CONSTEXPR const size_t value = _R0 - _Y0 > _Y0 / _Np ? _Np + 1 : _Np;
  35. };
  36. public:
  37. // types
  38. typedef _UIntType result_type;
  39. private:
  40. _Engine __e_;
  41. static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
  42. static_assert( 0 < __w, "independent_bits_engine invalid parameters");
  43. static_assert(__w <= _Dt, "independent_bits_engine invalid parameters");
  44. typedef typename _Engine::result_type _Engine_result_type;
  45. typedef __conditional_t<sizeof(_Engine_result_type) <= sizeof(result_type), result_type, _Engine_result_type>
  46. _Working_result_type;
  47. #ifdef _LIBCPP_CXX03_LANG
  48. static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
  49. + _Working_result_type(1);
  50. #else
  51. static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
  52. + _Working_result_type(1);
  53. #endif
  54. static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
  55. static _LIBCPP_CONSTEXPR const size_t __n = __get_n<_Working_result_type, _Rp, __w, __m>::value;
  56. static _LIBCPP_CONSTEXPR const size_t __w0 = __w / __n;
  57. static _LIBCPP_CONSTEXPR const size_t __n0 = __n - __w % __n;
  58. static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
  59. static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
  60. static _LIBCPP_CONSTEXPR const _Working_result_type __y0 = __w0 >= _WDt ? 0 :
  61. (_Rp >> __w0) << __w0;
  62. static _LIBCPP_CONSTEXPR const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 :
  63. (_Rp >> (__w0+1)) << (__w0+1);
  64. static _LIBCPP_CONSTEXPR const _Engine_result_type __mask0 = __w0 > 0 ?
  65. _Engine_result_type(~0) >> (_EDt - __w0) :
  66. _Engine_result_type(0);
  67. static _LIBCPP_CONSTEXPR const _Engine_result_type __mask1 = __w0 < _EDt - 1 ?
  68. _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) :
  69. _Engine_result_type(~0);
  70. public:
  71. static _LIBCPP_CONSTEXPR const result_type _Min = 0;
  72. static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
  73. (result_type(1) << __w) - result_type(1);
  74. static_assert(_Min < _Max, "independent_bits_engine invalid parameters");
  75. // engine characteristics
  76. _LIBCPP_INLINE_VISIBILITY
  77. static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
  78. _LIBCPP_INLINE_VISIBILITY
  79. static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
  80. // constructors and seeding functions
  81. _LIBCPP_INLINE_VISIBILITY
  82. independent_bits_engine() {}
  83. _LIBCPP_INLINE_VISIBILITY
  84. explicit independent_bits_engine(const _Engine& __e)
  85. : __e_(__e) {}
  86. #ifndef _LIBCPP_CXX03_LANG
  87. _LIBCPP_INLINE_VISIBILITY
  88. explicit independent_bits_engine(_Engine&& __e)
  89. : __e_(_VSTD::move(__e)) {}
  90. #endif // _LIBCPP_CXX03_LANG
  91. _LIBCPP_INLINE_VISIBILITY
  92. explicit independent_bits_engine(result_type __sd) : __e_(__sd) {}
  93. template<class _Sseq>
  94. _LIBCPP_INLINE_VISIBILITY
  95. explicit independent_bits_engine(_Sseq& __q,
  96. typename enable_if<__is_seed_sequence<_Sseq, independent_bits_engine>::value &&
  97. !is_convertible<_Sseq, _Engine>::value>::type* = 0)
  98. : __e_(__q) {}
  99. _LIBCPP_INLINE_VISIBILITY
  100. void seed() {__e_.seed();}
  101. _LIBCPP_INLINE_VISIBILITY
  102. void seed(result_type __sd) {__e_.seed(__sd);}
  103. template<class _Sseq>
  104. _LIBCPP_INLINE_VISIBILITY
  105. typename enable_if
  106. <
  107. __is_seed_sequence<_Sseq, independent_bits_engine>::value,
  108. void
  109. >::type
  110. seed(_Sseq& __q) {__e_.seed(__q);}
  111. // generating functions
  112. _LIBCPP_INLINE_VISIBILITY
  113. result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
  114. _LIBCPP_INLINE_VISIBILITY
  115. void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
  116. // property functions
  117. _LIBCPP_INLINE_VISIBILITY
  118. const _Engine& base() const _NOEXCEPT {return __e_;}
  119. template<class _Eng, size_t _Wp, class _UInt>
  120. friend
  121. bool
  122. operator==(
  123. const independent_bits_engine<_Eng, _Wp, _UInt>& __x,
  124. const independent_bits_engine<_Eng, _Wp, _UInt>& __y);
  125. template<class _Eng, size_t _Wp, class _UInt>
  126. friend
  127. bool
  128. operator!=(
  129. const independent_bits_engine<_Eng, _Wp, _UInt>& __x,
  130. const independent_bits_engine<_Eng, _Wp, _UInt>& __y);
  131. template <class _CharT, class _Traits,
  132. class _Eng, size_t _Wp, class _UInt>
  133. friend
  134. basic_ostream<_CharT, _Traits>&
  135. operator<<(basic_ostream<_CharT, _Traits>& __os,
  136. const independent_bits_engine<_Eng, _Wp, _UInt>& __x);
  137. template <class _CharT, class _Traits,
  138. class _Eng, size_t _Wp, class _UInt>
  139. friend
  140. basic_istream<_CharT, _Traits>&
  141. operator>>(basic_istream<_CharT, _Traits>& __is,
  142. independent_bits_engine<_Eng, _Wp, _UInt>& __x);
  143. private:
  144. _LIBCPP_INLINE_VISIBILITY
  145. result_type __eval(false_type);
  146. result_type __eval(true_type);
  147. template <size_t __count>
  148. _LIBCPP_INLINE_VISIBILITY
  149. static
  150. typename enable_if
  151. <
  152. __count < _Dt,
  153. result_type
  154. >::type
  155. __lshift(result_type __x) {return __x << __count;}
  156. template <size_t __count>
  157. _LIBCPP_INLINE_VISIBILITY
  158. static
  159. typename enable_if
  160. <
  161. (__count >= _Dt),
  162. result_type
  163. >::type
  164. __lshift(result_type) {return result_type(0);}
  165. };
  166. template<class _Engine, size_t __w, class _UIntType>
  167. inline
  168. _UIntType
  169. independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type)
  170. {
  171. return static_cast<result_type>(__e_() & __mask0);
  172. }
  173. template<class _Engine, size_t __w, class _UIntType>
  174. _UIntType
  175. independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type)
  176. {
  177. result_type _Sp = 0;
  178. for (size_t __k = 0; __k < __n0; ++__k)
  179. {
  180. _Engine_result_type __u;
  181. do
  182. {
  183. __u = __e_() - _Engine::min();
  184. } while (__u >= __y0);
  185. _Sp = static_cast<result_type>(__lshift<__w0>(_Sp) + (__u & __mask0));
  186. }
  187. for (size_t __k = __n0; __k < __n; ++__k)
  188. {
  189. _Engine_result_type __u;
  190. do
  191. {
  192. __u = __e_() - _Engine::min();
  193. } while (__u >= __y1);
  194. _Sp = static_cast<result_type>(__lshift<__w0+1>(_Sp) + (__u & __mask1));
  195. }
  196. return _Sp;
  197. }
  198. template<class _Eng, size_t _Wp, class _UInt>
  199. inline _LIBCPP_INLINE_VISIBILITY
  200. bool
  201. operator==(
  202. const independent_bits_engine<_Eng, _Wp, _UInt>& __x,
  203. const independent_bits_engine<_Eng, _Wp, _UInt>& __y)
  204. {
  205. return __x.base() == __y.base();
  206. }
  207. template<class _Eng, size_t _Wp, class _UInt>
  208. inline _LIBCPP_INLINE_VISIBILITY
  209. bool
  210. operator!=(
  211. const independent_bits_engine<_Eng, _Wp, _UInt>& __x,
  212. const independent_bits_engine<_Eng, _Wp, _UInt>& __y)
  213. {
  214. return !(__x == __y);
  215. }
  216. template <class _CharT, class _Traits,
  217. class _Eng, size_t _Wp, class _UInt>
  218. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  219. operator<<(basic_ostream<_CharT, _Traits>& __os,
  220. const independent_bits_engine<_Eng, _Wp, _UInt>& __x)
  221. {
  222. return __os << __x.base();
  223. }
  224. template <class _CharT, class _Traits,
  225. class _Eng, size_t _Wp, class _UInt>
  226. _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
  227. operator>>(basic_istream<_CharT, _Traits>& __is,
  228. independent_bits_engine<_Eng, _Wp, _UInt>& __x)
  229. {
  230. _Eng __e;
  231. __is >> __e;
  232. if (!__is.fail())
  233. __x.__e_ = __e;
  234. return __is;
  235. }
  236. _LIBCPP_END_NAMESPACE_STD
  237. _LIBCPP_POP_MACROS
  238. #endif // _LIBCPP___RANDOM_INDEPENDENT_BITS_ENGINE_H