… | |
… | |
2 | #define UTIL_H__ |
2 | #define UTIL_H__ |
3 | |
3 | |
4 | //#define PREFER_MALLOC |
4 | //#define PREFER_MALLOC |
5 | |
5 | |
6 | #if __GNUC__ >= 3 |
6 | #if __GNUC__ >= 3 |
7 | # 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) |
8 | #else |
10 | #else |
9 | # define is_constant(c) 0 |
11 | # define is_constant(c) 0 |
|
|
12 | # define expect(expr,value) (expr) |
|
|
13 | # define prefetch(addr,rw,locality) |
10 | #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) |
11 | |
21 | |
12 | #include <cstddef> |
22 | #include <cstddef> |
13 | #include <cmath> |
23 | #include <cmath> |
14 | #include <new> |
24 | #include <new> |
15 | #include <vector> |
25 | #include <vector> |
… | |
… | |
44 | 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; } |
45 | 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; } |
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; } |
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; } |
47 | |
57 | |
48 | 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 | } |
49 | |
66 | |
50 | // lots of stuff taken from FXT |
67 | // lots of stuff taken from FXT |
51 | |
68 | |
52 | /* Rotate right. This is used in various places for checksumming */ |
69 | /* Rotate right. This is used in various places for checksumming */ |
53 | //TODO: that sucks, use a better checksum algo |
70 | //TODO: that sucks, use a better checksum algo |
… | |
… | |
262 | |
279 | |
263 | void seed (uint32_t seed); |
280 | void seed (uint32_t seed); |
264 | uint32_t next (); |
281 | uint32_t next (); |
265 | |
282 | |
266 | // uniform distribution |
283 | // uniform distribution |
267 | uint32_t operator ()(uint32_t r_max) |
284 | uint32_t operator ()(uint32_t num) |
268 | { |
285 | { |
269 | return is_constant (r_max) |
286 | return is_constant (num) |
270 | ? (next () * (uint64_t)r_max) >> 32U |
287 | ? (next () * (uint64_t)num) >> 32U |
271 | : get_range (r_max); |
288 | : get_range (num); |
272 | } |
289 | } |
273 | |
290 | |
274 | // return a number within (min .. max) |
291 | // return a number within (min .. max) |
275 | int operator () (int r_min, int r_max) |
292 | int operator () (int r_min, int r_max) |
276 | { |
293 | { |
277 | return is_constant (r_min) && is_constant (r_max) |
294 | return is_constant (r_min) && is_constant (r_max) && r_min <= r_max |
278 | ? r_min + operator ()(max (r_max - r_min + 1, 1)) |
295 | ? r_min + operator ()(r_max - r_min + 1) |
279 | : get_range (r_min, r_max); |
296 | : get_range (r_min, r_max); |
280 | } |
297 | } |
281 | |
298 | |
282 | double operator ()() |
299 | double operator ()() |
283 | { |
300 | { |
… | |
… | |
434 | // return current time as timestampe |
451 | // return current time as timestampe |
435 | tstamp now (); |
452 | tstamp now (); |
436 | |
453 | |
437 | int similar_direction (int a, int b); |
454 | int similar_direction (int a, int b); |
438 | |
455 | |
|
|
456 | // like printf, but returns a std::string |
|
|
457 | const std::string format (const char *format, ...); |
|
|
458 | |
439 | #endif |
459 | #endif |
440 | |
460 | |