src/core/math_func.hpp
branchnoai
changeset 11126 72d4c9314c72
parent 9869 6404afe43575
equal deleted inserted replaced
11111:1b984dab8cec 11126:72d4c9314c72
    25  *
    25  *
    26  * @param a The first value
    26  * @param a The first value
    27  * @param b The second value
    27  * @param b The second value
    28  * @return The greater value or a if equals
    28  * @return The greater value or a if equals
    29  */
    29  */
    30 template<typename T> static inline T max(const T a, const T b)
    30 template <typename T>
       
    31 static FORCEINLINE T max(const T a, const T b)
    31 {
    32 {
    32 	return (a >= b) ? a : b;
    33 	return (a >= b) ? a : b;
    33 }
    34 }
    34 
    35 
    35 /**
    36 /**
    40  *
    41  *
    41  * @param a The first value
    42  * @param a The first value
    42  * @param b The second value
    43  * @param b The second value
    43  * @return The smaller value or b if equals
    44  * @return The smaller value or b if equals
    44  */
    45  */
    45 template<typename T> static inline T min(const T a, const T b)
    46 template <typename T>
       
    47 static FORCEINLINE T min(const T a, const T b)
    46 {
    48 {
    47 	return (a < b) ? a : b;
    49 	return (a < b) ? a : b;
    48 }
    50 }
    49 
    51 
    50 /**
    52 /**
    54  *
    56  *
    55  * @param a The first integer
    57  * @param a The first integer
    56  * @param b The second integer
    58  * @param b The second integer
    57  * @return The smaller value
    59  * @return The smaller value
    58  */
    60  */
    59 static inline int min(const int a, const int b)
    61 static FORCEINLINE int min(const int a, const int b)
    60 {
    62 {
    61 	return (a < b) ? a : b;
    63 	return (a < b) ? a : b;
    62 }
    64 }
    63 
    65 
    64 /**
    66 /**
    68  *
    70  *
    69  * @param a The first unsigned integer
    71  * @param a The first unsigned integer
    70  * @param b The second unsigned integer
    72  * @param b The second unsigned integer
    71  * @return The smaller value
    73  * @return The smaller value
    72  */
    74  */
    73 static inline uint minu(const uint a, const uint b)
    75 static FORCEINLINE uint minu(const uint a, const uint b)
    74 {
    76 {
    75 	return (a < b) ? a : b;
    77 	return (a < b) ? a : b;
    76 }
    78 }
    77 
    79 
    78 /**
    80 /**
    80  *
    82  *
    81  * @note assumes variable to be signed
    83  * @note assumes variable to be signed
    82  * @param a The value we want to unsign
    84  * @param a The value we want to unsign
    83  * @return The unsigned value
    85  * @return The unsigned value
    84  */
    86  */
    85 template <typename T> static inline T abs(const T a)
    87 template <typename T>
       
    88 static FORCEINLINE T abs(const T a)
    86 {
    89 {
    87 	return (a < (T)0) ? -a : a;
    90 	return (a < (T)0) ? -a : a;
    88 }
    91 }
    89 
    92 
    90 /**
    93 /**
    93  * @note n must be a power of 2
    96  * @note n must be a power of 2
    94  * @param x The min value
    97  * @param x The min value
    95  * @param n The base of the number we are searching
    98  * @param n The base of the number we are searching
    96  * @return The smallest multiple of n equal or greater than x
    99  * @return The smallest multiple of n equal or greater than x
    97  */
   100  */
    98 template<typename T> static inline T Align(const T x, uint n)
   101 template <typename T>
    99 {
   102 static FORCEINLINE T Align(const T x, uint n)
       
   103 {
       
   104 	assert((n & (n - 1)) == 0 && n != 0);
   100 	n--;
   105 	n--;
   101 	return (T)((x + n) & ~(n));
   106 	return (T)((x + n) & ~((T)n));
       
   107 }
       
   108 
       
   109 /**
       
   110  * Return the smallest multiple of n equal or greater than x
       
   111  * Applies to pointers only
       
   112  *
       
   113  * @note n must be a power of 2
       
   114  * @param x The min value
       
   115  * @param n The base of the number we are searching
       
   116  * @return The smallest multiple of n equal or greater than x
       
   117  * @see Align()
       
   118  */
       
   119 
       
   120 assert_compile(sizeof(size_t) == sizeof(void *));
       
   121 
       
   122 template <typename T>
       
   123 static FORCEINLINE T *AlignPtr(T *x, uint n)
       
   124 {
       
   125 	return (T *)Align((size_t)x, n);
   102 }
   126 }
   103 
   127 
   104 /**
   128 /**
   105  * Clamp an integer between an interval.
   129  * Clamp an integer between an interval.
   106  *
   130  *
   115  * @param min The minimum of the interval.
   139  * @param min The minimum of the interval.
   116  * @param max the maximum of the interval.
   140  * @param max the maximum of the interval.
   117  * @returns A value between min and max which is closest to a.
   141  * @returns A value between min and max which is closest to a.
   118  * @see ClampU(uint, uint, uint)
   142  * @see ClampU(uint, uint, uint)
   119  */
   143  */
   120 static inline int Clamp(const int a, const int min, const int max)
   144 static FORCEINLINE int Clamp(const int a, const int min, const int max)
   121 {
   145 {
   122 	if (a <= min) return min;
   146 	if (a <= min) return min;
   123 	if (a >= max) return max;
   147 	if (a >= max) return max;
   124 	return a;
   148 	return a;
   125 }
   149 }
   138  * @param min The minimum of the interval.
   162  * @param min The minimum of the interval.
   139  * @param max the maximum of the interval.
   163  * @param max the maximum of the interval.
   140  * @returns A value between min and max which is closest to a.
   164  * @returns A value between min and max which is closest to a.
   141  * @see Clamp(int, int, int)
   165  * @see Clamp(int, int, int)
   142  */
   166  */
   143 static inline uint ClampU(const uint a, const uint min, const uint max)
   167 static FORCEINLINE uint ClampU(const uint a, const uint min, const uint max)
   144 {
   168 {
   145 	if (a <= min) return min;
   169 	if (a <= min) return min;
   146 	if (a >= max) return max;
   170 	if (a >= max) return max;
   147 	return a;
   171 	return a;
   148 }
   172 }
   159  *
   183  *
   160  * @param a The 64-bit value to clamps
   184  * @param a The 64-bit value to clamps
   161  * @return The 64-bit value reduced to a 32-bit value
   185  * @return The 64-bit value reduced to a 32-bit value
   162  * @see Clamp(int, int, int)
   186  * @see Clamp(int, int, int)
   163  */
   187  */
   164 static inline int32 ClampToI32(const int64 a)
   188 static FORCEINLINE int32 ClampToI32(const int64 a)
   165 {
   189 {
   166 	if (a <= INT32_MIN) return INT32_MIN;
   190 	if (a <= INT32_MIN) return INT32_MIN;
   167 	if (a >= INT32_MAX) return INT32_MAX;
   191 	if (a >= INT32_MAX) return INT32_MAX;
   168 	return (int32)a;
   192 	return (int32)a;
   169 }
   193 }
   173  *
   197  *
   174  * @param a The 64-bit value to clamp
   198  * @param a The 64-bit value to clamp
   175  * @return The 64-bit value reduced to a 16-bit value
   199  * @return The 64-bit value reduced to a 16-bit value
   176  * @see ClampU(uint, uint, uint)
   200  * @see ClampU(uint, uint, uint)
   177  */
   201  */
   178 static inline uint16 ClampToU16(const uint64 a)
   202 static FORCEINLINE uint16 ClampToU16(const uint64 a)
   179 {
   203 {
   180 	return (uint16)(a <= UINT16_MAX ? a : UINT16_MAX);
   204 	return (uint16)(a <= UINT16_MAX ? a : UINT16_MAX);
   181 }
   205 }
   182 
   206 
   183 /**
   207 /**
   185  *
   209  *
   186  * @param a The first scalar
   210  * @param a The first scalar
   187  * @param b The second scalar
   211  * @param b The second scalar
   188  * @return The absolute difference between the given scalars
   212  * @return The absolute difference between the given scalars
   189  */
   213  */
   190 template <typename T> static inline T Delta(const T a, const T b) {
   214 template <typename T>
       
   215 static FORCEINLINE T Delta(const T a, const T b)
       
   216 {
   191 	return (a < b) ? b - a : a - b;
   217 	return (a < b) ? b - a : a - b;
   192 }
   218 }
   193 
   219 
   194 /**
   220 /**
   195  * Checks if a value is between a window started at some base point.
   221  * Checks if a value is between a window started at some base point.
   201  * @param x The value to check
   227  * @param x The value to check
   202  * @param base The base value of the interval
   228  * @param base The base value of the interval
   203  * @param size The size of the interval
   229  * @param size The size of the interval
   204  * @return True if the value is in the interval, false else.
   230  * @return True if the value is in the interval, false else.
   205  */
   231  */
   206 template<typename T> static inline bool IsInsideBS(const T x, const uint base, const uint size)
   232 template <typename T>
       
   233 static FORCEINLINE bool IsInsideBS(const T x, const uint base, const uint size)
   207 {
   234 {
   208 	return (uint)(x - base) < size;
   235 	return (uint)(x - base) < size;
   209 }
   236 }
   210 
   237 
   211 /**
   238 /**
   216  * @param a The value to check
   243  * @param a The value to check
   217  * @param min The minimum of the interval
   244  * @param min The minimum of the interval
   218  * @param max The maximum of the interval
   245  * @param max The maximum of the interval
   219  * @see IsInsideBS()
   246  * @see IsInsideBS()
   220  */
   247  */
   221 template<typename T> static inline bool IsInsideMM(const T x, const uint min, const uint max)
   248 template <typename T>
       
   249 static FORCEINLINE bool IsInsideMM(const T x, const uint min, const uint max)
   222 {
   250 {
   223 	return (uint)(x - min) < (max - min);
   251 	return (uint)(x - min) < (max - min);
   224 }
   252 }
   225 
   253 
   226 /**
   254 /**
   227  * Type safe swap operation
   255  * Type safe swap operation
   228  * @param a variable to swap with b
   256  * @param a variable to swap with b
   229  * @param b variable to swap with a
   257  * @param b variable to swap with a
   230  */
   258  */
   231 template<typename T> void Swap(T& a, T& b)
   259 template <typename T>
       
   260 static FORCEINLINE void Swap(T &a, T &b)
   232 {
   261 {
   233 	T t = a;
   262 	T t = a;
   234 	a = b;
   263 	a = b;
   235 	b = t;
   264 	b = t;
   236 }
   265 }