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.32 by root, Thu Jan 18 19:32:37 2007 UTC

32 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) 32 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg))
33 33
34// in range excluding end 34// in range excluding end
35#define IN_RANGE_EXC(val,beg,end) \ 35#define IN_RANGE_EXC(val,beg,end) \
36 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) 36 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg))
37
38void fork_abort (const char *msg);
39
40template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; }
41template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)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; }
43
44template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
37 45
38// this is much faster than crossfires original algorithm 46// this is much faster than crossfires original algorithm
39// on modern cpus 47// on modern cpus
40inline int 48inline int
41isqrt (int n) 49isqrt (int n)
58#if 0 66#if 0
59 return dx_ > dy_ 67 return dx_ > dy_
60 ? (dx_ * 61685 + dy_ * 26870) >> 16 68 ? (dx_ * 61685 + dy_ * 26870) >> 16
61 : (dy_ * 61685 + dx_ * 26870) >> 16; 69 : (dy_ * 61685 + dx_ * 26870) >> 16;
62#else 70#else
63 return dx + dy - min (dx, dy) * 5 / 8; 71 return dx_ + dy_ - min (dx_, dy_) * 5 / 8;
64#endif 72#endif
65} 73}
66 74
75/*
76 * absdir(int): Returns a number between 1 and 8, which represent
77 * the "absolute" direction of a number (it actually takes care of
78 * "overflow" in previous calculations of a direction).
79 */
80inline int
81absdir (int d)
82{
83 return ((d - 1) & 7) + 1;
84}
67 85
68// makes dynamically allocated objects zero-initialised 86// makes dynamically allocated objects zero-initialised
69struct zero_initialised 87struct zero_initialised
70{ 88{
71 void *operator new (size_t s, void *p) 89 void *operator new (size_t s, void *p)
170 void destroy (pointer p) 188 void destroy (pointer p)
171 { 189 {
172 p->~Tp (); 190 p->~Tp ();
173 } 191 }
174}; 192};
193
194// 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
196// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
197struct tausworthe_random_generator
198{
199 uint32_t state [4];
200
201 tausworthe_random_generator (uint32_t seed);
202 uint32_t next ();
203
204 uint32_t operator ()(uint32_t r_max)
205 {
206 return next () % r_max;
207 }
208
209 // return a number within (min .. max)
210 int operator () (int r_min, int r_max)
211 {
212 return r_min + next () % max (r_max - r_min + 1, 1);
213 }
214
215 double operator ()()
216 {
217 return next () / (double)0xFFFFFFFFU;
218 }
219};
220
221typedef tausworthe_random_generator rand_gen;
222
223extern rand_gen rndm;
175 224
176template<class T> 225template<class T>
177struct refptr 226struct refptr
178{ 227{
179 T *p; 228 T *p;
297 { 346 {
298 errase (&obj); 347 errase (&obj);
299 } 348 }
300}; 349};
301 350
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 351// basically does what strncpy should do, but appends "..." to strings exceeding length
309void assign (char *dst, const char *src, int maxlen); 352void assign (char *dst, const char *src, int maxlen);
310 353
311// type-safe version of assign 354// type-safe version of assign
312template<int N> 355template<int N>

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines