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 | |
38 | void fork_abort (const char *msg); |
40 | void 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. |
40 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
44 | template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; } |
41 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
45 | template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; } |
42 | 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; } |
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; } |
43 | |
47 | |
44 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
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: that sucks, use a better checksum algo |
|
|
54 | static inline uint32_t |
|
|
55 | rotate_right (uint32_t c, uint32_t count = 1) |
|
|
56 | { |
|
|
57 | return (c << (32 - count)) | (c >> count); |
|
|
58 | } |
|
|
59 | |
|
|
60 | static inline uint32_t |
|
|
61 | rotate_left (uint32_t c, uint32_t count = 1) |
|
|
62 | { |
|
|
63 | return (c >> (32 - count)) | (c << count); |
|
|
64 | } |
|
|
65 | |
|
|
66 | // Return abs(a-b) |
|
|
67 | // Both a and b must not have the most significant bit set |
|
|
68 | static inline uint32_t |
|
|
69 | upos_abs_diff (uint32_t a, uint32_t b) |
|
|
70 | { |
|
|
71 | long d1 = b - a; |
|
|
72 | long d2 = (d1 & (d1 >> 31)) << 1; |
|
|
73 | |
|
|
74 | return d1 - d2; // == (b - d) - (a + d); |
|
|
75 | } |
|
|
76 | |
|
|
77 | // Both a and b must not have the most significant bit set |
|
|
78 | static inline uint32_t |
|
|
79 | upos_min (uint32_t a, uint32_t b) |
|
|
80 | { |
|
|
81 | int32_t d = b - a; |
|
|
82 | d &= d >> 31; |
|
|
83 | return a + d; |
|
|
84 | } |
|
|
85 | |
|
|
86 | // Both a and b must not have the most significant bit set |
|
|
87 | static inline uint32_t |
|
|
88 | upos_max (uint32_t a, uint32_t b) |
|
|
89 | { |
|
|
90 | int32_t d = b - a; |
|
|
91 | d &= d >> 31; |
|
|
92 | return b - d; |
|
|
93 | } |
45 | |
94 | |
46 | // this is much faster than crossfires original algorithm |
95 | // this is much faster than crossfires original algorithm |
47 | // on modern cpus |
96 | // on modern cpus |
48 | inline int |
97 | inline int |
49 | isqrt (int n) |
98 | isqrt (int n) |
… | |
… | |
131 | |
180 | |
132 | // for symmetry |
181 | // for symmetry |
133 | template<typename T> |
182 | template<typename T> |
134 | inline void sfree (T *ptr, int n = 1) throw () |
183 | inline void sfree (T *ptr, int n = 1) throw () |
135 | { |
184 | { |
|
|
185 | #ifdef PREFER_MALLOC |
|
|
186 | free (ptr); |
|
|
187 | #else |
136 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
188 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
|
|
189 | #endif |
137 | } |
190 | } |
138 | |
191 | |
139 | // a STL-compatible allocator that uses g_slice |
192 | // a STL-compatible allocator that uses g_slice |
140 | // boy, this is verbose |
193 | // boy, this is verbose |
141 | template<typename Tp> |
194 | template<typename Tp> |
… | |
… | |
346 | } |
399 | } |
347 | |
400 | |
348 | void erase (T *obj) |
401 | void erase (T *obj) |
349 | { |
402 | { |
350 | assert (obj->*index); |
403 | assert (obj->*index); |
351 | int pos = obj->*index; |
404 | unsigned int pos = obj->*index; |
352 | obj->*index = 0; |
405 | obj->*index = 0; |
353 | |
406 | |
354 | if (pos < this->size ()) |
407 | if (pos < this->size ()) |
355 | { |
408 | { |
356 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
409 | (*this)[pos - 1] = (*this)[this->size () - 1]; |