1 
2 /*
3  *  mt19937.h
4  *
5  *  Mersenne Twister.
6  *
7    A C-program for MT19937, with initialization improved 2002/1/26.
8    Coded by Takuji Nishimura and Makoto Matsumoto.
9 
10    Before using, initialize the state by using init_genrand(seed)
11    or init_by_array(init_key, key_length).
12 
13    Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
14    All rights reserved.
15 
16    Redistribution and use in source and binary forms, with or without
17    modification, are permitted provided that the following conditions
18    are met:
19 
20      1. Redistributions of source code must retain the above copyright
21         notice, this list of conditions and the following disclaimer.
22 
23      2. Redistributions in binary form must reproduce the above copyright
24         notice, this list of conditions and the following disclaimer in the
25         documentation and/or other materials provided with the distribution.
26 
27      3. The names of its contributors may not be used to endorse or promote
28         products derived from this software without specific prior written
29         permission.
30 
31    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER
35  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
36    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
37    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
38    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
39    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
40    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
41    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 
43 
44    Any feedback is very welcome.
45    http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
46    email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
47  */
48 
49 #ifndef MT19937_H
50 #define MT19937_H 1
51 
52 #if defined(__APPLE__)
53 #include <OpenCL/cl_platform.h>
54 #else
55 #include <CL/cl_platform.h>
56 #endif
57 
58 /*
59  *      Interfaces here have been modified from original sources so that they
60  *      are safe to call reentrantly, so long as a different MTdata is used
61  *      on each thread.
62  */
63 
64 typedef struct _MTdata *MTdata;
65 
66 /* Create the random number generator with seed */
67 MTdata init_genrand(cl_uint /*seed*/);
68 
69 /* release memory used by a MTdata private data */
70 void free_mtdata(MTdata /*data*/);
71 
72 /* generates a random number on [0,0xffffffff]-interval */
73 cl_uint genrand_int32(MTdata /*data*/);
74 
75 /* generates a random number on [0,0xffffffffffffffffULL]-interval */
76 cl_ulong genrand_int64(MTdata /*data*/);
77 
78 /* generates a random number on [0,1]-real-interval */
79 double genrand_real1(MTdata /*data*/);
80 
81 /* generates a random number on [0,1)-real-interval */
82 double genrand_real2(MTdata /*data*/);
83 
84 /* generates a random number on (0,1)-real-interval */
85 double genrand_real3(MTdata /*data*/);
86 
87 /* generates a random number on [0,1) with 53-bit resolution*/
88 double genrand_res53(MTdata /*data*/);
89 
90 
91 #ifdef __cplusplus
92 
93 #include <cassert>
94 
95 struct MTdataHolder
96 {
MTdataHolderMTdataHolder97     MTdataHolder(cl_uint seed)
98     {
99         m_mtdata = init_genrand(seed);
100         assert(m_mtdata != nullptr);
101     }
102 
MTdataHolderMTdataHolder103     MTdataHolder(MTdata mtdata): m_mtdata(mtdata) {}
104 
~MTdataHolderMTdataHolder105     ~MTdataHolder() { free_mtdata(m_mtdata); }
106 
MTdataMTdataHolder107     operator MTdata() const { return m_mtdata; }
108 
109 private:
110     MTdata m_mtdata;
111 };
112 
113 #endif // #ifdef __cplusplus
114 
115 #endif /* MT19937_H */
116