]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libstdc++-v3/contrib/libstdc++-v3-4.3.3/include/parallel/random_number.h
update
[l4.git] / l4 / pkg / libstdc++-v3 / contrib / libstdc++-v3-4.3.3 / include / parallel / random_number.h
1 // -*- C++ -*-
2
3 // Copyright (C) 2007, 2008 Free Software Foundation, Inc.
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
8 // Foundation; either version 2, or (at your option) any later
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
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING.  If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
20
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction.  Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License.  This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
29 // Public License.
30
31 /** @file parallel/random_number.h
32  *  @brief Random number generator based on the Mersenne twister.
33  *  This file is a GNU parallel extension to the Standard C++ Library.
34  */
35
36 // Written by Johannes Singler.
37
38 #ifndef _GLIBCXX_PARALLEL_RANDOM_NUMBER_H
39 #define _GLIBCXX_PARALLEL_RANDOM_NUMBER_H 1
40
41 #include <parallel/types.h>
42 #include <tr1/random>
43
44 namespace __gnu_parallel
45 {
46   /** @brief Random number generator, based on the Mersenne twister. */
47   class random_number
48   {
49   private:
50     std::tr1::mt19937   mt;
51     uint64              supremum;
52     uint64              RAND_SUP;
53     double              supremum_reciprocal;
54     double              RAND_SUP_REC;
55
56     // Assumed to be twice as long as the usual random number.
57     uint64              cache;  
58
59     // Bit results.
60     int bits_left;
61     
62     static uint32
63     scale_down(uint64 x,
64 #if _GLIBCXX_SCALE_DOWN_FPU
65                uint64 /*supremum*/, double supremum_reciprocal)
66 #else
67                uint64 supremum, double /*supremum_reciprocal*/)
68 #endif
69         {
70 #if _GLIBCXX_SCALE_DOWN_FPU
71           return uint32(x * supremum_reciprocal);
72 #else
73           return static_cast<uint32>(x % supremum);
74 #endif
75         }
76
77   public:
78     /** @brief Default constructor. Seed with 0. */
79     random_number()
80     : mt(0), supremum(0x100000000ULL),
81       RAND_SUP(1ULL << (sizeof(uint32) * 8)),
82       supremum_reciprocal(double(supremum) / double(RAND_SUP)),
83       RAND_SUP_REC(1.0 / double(RAND_SUP)),
84       cache(0), bits_left(0) { }
85
86     /** @brief Constructor.
87      *  @param seed Random seed.
88      *  @param supremum Generate integer random numbers in the
89      *                  interval @c [0,supremum). */
90     random_number(uint32 seed, uint64 supremum = 0x100000000ULL)
91     : mt(seed), supremum(supremum),
92       RAND_SUP(1ULL << (sizeof(uint32) * 8)),
93       supremum_reciprocal(double(supremum) / double(RAND_SUP)),
94       RAND_SUP_REC(1.0 / double(RAND_SUP)),
95       cache(0), bits_left(0) { }
96
97     /** @brief Generate unsigned random 32-bit integer. */
98     uint32
99     operator()()
100     { return scale_down(mt(), supremum, supremum_reciprocal); }
101
102     /** @brief Generate unsigned random 32-bit integer in the
103         interval @c [0,local_supremum). */
104     uint32
105     operator()(uint64 local_supremum)
106     {
107       return scale_down(mt(), local_supremum,
108                         double(local_supremum * RAND_SUP_REC));
109     }
110
111     /** @brief Generate a number of random bits, run-time parameter.
112      *  @param bits Number of bits to generate. */
113     unsigned long
114     genrand_bits(int bits)
115     {
116       unsigned long res = cache & ((1 << bits) - 1);
117       cache = cache >> bits;
118       bits_left -= bits;
119       if (bits_left < 32)
120         {
121           cache |= ((uint64(mt())) << bits_left);
122           bits_left += 32;
123         }
124       return res;
125     }
126 };
127
128 } // namespace __gnu_parallel
129
130 #endif