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.84 by root, Wed Dec 31 17:35:37 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
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)
673 erase (&obj); 688 erase (&obj);
674 } 689 }
675}; 690};
676 691
677// 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)
678void assign (char *dst, const char *src, int maxlen); 694int assign (char *dst, const char *src, int maxsize);
679 695
680// type-safe version of assign 696// type-safe version of assign
681template<int N> 697template<int N>
682inline void assign (char (&dst)[N], const char *src) 698inline int assign (char (&dst)[N], const char *src)
683{ 699{
684 assign ((char *)&dst, src, N); 700 return assign ((char *)&dst, src, N);
685} 701}
686 702
687typedef double tstamp; 703typedef double tstamp;
688 704
689// return current time as timestamp 705// return current time as timestamp

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines