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.83 by root, Tue Dec 30 07:24:16 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);
407 421
408// Xorshift RNGs, George Marsaglia 422// Xorshift RNGs, George Marsaglia
409// http://www.jstatsoft.org/v08/i14/paper 423// http://www.jstatsoft.org/v08/i14/paper
410// this one is about 40% faster than the tausworthe one above (i.e. not much), 424// 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. 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
412struct xorshift_random_generator 427struct xorshift_random_generator
413{ 428{
414 uint32_t x, y; 429 uint32_t x, y;
415 430
416 void operator =(const xorshift_random_generator &src) 431 void operator =(const xorshift_random_generator &src)
540 555
541struct str_hash 556struct str_hash
542{ 557{
543 std::size_t operator ()(const char *s) const 558 std::size_t operator ()(const char *s) const
544 { 559 {
545 unsigned long hash = 0; 560#if 0
561 uint32_t hash = 0;
546 562
547 /* use the one-at-a-time hash function, which supposedly is 563 /* use the one-at-a-time hash function, which supposedly is
548 * better than the djb2-like one used by perl5.005, but 564 * better than the djb2-like one used by perl5.005, but
549 * certainly is better then the bug used here before. 565 * certainly is better then the bug used here before.
550 * see http://burtleburtle.net/bob/hash/doobs.html 566 * see http://burtleburtle.net/bob/hash/doobs.html
557 } 573 }
558 574
559 hash += hash << 3; 575 hash += hash << 3;
560 hash ^= hash >> 11; 576 hash ^= hash >> 11;
561 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
562 589
563 return hash; 590 return hash;
564 } 591 }
565}; 592};
566 593
661 erase (&obj); 688 erase (&obj);
662 } 689 }
663}; 690};
664 691
665// 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)
666void assign (char *dst, const char *src, int maxlen); 694int assign (char *dst, const char *src, int maxsize);
667 695
668// type-safe version of assign 696// type-safe version of assign
669template<int N> 697template<int N>
670inline void assign (char (&dst)[N], const char *src) 698inline int assign (char (&dst)[N], const char *src)
671{ 699{
672 assign ((char *)&dst, src, N); 700 return assign ((char *)&dst, src, N);
673} 701}
674 702
675typedef double tstamp; 703typedef double tstamp;
676 704
677// return current time as timestamp 705// return current time as timestamp

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines