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