--- deliantra/server/include/util.h 2007/01/15 01:39:42 1.29 +++ deliantra/server/include/util.h 2007/01/18 22:20:00 1.33 @@ -35,6 +35,14 @@ #define IN_RANGE_EXC(val,beg,end) \ ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) +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 @@ -60,7 +68,7 @@ ? (dx_ * 61685 + dy_ * 26870) >> 16 : (dy_ * 61685 + dx_ * 26870) >> 16; #else - return dx + dy - min (dx, dy) * 5 / 8; + return dx_ + dy_ - min (dx_, dy_) * 5 / 8; #endif } @@ -183,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 + (*this) (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 { @@ -309,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);