xref: /aosp_15_r20/external/OpenCL-CTS/test_common/harness/mt19937.h (revision 6467f958c7de8070b317fc65bcb0f6472e388d82)
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 /* generates a random boolean */
94 bool genrand_bool(MTdata /*data*/);
95 
96 #include <cassert>
97 #include <utility>
98 
99 class MTdataHolder {
100 public:
101     MTdataHolder() = default;
MTdataHolder(cl_uint seed)102     explicit MTdataHolder(cl_uint seed)
103     {
104         m_mtdata = init_genrand(seed);
105         assert(m_mtdata != nullptr);
106     }
107 
108     // Forbid copy.
109     MTdataHolder(const MTdataHolder&) = delete;
110     MTdataHolder& operator=(const MTdataHolder&) = delete;
111 
112     // Support move semantics.
MTdataHolder(MTdataHolder && h)113     MTdataHolder(MTdataHolder&& h) { std::swap(m_mtdata, h.m_mtdata); }
114     MTdataHolder& operator=(MTdataHolder&& h)
115     {
116         std::swap(m_mtdata, h.m_mtdata);
117         return *this;
118     }
119 
~MTdataHolder()120     ~MTdataHolder()
121     {
122         if (m_mtdata) free_mtdata(m_mtdata);
123     }
124 
MTdata()125     operator MTdata() const
126     {
127         assert(m_mtdata && "Object wasn't initialised");
128         return m_mtdata;
129     }
130 
131 private:
132     MTdata m_mtdata = nullptr;
133 };
134 
135 #endif // #ifdef __cplusplus
136 
137 #endif /* MT19937_H */
138