--- deliantra/server/include/util.h 2007/01/15 02:39:41 1.31 +++ deliantra/server/include/util.h 2007/01/18 19:32:37 1.32 @@ -37,6 +37,12 @@ void fork_abort (const char *msg); +template static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } +template static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } +template static inline T clamp (T v, U a, V b) { return v < (T)a ? a : v >(T)b ? b : v; } + +template static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } + // this is much faster than crossfires original algorithm // on modern cpus inline int @@ -185,6 +191,37 @@ } }; +// P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213. +// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps +// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps +struct tausworthe_random_generator +{ + uint32_t state [4]; + + tausworthe_random_generator (uint32_t seed); + uint32_t next (); + + uint32_t operator ()(uint32_t r_max) + { + return next () % r_max; + } + + // return a number within (min .. max) + int operator () (int r_min, int r_max) + { + return r_min + next () % max (r_max - r_min + 1, 1); + } + + double operator ()() + { + return next () / (double)0xFFFFFFFFU; + } +}; + +typedef tausworthe_random_generator rand_gen; + +extern rand_gen rndm; + template struct refptr { @@ -311,12 +348,6 @@ } }; -template static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } -template static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } -template static inline T clamp (T v, U a, V b) { return v < (T)a ? a : v >(T)b ? b : v; } - -template static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } - // basically does what strncpy should do, but appends "..." to strings exceeding length void assign (char *dst, const char *src, int maxlen);