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.44 by root, Fri May 11 08:00:00 2007 UTC

1#ifndef UTIL_H__ 1#ifndef UTIL_H__
2#define UTIL_H__ 2#define UTIL_H__
3
4//#define PREFER_MALLOC
3 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)
6#else 8#else
7# define is_constant(c) 0 9# define is_constant(c) 0
16 18
17#include <shstr.h> 19#include <shstr.h>
18#include <traits.h> 20#include <traits.h>
19 21
20// use a gcc extension for auto declarations until ISO C++ sanctifies them 22// use a gcc extension for auto declarations until ISO C++ sanctifies them
21#define AUTODECL(var,expr) typeof(expr) var = (expr) 23#define auto(var,expr) typeof(expr) var = (expr)
22 24
23// very ugly macro that basicaly declares and initialises a variable 25// very ugly macro that basicaly declares and initialises a variable
24// that is in scope for the next statement only 26// that is in scope for the next statement only
25// works only for stuff that can be assigned 0 and converts to false 27// works only for stuff that can be assigned 0 and converts to false
26// (note: works great for pointers) 28// (note: works great for pointers)
35#define IN_RANGE_EXC(val,beg,end) \ 37#define IN_RANGE_EXC(val,beg,end) \
36 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) 38 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg))
37 39
38void fork_abort (const char *msg); 40void fork_abort (const char *msg);
39 41
42// rationale for using (U) not (T) is to reduce signed/unsigned issues,
43// 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; } 44template<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; } 45template<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; } 46template<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 47
44template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } 48template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
49
50template<typename T>
51static inline T
52lerp (T val, T min_in, T max_in, T min_out, T max_out)
53{
54 return (val - min_in) * (max_out - min_out) / (max_in - min_in) + min_out;
55}
56
57// lots of stuff taken from FXT
58
59/* Rotate right. This is used in various places for checksumming */
60//TODO: that sucks, use a better checksum algo
61static inline uint32_t
62rotate_right (uint32_t c, uint32_t count = 1)
63{
64 return (c << (32 - count)) | (c >> count);
65}
66
67static inline uint32_t
68rotate_left (uint32_t c, uint32_t count = 1)
69{
70 return (c >> (32 - count)) | (c << count);
71}
72
73// Return abs(a-b)
74// Both a and b must not have the most significant bit set
75static inline uint32_t
76upos_abs_diff (uint32_t a, uint32_t b)
77{
78 long d1 = b - a;
79 long d2 = (d1 & (d1 >> 31)) << 1;
80
81 return d1 - d2; // == (b - d) - (a + d);
82}
83
84// Both a and b must not have the most significant bit set
85static inline uint32_t
86upos_min (uint32_t a, uint32_t b)
87{
88 int32_t d = b - a;
89 d &= d >> 31;
90 return a + d;
91}
92
93// Both a and b must not have the most significant bit set
94static inline uint32_t
95upos_max (uint32_t a, uint32_t b)
96{
97 int32_t d = b - a;
98 d &= d >> 31;
99 return b - d;
100}
45 101
46// this is much faster than crossfires original algorithm 102// this is much faster than crossfires original algorithm
47// on modern cpus 103// on modern cpus
48inline int 104inline int
49isqrt (int n) 105isqrt (int n)
131 187
132// for symmetry 188// for symmetry
133template<typename T> 189template<typename T>
134inline void sfree (T *ptr, int n = 1) throw () 190inline void sfree (T *ptr, int n = 1) throw ()
135{ 191{
192#ifdef PREFER_MALLOC
193 free (ptr);
194#else
136 g_slice_free1 (n * sizeof (T), (void *)ptr); 195 g_slice_free1 (n * sizeof (T), (void *)ptr);
196#endif
137} 197}
138 198
139// a STL-compatible allocator that uses g_slice 199// a STL-compatible allocator that uses g_slice
140// boy, this is verbose 200// boy, this is verbose
141template<typename Tp> 201template<typename Tp>
194// P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213. 254// 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 255// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
196// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps 256// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
197struct tausworthe_random_generator 257struct tausworthe_random_generator
198{ 258{
259 // generator
199 uint32_t state [4]; 260 uint32_t state [4];
200 261
201 tausworthe_random_generator (uint32_t seed); 262 void operator =(const tausworthe_random_generator &src)
263 {
264 state [0] = src.state [0];
265 state [1] = src.state [1];
266 state [2] = src.state [2];
267 state [3] = src.state [3];
268 }
269
270 void seed (uint32_t seed);
202 uint32_t next (); 271 uint32_t next ();
203 272
273 // uniform distribution
204 uint32_t operator ()(uint32_t r_max) 274 uint32_t operator ()(uint32_t num)
205 { 275 {
206 return next () % r_max; 276 return is_constant (num)
277 ? (next () * (uint64_t)num) >> 32U
278 : get_range (num);
207 } 279 }
208 280
209 // return a number within (min .. max) 281 // return a number within (min .. max)
210 int operator () (int r_min, int r_max) 282 int operator () (int r_min, int r_max)
211 { 283 {
212 return r_min + next () % max (r_max - r_min + 1, 1); 284 return is_constant (r_min) && is_constant (r_max) && r_min <= r_max
285 ? r_min + operator ()(r_max - r_min + 1)
286 : get_range (r_min, r_max);
213 } 287 }
214 288
215 double operator ()() 289 double operator ()()
216 { 290 {
217 return next () / (double)0xFFFFFFFFU; 291 return this->next () / (double)0xFFFFFFFFU;
218 } 292 }
293
294protected:
295 uint32_t get_range (uint32_t r_max);
296 int get_range (int r_min, int r_max);
219}; 297};
220 298
221typedef tausworthe_random_generator rand_gen; 299typedef tausworthe_random_generator rand_gen;
222 300
223extern rand_gen rndm; 301extern rand_gen rndm;
328 } 406 }
329 407
330 void erase (T *obj) 408 void erase (T *obj)
331 { 409 {
332 assert (obj->*index); 410 assert (obj->*index);
333 int pos = obj->*index; 411 unsigned int pos = obj->*index;
334 obj->*index = 0; 412 obj->*index = 0;
335 413
336 if (pos < this->size ()) 414 if (pos < this->size ())
337 { 415 {
338 (*this)[pos - 1] = (*this)[this->size () - 1]; 416 (*this)[pos - 1] = (*this)[this->size () - 1];
363// return current time as timestampe 441// return current time as timestampe
364tstamp now (); 442tstamp now ();
365 443
366int similar_direction (int a, int b); 444int similar_direction (int a, int b);
367 445
446// like printf, but returns a std::string
447const std::string format (const char *format, ...);
448
368#endif 449#endif
369 450

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines