1 | #ifndef UTIL_H__ |
1 | #ifndef UTIL_H__ |
2 | #define UTIL_H__ |
2 | #define UTIL_H__ |
3 | |
3 | |
|
|
4 | //#define PREFER_MALLOC |
|
|
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) |
|
|
8 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
9 | # define prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality) |
6 | #else |
10 | #else |
7 | # define is_constant(c) 0 |
11 | # define is_constant(c) 0 |
|
|
12 | # define expect(expr,value) (expr) |
|
|
13 | # define prefetch(addr,rw,locality) |
8 | #endif |
14 | #endif |
|
|
15 | |
|
|
16 | // put into ifs if you are very sure that the expression |
|
|
17 | // is mostly true or mosty false. note that these return |
|
|
18 | // booleans, not the expression. |
|
|
19 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
20 | #define expect_true(expr) expect ((expr) != 0, 1) |
9 | |
21 | |
10 | #include <cstddef> |
22 | #include <cstddef> |
11 | #include <cmath> |
23 | #include <cmath> |
12 | #include <new> |
24 | #include <new> |
13 | #include <vector> |
25 | #include <vector> |
… | |
… | |
16 | |
28 | |
17 | #include <shstr.h> |
29 | #include <shstr.h> |
18 | #include <traits.h> |
30 | #include <traits.h> |
19 | |
31 | |
20 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
32 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
21 | #define AUTODECL(var,expr) typeof(expr) var = (expr) |
33 | #define auto(var,expr) typeof(expr) var = (expr) |
22 | |
34 | |
23 | // very ugly macro that basicaly declares and initialises a variable |
35 | // very ugly macro that basicaly declares and initialises a variable |
24 | // that is in scope for the next statement only |
36 | // that is in scope for the next statement only |
25 | // works only for stuff that can be assigned 0 and converts to false |
37 | // works only for stuff that can be assigned 0 and converts to false |
26 | // (note: works great for pointers) |
38 | // (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; } |
54 | 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; } |
55 | 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; } |
56 | 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 | |
57 | |
46 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
58 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
|
|
59 | |
|
|
60 | template<typename T> |
|
|
61 | static inline T |
|
|
62 | lerp (T val, T min_in, T max_in, T min_out, T max_out) |
|
|
63 | { |
|
|
64 | return (val - min_in) * (max_out - min_out) / (max_in - min_in) + min_out; |
|
|
65 | } |
|
|
66 | |
|
|
67 | // lots of stuff taken from FXT |
|
|
68 | |
|
|
69 | /* Rotate right. This is used in various places for checksumming */ |
|
|
70 | //TODO: that sucks, use a better checksum algo |
|
|
71 | static inline uint32_t |
|
|
72 | rotate_right (uint32_t c, uint32_t count = 1) |
|
|
73 | { |
|
|
74 | return (c << (32 - count)) | (c >> count); |
|
|
75 | } |
|
|
76 | |
|
|
77 | static inline uint32_t |
|
|
78 | rotate_left (uint32_t c, uint32_t count = 1) |
|
|
79 | { |
|
|
80 | return (c >> (32 - count)) | (c << count); |
|
|
81 | } |
|
|
82 | |
|
|
83 | // Return abs(a-b) |
|
|
84 | // Both a and b must not have the most significant bit set |
|
|
85 | static inline uint32_t |
|
|
86 | upos_abs_diff (uint32_t a, uint32_t b) |
|
|
87 | { |
|
|
88 | long d1 = b - a; |
|
|
89 | long d2 = (d1 & (d1 >> 31)) << 1; |
|
|
90 | |
|
|
91 | return d1 - d2; // == (b - d) - (a + d); |
|
|
92 | } |
|
|
93 | |
|
|
94 | // Both a and b must not have the most significant bit set |
|
|
95 | static inline uint32_t |
|
|
96 | upos_min (uint32_t a, uint32_t b) |
|
|
97 | { |
|
|
98 | int32_t d = b - a; |
|
|
99 | d &= d >> 31; |
|
|
100 | return a + d; |
|
|
101 | } |
|
|
102 | |
|
|
103 | // Both a and b must not have the most significant bit set |
|
|
104 | static inline uint32_t |
|
|
105 | upos_max (uint32_t a, uint32_t b) |
|
|
106 | { |
|
|
107 | int32_t d = b - a; |
|
|
108 | d &= d >> 31; |
|
|
109 | return b - d; |
|
|
110 | } |
47 | |
111 | |
48 | // this is much faster than crossfires original algorithm |
112 | // this is much faster than crossfires original algorithm |
49 | // on modern cpus |
113 | // on modern cpus |
50 | inline int |
114 | inline int |
51 | isqrt (int n) |
115 | isqrt (int n) |
… | |
… | |
133 | |
197 | |
134 | // for symmetry |
198 | // for symmetry |
135 | template<typename T> |
199 | template<typename T> |
136 | inline void sfree (T *ptr, int n = 1) throw () |
200 | inline void sfree (T *ptr, int n = 1) throw () |
137 | { |
201 | { |
|
|
202 | #ifdef PREFER_MALLOC |
|
|
203 | free (ptr); |
|
|
204 | #else |
138 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
205 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
|
|
206 | #endif |
139 | } |
207 | } |
140 | |
208 | |
141 | // a STL-compatible allocator that uses g_slice |
209 | // a STL-compatible allocator that uses g_slice |
142 | // boy, this is verbose |
210 | // boy, this is verbose |
143 | template<typename Tp> |
211 | template<typename Tp> |
… | |
… | |
211 | |
279 | |
212 | void seed (uint32_t seed); |
280 | void seed (uint32_t seed); |
213 | uint32_t next (); |
281 | uint32_t next (); |
214 | |
282 | |
215 | // uniform distribution |
283 | // uniform distribution |
216 | uint32_t operator ()(uint32_t r_max) |
284 | uint32_t operator ()(uint32_t num) |
217 | { |
285 | { |
218 | return is_constant (r_max) |
286 | return is_constant (num) |
219 | ? this->next () % r_max |
287 | ? (next () * (uint64_t)num) >> 32U |
220 | : get_range (r_max); |
288 | : get_range (num); |
221 | } |
289 | } |
222 | |
290 | |
223 | // return a number within (min .. max) |
291 | // return a number within (min .. max) |
224 | int operator () (int r_min, int r_max) |
292 | int operator () (int r_min, int r_max) |
225 | { |
293 | { |
226 | return is_constant (r_min) && is_constant (r_max) |
294 | return is_constant (r_min) && is_constant (r_max) && r_min <= r_max |
227 | ? r_min + (*this) (max (r_max - r_min + 1, 1)) |
295 | ? r_min + operator ()(r_max - r_min + 1) |
228 | : get_range (r_min, r_max); |
296 | : get_range (r_min, r_max); |
229 | } |
297 | } |
230 | |
298 | |
231 | double operator ()() |
299 | double operator ()() |
232 | { |
300 | { |
… | |
… | |
348 | } |
416 | } |
349 | |
417 | |
350 | void erase (T *obj) |
418 | void erase (T *obj) |
351 | { |
419 | { |
352 | assert (obj->*index); |
420 | assert (obj->*index); |
353 | int pos = obj->*index; |
421 | unsigned int pos = obj->*index; |
354 | obj->*index = 0; |
422 | obj->*index = 0; |
355 | |
423 | |
356 | if (pos < this->size ()) |
424 | if (pos < this->size ()) |
357 | { |
425 | { |
358 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
426 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
… | |
… | |
383 | // return current time as timestampe |
451 | // return current time as timestampe |
384 | tstamp now (); |
452 | tstamp now (); |
385 | |
453 | |
386 | int similar_direction (int a, int b); |
454 | int similar_direction (int a, int b); |
387 | |
455 | |
|
|
456 | // like printf, but returns a std::string |
|
|
457 | const std::string format (const char *format, ...); |
|
|
458 | |
388 | #endif |
459 | #endif |
389 | |
460 | |