mersenne_twister_engine.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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_MERSENNE_TWISTER_ENGINE_H
  9. #define _LIBCPP___RANDOM_MERSENNE_TWISTER_ENGINE_H
  10. #include <__algorithm/equal.h>
  11. #include <__algorithm/min.h>
  12. #include <__config>
  13. #include <__random/is_seed_sequence.h>
  14. #include <cstddef>
  15. #include <cstdint>
  16. #include <iosfwd>
  17. #include <limits>
  18. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  19. # pragma GCC system_header
  20. #endif
  21. _LIBCPP_PUSH_MACROS
  22. #include <__undef_macros>
  23. _LIBCPP_BEGIN_NAMESPACE_STD
  24. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  25. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  26. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  27. class _LIBCPP_TEMPLATE_VIS mersenne_twister_engine;
  28. template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
  29. _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
  30. _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
  31. _LIBCPP_HIDE_FROM_ABI bool
  32. operator==(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  33. _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
  34. const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  35. _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
  36. template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
  37. _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
  38. _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
  39. _LIBCPP_INLINE_VISIBILITY
  40. bool
  41. operator!=(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  42. _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
  43. const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  44. _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
  45. template <class _CharT, class _Traits,
  46. class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
  47. _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
  48. _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
  49. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  50. operator<<(basic_ostream<_CharT, _Traits>& __os,
  51. const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  52. _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
  53. template <class _CharT, class _Traits,
  54. class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
  55. _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
  56. _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
  57. _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
  58. operator>>(basic_istream<_CharT, _Traits>& __is,
  59. mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  60. _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
  61. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  62. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  63. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  64. class _LIBCPP_TEMPLATE_VIS mersenne_twister_engine
  65. {
  66. public:
  67. // types
  68. typedef _UIntType result_type;
  69. private:
  70. result_type __x_[__n];
  71. size_t __i_;
  72. static_assert( 0 < __m, "mersenne_twister_engine invalid parameters");
  73. static_assert(__m <= __n, "mersenne_twister_engine invalid parameters");
  74. static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
  75. static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters");
  76. static_assert( 2 <= __w, "mersenne_twister_engine invalid parameters");
  77. static_assert(__r <= __w, "mersenne_twister_engine invalid parameters");
  78. static_assert(__u <= __w, "mersenne_twister_engine invalid parameters");
  79. static_assert(__s <= __w, "mersenne_twister_engine invalid parameters");
  80. static_assert(__t <= __w, "mersenne_twister_engine invalid parameters");
  81. static_assert(__l <= __w, "mersenne_twister_engine invalid parameters");
  82. public:
  83. static _LIBCPP_CONSTEXPR const result_type _Min = 0;
  84. static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
  85. (result_type(1) << __w) - result_type(1);
  86. static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters");
  87. static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters");
  88. static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters");
  89. static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters");
  90. static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters");
  91. static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters");
  92. // engine characteristics
  93. static _LIBCPP_CONSTEXPR const size_t word_size = __w;
  94. static _LIBCPP_CONSTEXPR const size_t state_size = __n;
  95. static _LIBCPP_CONSTEXPR const size_t shift_size = __m;
  96. static _LIBCPP_CONSTEXPR const size_t mask_bits = __r;
  97. static _LIBCPP_CONSTEXPR const result_type xor_mask = __a;
  98. static _LIBCPP_CONSTEXPR const size_t tempering_u = __u;
  99. static _LIBCPP_CONSTEXPR const result_type tempering_d = __d;
  100. static _LIBCPP_CONSTEXPR const size_t tempering_s = __s;
  101. static _LIBCPP_CONSTEXPR const result_type tempering_b = __b;
  102. static _LIBCPP_CONSTEXPR const size_t tempering_t = __t;
  103. static _LIBCPP_CONSTEXPR const result_type tempering_c = __c;
  104. static _LIBCPP_CONSTEXPR const size_t tempering_l = __l;
  105. static _LIBCPP_CONSTEXPR const result_type initialization_multiplier = __f;
  106. _LIBCPP_INLINE_VISIBILITY
  107. static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
  108. _LIBCPP_INLINE_VISIBILITY
  109. static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
  110. static _LIBCPP_CONSTEXPR const result_type default_seed = 5489u;
  111. // constructors and seeding functions
  112. #ifndef _LIBCPP_CXX03_LANG
  113. _LIBCPP_INLINE_VISIBILITY
  114. mersenne_twister_engine() : mersenne_twister_engine(default_seed) {}
  115. _LIBCPP_INLINE_VISIBILITY
  116. explicit mersenne_twister_engine(result_type __sd) { seed(__sd); }
  117. #else
  118. _LIBCPP_INLINE_VISIBILITY
  119. explicit mersenne_twister_engine(result_type __sd = default_seed) {
  120. seed(__sd);
  121. }
  122. #endif
  123. template<class _Sseq, __enable_if_t<__is_seed_sequence<_Sseq, mersenne_twister_engine>::value, int> = 0>
  124. _LIBCPP_INLINE_VISIBILITY
  125. explicit mersenne_twister_engine(_Sseq& __q)
  126. {seed(__q);}
  127. _LIBCPP_HIDE_FROM_ABI void seed(result_type __sd = default_seed);
  128. template<class _Sseq, __enable_if_t<__is_seed_sequence<_Sseq, mersenne_twister_engine>::value, int> = 0>
  129. _LIBCPP_INLINE_VISIBILITY
  130. void
  131. seed(_Sseq& __q)
  132. {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
  133. // generating functions
  134. _LIBCPP_HIDE_FROM_ABI result_type operator()();
  135. _LIBCPP_INLINE_VISIBILITY
  136. void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
  137. template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
  138. _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
  139. _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
  140. friend
  141. bool
  142. operator==(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  143. _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
  144. const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  145. _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
  146. template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
  147. _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
  148. _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
  149. friend
  150. bool
  151. operator!=(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  152. _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
  153. const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  154. _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
  155. template <class _CharT, class _Traits,
  156. class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
  157. _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
  158. _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
  159. friend
  160. basic_ostream<_CharT, _Traits>&
  161. operator<<(basic_ostream<_CharT, _Traits>& __os,
  162. const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  163. _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
  164. template <class _CharT, class _Traits,
  165. class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
  166. _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
  167. _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
  168. friend
  169. basic_istream<_CharT, _Traits>&
  170. operator>>(basic_istream<_CharT, _Traits>& __is,
  171. mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  172. _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
  173. private:
  174. template<class _Sseq>
  175. _LIBCPP_HIDE_FROM_ABI void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
  176. template<class _Sseq>
  177. _LIBCPP_HIDE_FROM_ABI void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
  178. template <size_t __count, __enable_if_t<__count < __w, int> = 0>
  179. _LIBCPP_INLINE_VISIBILITY
  180. static
  181. result_type
  182. __lshift(result_type __x) {return (__x << __count) & _Max;}
  183. template <size_t __count, __enable_if_t<(__count >= __w), int> = 0>
  184. _LIBCPP_INLINE_VISIBILITY
  185. static
  186. result_type
  187. __lshift(result_type) {return result_type(0);}
  188. template <size_t __count, __enable_if_t<__count < _Dt, int> = 0>
  189. _LIBCPP_INLINE_VISIBILITY
  190. static
  191. result_type
  192. __rshift(result_type __x) {return __x >> __count;}
  193. template <size_t __count, __enable_if_t<(__count >= _Dt), int> = 0>
  194. _LIBCPP_INLINE_VISIBILITY
  195. static
  196. result_type
  197. __rshift(result_type) {return result_type(0);}
  198. };
  199. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  200. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  201. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  202. _LIBCPP_CONSTEXPR const size_t
  203. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::word_size;
  204. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  205. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  206. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  207. _LIBCPP_CONSTEXPR const size_t
  208. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::state_size;
  209. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  210. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  211. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  212. _LIBCPP_CONSTEXPR const size_t
  213. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::shift_size;
  214. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  215. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  216. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  217. _LIBCPP_CONSTEXPR const size_t
  218. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::mask_bits;
  219. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  220. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  221. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  222. _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
  223. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::xor_mask;
  224. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  225. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  226. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  227. _LIBCPP_CONSTEXPR const size_t
  228. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_u;
  229. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  230. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  231. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  232. _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
  233. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_d;
  234. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  235. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  236. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  237. _LIBCPP_CONSTEXPR const size_t
  238. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_s;
  239. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  240. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  241. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  242. _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
  243. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_b;
  244. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  245. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  246. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  247. _LIBCPP_CONSTEXPR const size_t
  248. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_t;
  249. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  250. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  251. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  252. _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
  253. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_c;
  254. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  255. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  256. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  257. _LIBCPP_CONSTEXPR const size_t
  258. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_l;
  259. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  260. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  261. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  262. _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
  263. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::initialization_multiplier;
  264. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  265. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  266. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  267. _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
  268. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::default_seed;
  269. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  270. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  271. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  272. void
  273. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
  274. __t, __c, __l, __f>::seed(result_type __sd)
  275. _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
  276. { // __w >= 2
  277. __x_[0] = __sd & _Max;
  278. for (size_t __i = 1; __i < __n; ++__i)
  279. __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max;
  280. __i_ = 0;
  281. }
  282. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  283. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  284. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  285. template<class _Sseq>
  286. void
  287. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
  288. __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>)
  289. {
  290. const unsigned __k = 1;
  291. uint32_t __ar[__n * __k];
  292. __q.generate(__ar, __ar + __n * __k);
  293. for (size_t __i = 0; __i < __n; ++__i)
  294. __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
  295. const result_type __mask = __r == _Dt ? result_type(~0) :
  296. (result_type(1) << __r) - result_type(1);
  297. __i_ = 0;
  298. if ((__x_[0] & ~__mask) == 0)
  299. {
  300. for (size_t __i = 1; __i < __n; ++__i)
  301. if (__x_[__i] != 0)
  302. return;
  303. __x_[0] = result_type(1) << (__w - 1);
  304. }
  305. }
  306. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  307. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  308. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  309. template<class _Sseq>
  310. void
  311. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
  312. __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>)
  313. {
  314. const unsigned __k = 2;
  315. uint32_t __ar[__n * __k];
  316. __q.generate(__ar, __ar + __n * __k);
  317. for (size_t __i = 0; __i < __n; ++__i)
  318. __x_[__i] = static_cast<result_type>(
  319. (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
  320. const result_type __mask = __r == _Dt ? result_type(~0) :
  321. (result_type(1) << __r) - result_type(1);
  322. __i_ = 0;
  323. if ((__x_[0] & ~__mask) == 0)
  324. {
  325. for (size_t __i = 1; __i < __n; ++__i)
  326. if (__x_[__i] != 0)
  327. return;
  328. __x_[0] = result_type(1) << (__w - 1);
  329. }
  330. }
  331. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  332. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  333. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  334. _UIntType
  335. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
  336. __t, __c, __l, __f>::operator()()
  337. {
  338. const size_t __j = (__i_ + 1) % __n;
  339. const result_type __mask = __r == _Dt ? result_type(~0) :
  340. (result_type(1) << __r) - result_type(1);
  341. const result_type __yp = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask);
  342. const size_t __k = (__i_ + __m) % __n;
  343. __x_[__i_] = __x_[__k] ^ __rshift<1>(__yp) ^ (__a * (__yp & 1));
  344. result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d);
  345. __i_ = __j;
  346. __z ^= __lshift<__s>(__z) & __b;
  347. __z ^= __lshift<__t>(__z) & __c;
  348. return __z ^ __rshift<__l>(__z);
  349. }
  350. template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
  351. _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
  352. _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
  353. _LIBCPP_HIDE_FROM_ABI bool
  354. operator==(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  355. _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
  356. const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  357. _Bp, _Tp, _Cp, _Lp, _Fp>& __y)
  358. {
  359. if (__x.__i_ == __y.__i_)
  360. return _VSTD::equal(__x.__x_, __x.__x_ + _Np, __y.__x_);
  361. if (__x.__i_ == 0 || __y.__i_ == 0)
  362. {
  363. size_t __j = _VSTD::min(_Np - __x.__i_, _Np - __y.__i_);
  364. if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
  365. __y.__x_ + __y.__i_))
  366. return false;
  367. if (__x.__i_ == 0)
  368. return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _Np, __y.__x_);
  369. return _VSTD::equal(__x.__x_, __x.__x_ + (_Np - __j), __y.__x_ + __j);
  370. }
  371. if (__x.__i_ < __y.__i_)
  372. {
  373. size_t __j = _Np - __y.__i_;
  374. if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
  375. __y.__x_ + __y.__i_))
  376. return false;
  377. if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _Np,
  378. __y.__x_))
  379. return false;
  380. return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_,
  381. __y.__x_ + (_Np - (__x.__i_ + __j)));
  382. }
  383. size_t __j = _Np - __x.__i_;
  384. if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
  385. __x.__x_ + __x.__i_))
  386. return false;
  387. if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _Np,
  388. __x.__x_))
  389. return false;
  390. return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_,
  391. __x.__x_ + (_Np - (__y.__i_ + __j)));
  392. }
  393. template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
  394. _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
  395. _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
  396. inline _LIBCPP_INLINE_VISIBILITY
  397. bool
  398. operator!=(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  399. _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
  400. const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  401. _Bp, _Tp, _Cp, _Lp, _Fp>& __y)
  402. {
  403. return !(__x == __y);
  404. }
  405. template <class _CharT, class _Traits,
  406. class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
  407. _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
  408. _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
  409. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  410. operator<<(basic_ostream<_CharT, _Traits>& __os,
  411. const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  412. _Bp, _Tp, _Cp, _Lp, _Fp>& __x)
  413. {
  414. __save_flags<_CharT, _Traits> __lx(__os);
  415. typedef basic_ostream<_CharT, _Traits> _Ostream;
  416. __os.flags(_Ostream::dec | _Ostream::left);
  417. _CharT __sp = __os.widen(' ');
  418. __os.fill(__sp);
  419. __os << __x.__x_[__x.__i_];
  420. for (size_t __j = __x.__i_ + 1; __j < _Np; ++__j)
  421. __os << __sp << __x.__x_[__j];
  422. for (size_t __j = 0; __j < __x.__i_; ++__j)
  423. __os << __sp << __x.__x_[__j];
  424. return __os;
  425. }
  426. template <class _CharT, class _Traits,
  427. class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
  428. _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
  429. _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
  430. _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
  431. operator>>(basic_istream<_CharT, _Traits>& __is,
  432. mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  433. _Bp, _Tp, _Cp, _Lp, _Fp>& __x)
  434. {
  435. __save_flags<_CharT, _Traits> __lx(__is);
  436. typedef basic_istream<_CharT, _Traits> _Istream;
  437. __is.flags(_Istream::dec | _Istream::skipws);
  438. _UInt __t[_Np];
  439. for (size_t __i = 0; __i < _Np; ++__i)
  440. __is >> __t[__i];
  441. if (!__is.fail())
  442. {
  443. for (size_t __i = 0; __i < _Np; ++__i)
  444. __x.__x_[__i] = __t[__i];
  445. __x.__i_ = 0;
  446. }
  447. return __is;
  448. }
  449. typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
  450. 0x9908b0df, 11, 0xffffffff,
  451. 7, 0x9d2c5680,
  452. 15, 0xefc60000,
  453. 18, 1812433253> mt19937;
  454. typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
  455. 0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL,
  456. 17, 0x71d67fffeda60000ULL,
  457. 37, 0xfff7eee000000000ULL,
  458. 43, 6364136223846793005ULL> mt19937_64;
  459. _LIBCPP_END_NAMESPACE_STD
  460. _LIBCPP_POP_MACROS
  461. #endif // _LIBCPP___RANDOM_MERSENNE_TWISTER_ENGINE_H