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