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.32 by root, Thu Jan 18 19:32:37 2007 UTC vs.
Revision 1.45 by root, Sat May 26 15:44:05 2007 UTC

1#ifndef UTIL_H__ 1#ifndef UTIL_H__
2#define UTIL_H__ 2#define UTIL_H__
3 3
4//#define PREFER_MALLOC
5
4#if __GNUC__ >= 3 6#if __GNUC__ >= 3
5# define is_constant(c) __builtin_constant_p (c) 7# define is_constant(c) __builtin_constant_p (c)
8# define expect(expr,value) __builtin_expect ((expr),(value))
9# define prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality)
6#else 10#else
7# define is_constant(c) 0 11# define is_constant(c) 0
12# define expect(expr,value) (expr)
13# define prefetch(addr,rw,locality)
8#endif 14#endif
15
16// put into ifs if you are very sure that the expression
17// is mostly true or mosty false. note that these return
18// booleans, not the expression.
19#define expect_false(expr) expect ((expr) != 0, 0)
20#define expect_true(expr) expect ((expr) != 0, 1)
9 21
10#include <cstddef> 22#include <cstddef>
11#include <cmath> 23#include <cmath>
12#include <new> 24#include <new>
13#include <vector> 25#include <vector>
16 28
17#include <shstr.h> 29#include <shstr.h>
18#include <traits.h> 30#include <traits.h>
19 31
20// use a gcc extension for auto declarations until ISO C++ sanctifies them 32// use a gcc extension for auto declarations until ISO C++ sanctifies them
21#define AUTODECL(var,expr) typeof(expr) var = (expr) 33#define auto(var,expr) typeof(expr) var = (expr)
22 34
23// very ugly macro that basicaly declares and initialises a variable 35// very ugly macro that basicaly declares and initialises a variable
24// that is in scope for the next statement only 36// that is in scope for the next statement only
25// works only for stuff that can be assigned 0 and converts to false 37// works only for stuff that can be assigned 0 and converts to false
26// (note: works great for pointers) 38// (note: works great for pointers)
35#define IN_RANGE_EXC(val,beg,end) \ 47#define IN_RANGE_EXC(val,beg,end) \
36 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) 48 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg))
37 49
38void fork_abort (const char *msg); 50void fork_abort (const char *msg);
39 51
52// rationale for using (U) not (T) is to reduce signed/unsigned issues,
53// as a is often a constant while b is the variable. it is still a bug, though.
40template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } 54template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; }
41template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } 55template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; }
42template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? a : v >(T)b ? b : v; } 56template<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; }
43 57
44template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } 58template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
59
60template<typename T>
61static inline T
62lerp (T val, T min_in, T max_in, T min_out, T max_out)
63{
64 return (val - min_in) * (max_out - min_out) / (max_in - min_in) + min_out;
65}
66
67// lots of stuff taken from FXT
68
69/* Rotate right. This is used in various places for checksumming */
70//TODO: that sucks, use a better checksum algo
71static inline uint32_t
72rotate_right (uint32_t c, uint32_t count = 1)
73{
74 return (c << (32 - count)) | (c >> count);
75}
76
77static inline uint32_t
78rotate_left (uint32_t c, uint32_t count = 1)
79{
80 return (c >> (32 - count)) | (c << count);
81}
82
83// Return abs(a-b)
84// Both a and b must not have the most significant bit set
85static inline uint32_t
86upos_abs_diff (uint32_t a, uint32_t b)
87{
88 long d1 = b - a;
89 long d2 = (d1 & (d1 >> 31)) << 1;
90
91 return d1 - d2; // == (b - d) - (a + d);
92}
93
94// Both a and b must not have the most significant bit set
95static inline uint32_t
96upos_min (uint32_t a, uint32_t b)
97{
98 int32_t d = b - a;
99 d &= d >> 31;
100 return a + d;
101}
102
103// Both a and b must not have the most significant bit set
104static inline uint32_t
105upos_max (uint32_t a, uint32_t b)
106{
107 int32_t d = b - a;
108 d &= d >> 31;
109 return b - d;
110}
45 111
46// this is much faster than crossfires original algorithm 112// this is much faster than crossfires original algorithm
47// on modern cpus 113// on modern cpus
48inline int 114inline int
49isqrt (int n) 115isqrt (int n)
131 197
132// for symmetry 198// for symmetry
133template<typename T> 199template<typename T>
134inline void sfree (T *ptr, int n = 1) throw () 200inline void sfree (T *ptr, int n = 1) throw ()
135{ 201{
202#ifdef PREFER_MALLOC
203 free (ptr);
204#else
136 g_slice_free1 (n * sizeof (T), (void *)ptr); 205 g_slice_free1 (n * sizeof (T), (void *)ptr);
206#endif
137} 207}
138 208
139// a STL-compatible allocator that uses g_slice 209// a STL-compatible allocator that uses g_slice
140// boy, this is verbose 210// boy, this is verbose
141template<typename Tp> 211template<typename Tp>
194// P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213. 264// P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213.
195// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps 265// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
196// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps 266// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
197struct tausworthe_random_generator 267struct tausworthe_random_generator
198{ 268{
269 // generator
199 uint32_t state [4]; 270 uint32_t state [4];
200 271
201 tausworthe_random_generator (uint32_t seed); 272 void operator =(const tausworthe_random_generator &src)
273 {
274 state [0] = src.state [0];
275 state [1] = src.state [1];
276 state [2] = src.state [2];
277 state [3] = src.state [3];
278 }
279
280 void seed (uint32_t seed);
202 uint32_t next (); 281 uint32_t next ();
203 282
283 // uniform distribution
204 uint32_t operator ()(uint32_t r_max) 284 uint32_t operator ()(uint32_t num)
205 { 285 {
206 return next () % r_max; 286 return is_constant (num)
287 ? (next () * (uint64_t)num) >> 32U
288 : get_range (num);
207 } 289 }
208 290
209 // return a number within (min .. max) 291 // return a number within (min .. max)
210 int operator () (int r_min, int r_max) 292 int operator () (int r_min, int r_max)
211 { 293 {
212 return r_min + next () % max (r_max - r_min + 1, 1); 294 return is_constant (r_min) && is_constant (r_max) && r_min <= r_max
295 ? r_min + operator ()(r_max - r_min + 1)
296 : get_range (r_min, r_max);
213 } 297 }
214 298
215 double operator ()() 299 double operator ()()
216 { 300 {
217 return next () / (double)0xFFFFFFFFU; 301 return this->next () / (double)0xFFFFFFFFU;
218 } 302 }
303
304protected:
305 uint32_t get_range (uint32_t r_max);
306 int get_range (int r_min, int r_max);
219}; 307};
220 308
221typedef tausworthe_random_generator rand_gen; 309typedef tausworthe_random_generator rand_gen;
222 310
223extern rand_gen rndm; 311extern rand_gen rndm;
328 } 416 }
329 417
330 void erase (T *obj) 418 void erase (T *obj)
331 { 419 {
332 assert (obj->*index); 420 assert (obj->*index);
333 int pos = obj->*index; 421 unsigned int pos = obj->*index;
334 obj->*index = 0; 422 obj->*index = 0;
335 423
336 if (pos < this->size ()) 424 if (pos < this->size ())
337 { 425 {
338 (*this)[pos - 1] = (*this)[this->size () - 1]; 426 (*this)[pos - 1] = (*this)[this->size () - 1];
363// return current time as timestampe 451// return current time as timestampe
364tstamp now (); 452tstamp now ();
365 453
366int similar_direction (int a, int b); 454int similar_direction (int a, int b);
367 455
456// like printf, but returns a std::string
457const std::string format (const char *format, ...);
458
368#endif 459#endif
369 460

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines