smatz@8587: /* $Id$ */ skidd13@8433: smatz@8587: /** @file math_func.hpp Integer math functions */ skidd13@8433: skidd13@8433: #ifndef MATH_FUNC_HPP skidd13@8433: #define MATH_FUNC_HPP skidd13@8433: skidd13@8433: #ifdef min skidd13@8433: #undef min skidd13@8433: #endif skidd13@8433: skidd13@8433: #ifdef max skidd13@8433: #undef max skidd13@8433: #endif skidd13@8433: skidd13@8433: #ifdef abs skidd13@8433: #undef abs skidd13@8433: #endif skidd13@8433: skidd13@8433: /** skidd13@8433: * Returns the maximum of two values. skidd13@8433: * skidd13@8433: * This function returns the greater value of two given values. skidd13@8433: * If they are equal the value of a is returned. skidd13@8433: * skidd13@8433: * @param a The first value skidd13@8433: * @param b The second value skidd13@8433: * @return The greater value or a if equals skidd13@8433: */ skidd13@8433: template static inline T max(const T a, const T b) skidd13@8433: { skidd13@8433: return (a >= b) ? a : b; skidd13@8433: } skidd13@8433: skidd13@8433: /** skidd13@8433: * Returns the minimum of two values. skidd13@8433: * skidd13@8433: * This function returns the smaller value of two given values. skidd13@8433: * If they are equal the value of b is returned. skidd13@8433: * skidd13@8433: * @param a The first value skidd13@8433: * @param b The second value skidd13@8433: * @return The smaller value or b if equals skidd13@8433: */ skidd13@8433: template static inline T min(const T a, const T b) skidd13@8433: { skidd13@8433: return (a < b) ? a : b; skidd13@8433: } skidd13@8433: skidd13@8433: /** skidd13@8433: * Returns the minimum of two integer. skidd13@8433: * skidd13@8433: * This function returns the smaller value of two given integers. skidd13@8433: * skidd13@8433: * @param a The first integer skidd13@8433: * @param b The second integer skidd13@8433: * @return The smaller value skidd13@8433: */ skidd13@8433: static inline int min(const int a, const int b) skidd13@8433: { skidd13@8433: return (a < b) ? a : b; skidd13@8433: } skidd13@8433: skidd13@8433: /** skidd13@8433: * Returns the minimum of two unsigned integers. skidd13@8433: * skidd13@8433: * This function returns the smaller value of two given unsigned integers. skidd13@8433: * skidd13@8433: * @param a The first unsigned integer skidd13@8433: * @param b The second unsigned integer skidd13@8433: * @return The smaller value skidd13@8433: */ skidd13@8433: static inline uint minu(const uint a, const uint b) skidd13@8433: { skidd13@8433: return (a < b) ? a : b; skidd13@8433: } skidd13@8433: skidd13@8433: /** skidd13@8433: * Returns the absolute value of (scalar) variable. skidd13@8433: * skidd13@8433: * @note assumes variable to be signed skidd13@8433: * @param a The value we want to unsign skidd13@8433: * @return The unsigned value skidd13@8433: */ skidd13@8450: template static inline T abs(const T a) skidd13@8433: { skidd13@8433: return (a < (T)0) ? -a : a; skidd13@8433: } skidd13@8433: skidd13@8433: /** skidd13@8433: * Return the smallest multiple of n equal or greater than x skidd13@8433: * skidd13@8433: * @note n must be a power of 2 skidd13@8433: * @param x The min value skidd13@8433: * @param n The base of the number we are searching skidd13@8433: * @return The smallest multiple of n equal or greater than x skidd13@8433: */ skidd13@8433: template static inline T Align(const T x, uint n) skidd13@8433: { skidd13@8433: n--; skidd13@8433: return (T)((x + n) & ~(n)); skidd13@8433: } skidd13@8433: skidd13@8433: /** skidd13@8433: * Clamp an integer between an interval. skidd13@8433: * skidd13@8433: * This function returns a value which is between the given interval of skidd13@8433: * min and max. If the given value is in this interval the value itself skidd13@8433: * is returned otherwise the border of the interval is returned, according skidd13@8433: * which side of the interval was 'left'. skidd13@8433: * skidd13@8433: * @note The min value must be less or equal of max or you get some skidd13@8433: * unexpected results. skidd13@8433: * @param a The value to clamp/truncate. skidd13@8433: * @param min The minimum of the interval. skidd13@8433: * @param max the maximum of the interval. skidd13@8433: * @returns A value between min and max which is closest to a. skidd13@8433: * @see ClampU(uint, uint, uint) skidd13@8433: */ skidd13@8433: static inline int Clamp(const int a, const int min, const int max) skidd13@8433: { skidd13@8433: if (a <= min) return min; skidd13@8433: if (a >= max) return max; skidd13@8433: return a; skidd13@8433: } skidd13@8433: skidd13@8433: /** skidd13@8433: * Clamp an unsigned integer between an interval. skidd13@8433: * skidd13@8433: * This function returns a value which is between the given interval of skidd13@8433: * min and max. If the given value is in this interval the value itself skidd13@8433: * is returned otherwise the border of the interval is returned, according skidd13@8433: * which side of the interval was 'left'. skidd13@8433: * skidd13@8433: * @note The min value must be less or equal of max or you get some skidd13@8433: * unexpected results. skidd13@8433: * @param a The value to clamp/truncate. skidd13@8433: * @param min The minimum of the interval. skidd13@8433: * @param max the maximum of the interval. skidd13@8433: * @returns A value between min and max which is closest to a. skidd13@8433: * @see Clamp(int, int, int) skidd13@8433: */ skidd13@8433: static inline uint ClampU(const uint a, const uint min, const uint max) skidd13@8433: { skidd13@8433: if (a <= min) return min; skidd13@8433: if (a >= max) return max; skidd13@8433: return a; skidd13@8433: } skidd13@8433: skidd13@8433: /** skidd13@8433: * Reduce a signed 64-bit int to a signed 32-bit one skidd13@8433: * skidd13@8433: * This function clamps a 64-bit integer to a 32-bit integer. skidd13@8433: * If the 64-bit value is smaller than the smallest 32-bit integer skidd13@8433: * value 0x80000000 this value is returned (the left one bit is the sign bit). skidd13@8433: * If the 64-bit value is greater than the greatest 32-bit integer value 0x7FFFFFFF skidd13@8433: * this value is returned. In all other cases the 64-bit value 'fits' in a skidd13@8433: * 32-bits integer field and so the value is casted to int32 and returned. skidd13@8433: * skidd13@8433: * @param a The 64-bit value to clamps skidd13@8433: * @return The 64-bit value reduced to a 32-bit value skidd13@8433: * @see Clamp(int, int, int) skidd13@8433: */ skidd13@8433: static inline int32 ClampToI32(const int64 a) skidd13@8433: { skidd13@8433: if (a <= (int32)0x80000000) return 0x80000000; skidd13@8433: if (a >= (int32)0x7FFFFFFF) return 0x7FFFFFFF; skidd13@8433: return (int32)a; skidd13@8433: } skidd13@8433: skidd13@8433: /** smatz@9106: * Reduce an usigned 64-bit int to an unsigned 16-bit one smatz@9106: * smatz@9106: * @param a The 64-bit value to clamp smatz@9106: * @return The 64-bit value reduced to a 16-bit value smatz@9106: * @see ClampU(uint, uint, uint) smatz@9106: */ smatz@9106: static inline uint16 ClampToU16(const uint64 a) smatz@9106: { smatz@9117: return min(a, 0xFFFF); smatz@9106: } smatz@9106: smatz@9106: /** skidd13@8433: * Returns the (absolute) difference between two (scalar) variables skidd13@8433: * skidd13@8433: * @param a The first scalar skidd13@8433: * @param b The second scalar skidd13@8433: * @return The absolute difference between the given scalars skidd13@8433: */ skidd13@8466: template static inline T Delta(const T a, const T b) { skidd13@8433: return (a < b) ? b - a : a - b; skidd13@8433: } skidd13@8433: skidd13@8433: /** skidd13@8433: * Checks if a value is between a window started at some base point. skidd13@8433: * skidd13@8433: * This function checks if the value x is between the value of base skidd13@8433: * and base+size. If x equals base this returns true. If x equals skidd13@8433: * base+size this returns false. skidd13@8433: * skidd13@8433: * @param x The value to check skidd13@8433: * @param base The base value of the interval skidd13@8433: * @param size The size of the interval skidd13@8433: * @return True if the value is in the interval, false else. skidd13@8433: */ skidd13@8450: template static inline bool IsInsideBS(const T x, const uint base, const uint size) skidd13@8433: { skidd13@8433: return (uint)(x - base) < size; skidd13@8433: } skidd13@8433: skidd13@8433: /** skidd13@8450: * Checks if a value is in an interval. skidd13@8433: * skidd13@8450: * Returns true if a value is in the interval of [min, max). skidd13@8433: * skidd13@8450: * @param a The value to check skidd13@8433: * @param min The minimum of the interval skidd13@8433: * @param max The maximum of the interval skidd13@8450: * @see IsInsideBS() skidd13@8433: */ skidd13@8450: template static inline bool IsInsideMM(const T x, const uint min, const uint max) skidd13@8433: { skidd13@8433: return (uint)(x - min) < (max - min); skidd13@8433: } skidd13@8433: rubidium@8626: /** rubidium@8626: * Type safe swap operation rubidium@8626: * @param a variable to swap with b rubidium@8626: * @param b variable to swap with a rubidium@8626: */ rubidium@8626: template void Swap(T& a, T& b) rubidium@8626: { rubidium@8626: T t = a; rubidium@8626: a = b; rubidium@8626: b = t; rubidium@8626: } rubidium@8626: skidd13@8433: #endif /* MATH_FUNC_HPP */