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 |
… | |
… | |
32 | ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
34 | ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
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)) |
|
|
39 | |
|
|
40 | void 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. |
|
|
44 | template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; } |
|
|
45 | template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; } |
|
|
46 | template<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 | |
|
|
48 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
|
|
49 | |
|
|
50 | // lots of stuff taken from FXT |
|
|
51 | |
|
|
52 | /* Rotate right. This is used in various places for checksumming */ |
|
|
53 | //TODO: this sucks, use a better checksum algo |
|
|
54 | static inline uint32_t |
|
|
55 | rotate_right (uint32_t c) |
|
|
56 | { |
|
|
57 | return (c << 31) | (c >> 1); |
|
|
58 | } |
|
|
59 | |
|
|
60 | // Return abs(a-b) |
|
|
61 | // Both a and b must not have the most significant bit set |
|
|
62 | static inline uint32_t |
|
|
63 | upos_abs_diff (uint32_t a, uint32_t b) |
|
|
64 | { |
|
|
65 | long d1 = b - a; |
|
|
66 | long d2 = (d1 & (d1 >> 31)) << 1; |
|
|
67 | |
|
|
68 | return d1 - d2; // == (b - d) - (a + d); |
|
|
69 | } |
|
|
70 | |
|
|
71 | // Both a and b must not have the most significant bit set |
|
|
72 | static inline uint32_t |
|
|
73 | upos_min (uint32_t a, uint32_t b) |
|
|
74 | { |
|
|
75 | int32_t d = b - a; |
|
|
76 | d &= d >> 31; |
|
|
77 | return a + d; |
|
|
78 | } |
|
|
79 | |
|
|
80 | // Both a and b must not have the most significant bit set |
|
|
81 | static inline uint32_t |
|
|
82 | upos_max (uint32_t a, uint32_t b) |
|
|
83 | { |
|
|
84 | int32_t d = b - a; |
|
|
85 | d &= d >> 31; |
|
|
86 | return b - d; |
|
|
87 | } |
37 | |
88 | |
38 | // this is much faster than crossfires original algorithm |
89 | // this is much faster than crossfires original algorithm |
39 | // on modern cpus |
90 | // on modern cpus |
40 | inline int |
91 | inline int |
41 | isqrt (int n) |
92 | isqrt (int n) |
… | |
… | |
58 | #if 0 |
109 | #if 0 |
59 | return dx_ > dy_ |
110 | return dx_ > dy_ |
60 | ? (dx_ * 61685 + dy_ * 26870) >> 16 |
111 | ? (dx_ * 61685 + dy_ * 26870) >> 16 |
61 | : (dy_ * 61685 + dx_ * 26870) >> 16; |
112 | : (dy_ * 61685 + dx_ * 26870) >> 16; |
62 | #else |
113 | #else |
63 | return dx + dy - min (dx, dy) * 5 / 8; |
114 | return dx_ + dy_ - min (dx_, dy_) * 5 / 8; |
64 | #endif |
115 | #endif |
65 | } |
116 | } |
66 | |
117 | |
|
|
118 | /* |
|
|
119 | * absdir(int): Returns a number between 1 and 8, which represent |
|
|
120 | * the "absolute" direction of a number (it actually takes care of |
|
|
121 | * "overflow" in previous calculations of a direction). |
|
|
122 | */ |
|
|
123 | inline int |
|
|
124 | absdir (int d) |
|
|
125 | { |
|
|
126 | return ((d - 1) & 7) + 1; |
|
|
127 | } |
67 | |
128 | |
68 | // makes dynamically allocated objects zero-initialised |
129 | // makes dynamically allocated objects zero-initialised |
69 | struct zero_initialised |
130 | struct zero_initialised |
70 | { |
131 | { |
71 | void *operator new (size_t s, void *p) |
132 | void *operator new (size_t s, void *p) |
… | |
… | |
113 | |
174 | |
114 | // for symmetry |
175 | // for symmetry |
115 | template<typename T> |
176 | template<typename T> |
116 | inline void sfree (T *ptr, int n = 1) throw () |
177 | inline void sfree (T *ptr, int n = 1) throw () |
117 | { |
178 | { |
|
|
179 | #ifdef PREFER_MALLOC |
|
|
180 | free (ptr); |
|
|
181 | #else |
118 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
182 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
|
|
183 | #endif |
119 | } |
184 | } |
120 | |
185 | |
121 | // a STL-compatible allocator that uses g_slice |
186 | // a STL-compatible allocator that uses g_slice |
122 | // boy, this is verbose |
187 | // boy, this is verbose |
123 | template<typename Tp> |
188 | template<typename Tp> |
… | |
… | |
170 | void destroy (pointer p) |
235 | void destroy (pointer p) |
171 | { |
236 | { |
172 | p->~Tp (); |
237 | p->~Tp (); |
173 | } |
238 | } |
174 | }; |
239 | }; |
|
|
240 | |
|
|
241 | // P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213. |
|
|
242 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps |
|
|
243 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps |
|
|
244 | struct tausworthe_random_generator |
|
|
245 | { |
|
|
246 | // generator |
|
|
247 | uint32_t state [4]; |
|
|
248 | |
|
|
249 | void operator =(const tausworthe_random_generator &src) |
|
|
250 | { |
|
|
251 | state [0] = src.state [0]; |
|
|
252 | state [1] = src.state [1]; |
|
|
253 | state [2] = src.state [2]; |
|
|
254 | state [3] = src.state [3]; |
|
|
255 | } |
|
|
256 | |
|
|
257 | void seed (uint32_t seed); |
|
|
258 | uint32_t next (); |
|
|
259 | |
|
|
260 | // uniform distribution |
|
|
261 | uint32_t operator ()(uint32_t r_max) |
|
|
262 | { |
|
|
263 | return is_constant (r_max) |
|
|
264 | ? this->next () % r_max |
|
|
265 | : get_range (r_max); |
|
|
266 | } |
|
|
267 | |
|
|
268 | // return a number within (min .. max) |
|
|
269 | int operator () (int r_min, int r_max) |
|
|
270 | { |
|
|
271 | return is_constant (r_min) && is_constant (r_max) |
|
|
272 | ? r_min + (*this) (max (r_max - r_min + 1, 1)) |
|
|
273 | : get_range (r_min, r_max); |
|
|
274 | } |
|
|
275 | |
|
|
276 | double operator ()() |
|
|
277 | { |
|
|
278 | return this->next () / (double)0xFFFFFFFFU; |
|
|
279 | } |
|
|
280 | |
|
|
281 | protected: |
|
|
282 | uint32_t get_range (uint32_t r_max); |
|
|
283 | int get_range (int r_min, int r_max); |
|
|
284 | }; |
|
|
285 | |
|
|
286 | typedef tausworthe_random_generator rand_gen; |
|
|
287 | |
|
|
288 | extern rand_gen rndm; |
175 | |
289 | |
176 | template<class T> |
290 | template<class T> |
177 | struct refptr |
291 | struct refptr |
178 | { |
292 | { |
179 | T *p; |
293 | T *p; |
… | |
… | |
297 | { |
411 | { |
298 | errase (&obj); |
412 | errase (&obj); |
299 | } |
413 | } |
300 | }; |
414 | }; |
301 | |
415 | |
302 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
|
|
303 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
|
|
304 | template<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 | |
|
|
306 | template<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 |
416 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
309 | void assign (char *dst, const char *src, int maxlen); |
417 | void assign (char *dst, const char *src, int maxlen); |
310 | |
418 | |
311 | // type-safe version of assign |
419 | // type-safe version of assign |
312 | template<int N> |
420 | template<int N> |