ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/util.h
(Generate patch)

Comparing deliantra/server/include/util.h (file contents):
Revision 1.32 by root, Thu Jan 18 19:32:37 2007 UTC vs.
Revision 1.35 by root, Fri Jan 19 22:47:57 2007 UTC

35#define IN_RANGE_EXC(val,beg,end) \ 35#define IN_RANGE_EXC(val,beg,end) \
36 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) 36 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg))
37 37
38void fork_abort (const char *msg); 38void fork_abort (const char *msg);
39 39
40// rationale for using (U) not (T) is to reduce signed/unsigned issues,
41// as a is often a constant while b is the variable. it is still a bug, though.
40template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } 42template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; }
41template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } 43template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; }
42template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? a : v >(T)b ? b : v; } 44template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? (T)a : v >(T)b ? (T)b : v; }
43 45
44template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } 46template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
45 47
46// this is much faster than crossfires original algorithm 48// this is much faster than crossfires original algorithm
47// on modern cpus 49// on modern cpus
194// P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213. 196// P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213.
195// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps 197// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
196// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps 198// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
197struct tausworthe_random_generator 199struct tausworthe_random_generator
198{ 200{
201 // generator
199 uint32_t state [4]; 202 uint32_t state [4];
200 203
201 tausworthe_random_generator (uint32_t seed); 204 void operator =(const tausworthe_random_generator &src)
205 {
206 state [0] = src.state [0];
207 state [1] = src.state [1];
208 state [2] = src.state [2];
209 state [3] = src.state [3];
210 }
211
212 void seed (uint32_t seed);
202 uint32_t next (); 213 uint32_t next ();
203 214
215 // uniform distribution
204 uint32_t operator ()(uint32_t r_max) 216 uint32_t operator ()(uint32_t r_max)
205 { 217 {
206 return next () % r_max; 218 return is_constant (r_max)
219 ? this->next () % r_max
220 : get_range (r_max);
207 } 221 }
208 222
209 // return a number within (min .. max) 223 // return a number within (min .. max)
210 int operator () (int r_min, int r_max) 224 int operator () (int r_min, int r_max)
211 { 225 {
212 return r_min + next () % max (r_max - r_min + 1, 1); 226 return is_constant (r_min) && is_constant (r_max)
227 ? r_min + (*this) (max (r_max - r_min + 1, 1))
228 : get_range (r_min, r_max);
213 } 229 }
214 230
215 double operator ()() 231 double operator ()()
216 { 232 {
217 return next () / (double)0xFFFFFFFFU; 233 return this->next () / (double)0xFFFFFFFFU;
218 } 234 }
235
236protected:
237 uint32_t get_range (uint32_t r_max);
238 int get_range (int r_min, int r_max);
219}; 239};
220 240
221typedef tausworthe_random_generator rand_gen; 241typedef tausworthe_random_generator rand_gen;
222 242
223extern rand_gen rndm; 243extern rand_gen rndm;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines