--- deliantra/server/include/util.h 2007/01/15 01:25:41 1.28 +++ deliantra/server/include/util.h 2007/02/15 15:43:36 1.37 @@ -1,6 +1,8 @@ #ifndef UTIL_H__ #define UTIL_H__ +//#define PREFER_MALLOC + #if __GNUC__ >= 3 # define is_constant(c) __builtin_constant_p (c) #else @@ -35,6 +37,55 @@ #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); + +// rationale for using (U) not (T) is to reduce signed/unsigned issues, +// as a is often a constant while b is the variable. it is still a bug, though. +template static inline T min (T a, U b) { return (U)a < b ? (U)a : b; } +template static inline T max (T a, U b) { return (U)a > b ? (U)a : b; } +template static inline T clamp (T v, U a, V b) { return v < (T)a ? (T)a : v >(T)b ? (T)b : v; } + +template static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } + +// lots of stuff taken from FXT + +/* Rotate right. This is used in various places for checksumming */ +//TODO: this sucks, use a better checksum algo +static inline uint32_t +rotate_right (uint32_t c) +{ + return (c << 31) | (c >> 1); +} + +// Return abs(a-b) +// Both a and b must not have the most significant bit set +static inline uint32_t +upos_abs_diff (uint32_t a, uint32_t b) +{ + long d1 = b - a; + long d2 = (d1 & (d1 >> 31)) << 1; + + return d1 - d2; // == (b - d) - (a + d); +} + +// Both a and b must not have the most significant bit set +static inline uint32_t +upos_min (uint32_t a, uint32_t b) +{ + int32_t d = b - a; + d &= d >> 31; + return a + d; +} + +// Both a and b must not have the most significant bit set +static inline uint32_t +upos_max (uint32_t a, uint32_t b) +{ + int32_t d = b - a; + d &= d >> 31; + return b - d; +} + // this is much faster than crossfires original algorithm // on modern cpus inline int @@ -60,10 +111,20 @@ ? (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 } +/* + * absdir(int): Returns a number between 1 and 8, which represent + * the "absolute" direction of a number (it actually takes care of + * "overflow" in previous calculations of a direction). + */ +inline int +absdir (int d) +{ + return ((d - 1) & 7) + 1; +} // makes dynamically allocated objects zero-initialised struct zero_initialised @@ -115,7 +176,11 @@ template inline void sfree (T *ptr, int n = 1) throw () { +#ifdef PREFER_MALLOC + free (ptr); +#else g_slice_free1 (n * sizeof (T), (void *)ptr); +#endif } // a STL-compatible allocator that uses g_slice @@ -173,6 +238,55 @@ } }; +// 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 +{ + // generator + uint32_t state [4]; + + void operator =(const tausworthe_random_generator &src) + { + state [0] = src.state [0]; + state [1] = src.state [1]; + state [2] = src.state [2]; + state [3] = src.state [3]; + } + + void seed (uint32_t seed); + uint32_t next (); + + // uniform distribution + uint32_t operator ()(uint32_t r_max) + { + return is_constant (r_max) + ? this->next () % r_max + : get_range (r_max); + } + + // return a number within (min .. max) + int operator () (int r_min, int r_max) + { + return is_constant (r_min) && is_constant (r_max) + ? r_min + (*this) (max (r_max - r_min + 1, 1)) + : get_range (r_min, r_max); + } + + double operator ()() + { + return this->next () / (double)0xFFFFFFFFU; + } + +protected: + uint32_t get_range (uint32_t r_max); + int get_range (int r_min, int r_max); +}; + +typedef tausworthe_random_generator rand_gen; + +extern rand_gen rndm; + template struct refptr { @@ -299,12 +413,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);