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.35 by root, Fri Jan 19 22:47:57 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
40// rationale for using (U) not (T) is to reduce signed/unsigned issues,
41// as a is often a constant while b is the variable. it is still a bug, though.
42template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; }
43template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; }
44template<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; }
45
46template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
37 47
38// this is much faster than crossfires original algorithm 48// this is much faster than crossfires original algorithm
39// on modern cpus 49// on modern cpus
40inline int 50inline int
41isqrt (int n) 51isqrt (int n)
58#if 0 68#if 0
59 return dx_ > dy_ 69 return dx_ > dy_
60 ? (dx_ * 61685 + dy_ * 26870) >> 16 70 ? (dx_ * 61685 + dy_ * 26870) >> 16
61 : (dy_ * 61685 + dx_ * 26870) >> 16; 71 : (dy_ * 61685 + dx_ * 26870) >> 16;
62#else 72#else
63 return dx + dy - min (dx, dy) * 5 / 8; 73 return dx_ + dy_ - min (dx_, dy_) * 5 / 8;
64#endif 74#endif
65} 75}
66 76
77/*
78 * absdir(int): Returns a number between 1 and 8, which represent
79 * the "absolute" direction of a number (it actually takes care of
80 * "overflow" in previous calculations of a direction).
81 */
82inline int
83absdir (int d)
84{
85 return ((d - 1) & 7) + 1;
86}
67 87
68// makes dynamically allocated objects zero-initialised 88// makes dynamically allocated objects zero-initialised
69struct zero_initialised 89struct zero_initialised
70{ 90{
71 void *operator new (size_t s, void *p) 91 void *operator new (size_t s, void *p)
170 void destroy (pointer p) 190 void destroy (pointer p)
171 { 191 {
172 p->~Tp (); 192 p->~Tp ();
173 } 193 }
174}; 194};
195
196// P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213.
197// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
198// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
199struct tausworthe_random_generator
200{
201 // generator
202 uint32_t state [4];
203
204 void operator =(const tausworthe_random_generator &src)
205 {
206 state [0] = src.state [0];
207 state [1] = src.state [1];
208 state [2] = src.state [2];
209 state [3] = src.state [3];
210 }
211
212 void seed (uint32_t seed);
213 uint32_t next ();
214
215 // uniform distribution
216 uint32_t operator ()(uint32_t r_max)
217 {
218 return is_constant (r_max)
219 ? this->next () % r_max
220 : get_range (r_max);
221 }
222
223 // return a number within (min .. max)
224 int operator () (int r_min, int r_max)
225 {
226 return is_constant (r_min) && is_constant (r_max)
227 ? r_min + (*this) (max (r_max - r_min + 1, 1))
228 : get_range (r_min, r_max);
229 }
230
231 double operator ()()
232 {
233 return this->next () / (double)0xFFFFFFFFU;
234 }
235
236protected:
237 uint32_t get_range (uint32_t r_max);
238 int get_range (int r_min, int r_max);
239};
240
241typedef tausworthe_random_generator rand_gen;
242
243extern rand_gen rndm;
175 244
176template<class T> 245template<class T>
177struct refptr 246struct refptr
178{ 247{
179 T *p; 248 T *p;
297 { 366 {
298 errase (&obj); 367 errase (&obj);
299 } 368 }
300}; 369};
301 370
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 371// basically does what strncpy should do, but appends "..." to strings exceeding length
309void assign (char *dst, const char *src, int maxlen); 372void assign (char *dst, const char *src, int maxlen);
310 373
311// type-safe version of assign 374// type-safe version of assign
312template<int N> 375template<int N>

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines