mersenne_twister_engine.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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,
  25. size_t __w,
  26. size_t __n,
  27. size_t __m,
  28. size_t __r,
  29. _UIntType __a,
  30. size_t __u,
  31. _UIntType __d,
  32. size_t __s,
  33. _UIntType __b,
  34. size_t __t,
  35. _UIntType __c,
  36. size_t __l,
  37. _UIntType __f>
  38. class _LIBCPP_TEMPLATE_VIS mersenne_twister_engine;
  39. template <class _UInt,
  40. size_t _Wp,
  41. size_t _Np,
  42. size_t _Mp,
  43. size_t _Rp,
  44. _UInt _Ap,
  45. size_t _Up,
  46. _UInt _Dp,
  47. size_t _Sp,
  48. _UInt _Bp,
  49. size_t _Tp,
  50. _UInt _Cp,
  51. size_t _Lp,
  52. _UInt _Fp>
  53. _LIBCPP_HIDE_FROM_ABI bool
  54. operator==(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
  55. const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
  56. template <class _UInt,
  57. size_t _Wp,
  58. size_t _Np,
  59. size_t _Mp,
  60. size_t _Rp,
  61. _UInt _Ap,
  62. size_t _Up,
  63. _UInt _Dp,
  64. size_t _Sp,
  65. _UInt _Bp,
  66. size_t _Tp,
  67. _UInt _Cp,
  68. size_t _Lp,
  69. _UInt _Fp>
  70. _LIBCPP_HIDE_FROM_ABI bool
  71. operator!=(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
  72. const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
  73. template <class _CharT,
  74. class _Traits,
  75. class _UInt,
  76. size_t _Wp,
  77. size_t _Np,
  78. size_t _Mp,
  79. size_t _Rp,
  80. _UInt _Ap,
  81. size_t _Up,
  82. _UInt _Dp,
  83. size_t _Sp,
  84. _UInt _Bp,
  85. size_t _Tp,
  86. _UInt _Cp,
  87. size_t _Lp,
  88. _UInt _Fp>
  89. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  90. operator<<(basic_ostream<_CharT, _Traits>& __os,
  91. const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
  92. template <class _CharT,
  93. class _Traits,
  94. class _UInt,
  95. size_t _Wp,
  96. size_t _Np,
  97. size_t _Mp,
  98. size_t _Rp,
  99. _UInt _Ap,
  100. size_t _Up,
  101. _UInt _Dp,
  102. size_t _Sp,
  103. _UInt _Bp,
  104. size_t _Tp,
  105. _UInt _Cp,
  106. size_t _Lp,
  107. _UInt _Fp>
  108. _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
  109. operator>>(basic_istream<_CharT, _Traits>& __is,
  110. mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
  111. template <class _UIntType,
  112. size_t __w,
  113. size_t __n,
  114. size_t __m,
  115. size_t __r,
  116. _UIntType __a,
  117. size_t __u,
  118. _UIntType __d,
  119. size_t __s,
  120. _UIntType __b,
  121. size_t __t,
  122. _UIntType __c,
  123. size_t __l,
  124. _UIntType __f>
  125. class _LIBCPP_TEMPLATE_VIS mersenne_twister_engine {
  126. public:
  127. // types
  128. typedef _UIntType result_type;
  129. private:
  130. result_type __x_[__n];
  131. size_t __i_;
  132. static_assert(0 < __m, "mersenne_twister_engine invalid parameters");
  133. static_assert(__m <= __n, "mersenne_twister_engine invalid parameters");
  134. static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
  135. static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters");
  136. static_assert(2 <= __w, "mersenne_twister_engine invalid parameters");
  137. static_assert(__r <= __w, "mersenne_twister_engine invalid parameters");
  138. static_assert(__u <= __w, "mersenne_twister_engine invalid parameters");
  139. static_assert(__s <= __w, "mersenne_twister_engine invalid parameters");
  140. static_assert(__t <= __w, "mersenne_twister_engine invalid parameters");
  141. static_assert(__l <= __w, "mersenne_twister_engine invalid parameters");
  142. public:
  143. static _LIBCPP_CONSTEXPR const result_type _Min = 0;
  144. static _LIBCPP_CONSTEXPR const result_type _Max =
  145. __w == _Dt ? result_type(~0) : (result_type(1) << __w) - result_type(1);
  146. static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters");
  147. static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters");
  148. static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters");
  149. static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters");
  150. static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters");
  151. static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters");
  152. // engine characteristics
  153. static _LIBCPP_CONSTEXPR const size_t word_size = __w;
  154. static _LIBCPP_CONSTEXPR const size_t state_size = __n;
  155. static _LIBCPP_CONSTEXPR const size_t shift_size = __m;
  156. static _LIBCPP_CONSTEXPR const size_t mask_bits = __r;
  157. static _LIBCPP_CONSTEXPR const result_type xor_mask = __a;
  158. static _LIBCPP_CONSTEXPR const size_t tempering_u = __u;
  159. static _LIBCPP_CONSTEXPR const result_type tempering_d = __d;
  160. static _LIBCPP_CONSTEXPR const size_t tempering_s = __s;
  161. static _LIBCPP_CONSTEXPR const result_type tempering_b = __b;
  162. static _LIBCPP_CONSTEXPR const size_t tempering_t = __t;
  163. static _LIBCPP_CONSTEXPR const result_type tempering_c = __c;
  164. static _LIBCPP_CONSTEXPR const size_t tempering_l = __l;
  165. static _LIBCPP_CONSTEXPR const result_type initialization_multiplier = __f;
  166. _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
  167. _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
  168. static _LIBCPP_CONSTEXPR const result_type default_seed = 5489u;
  169. // constructors and seeding functions
  170. #ifndef _LIBCPP_CXX03_LANG
  171. _LIBCPP_HIDE_FROM_ABI mersenne_twister_engine() : mersenne_twister_engine(default_seed) {}
  172. _LIBCPP_HIDE_FROM_ABI explicit mersenne_twister_engine(result_type __sd) { seed(__sd); }
  173. #else
  174. _LIBCPP_HIDE_FROM_ABI explicit mersenne_twister_engine(result_type __sd = default_seed) { seed(__sd); }
  175. #endif
  176. template <class _Sseq, __enable_if_t<__is_seed_sequence<_Sseq, mersenne_twister_engine>::value, int> = 0>
  177. _LIBCPP_HIDE_FROM_ABI explicit mersenne_twister_engine(_Sseq& __q) {
  178. seed(__q);
  179. }
  180. _LIBCPP_HIDE_FROM_ABI void seed(result_type __sd = default_seed);
  181. template <class _Sseq, __enable_if_t<__is_seed_sequence<_Sseq, mersenne_twister_engine>::value, int> = 0>
  182. _LIBCPP_HIDE_FROM_ABI void seed(_Sseq& __q) {
  183. __seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());
  184. }
  185. // generating functions
  186. _LIBCPP_HIDE_FROM_ABI result_type operator()();
  187. _LIBCPP_HIDE_FROM_ABI void discard(unsigned long long __z) {
  188. for (; __z; --__z)
  189. operator()();
  190. }
  191. template <class _UInt,
  192. size_t _Wp,
  193. size_t _Np,
  194. size_t _Mp,
  195. size_t _Rp,
  196. _UInt _Ap,
  197. size_t _Up,
  198. _UInt _Dp,
  199. size_t _Sp,
  200. _UInt _Bp,
  201. size_t _Tp,
  202. _UInt _Cp,
  203. size_t _Lp,
  204. _UInt _Fp>
  205. friend bool operator==(
  206. const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
  207. const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
  208. template <class _UInt,
  209. size_t _Wp,
  210. size_t _Np,
  211. size_t _Mp,
  212. size_t _Rp,
  213. _UInt _Ap,
  214. size_t _Up,
  215. _UInt _Dp,
  216. size_t _Sp,
  217. _UInt _Bp,
  218. size_t _Tp,
  219. _UInt _Cp,
  220. size_t _Lp,
  221. _UInt _Fp>
  222. friend bool operator!=(
  223. const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
  224. const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
  225. template <class _CharT,
  226. class _Traits,
  227. class _UInt,
  228. size_t _Wp,
  229. size_t _Np,
  230. size_t _Mp,
  231. size_t _Rp,
  232. _UInt _Ap,
  233. size_t _Up,
  234. _UInt _Dp,
  235. size_t _Sp,
  236. _UInt _Bp,
  237. size_t _Tp,
  238. _UInt _Cp,
  239. size_t _Lp,
  240. _UInt _Fp>
  241. friend basic_ostream<_CharT, _Traits>& operator<<(
  242. basic_ostream<_CharT, _Traits>& __os,
  243. const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
  244. template <class _CharT,
  245. class _Traits,
  246. class _UInt,
  247. size_t _Wp,
  248. size_t _Np,
  249. size_t _Mp,
  250. size_t _Rp,
  251. _UInt _Ap,
  252. size_t _Up,
  253. _UInt _Dp,
  254. size_t _Sp,
  255. _UInt _Bp,
  256. size_t _Tp,
  257. _UInt _Cp,
  258. size_t _Lp,
  259. _UInt _Fp>
  260. friend basic_istream<_CharT, _Traits>&
  261. operator>>(basic_istream<_CharT, _Traits>& __is,
  262. mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
  263. private:
  264. template <class _Sseq>
  265. _LIBCPP_HIDE_FROM_ABI void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
  266. template <class _Sseq>
  267. _LIBCPP_HIDE_FROM_ABI void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
  268. template <size_t __count,
  269. __enable_if_t<__count< __w, int> = 0> _LIBCPP_HIDE_FROM_ABI static result_type __lshift(result_type __x) {
  270. return (__x << __count) & _Max;
  271. }
  272. template <size_t __count, __enable_if_t<(__count >= __w), int> = 0>
  273. _LIBCPP_HIDE_FROM_ABI static result_type __lshift(result_type) {
  274. return result_type(0);
  275. }
  276. template <size_t __count,
  277. __enable_if_t<__count< _Dt, int> = 0> _LIBCPP_HIDE_FROM_ABI static result_type __rshift(result_type __x) {
  278. return __x >> __count;
  279. }
  280. template <size_t __count, __enable_if_t<(__count >= _Dt), int> = 0>
  281. _LIBCPP_HIDE_FROM_ABI static result_type __rshift(result_type) {
  282. return result_type(0);
  283. }
  284. };
  285. template <class _UIntType,
  286. size_t __w,
  287. size_t __n,
  288. size_t __m,
  289. size_t __r,
  290. _UIntType __a,
  291. size_t __u,
  292. _UIntType __d,
  293. size_t __s,
  294. _UIntType __b,
  295. size_t __t,
  296. _UIntType __c,
  297. size_t __l,
  298. _UIntType __f>
  299. _LIBCPP_CONSTEXPR const size_t
  300. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::word_size;
  301. template <class _UIntType,
  302. size_t __w,
  303. size_t __n,
  304. size_t __m,
  305. size_t __r,
  306. _UIntType __a,
  307. size_t __u,
  308. _UIntType __d,
  309. size_t __s,
  310. _UIntType __b,
  311. size_t __t,
  312. _UIntType __c,
  313. size_t __l,
  314. _UIntType __f>
  315. _LIBCPP_CONSTEXPR const size_t
  316. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::state_size;
  317. template <class _UIntType,
  318. size_t __w,
  319. size_t __n,
  320. size_t __m,
  321. size_t __r,
  322. _UIntType __a,
  323. size_t __u,
  324. _UIntType __d,
  325. size_t __s,
  326. _UIntType __b,
  327. size_t __t,
  328. _UIntType __c,
  329. size_t __l,
  330. _UIntType __f>
  331. _LIBCPP_CONSTEXPR const size_t
  332. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::shift_size;
  333. template <class _UIntType,
  334. size_t __w,
  335. size_t __n,
  336. size_t __m,
  337. size_t __r,
  338. _UIntType __a,
  339. size_t __u,
  340. _UIntType __d,
  341. size_t __s,
  342. _UIntType __b,
  343. size_t __t,
  344. _UIntType __c,
  345. size_t __l,
  346. _UIntType __f>
  347. _LIBCPP_CONSTEXPR const size_t
  348. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::mask_bits;
  349. template <class _UIntType,
  350. size_t __w,
  351. size_t __n,
  352. size_t __m,
  353. size_t __r,
  354. _UIntType __a,
  355. size_t __u,
  356. _UIntType __d,
  357. size_t __s,
  358. _UIntType __b,
  359. size_t __t,
  360. _UIntType __c,
  361. size_t __l,
  362. _UIntType __f>
  363. _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<
  364. _UIntType,
  365. __w,
  366. __n,
  367. __m,
  368. __r,
  369. __a,
  370. __u,
  371. __d,
  372. __s,
  373. __b,
  374. __t,
  375. __c,
  376. __l,
  377. __f>::result_type
  378. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::xor_mask;
  379. template <class _UIntType,
  380. size_t __w,
  381. size_t __n,
  382. size_t __m,
  383. size_t __r,
  384. _UIntType __a,
  385. size_t __u,
  386. _UIntType __d,
  387. size_t __s,
  388. _UIntType __b,
  389. size_t __t,
  390. _UIntType __c,
  391. size_t __l,
  392. _UIntType __f>
  393. _LIBCPP_CONSTEXPR const size_t
  394. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_u;
  395. template <class _UIntType,
  396. size_t __w,
  397. size_t __n,
  398. size_t __m,
  399. size_t __r,
  400. _UIntType __a,
  401. size_t __u,
  402. _UIntType __d,
  403. size_t __s,
  404. _UIntType __b,
  405. size_t __t,
  406. _UIntType __c,
  407. size_t __l,
  408. _UIntType __f>
  409. _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<
  410. _UIntType,
  411. __w,
  412. __n,
  413. __m,
  414. __r,
  415. __a,
  416. __u,
  417. __d,
  418. __s,
  419. __b,
  420. __t,
  421. __c,
  422. __l,
  423. __f>::result_type
  424. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_d;
  425. template <class _UIntType,
  426. size_t __w,
  427. size_t __n,
  428. size_t __m,
  429. size_t __r,
  430. _UIntType __a,
  431. size_t __u,
  432. _UIntType __d,
  433. size_t __s,
  434. _UIntType __b,
  435. size_t __t,
  436. _UIntType __c,
  437. size_t __l,
  438. _UIntType __f>
  439. _LIBCPP_CONSTEXPR const size_t
  440. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_s;
  441. template <class _UIntType,
  442. size_t __w,
  443. size_t __n,
  444. size_t __m,
  445. size_t __r,
  446. _UIntType __a,
  447. size_t __u,
  448. _UIntType __d,
  449. size_t __s,
  450. _UIntType __b,
  451. size_t __t,
  452. _UIntType __c,
  453. size_t __l,
  454. _UIntType __f>
  455. _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<
  456. _UIntType,
  457. __w,
  458. __n,
  459. __m,
  460. __r,
  461. __a,
  462. __u,
  463. __d,
  464. __s,
  465. __b,
  466. __t,
  467. __c,
  468. __l,
  469. __f>::result_type
  470. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_b;
  471. template <class _UIntType,
  472. size_t __w,
  473. size_t __n,
  474. size_t __m,
  475. size_t __r,
  476. _UIntType __a,
  477. size_t __u,
  478. _UIntType __d,
  479. size_t __s,
  480. _UIntType __b,
  481. size_t __t,
  482. _UIntType __c,
  483. size_t __l,
  484. _UIntType __f>
  485. _LIBCPP_CONSTEXPR const size_t
  486. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_t;
  487. template <class _UIntType,
  488. size_t __w,
  489. size_t __n,
  490. size_t __m,
  491. size_t __r,
  492. _UIntType __a,
  493. size_t __u,
  494. _UIntType __d,
  495. size_t __s,
  496. _UIntType __b,
  497. size_t __t,
  498. _UIntType __c,
  499. size_t __l,
  500. _UIntType __f>
  501. _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<
  502. _UIntType,
  503. __w,
  504. __n,
  505. __m,
  506. __r,
  507. __a,
  508. __u,
  509. __d,
  510. __s,
  511. __b,
  512. __t,
  513. __c,
  514. __l,
  515. __f>::result_type
  516. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_c;
  517. template <class _UIntType,
  518. size_t __w,
  519. size_t __n,
  520. size_t __m,
  521. size_t __r,
  522. _UIntType __a,
  523. size_t __u,
  524. _UIntType __d,
  525. size_t __s,
  526. _UIntType __b,
  527. size_t __t,
  528. _UIntType __c,
  529. size_t __l,
  530. _UIntType __f>
  531. _LIBCPP_CONSTEXPR const size_t
  532. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_l;
  533. template <class _UIntType,
  534. size_t __w,
  535. size_t __n,
  536. size_t __m,
  537. size_t __r,
  538. _UIntType __a,
  539. size_t __u,
  540. _UIntType __d,
  541. size_t __s,
  542. _UIntType __b,
  543. size_t __t,
  544. _UIntType __c,
  545. size_t __l,
  546. _UIntType __f>
  547. _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<
  548. _UIntType,
  549. __w,
  550. __n,
  551. __m,
  552. __r,
  553. __a,
  554. __u,
  555. __d,
  556. __s,
  557. __b,
  558. __t,
  559. __c,
  560. __l,
  561. __f>::result_type
  562. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::
  563. initialization_multiplier;
  564. template <class _UIntType,
  565. size_t __w,
  566. size_t __n,
  567. size_t __m,
  568. size_t __r,
  569. _UIntType __a,
  570. size_t __u,
  571. _UIntType __d,
  572. size_t __s,
  573. _UIntType __b,
  574. size_t __t,
  575. _UIntType __c,
  576. size_t __l,
  577. _UIntType __f>
  578. _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<
  579. _UIntType,
  580. __w,
  581. __n,
  582. __m,
  583. __r,
  584. __a,
  585. __u,
  586. __d,
  587. __s,
  588. __b,
  589. __t,
  590. __c,
  591. __l,
  592. __f>::result_type
  593. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::default_seed;
  594. template <class _UIntType,
  595. size_t __w,
  596. size_t __n,
  597. size_t __m,
  598. size_t __r,
  599. _UIntType __a,
  600. size_t __u,
  601. _UIntType __d,
  602. size_t __s,
  603. _UIntType __b,
  604. size_t __t,
  605. _UIntType __c,
  606. size_t __l,
  607. _UIntType __f>
  608. void mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::seed(
  609. result_type __sd) _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK { // __w >= 2
  610. __x_[0] = __sd & _Max;
  611. for (size_t __i = 1; __i < __n; ++__i)
  612. __x_[__i] = (__f * (__x_[__i - 1] ^ __rshift<__w - 2>(__x_[__i - 1])) + __i) & _Max;
  613. __i_ = 0;
  614. }
  615. template <class _UIntType,
  616. size_t __w,
  617. size_t __n,
  618. size_t __m,
  619. size_t __r,
  620. _UIntType __a,
  621. size_t __u,
  622. _UIntType __d,
  623. size_t __s,
  624. _UIntType __b,
  625. size_t __t,
  626. _UIntType __c,
  627. size_t __l,
  628. _UIntType __f>
  629. template <class _Sseq>
  630. void mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::__seed(
  631. _Sseq& __q, integral_constant<unsigned, 1>) {
  632. const unsigned __k = 1;
  633. uint32_t __ar[__n * __k];
  634. __q.generate(__ar, __ar + __n * __k);
  635. for (size_t __i = 0; __i < __n; ++__i)
  636. __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
  637. const result_type __mask = __r == _Dt ? result_type(~0) : (result_type(1) << __r) - result_type(1);
  638. __i_ = 0;
  639. if ((__x_[0] & ~__mask) == 0) {
  640. for (size_t __i = 1; __i < __n; ++__i)
  641. if (__x_[__i] != 0)
  642. return;
  643. __x_[0] = result_type(1) << (__w - 1);
  644. }
  645. }
  646. template <class _UIntType,
  647. size_t __w,
  648. size_t __n,
  649. size_t __m,
  650. size_t __r,
  651. _UIntType __a,
  652. size_t __u,
  653. _UIntType __d,
  654. size_t __s,
  655. _UIntType __b,
  656. size_t __t,
  657. _UIntType __c,
  658. size_t __l,
  659. _UIntType __f>
  660. template <class _Sseq>
  661. void mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::__seed(
  662. _Sseq& __q, integral_constant<unsigned, 2>) {
  663. const unsigned __k = 2;
  664. uint32_t __ar[__n * __k];
  665. __q.generate(__ar, __ar + __n * __k);
  666. for (size_t __i = 0; __i < __n; ++__i)
  667. __x_[__i] = static_cast<result_type>((__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
  668. const result_type __mask = __r == _Dt ? result_type(~0) : (result_type(1) << __r) - result_type(1);
  669. __i_ = 0;
  670. if ((__x_[0] & ~__mask) == 0) {
  671. for (size_t __i = 1; __i < __n; ++__i)
  672. if (__x_[__i] != 0)
  673. return;
  674. __x_[0] = result_type(1) << (__w - 1);
  675. }
  676. }
  677. template <class _UIntType,
  678. size_t __w,
  679. size_t __n,
  680. size_t __m,
  681. size_t __r,
  682. _UIntType __a,
  683. size_t __u,
  684. _UIntType __d,
  685. size_t __s,
  686. _UIntType __b,
  687. size_t __t,
  688. _UIntType __c,
  689. size_t __l,
  690. _UIntType __f>
  691. _UIntType
  692. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::operator()() {
  693. const size_t __j = (__i_ + 1) % __n;
  694. const result_type __mask = __r == _Dt ? result_type(~0) : (result_type(1) << __r) - result_type(1);
  695. const result_type __yp = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask);
  696. const size_t __k = (__i_ + __m) % __n;
  697. __x_[__i_] = __x_[__k] ^ __rshift<1>(__yp) ^ (__a * (__yp & 1));
  698. result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d);
  699. __i_ = __j;
  700. __z ^= __lshift<__s>(__z) & __b;
  701. __z ^= __lshift<__t>(__z) & __c;
  702. return __z ^ __rshift<__l>(__z);
  703. }
  704. template <class _UInt,
  705. size_t _Wp,
  706. size_t _Np,
  707. size_t _Mp,
  708. size_t _Rp,
  709. _UInt _Ap,
  710. size_t _Up,
  711. _UInt _Dp,
  712. size_t _Sp,
  713. _UInt _Bp,
  714. size_t _Tp,
  715. _UInt _Cp,
  716. size_t _Lp,
  717. _UInt _Fp>
  718. _LIBCPP_HIDE_FROM_ABI bool
  719. operator==(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
  720. const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __y) {
  721. if (__x.__i_ == __y.__i_)
  722. return std::equal(__x.__x_, __x.__x_ + _Np, __y.__x_);
  723. if (__x.__i_ == 0 || __y.__i_ == 0) {
  724. size_t __j = std::min(_Np - __x.__i_, _Np - __y.__i_);
  725. if (!std::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j, __y.__x_ + __y.__i_))
  726. return false;
  727. if (__x.__i_ == 0)
  728. return std::equal(__x.__x_ + __j, __x.__x_ + _Np, __y.__x_);
  729. return std::equal(__x.__x_, __x.__x_ + (_Np - __j), __y.__x_ + __j);
  730. }
  731. if (__x.__i_ < __y.__i_) {
  732. size_t __j = _Np - __y.__i_;
  733. if (!std::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j), __y.__x_ + __y.__i_))
  734. return false;
  735. if (!std::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _Np, __y.__x_))
  736. return false;
  737. return std::equal(__x.__x_, __x.__x_ + __x.__i_, __y.__x_ + (_Np - (__x.__i_ + __j)));
  738. }
  739. size_t __j = _Np - __x.__i_;
  740. if (!std::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j), __x.__x_ + __x.__i_))
  741. return false;
  742. if (!std::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _Np, __x.__x_))
  743. return false;
  744. return std::equal(__y.__x_, __y.__x_ + __y.__i_, __x.__x_ + (_Np - (__y.__i_ + __j)));
  745. }
  746. template <class _UInt,
  747. size_t _Wp,
  748. size_t _Np,
  749. size_t _Mp,
  750. size_t _Rp,
  751. _UInt _Ap,
  752. size_t _Up,
  753. _UInt _Dp,
  754. size_t _Sp,
  755. _UInt _Bp,
  756. size_t _Tp,
  757. _UInt _Cp,
  758. size_t _Lp,
  759. _UInt _Fp>
  760. inline _LIBCPP_HIDE_FROM_ABI bool
  761. operator!=(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
  762. const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __y) {
  763. return !(__x == __y);
  764. }
  765. template <class _CharT,
  766. class _Traits,
  767. class _UInt,
  768. size_t _Wp,
  769. size_t _Np,
  770. size_t _Mp,
  771. size_t _Rp,
  772. _UInt _Ap,
  773. size_t _Up,
  774. _UInt _Dp,
  775. size_t _Sp,
  776. _UInt _Bp,
  777. size_t _Tp,
  778. _UInt _Cp,
  779. size_t _Lp,
  780. _UInt _Fp>
  781. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  782. operator<<(basic_ostream<_CharT, _Traits>& __os,
  783. const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __x) {
  784. __save_flags<_CharT, _Traits> __lx(__os);
  785. typedef basic_ostream<_CharT, _Traits> _Ostream;
  786. __os.flags(_Ostream::dec | _Ostream::left);
  787. _CharT __sp = __os.widen(' ');
  788. __os.fill(__sp);
  789. __os << __x.__x_[__x.__i_];
  790. for (size_t __j = __x.__i_ + 1; __j < _Np; ++__j)
  791. __os << __sp << __x.__x_[__j];
  792. for (size_t __j = 0; __j < __x.__i_; ++__j)
  793. __os << __sp << __x.__x_[__j];
  794. return __os;
  795. }
  796. template <class _CharT,
  797. class _Traits,
  798. class _UInt,
  799. size_t _Wp,
  800. size_t _Np,
  801. size_t _Mp,
  802. size_t _Rp,
  803. _UInt _Ap,
  804. size_t _Up,
  805. _UInt _Dp,
  806. size_t _Sp,
  807. _UInt _Bp,
  808. size_t _Tp,
  809. _UInt _Cp,
  810. size_t _Lp,
  811. _UInt _Fp>
  812. _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
  813. operator>>(basic_istream<_CharT, _Traits>& __is,
  814. mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, _Bp, _Tp, _Cp, _Lp, _Fp>& __x) {
  815. __save_flags<_CharT, _Traits> __lx(__is);
  816. typedef basic_istream<_CharT, _Traits> _Istream;
  817. __is.flags(_Istream::dec | _Istream::skipws);
  818. _UInt __t[_Np];
  819. for (size_t __i = 0; __i < _Np; ++__i)
  820. __is >> __t[__i];
  821. if (!__is.fail()) {
  822. for (size_t __i = 0; __i < _Np; ++__i)
  823. __x.__x_[__i] = __t[__i];
  824. __x.__i_ = 0;
  825. }
  826. return __is;
  827. }
  828. typedef mersenne_twister_engine<
  829. uint_fast32_t,
  830. 32,
  831. 624,
  832. 397,
  833. 31,
  834. 0x9908b0df,
  835. 11,
  836. 0xffffffff,
  837. 7,
  838. 0x9d2c5680,
  839. 15,
  840. 0xefc60000,
  841. 18,
  842. 1812433253>
  843. mt19937;
  844. typedef mersenne_twister_engine<
  845. uint_fast64_t,
  846. 64,
  847. 312,
  848. 156,
  849. 31,
  850. 0xb5026f5aa96619e9ULL,
  851. 29,
  852. 0x5555555555555555ULL,
  853. 17,
  854. 0x71d67fffeda60000ULL,
  855. 37,
  856. 0xfff7eee000000000ULL,
  857. 43,
  858. 6364136223846793005ULL>
  859. mt19937_64;
  860. _LIBCPP_END_NAMESPACE_STD
  861. _LIBCPP_POP_MACROS
  862. #endif // _LIBCPP___RANDOM_MERSENNE_TWISTER_ENGINE_H