… | |
… | |
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; } |