--- deliantra/server/include/util.h 2008/12/04 03:48:19 1.78 +++ deliantra/server/include/util.h 2008/12/26 10:36:42 1.81 @@ -74,7 +74,7 @@ // use C0X decltype for auto declarations until ISO C++ sanctifies them (if ever) #define auto(var,expr) decltype(expr) var = (expr) -// very ugly macro that basicaly declares and initialises a variable +// very ugly macro that basically declares and initialises a variable // that is in scope for the next statement only // works only for stuff that can be assigned 0 and converts to false // (note: works great for pointers) @@ -98,15 +98,26 @@ template static inline T max (T a, U b) { return (U)a > b ? (U)a : b; } template static inline T clamp (T v, U a, V b) { return v < (T)a ? (T)a : v >(T)b ? (T)b : v; } -template static inline void min_it (T &v, T m) { v = min (v, m); } -template static inline void max_it (T &v, T m) { v = max (v, m); } -template static inline void clamp_it (T &v, T a, T b) { v = clamp (v, a, b); } +template static inline void min_it (T &v, U m) { v = min (v, (T)m); } +template static inline void max_it (T &v, U m) { v = max (v, (T)m); } +template static inline void clamp_it (T &v, U a, V b) { v = clamp (v, (T)a, (T)b); } template static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } template static inline T min (T a, U b, V c) { return min (a, min (b, c)); } template static inline T max (T a, U b, V c) { return max (a, max (b, c)); } +// sign returns -1 or +1 +template +static inline T sign (T v) { return v < 0 ? -1 : +1; } +// relies on 2c representation +template<> +inline sint8 sign (sint8 v) { return 1 - (sint8 (uint8 (v) >> 7) * 2); } + +// sign0 returns -1, 0 or +1 +template +static inline T sign0 (T v) { return v ? sign (v) : 0; } + // div, with correct rounding (< 0.5 downwards, >=0.5 upwards) template static inline T div (T val, T div) { return (val + div / 2) / div; } // div, round-up