src/macros.h
changeset 7937 1e7a22ea20d4
parent 7932 6c3d71e8a129
child 7967 a230c063a672
equal deleted inserted replaced
7936:3df083e704f2 7937:1e7a22ea20d4
     2 
     2 
     3 /** @file macros.h */
     3 /** @file macros.h */
     4 
     4 
     5 #ifndef MACROS_H
     5 #ifndef MACROS_H
     6 #define MACROS_H
     6 #define MACROS_H
       
     7 
       
     8 #include "core/math_func.hpp"
     7 
     9 
     8 /**
    10 /**
     9  * Fetch n bits from x, started at bit s.
    11  * Fetch n bits from x, started at bit s.
    10  *
    12  *
    11  * This function can be used to fetch n bits from the value x. The
    13  * This function can be used to fetch n bits from the value x. The
    69 	const T mask = (T)(((1U << n) - 1) << s);
    71 	const T mask = (T)(((1U << n) - 1) << s);
    70 	x = (T)((x & ~mask) | ((x + (i << s)) & mask));
    72 	x = (T)((x & ~mask) | ((x + (i << s)) & mask));
    71 	return x;
    73 	return x;
    72 }
    74 }
    73 
    75 
    74 #ifdef min
       
    75 #undef min
       
    76 #endif
       
    77 
       
    78 #ifdef max
       
    79 #undef max
       
    80 #endif
       
    81 
       
    82 #ifdef abs
       
    83 #undef abs
       
    84 #endif
       
    85 
       
    86 /**
       
    87  * Returns the maximum of two values.
       
    88  *
       
    89  * This function returns the greater value of two given values.
       
    90  * If they are equal the value of a is returned.
       
    91  *
       
    92  * @param a The first value
       
    93  * @param b The second value
       
    94  * @return The greater value or a if equals
       
    95  */
       
    96 template<typename T> static inline T max(const T a, const T b)
       
    97 {
       
    98 	return a >= b ? a : b;
       
    99 }
       
   100 
       
   101 /**
       
   102  * Returns the minimum of two values.
       
   103  *
       
   104  * This function returns the smaller value of two given values.
       
   105  * If they are equal the value of b is returned.
       
   106  *
       
   107  * @param a The first value
       
   108  * @param b The second value
       
   109  * @return The smaller value or b if equals
       
   110  */
       
   111 template<typename T> static inline T min(const T a, const T b)
       
   112 {
       
   113 	return a < b ? a : b;
       
   114 }
       
   115 
       
   116 /**
       
   117  * Returns the minimum of two integer.
       
   118  *
       
   119  * This function returns the smaller value of two given integers.
       
   120  *
       
   121  * @param a The first integer
       
   122  * @param b The second integer
       
   123  * @return The smaller value
       
   124  */
       
   125 static inline int min(const int a, const int b)
       
   126 {
       
   127 	return a < b ? a : b;
       
   128 }
       
   129 
       
   130 /**
       
   131  * Returns the minimum of two unsigned integers.
       
   132  *
       
   133  * This function returns the smaller value of two given unsigned integers.
       
   134  *
       
   135  * @param a The first unsigned integer
       
   136  * @param b The second unsigned integer
       
   137  * @return The smaller value
       
   138  */
       
   139 static inline uint minu(const uint a, const uint b)
       
   140 {
       
   141 	return a < b ? a : b;
       
   142 }
       
   143 
       
   144 /**
       
   145  * Returns the absolute value of (scalar) variable.
       
   146  *
       
   147  * @note assumes variable to be signed
       
   148  * @param a The value we want to unsign
       
   149  * @return The unsigned value
       
   150  */
       
   151 template <typename T> static inline T abs(T a)
       
   152 {
       
   153 	return (a < (T)0) ? -a : a;
       
   154 }
       
   155 
       
   156 /**
       
   157  * Clamp an integer between an interval.
       
   158  *
       
   159  * This function returns a value which is between the given interval of
       
   160  * min and max. If the given value is in this interval the value itself
       
   161  * is returned otherwise the border of the interval is returned, according
       
   162  * which side of the interval was 'left'.
       
   163  *
       
   164  * @note The min value must be less or equal of max or you get some
       
   165  *       unexpected results.
       
   166  * @param a The value to clamp/truncate.
       
   167  * @param min The minimum of the interval.
       
   168  * @param max the maximum of the interval.
       
   169  * @returns A value between min and max which is closest to a.
       
   170  * @see ClampU(uint, uint, uint)
       
   171  */
       
   172 static inline int Clamp(const int a, const int min, const int max)
       
   173 {
       
   174 	if (a <= min) return min;
       
   175 	if (a >= max) return max;
       
   176 	return a;
       
   177 }
       
   178 
       
   179 /**
       
   180  * Clamp an unsigned integer between an interval.
       
   181  *
       
   182  * This function returns a value which is between the given interval of
       
   183  * min and max. If the given value is in this interval the value itself
       
   184  * is returned otherwise the border of the interval is returned, according
       
   185  * which side of the interval was 'left'.
       
   186  *
       
   187  * @note The min value must be less or equal of max or you get some
       
   188  *       unexpected results.
       
   189  * @param a The value to clamp/truncate.
       
   190  * @param min The minimum of the interval.
       
   191  * @param max the maximum of the interval.
       
   192  * @returns A value between min and max which is closest to a.
       
   193  * @see Clamp(int, int, int)
       
   194  */
       
   195 static inline uint ClampU(const uint a, const uint min, const uint max)
       
   196 {
       
   197 	if (a <= min) return min;
       
   198 	if (a >= max) return max;
       
   199 	return a;
       
   200 }
       
   201 
       
   202 /**
       
   203  * Reduce a signed 64-bit int to a signed 32-bit one
       
   204  *
       
   205  * This function clamps a 64-bit integer to a 32-bit integer.
       
   206  * If the 64-bit value is smaller than the smallest 32-bit integer
       
   207  * value 0x80000000 this value is returned (the left one bit is the sign bit).
       
   208  * If the 64-bit value is greater than the greatest 32-bit integer value 0x7FFFFFFF
       
   209  * this value is returned. In all other cases the 64-bit value 'fits' in a
       
   210  * 32-bits integer field and so the value is casted to int32 and returned.
       
   211  *
       
   212  * @param a The 64-bit value to clamps
       
   213  * @return The 64-bit value reduced to a 32-bit value
       
   214  * @see Clamp(int, int, int)
       
   215  */
       
   216 static inline int32 ClampToI32(const int64 a)
       
   217 {
       
   218 	if (a <= (int32)0x80000000) return 0x80000000;
       
   219 	if (a >= (int32)0x7FFFFFFF) return 0x7FFFFFFF;
       
   220 	return (int32)a;
       
   221 }
       
   222 
       
   223 /**
    76 /**
   224  * Checks if a bit in a value is set.
    77  * Checks if a bit in a value is set.
   225  *
    78  *
   226  * This function checks if a bit inside a value is set or not.
    79  * This function checks if a bit inside a value is set or not.
   227  * The y value specific the position of the bit, started at the
    80  * The y value specific the position of the bit, started at the
   407 
   260 
   408 	return num;
   261 	return num;
   409 }
   262 }
   410 
   263 
   411 /**
   264 /**
   412  * Checks if a value is between a window started at some base point.
       
   413  *
       
   414  * This function checks if the value x is between the value of base
       
   415  * and base+size. If x equals base this returns true. If x equals
       
   416  * base+size this returns false.
       
   417  *
       
   418  * @param x The value to check
       
   419  * @param base The base value of the interval
       
   420  * @param size The size of the interval
       
   421  * @return True if the value is in the interval, false else.
       
   422  */
       
   423 template<typename T> static inline bool IS_INSIDE_1D(const T x, const int base, const uint size)
       
   424 {
       
   425 	return (uint)(x - base) < size;
       
   426 }
       
   427 
       
   428 /**
       
   429  * Checks if a byte is in an interval.
       
   430  *
       
   431  * This macro returns true if a byte value is in the interval of [min, max).
       
   432  *
       
   433  * @param a The byte value to check
       
   434  * @param min The minimum of the interval
       
   435  * @param max The maximum of the interval
       
   436  * @see IS_INSIDE_1D
       
   437  */
       
   438 #define IS_BYTE_INSIDE(a, min, max) ((byte)((a) - (min)) < (byte)((max) - (min)))
       
   439 
       
   440 /**
       
   441  * Checks if an int is in an interval.
       
   442  *
       
   443  * This macro returns true if a integer value is in the interval of [min, max).
       
   444  *
       
   445  * @param a The integer value to check
       
   446  * @param min The minimum of the interval
       
   447  * @param max The maximum of the interval
       
   448  * @see IS_INSIDE_1D
       
   449  */
       
   450 #define IS_INT_INSIDE(a, min, max) ((uint)((a) - (min)) < (uint)((max) - (min)))
       
   451 
       
   452 /**
       
   453  * Flips a coin with a given probability.
   265  * Flips a coin with a given probability.
   454  *
   266  *
   455  * This macro can be used to get true or false randomized according to a
   267  * This macro can be used to get true or false randomized according to a
   456  * given probability. The parameter a and b create a percent value with
   268  * given probability. The parameter a and b create a percent value with
   457  * (a/b). The macro returns true in (a/b) percent.
   269  * (a/b). The macro returns true in (a/b) percent.
   543 template<typename T> static inline T ROR(const T x, const uint8 n)
   355 template<typename T> static inline T ROR(const T x, const uint8 n)
   544 {
   356 {
   545 	return (T)(x >> n | x << (sizeof(x) * 8 - n));
   357 	return (T)(x >> n | x << (sizeof(x) * 8 - n));
   546 }
   358 }
   547 
   359 
   548 /**
       
   549  * Return the smallest multiple of n equal or greater than x
       
   550  *
       
   551  * @note n must be a power of 2
       
   552  * @param x The min value
       
   553  * @param n The base of the number we are searching
       
   554  * @return The smallest multiple of n equal or greater than x
       
   555  */
       
   556 template<typename T> static inline T Align(const T x, uint n) {
       
   557 	n--;
       
   558 	return (T)((x + n) & ~(n));
       
   559 }
       
   560 
       
   561 /** return the largest value that can be entered in a variable.
   360 /** return the largest value that can be entered in a variable.
   562  */
   361  */
   563 #define MAX_UVALUE(type) ((type)~(type)0)
   362 #define MAX_UVALUE(type) ((type)~(type)0)
   564 
   363 
   565 #endif /* MACROS_H */
   364 #endif /* MACROS_H */