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.82 by root, Sat Dec 27 02:33:32 2008 UTC vs.
Revision 1.88 by root, Tue May 5 04:51:56 2009 UTC

28 28
29#if __GNUC__ >= 3 29#if __GNUC__ >= 3
30# define is_constant(c) __builtin_constant_p (c) 30# define is_constant(c) __builtin_constant_p (c)
31# define expect(expr,value) __builtin_expect ((expr),(value)) 31# define expect(expr,value) __builtin_expect ((expr),(value))
32# define prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality) 32# define prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality)
33# define noinline __attribute__((__noinline__))
33#else 34#else
34# define is_constant(c) 0 35# define is_constant(c) 0
35# define expect(expr,value) (expr) 36# define expect(expr,value) (expr)
36# define prefetch(addr,rw,locality) 37# define prefetch(addr,rw,locality)
38# define noinline
37#endif 39#endif
38 40
39#if __GNUC__ < 4 || (__GNUC__ == 4 || __GNUC_MINOR__ < 4) 41#if __GNUC__ < 4 || (__GNUC__ == 4 || __GNUC_MINOR__ < 4)
40# define decltype(x) typeof(x) 42# define decltype(x) typeof(x)
41#endif 43#endif
42 44
43// put into ifs if you are very sure that the expression 45// put into ifs if you are very sure that the expression
44// is mostly true or mosty false. note that these return 46// is mostly true or mosty false. note that these return
45// booleans, not the expression. 47// booleans, not the expression.
46#define expect_false(expr) expect ((expr) != 0, 0) 48#define expect_false(expr) expect ((expr) ? 1 : 0, 0)
47#define expect_true(expr) expect ((expr) != 0, 1) 49#define expect_true(expr) expect ((expr) ? 1 : 0, 1)
48 50
49#include <pthread.h> 51#include <pthread.h>
50 52
51#include <cstddef> 53#include <cstddef>
52#include <cmath> 54#include <cmath>
116 118
117// sign0 returns -1, 0 or +1 119// sign0 returns -1, 0 or +1
118template<typename T> 120template<typename T>
119static inline T sign0 (T v) { return v ? sign (v) : 0; } 121static inline T sign0 (T v) { return v ? sign (v) : 0; }
120 122
123// div* only work correctly for div > 0
121// div, with correct rounding (< 0.5 downwards, >=0.5 upwards) 124// div, with correct rounding (< 0.5 downwards, >=0.5 upwards)
122template<typename T> static inline T div (T val, T div) { return (val + div / 2) / div; } 125template<typename T> static inline T div (T val, T div)
126{
127 return expect_false (val < 0) ? - ((-val + (div - 1) / 2) / div) : (val + div / 2) / div;
128}
123// div, round-up 129// div, round-up
124template<typename T> static inline T div_ru (T val, T div) { return (val + div - 1) / div; } 130template<typename T> static inline T div_ru (T val, T div)
131{
132 return expect_false (val < 0) ? - ((-val ) / div) : (val + div - 1) / div;
133}
125// div, round-down 134// div, round-down
126template<typename T> static inline T div_rd (T val, T div) { return (val ) / div; } 135template<typename T> static inline T div_rd (T val, T div)
136{
137 return expect_false (val < 0) ? - ((-val + (div - 1) ) / div) : (val ) / div;
138}
127 139
140// lerp* only work correctly for min_in < max_in
141// Linear intERPolate, scales val from min_in..max_in to min_out..max_out
128template<typename T> 142template<typename T>
129static inline T 143static inline T
130lerp (T val, T min_in, T max_in, T min_out, T max_out) 144lerp (T val, T min_in, T max_in, T min_out, T max_out)
131{ 145{
132 return min_out + div <T> ((val - min_in) * (max_out - min_out), max_in - min_in); 146 return min_out + div <T> ((val - min_in) * (max_out - min_out), max_in - min_in);
389// P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213. 403// P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213.
390// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps 404// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
391// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps 405// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
392struct tausworthe_random_generator 406struct tausworthe_random_generator
393{ 407{
394 // generator
395 uint32_t state [4]; 408 uint32_t state [4];
396 409
397 void operator =(const tausworthe_random_generator &src) 410 void operator =(const tausworthe_random_generator &src)
398 { 411 {
399 state [0] = src.state [0]; 412 state [0] = src.state [0];
402 state [3] = src.state [3]; 415 state [3] = src.state [3];
403 } 416 }
404 417
405 void seed (uint32_t seed); 418 void seed (uint32_t seed);
406 uint32_t next (); 419 uint32_t next ();
420};
407 421
422// Xorshift RNGs, George Marsaglia
423// http://www.jstatsoft.org/v08/i14/paper
424// this one is about 40% faster than the tausworthe one above (i.e. not much),
425// despite the inlining, and has the issue of only creating 2**32-1 numbers.
426// see also http://www.iro.umontreal.ca/~lecuyer/myftp/papers/xorshift.pdf
427struct xorshift_random_generator
428{
429 uint32_t x, y;
430
431 void operator =(const xorshift_random_generator &src)
432 {
433 x = src.x;
434 y = src.y;
435 }
436
437 void seed (uint32_t seed)
438 {
439 x = seed;
440 y = seed * 69069U;
441 }
442
443 uint32_t next ()
444 {
445 uint32_t t = x ^ (x << 10);
446 x = y;
447 y = y ^ (y >> 13) ^ t ^ (t >> 10);
448 return y;
449 }
450};
451
452template<class generator>
453struct random_number_generator : generator
454{
408 // uniform distribution, 0 .. max (0, num - 1) 455 // uniform distribution, 0 .. max (0, num - 1)
409 uint32_t operator ()(uint32_t num) 456 uint32_t operator ()(uint32_t num)
410 { 457 {
411 return !is_constant (num) ? get_range (num) // non-constant 458 return !is_constant (num) ? get_range (num) // non-constant
412 : num & (num - 1) ? (next () * (uint64_t)num) >> 32U // constant, non-power-of-two 459 : num & (num - 1) ? (this->next () * (uint64_t)num) >> 32U // constant, non-power-of-two
413 : next () & (num - 1); // constant, power-of-two 460 : this->next () & (num - 1); // constant, power-of-two
414 } 461 }
415 462
416 // return a number within (min .. max) 463 // return a number within (min .. max)
417 int operator () (int r_min, int r_max) 464 int operator () (int r_min, int r_max)
418 { 465 {
429protected: 476protected:
430 uint32_t get_range (uint32_t r_max); 477 uint32_t get_range (uint32_t r_max);
431 int get_range (int r_min, int r_max); 478 int get_range (int r_min, int r_max);
432}; 479};
433 480
434typedef tausworthe_random_generator rand_gen; 481typedef random_number_generator<tausworthe_random_generator> rand_gen;
435 482
436extern rand_gen rndm, rmg_rndm; 483extern rand_gen rndm, rmg_rndm;
437 484
438INTERFACE_CLASS (attachable) 485INTERFACE_CLASS (attachable)
439struct refcnt_base 486struct refcnt_base
508 555
509struct str_hash 556struct str_hash
510{ 557{
511 std::size_t operator ()(const char *s) const 558 std::size_t operator ()(const char *s) const
512 { 559 {
513 unsigned long hash = 0; 560#if 0
561 uint32_t hash = 0;
514 562
515 /* use the one-at-a-time hash function, which supposedly is 563 /* use the one-at-a-time hash function, which supposedly is
516 * better than the djb2-like one used by perl5.005, but 564 * better than the djb2-like one used by perl5.005, but
517 * certainly is better then the bug used here before. 565 * certainly is better then the bug used here before.
518 * see http://burtleburtle.net/bob/hash/doobs.html 566 * see http://burtleburtle.net/bob/hash/doobs.html
525 } 573 }
526 574
527 hash += hash << 3; 575 hash += hash << 3;
528 hash ^= hash >> 11; 576 hash ^= hash >> 11;
529 hash += hash << 15; 577 hash += hash << 15;
578#else
579 // use FNV-1a hash (http://isthe.com/chongo/tech/comp/fnv/)
580 // it is about twice as fast as the one-at-a-time one,
581 // with good distribution.
582 // FNV-1a is faster on many cpus because the multiplication
583 // runs concurrent with the looping logic.
584 uint32_t hash = 2166136261;
585
586 while (*s)
587 hash = (hash ^ *s++) * 16777619;
588#endif
530 589
531 return hash; 590 return hash;
532 } 591 }
533}; 592};
534 593
629 erase (&obj); 688 erase (&obj);
630 } 689 }
631}; 690};
632 691
633// basically does what strncpy should do, but appends "..." to strings exceeding length 692// basically does what strncpy should do, but appends "..." to strings exceeding length
693// returns the number of bytes actually used (including \0)
634void assign (char *dst, const char *src, int maxlen); 694int assign (char *dst, const char *src, int maxsize);
635 695
636// type-safe version of assign 696// type-safe version of assign
637template<int N> 697template<int N>
638inline void assign (char (&dst)[N], const char *src) 698inline int assign (char (&dst)[N], const char *src)
639{ 699{
640 assign ((char *)&dst, src, N); 700 return assign ((char *)&dst, src, N);
641} 701}
642 702
643typedef double tstamp; 703typedef double tstamp;
644 704
645// return current time as timestamp 705// return current time as timestamp

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines