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.78 by root, Thu Dec 4 03:48:19 2008 UTC vs.
Revision 1.83 by root, Tue Dec 30 07:24:16 2008 UTC

72#endif 72#endif
73 73
74// use C0X decltype for auto declarations until ISO C++ sanctifies them (if ever) 74// use C0X decltype for auto declarations until ISO C++ sanctifies them (if ever)
75#define auto(var,expr) decltype(expr) var = (expr) 75#define auto(var,expr) decltype(expr) var = (expr)
76 76
77// very ugly macro that basicaly declares and initialises a variable 77// very ugly macro that basically declares and initialises a variable
78// that is in scope for the next statement only 78// that is in scope for the next statement only
79// works only for stuff that can be assigned 0 and converts to false 79// works only for stuff that can be assigned 0 and converts to false
80// (note: works great for pointers) 80// (note: works great for pointers)
81// most ugly macro I ever wrote 81// most ugly macro I ever wrote
82#define statementvar(type, name, value) if (type name = 0) { } else if (((name) = (value)), 1) 82#define statementvar(type, name, value) if (type name = 0) { } else if (((name) = (value)), 1)
96// as a is often a constant while b is the variable. it is still a bug, though. 96// as a is often a constant while b is the variable. it is still a bug, though.
97template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; } 97template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; }
98template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; } 98template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; }
99template<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; } 99template<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; }
100 100
101template<typename T> static inline void min_it (T &v, T m) { v = min (v, m); } 101template<typename T, typename U> static inline void min_it (T &v, U m) { v = min (v, (T)m); }
102template<typename T> static inline void max_it (T &v, T m) { v = max (v, m); } 102template<typename T, typename U> static inline void max_it (T &v, U m) { v = max (v, (T)m); }
103template<typename T> static inline void clamp_it (T &v, T a, T b) { v = clamp (v, a, b); } 103template<typename T, typename U, typename V> static inline void clamp_it (T &v, U a, V b) { v = clamp (v, (T)a, (T)b); }
104 104
105template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } 105template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
106 106
107template<typename T, typename U, typename V> static inline T min (T a, U b, V c) { return min (a, min (b, c)); } 107template<typename T, typename U, typename V> static inline T min (T a, U b, V c) { return min (a, min (b, c)); }
108template<typename T, typename U, typename V> static inline T max (T a, U b, V c) { return max (a, max (b, c)); } 108template<typename T, typename U, typename V> static inline T max (T a, U b, V c) { return max (a, max (b, c)); }
109
110// sign returns -1 or +1
111template<typename T>
112static inline T sign (T v) { return v < 0 ? -1 : +1; }
113// relies on 2c representation
114template<>
115inline sint8 sign (sint8 v) { return 1 - (sint8 (uint8 (v) >> 7) * 2); }
116
117// sign0 returns -1, 0 or +1
118template<typename T>
119static inline T sign0 (T v) { return v ? sign (v) : 0; }
109 120
110// div, with correct rounding (< 0.5 downwards, >=0.5 upwards) 121// div, with correct rounding (< 0.5 downwards, >=0.5 upwards)
111template<typename T> static inline T div (T val, T div) { return (val + div / 2) / div; } 122template<typename T> static inline T div (T val, T div) { return (val + div / 2) / div; }
112// div, round-up 123// div, round-up
113template<typename T> static inline T div_ru (T val, T div) { return (val + div - 1) / div; } 124template<typename T> static inline T div_ru (T val, T div) { return (val + div - 1) / div; }
378// P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213. 389// P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213.
379// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps 390// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
380// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps 391// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
381struct tausworthe_random_generator 392struct tausworthe_random_generator
382{ 393{
383 // generator
384 uint32_t state [4]; 394 uint32_t state [4];
385 395
386 void operator =(const tausworthe_random_generator &src) 396 void operator =(const tausworthe_random_generator &src)
387 { 397 {
388 state [0] = src.state [0]; 398 state [0] = src.state [0];
391 state [3] = src.state [3]; 401 state [3] = src.state [3];
392 } 402 }
393 403
394 void seed (uint32_t seed); 404 void seed (uint32_t seed);
395 uint32_t next (); 405 uint32_t next ();
406};
396 407
408// Xorshift RNGs, George Marsaglia
409// http://www.jstatsoft.org/v08/i14/paper
410// this one is about 40% faster than the tausworthe one above (i.e. not much),
411// despite the inlining, and has the issue of only creating 2**32-1 numbers.
412struct xorshift_random_generator
413{
414 uint32_t x, y;
415
416 void operator =(const xorshift_random_generator &src)
417 {
418 x = src.x;
419 y = src.y;
420 }
421
422 void seed (uint32_t seed)
423 {
424 x = seed;
425 y = seed * 69069U;
426 }
427
428 uint32_t next ()
429 {
430 uint32_t t = x ^ (x << 10);
431 x = y;
432 y = y ^ (y >> 13) ^ t ^ (t >> 10);
433 return y;
434 }
435};
436
437template<class generator>
438struct random_number_generator : generator
439{
397 // uniform distribution, 0 .. max (0, num - 1) 440 // uniform distribution, 0 .. max (0, num - 1)
398 uint32_t operator ()(uint32_t num) 441 uint32_t operator ()(uint32_t num)
399 { 442 {
400 return is_constant (num) 443 return !is_constant (num) ? get_range (num) // non-constant
401 ? (next () * (uint64_t)num) >> 32U 444 : num & (num - 1) ? (this->next () * (uint64_t)num) >> 32U // constant, non-power-of-two
402 : get_range (num); 445 : this->next () & (num - 1); // constant, power-of-two
403 } 446 }
404 447
405 // return a number within (min .. max) 448 // return a number within (min .. max)
406 int operator () (int r_min, int r_max) 449 int operator () (int r_min, int r_max)
407 { 450 {
418protected: 461protected:
419 uint32_t get_range (uint32_t r_max); 462 uint32_t get_range (uint32_t r_max);
420 int get_range (int r_min, int r_max); 463 int get_range (int r_min, int r_max);
421}; 464};
422 465
423typedef tausworthe_random_generator rand_gen; 466typedef random_number_generator<tausworthe_random_generator> rand_gen;
424 467
425extern rand_gen rndm, rmg_rndm; 468extern rand_gen rndm, rmg_rndm;
426 469
427INTERFACE_CLASS (attachable) 470INTERFACE_CLASS (attachable)
428struct refcnt_base 471struct refcnt_base

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines