]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/parallel/random_number.h
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / parallel / random_number.h
CommitLineData
c2ba9709
JS
1// -*- C++ -*-
2
8d9254fc 3// Copyright (C) 2007-2020 Free Software Foundation, Inc.
c2ba9709
JS
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the terms
7// of the GNU General Public License as published by the Free Software
748086b7 8// Foundation; either version 3, or (at your option) any later
c2ba9709
JS
9// version.
10
11// This library is distributed in the hope that it will be useful, but
12// WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15
748086b7
JJ
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
c2ba9709
JS
24
25/** @file parallel/random_number.h
26 * @brief Random number generator based on the Mersenne twister.
27 * This file is a GNU parallel extension to the Standard C++ Library.
28 */
29
30// Written by Johannes Singler.
31
32#ifndef _GLIBCXX_PARALLEL_RANDOM_NUMBER_H
33#define _GLIBCXX_PARALLEL_RANDOM_NUMBER_H 1
34
35#include <parallel/types.h>
459af5a0 36#include <tr1/random>
63ffc486 37#include <limits>
c2ba9709
JS
38
39namespace __gnu_parallel
40{
c2ba9709 41 /** @brief Random number generator, based on the Mersenne twister. */
1acba85b 42 class _RandomNumber
c2ba9709
JS
43 {
44 private:
15ac3c72 45 std::tr1::mt19937 _M_mt;
63ffc486
JS
46 uint64_t _M_supremum;
47 uint64_t _M_rand_sup;
15ac3c72
JS
48 double _M_supremum_reciprocal;
49 double _M_rand_sup_reciprocal;
459af5a0
BK
50
51 // Assumed to be twice as long as the usual random number.
63ffc486 52 uint64_t __cache;
459af5a0
BK
53
54 // Bit results.
1acba85b 55 int __bits_left;
459af5a0 56
63ffc486
JS
57 static uint32_t
58 __scale_down(uint64_t __x,
c2ba9709 59#if _GLIBCXX_SCALE_DOWN_FPU
63ffc486 60 uint64_t /*_M_supremum*/, double _M_supremum_reciprocal)
c2ba9709 61#else
63ffc486 62 uint64_t _M_supremum, double /*_M_supremum_reciprocal*/)
c2ba9709 63#endif
15ac3c72 64 {
c2ba9709 65#if _GLIBCXX_SCALE_DOWN_FPU
63ffc486 66 return uint32_t(__x * _M_supremum_reciprocal);
c2ba9709 67#else
63ffc486 68 return static_cast<uint32_t>(__x % _M_supremum);
c2ba9709 69#endif
15ac3c72 70 }
c2ba9709 71
5817ff8e
PC
72 public:
73 /** @brief Default constructor. Seed with 0. */
1acba85b
JS
74 _RandomNumber()
75 : _M_mt(0), _M_supremum(0x100000000ULL),
63ffc486 76 _M_rand_sup(1ULL << std::numeric_limits<uint32_t>::digits),
54384f7f
JS
77 _M_supremum_reciprocal(double(_M_supremum) / double(_M_rand_sup)),
78 _M_rand_sup_reciprocal(1.0 / double(_M_rand_sup)),
1acba85b 79 __cache(0), __bits_left(0) { }
5817ff8e
PC
80
81 /** @brief Constructor.
1acba85b
JS
82 * @param __seed Random __seed.
83 * @param _M_supremum Generate integer random numbers in the
8e32aa11 84 * interval @c [0,_M_supremum). */
63ffc486 85 _RandomNumber(uint32_t __seed, uint64_t _M_supremum = 0x100000000ULL)
1acba85b 86 : _M_mt(__seed), _M_supremum(_M_supremum),
63ffc486 87 _M_rand_sup(1ULL << std::numeric_limits<uint32_t>::digits),
54384f7f
JS
88 _M_supremum_reciprocal(double(_M_supremum) / double(_M_rand_sup)),
89 _M_rand_sup_reciprocal(1.0 / double(_M_rand_sup)),
1acba85b 90 __cache(0), __bits_left(0) { }
5817ff8e
PC
91
92 /** @brief Generate unsigned random 32-bit integer. */
63ffc486 93 uint32_t
5817ff8e 94 operator()()
1acba85b 95 { return __scale_down(_M_mt(), _M_supremum, _M_supremum_reciprocal); }
5817ff8e
PC
96
97 /** @brief Generate unsigned random 32-bit integer in the
8e32aa11 98 interval @c [0,local_supremum). */
63ffc486
JS
99 uint32_t
100 operator()(uint64_t local_supremum)
5817ff8e 101 {
1acba85b 102 return __scale_down(_M_mt(), local_supremum,
15ac3c72 103 double(local_supremum * _M_rand_sup_reciprocal));
5817ff8e 104 }
c2ba9709 105
5817ff8e 106 /** @brief Generate a number of random bits, run-time parameter.
93c66bc6 107 * @param __bits Number of bits to generate. */
5817ff8e 108 unsigned long
54384f7f 109 __genrand_bits(int __bits)
5817ff8e 110 {
54384f7f
JS
111 unsigned long __res = __cache & ((1 << __bits) - 1);
112 __cache = __cache >> __bits;
113 __bits_left -= __bits;
1acba85b 114 if (__bits_left < 32)
15ac3c72 115 {
63ffc486 116 __cache |= ((uint64_t(_M_mt())) << __bits_left);
15ac3c72
JS
117 __bits_left += 32;
118 }
1acba85b 119 return __res;
5817ff8e 120 }
c2ba9709
JS
121};
122
123} // namespace __gnu_parallel
124
cbcd1e45 125#endif /* _GLIBCXX_PARALLEL_RANDOM_NUMBER_H */