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.29 by root, Mon Jan 15 01:39:42 2007 UTC vs.
Revision 1.34 by root, Fri Jan 19 15:15:50 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
67/* 75/*
68 * absdir(int): Returns a number between 1 and 8, which represent 76 * absdir(int): Returns a number between 1 and 8, which represent
180 void destroy (pointer p) 188 void destroy (pointer p)
181 { 189 {
182 p->~Tp (); 190 p->~Tp ();
183 } 191 }
184}; 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 // generator
200 uint32_t state [4];
201
202 void operator =(const tausworthe_random_generator &src)
203 {
204 state [0] = src.state [0];
205 state [1] = src.state [1];
206 state [2] = src.state [2];
207 state [3] = src.state [3];
208 }
209
210 void seed (uint32_t seed);
211 uint32_t next ();
212
213 // uniform distribution
214 uint32_t operator ()(uint32_t r_max)
215 {
216 return is_constant (r_max)
217 ? this->next () % r_max
218 : get_range (r_max);
219 }
220
221 // return a number within (min .. max)
222 int operator () (int r_min, int r_max)
223 {
224 return is_constant (r_min) && is_constant (r_max)
225 ? r_min + (*this) (max (r_max - r_min + 1, 1))
226 : get_range (r_min, r_max);
227 }
228
229 double operator ()()
230 {
231 return this->next () / (double)0xFFFFFFFFU;
232 }
233
234protected:
235 uint32_t get_range (uint32_t r_max);
236 int get_range (int r_min, int r_max);
237};
238
239typedef tausworthe_random_generator rand_gen;
240
241extern rand_gen rndm;
185 242
186template<class T> 243template<class T>
187struct refptr 244struct refptr
188{ 245{
189 T *p; 246 T *p;
307 { 364 {
308 errase (&obj); 365 errase (&obj);
309 } 366 }
310}; 367};
311 368
312template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; }
313template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }
314template<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; }
315
316template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
317
318// basically does what strncpy should do, but appends "..." to strings exceeding length 369// basically does what strncpy should do, but appends "..." to strings exceeding length
319void assign (char *dst, const char *src, int maxlen); 370void assign (char *dst, const char *src, int maxlen);
320 371
321// type-safe version of assign 372// type-safe version of assign
322template<int N> 373template<int N>

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines