… | |
… | |
72 | #endif |
72 | #endif |
73 | |
73 | |
74 | // use C0X decltype for auto declarations until ISO C++ sanctifies them (if ever) |
74 | // use C0X decltype for auto declarations until ISO C++ sanctifies them (if ever) |
75 | #define auto(var,expr) decltype(expr) var = (expr) |
75 | #define auto(var,expr) decltype(expr) var = (expr) |
76 | |
76 | |
77 | // very ugly macro that basicaly declares and initialises a variable |
77 | // very ugly macro that basically declares and initialises a variable |
78 | // that is in scope for the next statement only |
78 | // that is in scope for the next statement only |
79 | // works only for stuff that can be assigned 0 and converts to false |
79 | // works only for stuff that can be assigned 0 and converts to false |
80 | // (note: works great for pointers) |
80 | // (note: works great for pointers) |
81 | // most ugly macro I ever wrote |
81 | // most ugly macro I ever wrote |
82 | #define statementvar(type, name, value) if (type name = 0) { } else if (((name) = (value)), 1) |
82 | #define statementvar(type, name, value) if (type name = 0) { } else if (((name) = (value)), 1) |
… | |
… | |
96 | // as a is often a constant while b is the variable. it is still a bug, though. |
96 | // as a is often a constant while b is the variable. it is still a bug, though. |
97 | template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; } |
97 | template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; } |
98 | template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; } |
98 | template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; } |
99 | 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; } |
99 | 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; } |
100 | |
100 | |
101 | template<typename T> static inline void min_it (T &v, T m) { v = min (v, m); } |
101 | template<typename T, typename U> static inline void min_it (T &v, U m) { v = min (v, (T)m); } |
102 | template<typename T> static inline void max_it (T &v, T m) { v = max (v, m); } |
102 | template<typename T, typename U> static inline void max_it (T &v, U m) { v = max (v, (T)m); } |
103 | template<typename T> static inline void clamp_it (T &v, T a, T b) { v = clamp (v, a, b); } |
103 | template<typename T, typename U, typename V> static inline void clamp_it (T &v, U a, V b) { v = clamp (v, (T)a, (T)b); } |
104 | |
104 | |
105 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
105 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
106 | |
106 | |
107 | template<typename T, typename U, typename V> static inline T min (T a, U b, V c) { return min (a, min (b, c)); } |
107 | template<typename T, typename U, typename V> static inline T min (T a, U b, V c) { return min (a, min (b, c)); } |
108 | template<typename T, typename U, typename V> static inline T max (T a, U b, V c) { return max (a, max (b, c)); } |
108 | template<typename T, typename U, typename V> static inline T max (T a, U b, V c) { return max (a, max (b, c)); } |
|
|
109 | |
|
|
110 | // sign returns -1 or +1 |
|
|
111 | template<typename T> |
|
|
112 | static inline T sign (T v) { return v < 0 ? -1 : +1; } |
|
|
113 | // relies on 2c representation |
|
|
114 | template<> |
|
|
115 | inline sint8 sign (sint8 v) { return 1 - (sint8 (uint8 (v) >> 7) * 2); } |
|
|
116 | |
|
|
117 | // sign0 returns -1, 0 or +1 |
|
|
118 | template<typename T> |
|
|
119 | static inline T sign0 (T v) { return v ? sign (v) : 0; } |
109 | |
120 | |
110 | // div, with correct rounding (< 0.5 downwards, >=0.5 upwards) |
121 | // div, with correct rounding (< 0.5 downwards, >=0.5 upwards) |
111 | template<typename T> static inline T div (T val, T div) { return (val + div / 2) / div; } |
122 | template<typename T> static inline T div (T val, T div) { return (val + div / 2) / div; } |
112 | // div, round-up |
123 | // div, round-up |
113 | template<typename T> static inline T div_ru (T val, T div) { return (val + div - 1) / div; } |
124 | template<typename T> static inline T div_ru (T val, T div) { return (val + div - 1) / div; } |
… | |
… | |
395 | uint32_t next (); |
406 | uint32_t next (); |
396 | |
407 | |
397 | // uniform distribution, 0 .. max (0, num - 1) |
408 | // uniform distribution, 0 .. max (0, num - 1) |
398 | uint32_t operator ()(uint32_t num) |
409 | uint32_t operator ()(uint32_t num) |
399 | { |
410 | { |
400 | return is_constant (num) |
411 | return !is_constant (num) ? get_range (num) // non-constant |
401 | ? (next () * (uint64_t)num) >> 32U |
412 | : num & (num - 1) ? (next () * (uint64_t)num) >> 32U // constant, non-power-of-two |
402 | : get_range (num); |
413 | : next () & (num - 1); // constant, power-of-two |
403 | } |
414 | } |
404 | |
415 | |
405 | // return a number within (min .. max) |
416 | // return a number within (min .. max) |
406 | int operator () (int r_min, int r_max) |
417 | int operator () (int r_min, int r_max) |
407 | { |
418 | { |