independent_bits_engine.h 9.2 KB

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