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.28 by root, Mon Jan 15 01:25:41 2007 UTC vs.
Revision 1.36 by root, Thu Jan 25 03:54:45 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
33 35
34// in range excluding end 36// in range excluding end
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
40void fork_abort (const char *msg);
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.
44template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; }
45template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; }
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; }
47
48template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
49
38// this is much faster than crossfires original algorithm 50// this is much faster than crossfires original algorithm
39// on modern cpus 51// on modern cpus
40inline int 52inline int
41isqrt (int n) 53isqrt (int n)
42{ 54{
58#if 0 70#if 0
59 return dx_ > dy_ 71 return dx_ > dy_
60 ? (dx_ * 61685 + dy_ * 26870) >> 16 72 ? (dx_ * 61685 + dy_ * 26870) >> 16
61 : (dy_ * 61685 + dx_ * 26870) >> 16; 73 : (dy_ * 61685 + dx_ * 26870) >> 16;
62#else 74#else
63 return dx + dy - min (dx, dy) * 5 / 8; 75 return dx_ + dy_ - min (dx_, dy_) * 5 / 8;
64#endif 76#endif
65} 77}
66 78
79/*
80 * absdir(int): Returns a number between 1 and 8, which represent
81 * the "absolute" direction of a number (it actually takes care of
82 * "overflow" in previous calculations of a direction).
83 */
84inline int
85absdir (int d)
86{
87 return ((d - 1) & 7) + 1;
88}
67 89
68// makes dynamically allocated objects zero-initialised 90// makes dynamically allocated objects zero-initialised
69struct zero_initialised 91struct zero_initialised
70{ 92{
71 void *operator new (size_t s, void *p) 93 void *operator new (size_t s, void *p)
113 135
114// for symmetry 136// for symmetry
115template<typename T> 137template<typename T>
116inline void sfree (T *ptr, int n = 1) throw () 138inline void sfree (T *ptr, int n = 1) throw ()
117{ 139{
140#ifdef PREFER_MALLOC
141 free (ptr);
142#else
118 g_slice_free1 (n * sizeof (T), (void *)ptr); 143 g_slice_free1 (n * sizeof (T), (void *)ptr);
144#endif
119} 145}
120 146
121// a STL-compatible allocator that uses g_slice 147// a STL-compatible allocator that uses g_slice
122// boy, this is verbose 148// boy, this is verbose
123template<typename Tp> 149template<typename Tp>
170 void destroy (pointer p) 196 void destroy (pointer p)
171 { 197 {
172 p->~Tp (); 198 p->~Tp ();
173 } 199 }
174}; 200};
201
202// P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213.
203// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
204// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
205struct tausworthe_random_generator
206{
207 // generator
208 uint32_t state [4];
209
210 void operator =(const tausworthe_random_generator &src)
211 {
212 state [0] = src.state [0];
213 state [1] = src.state [1];
214 state [2] = src.state [2];
215 state [3] = src.state [3];
216 }
217
218 void seed (uint32_t seed);
219 uint32_t next ();
220
221 // uniform distribution
222 uint32_t operator ()(uint32_t r_max)
223 {
224 return is_constant (r_max)
225 ? this->next () % r_max
226 : get_range (r_max);
227 }
228
229 // return a number within (min .. max)
230 int operator () (int r_min, int r_max)
231 {
232 return is_constant (r_min) && is_constant (r_max)
233 ? r_min + (*this) (max (r_max - r_min + 1, 1))
234 : get_range (r_min, r_max);
235 }
236
237 double operator ()()
238 {
239 return this->next () / (double)0xFFFFFFFFU;
240 }
241
242protected:
243 uint32_t get_range (uint32_t r_max);
244 int get_range (int r_min, int r_max);
245};
246
247typedef tausworthe_random_generator rand_gen;
248
249extern rand_gen rndm;
175 250
176template<class T> 251template<class T>
177struct refptr 252struct refptr
178{ 253{
179 T *p; 254 T *p;
297 { 372 {
298 errase (&obj); 373 errase (&obj);
299 } 374 }
300}; 375};
301 376
302template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; }
303template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }
304template<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; }
305
306template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
307
308// basically does what strncpy should do, but appends "..." to strings exceeding length 377// basically does what strncpy should do, but appends "..." to strings exceeding length
309void assign (char *dst, const char *src, int maxlen); 378void assign (char *dst, const char *src, int maxlen);
310 379
311// type-safe version of assign 380// type-safe version of assign
312template<int N> 381template<int N>

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines