NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Random.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2011, NVIDIA Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA Corporation nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "base/Random.hpp"
29 #include "base/DLLImports.hpp"
30 
31 using namespace FW;
32 
33 //------------------------------------------------------------------------
34 
35 #define FW_USE_MERSENNE_TWISTER 0
36 
37 //------------------------------------------------------------------------
38 // Mersenne Twister
39 //------------------------------------------------------------------------
40 
41 #if FW_USE_MERSENNE_TWISTER
42 extern "C"
43 {
44 #include "3rdparty/mt19937ar/mt19937ar_ctx.h"
45 }
46 #endif
47 
48 //------------------------------------------------------------------------
49 // RANROT-A
50 //------------------------------------------------------------------------
51 
52 class RanrotA
53 {
54 private:
55  S32 p1;
56  S32 p2;
57  U32 buffer[11];
58 
59 public:
60  void reset(U32 seed)
61  {
62  if (seed == 0)
63  seed--;
64 
65  for (int i = 0; i < (int)FW_ARRAY_SIZE(buffer); i++)
66  {
67  seed ^= seed << 13;
68  seed ^= seed >> 17;
69  seed ^= seed << 5;
70  buffer[i] = seed;
71  }
72 
73  p1 = 0;
74  p2 = 7;
75 
76  for (int i = 0; i < (int)FW_ARRAY_SIZE(buffer); i++)
77  get();
78  }
79 
80  U32 get(void)
81  {
82  U32 x = buffer[p1] + buffer[p2];
83  x = (x << 13) | (x >> 19);
84  buffer[p1] = x;
85 
86  p1--;
87  p1 += (p1 >> 31) & FW_ARRAY_SIZE(buffer);
88  p2--;
89  p2 += (p2 >> 31) & FW_ARRAY_SIZE(buffer);
90  return x;
91  }
92 };
93 
94 //------------------------------------------------------------------------
95 // Common functionality.
96 //------------------------------------------------------------------------
97 
98 void Random::reset(void)
99 {
100  LARGE_INTEGER ticks;
101  if (!QueryPerformanceCounter(&ticks))
102  failWin32Error("QueryPerformanceCounter");
103  reset(ticks.LowPart);
104 }
105 
106 //------------------------------------------------------------------------
107 
108 void Random::reset(U32 seed)
109 {
110  resetImpl(seed);
111 
112  m_normalF32Valid = false;
113  m_normalF32 = 0.0f;
114  m_normalF64Valid = false;
115  m_normalF64 = 0.0;
116 }
117 
118 //------------------------------------------------------------------------
119 
120 void Random::reset(const Random& other)
121 {
122  assignImpl(other);
123 
124  m_normalF32Valid = other.m_normalF32Valid;
125  m_normalF32 = other.m_normalF32;
126  m_normalF64Valid = other.m_normalF64Valid;
127  m_normalF64 = other.m_normalF64;
128 }
129 
130 //------------------------------------------------------------------------
131 
132 int Random::read(void* ptr, int size)
133 {
134  for (int i = 0; i < size; i++)
135  ((U8*)ptr)[i] = (U8)getU32();
136  return max(size, 0);
137 }
138 
139 //------------------------------------------------------------------------
140 
142 {
143  m_normalF32Valid = (!m_normalF32Valid);
144  if (!m_normalF32Valid)
145  return m_normalF32;
146 
147  F32 a, b, c;
148  do
149  {
150  a = (F32)getU32() * (2.0f / 4294967296.0f) + (1.0f / 4294967296.0f - 1.0f);
151  b = (F32)getU32() * (2.0f / 4294967296.0f) + (1.0f / 4294967296.0f - 1.0f);
152  c = a * a + b * b;
153  }
154  while (c >= 1.0f);
155 
156  c = sqrt(-2.0f * log(c) / c);
157  m_normalF32 = b * c;
158  return a * c;
159 }
160 
161 //------------------------------------------------------------------------
162 
164 {
165  m_normalF64Valid = (!m_normalF64Valid);
166  if (!m_normalF64Valid)
167  return m_normalF64;
168 
169  F64 a, b, c;
170  do
171  {
172  a = (F64)getU64() * (2.0 / 18446744073709551616.0) + (1.0 / 18446744073709551616.0 - 1.0);
173  b = (F64)getU64() * (2.0 / 18446744073709551616.0) + (1.0 / 18446744073709551616.0 - 1.0);
174  c = a * a + b * b;
175  }
176  while (c >= 1.0);
177 
178  c = sqrt(-2.0 * log(c) / c);
179  m_normalF64 = b * c;
180  return a * c;
181 }
182 
183 //------------------------------------------------------------------------
184 // Implementation wrappers.
185 //------------------------------------------------------------------------
186 
187 void Random::initImpl(void)
188 {
189 #if FW_USE_MERSENNE_TWISTER
190  m_impl = (void*)new genrand;
191 #else
192  m_impl = (void*)new RanrotA;
193 #endif
194 }
195 
196 //------------------------------------------------------------------------
197 
198 void Random::deinitImpl(void)
199 {
200 #if FW_USE_MERSENNE_TWISTER
201  delete (genrand*)m_impl;
202 #else
203  delete (RanrotA*)m_impl;
204 #endif
205 }
206 
207 //------------------------------------------------------------------------
208 
209 void Random::resetImpl(U32 seed)
210 {
211 #if FW_USE_MERSENNE_TWISTER
212  init_genrand((genrand*)m_impl, seed);
213 #else
214  ((RanrotA*)m_impl)->reset(seed);
215 #endif
216 }
217 
218 //------------------------------------------------------------------------
219 
220 void Random::assignImpl(const Random& other)
221 {
222 #if FW_USE_MERSENNE_TWISTER
223  *(genrand*)m_impl = *(const genrand*)other.m_impl;
224 #else
225  *(RanrotA*)m_impl = *(const RanrotA*)other.m_impl;
226 #endif
227 }
228 
229 //------------------------------------------------------------------------
230 
231 U32 Random::getImpl(void)
232 {
233 #if FW_USE_MERSENNE_TWISTER
234  return genrand_int32((genrand*)m_impl);
235 #else
236  return ((RanrotA*)m_impl)->get();
237 #endif
238 }
239 
240 //------------------------------------------------------------------------
F32 getF32Normal(void)
Definition: Random.cpp:141
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer
Definition: DLLImports.inl:315
void ** ptr
Definition: DLLImports.cpp:74
U64 getU64(void)
Definition: Random.hpp:59
double F64
Definition: Defs.hpp:90
virtual int read(void *ptr, int size)
Definition: Random.cpp:132
void failWin32Error(const char *funcName)
Definition: Defs.cpp:345
FW_CUDA_FUNC F32 sqrt(F32 a)
Definition: Math.hpp:39
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr const GLvoid GLenum usage GLuint shader GLenum type GLsizei const GLuint framebuffers GLsizei const GLuint renderbuffers GLuint v GLuint v GLenum GLenum GLenum GLuint GLint level GLsizei GLuint framebuffers GLuint const GLchar name GLenum GLintptr GLsizeiptr GLvoid data GLuint GLenum GLint param GLuint GLenum GLint param GLhandleARB programObj GLenum GLenum GLsizei GLsizei height GLenum GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid pixels GLint GLsizei const GLfloat value GLint GLfloat GLfloat v1 GLint GLfloat GLfloat GLfloat v2 GLint GLsizei const GLfloat value GLint GLsizei GLboolean const GLfloat value GLuint program GLuint GLfloat x
Definition: DLLImports.inl:363
float F32
Definition: Defs.hpp:89
void reset(U32 seed)
Definition: Random.cpp:60
FW_CUDA_FUNC T max(const VectorBase< T, L, S > &v)
Definition: Math.hpp:462
signed int S32
Definition: Defs.hpp:88
F64 getF64Normal(void)
Definition: Random.cpp:163
unsigned int U32
Definition: Defs.hpp:85
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction f
Definition: DLLImports.inl:88
#define FW_ARRAY_SIZE(X)
Definition: Defs.hpp:79
unsigned char U8
Definition: Defs.hpp:83
FW_CUDA_FUNC F64 log(F64 a)
Definition: Math.hpp:47
U32 getU32(void)
Definition: Random.hpp:51
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx void
Definition: DLLImports.inl:58
void reset(void)
Definition: Random.cpp:98
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr size
Definition: DLLImports.inl:319