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 |
… | |
… | |
16 | |
18 | |
17 | #include <shstr.h> |
19 | #include <shstr.h> |
18 | #include <traits.h> |
20 | #include <traits.h> |
19 | |
21 | |
20 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
22 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
21 | #define AUTODECL(var,expr) typeof(expr) var = (expr) |
23 | #define auto(var,expr) typeof(expr) var = (expr) |
22 | |
24 | |
23 | // very ugly macro that basicaly declares and initialises a variable |
25 | // very ugly macro that basicaly declares and initialises a variable |
24 | // that is in scope for the next statement only |
26 | // that is in scope for the next statement only |
25 | // works only for stuff that can be assigned 0 and converts to false |
27 | // works only for stuff that can be assigned 0 and converts to false |
26 | // (note: works great for pointers) |
28 | // (note: works great for pointers) |
… | |
… | |
42 | template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; } |
44 | template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; } |
43 | template<typename T, typename U> static inline T max (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; } |
44 | 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; } |
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; } |
45 | |
47 | |
46 | 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 | template<typename T> |
|
|
51 | static inline T |
|
|
52 | lerp (T val, T min_in, T max_in, T min_out, T max_out) |
|
|
53 | { |
|
|
54 | return (val - min_in) * (max_out - min_out) / (max_in - min_in) + min_out; |
|
|
55 | } |
|
|
56 | |
|
|
57 | // lots of stuff taken from FXT |
|
|
58 | |
|
|
59 | /* Rotate right. This is used in various places for checksumming */ |
|
|
60 | //TODO: that sucks, use a better checksum algo |
|
|
61 | static inline uint32_t |
|
|
62 | rotate_right (uint32_t c, uint32_t count = 1) |
|
|
63 | { |
|
|
64 | return (c << (32 - count)) | (c >> count); |
|
|
65 | } |
|
|
66 | |
|
|
67 | static inline uint32_t |
|
|
68 | rotate_left (uint32_t c, uint32_t count = 1) |
|
|
69 | { |
|
|
70 | return (c >> (32 - count)) | (c << count); |
|
|
71 | } |
|
|
72 | |
|
|
73 | // Return abs(a-b) |
|
|
74 | // Both a and b must not have the most significant bit set |
|
|
75 | static inline uint32_t |
|
|
76 | upos_abs_diff (uint32_t a, uint32_t b) |
|
|
77 | { |
|
|
78 | long d1 = b - a; |
|
|
79 | long d2 = (d1 & (d1 >> 31)) << 1; |
|
|
80 | |
|
|
81 | return d1 - d2; // == (b - d) - (a + d); |
|
|
82 | } |
|
|
83 | |
|
|
84 | // Both a and b must not have the most significant bit set |
|
|
85 | static inline uint32_t |
|
|
86 | upos_min (uint32_t a, uint32_t b) |
|
|
87 | { |
|
|
88 | int32_t d = b - a; |
|
|
89 | d &= d >> 31; |
|
|
90 | return a + d; |
|
|
91 | } |
|
|
92 | |
|
|
93 | // Both a and b must not have the most significant bit set |
|
|
94 | static inline uint32_t |
|
|
95 | upos_max (uint32_t a, uint32_t b) |
|
|
96 | { |
|
|
97 | int32_t d = b - a; |
|
|
98 | d &= d >> 31; |
|
|
99 | return b - d; |
|
|
100 | } |
47 | |
101 | |
48 | // this is much faster than crossfires original algorithm |
102 | // this is much faster than crossfires original algorithm |
49 | // on modern cpus |
103 | // on modern cpus |
50 | inline int |
104 | inline int |
51 | isqrt (int n) |
105 | isqrt (int n) |
… | |
… | |
133 | |
187 | |
134 | // for symmetry |
188 | // for symmetry |
135 | template<typename T> |
189 | template<typename T> |
136 | inline void sfree (T *ptr, int n = 1) throw () |
190 | inline void sfree (T *ptr, int n = 1) throw () |
137 | { |
191 | { |
|
|
192 | #ifdef PREFER_MALLOC |
|
|
193 | free (ptr); |
|
|
194 | #else |
138 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
195 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
|
|
196 | #endif |
139 | } |
197 | } |
140 | |
198 | |
141 | // a STL-compatible allocator that uses g_slice |
199 | // a STL-compatible allocator that uses g_slice |
142 | // boy, this is verbose |
200 | // boy, this is verbose |
143 | template<typename Tp> |
201 | template<typename Tp> |
… | |
… | |
211 | |
269 | |
212 | void seed (uint32_t seed); |
270 | void seed (uint32_t seed); |
213 | uint32_t next (); |
271 | uint32_t next (); |
214 | |
272 | |
215 | // uniform distribution |
273 | // uniform distribution |
216 | uint32_t operator ()(uint32_t r_max) |
274 | uint32_t operator ()(uint32_t num) |
217 | { |
275 | { |
218 | return is_constant (r_max) |
276 | return is_constant (num) |
219 | ? this->next () % r_max |
277 | ? (next () * (uint64_t)num) >> 32U |
220 | : get_range (r_max); |
278 | : get_range (num); |
221 | } |
279 | } |
222 | |
280 | |
223 | // return a number within (min .. max) |
281 | // return a number within (min .. max) |
224 | int operator () (int r_min, int r_max) |
282 | int operator () (int r_min, int r_max) |
225 | { |
283 | { |
226 | return is_constant (r_min) && is_constant (r_max) |
284 | return is_constant (r_min) && is_constant (r_max) && r_min <= r_max |
227 | ? r_min + (*this) (max (r_max - r_min + 1, 1)) |
285 | ? r_min + operator ()(r_max - r_min + 1) |
228 | : get_range (r_min, r_max); |
286 | : get_range (r_min, r_max); |
229 | } |
287 | } |
230 | |
288 | |
231 | double operator ()() |
289 | double operator ()() |
232 | { |
290 | { |
… | |
… | |
348 | } |
406 | } |
349 | |
407 | |
350 | void erase (T *obj) |
408 | void erase (T *obj) |
351 | { |
409 | { |
352 | assert (obj->*index); |
410 | assert (obj->*index); |
353 | int pos = obj->*index; |
411 | unsigned int pos = obj->*index; |
354 | obj->*index = 0; |
412 | obj->*index = 0; |
355 | |
413 | |
356 | if (pos < this->size ()) |
414 | if (pos < this->size ()) |
357 | { |
415 | { |
358 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
416 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
… | |
… | |
383 | // return current time as timestampe |
441 | // return current time as timestampe |
384 | tstamp now (); |
442 | tstamp now (); |
385 | |
443 | |
386 | int similar_direction (int a, int b); |
444 | int similar_direction (int a, int b); |
387 | |
445 | |
|
|
446 | // like printf, but returns a std::string |
|
|
447 | const std::string format (const char *format, ...); |
|
|
448 | |
388 | #endif |
449 | #endif |
389 | |
450 | |