independent_bits_engine.h 9.2 KB

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