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.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
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; }
45 49
46// this is much faster than crossfires original algorithm 50// this is much faster than crossfires original algorithm
47// on modern cpus 51// on modern cpus
131 135
132// for symmetry 136// for symmetry
133template<typename T> 137template<typename T>
134inline void sfree (T *ptr, int n = 1) throw () 138inline void sfree (T *ptr, int n = 1) throw ()
135{ 139{
140#ifdef PREFER_MALLOC
141 free (ptr);
142#else
136 g_slice_free1 (n * sizeof (T), (void *)ptr); 143 g_slice_free1 (n * sizeof (T), (void *)ptr);
144#endif
137} 145}
138 146
139// a STL-compatible allocator that uses g_slice 147// a STL-compatible allocator that uses g_slice
140// boy, this is verbose 148// boy, this is verbose
141template<typename Tp> 149template<typename Tp>
194// P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213. 202// 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 203// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
196// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps 204// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
197struct tausworthe_random_generator 205struct tausworthe_random_generator
198{ 206{
207 // generator
199 uint32_t state [4]; 208 uint32_t state [4];
200 209
201 tausworthe_random_generator (uint32_t seed); 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);
202 uint32_t next (); 219 uint32_t next ();
203 220
221 // uniform distribution
204 uint32_t operator ()(uint32_t r_max) 222 uint32_t operator ()(uint32_t r_max)
205 { 223 {
206 return next () % r_max; 224 return is_constant (r_max)
225 ? this->next () % r_max
226 : get_range (r_max);
207 } 227 }
208 228
209 // return a number within (min .. max) 229 // return a number within (min .. max)
210 int operator () (int r_min, int r_max) 230 int operator () (int r_min, int r_max)
211 { 231 {
212 return r_min + next () % max (r_max - r_min + 1, 1); 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);
213 } 235 }
214 236
215 double operator ()() 237 double operator ()()
216 { 238 {
217 return next () / (double)0xFFFFFFFFU; 239 return this->next () / (double)0xFFFFFFFFU;
218 } 240 }
241
242protected:
243 uint32_t get_range (uint32_t r_max);
244 int get_range (int r_min, int r_max);
219}; 245};
220 246
221typedef tausworthe_random_generator rand_gen; 247typedef tausworthe_random_generator rand_gen;
222 248
223extern rand_gen rndm; 249extern rand_gen rndm;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines