src/macros.h
branchcpp_gui
changeset 6268 4b5241e5dd10
parent 6254 abc6ad7c035c
child 6285 187e3ef04cc9
equal deleted inserted replaced
6267:7c8ec33959b1 6268:4b5241e5dd10
    16 
    16 
    17 #ifdef max
    17 #ifdef max
    18 #undef max
    18 #undef max
    19 #endif
    19 #endif
    20 
    20 
    21 /* Objective C don't like templates */
       
    22 #ifdef __cplusplus
       
    23 template <typename T>
    21 template <typename T>
    24 static inline T max(T a, T b)
    22 static inline T max(T a, T b)
    25 {
    23 {
    26 	return a >= b ? a : b;
    24 	return a >= b ? a : b;
    27 }
    25 }
    28 #endif
       
    29 
    26 
    30 static inline int min(int a, int b) { if (a <= b) return a; return b; }
    27 static inline int min(int a, int b) { if (a <= b) return a; return b; }
    31 
    28 
    32 static inline uint minu(uint a, uint b) { if (a <= b) return a; return b; }
    29 static inline uint minu(uint a, uint b) { if (a <= b) return a; return b; }
    33 
    30 
    44 	if (a <= min) return min;
    41 	if (a <= min) return min;
    45 	if (a >= max) return max;
    42 	if (a >= max) return max;
    46 	return a;
    43 	return a;
    47 }
    44 }
    48 
    45 
    49 static inline int32 BIGMULSS(int32 a, int32 b, int shift) {
    46 static inline int32 BIGMULSS(int32 a, int32 b, int shift)
    50 	return (int32)(((int64)(a) * (int64)(b)) >> (shift));
    47 {
       
    48 	return (int32)((int64)a * (int64)b >> shift);
    51 }
    49 }
    52 
    50 
    53 static inline uint32 BIGMULUS(uint32 a, uint32 b, int shift) {
    51 static inline uint32 BIGMULUS(uint32 a, uint32 b, int shift)
    54 	return (uint32)(((uint64)(a) * (uint64)(b)) >> (shift));
    52 {
       
    53 	return (uint32)((uint64)a * (uint64)b >> shift);
    55 }
    54 }
    56 
    55 
    57 static inline int64 BIGMULS(int32 a, int32 b) {
       
    58 	return (int64)(a) * (int64)(b);
       
    59 }
       
    60 
    56 
    61 /* OPT: optimized into an unsigned comparison */
    57 /* OPT: optimized into an unsigned comparison */
    62 //#define IS_INSIDE_1D(x, base, size) ((x) >= (base) && (x) < (base) + (size))
    58 //#define IS_INSIDE_1D(x, base, size) ((x) >= (base) && (x) < (base) + (size))
    63 #define IS_INSIDE_1D(x, base, size) ( (uint)((x) - (base)) < ((uint)(size)) )
    59 #define IS_INSIDE_1D(x, base, size) ( (uint)((x) - (base)) < ((uint)(size)) )
    64 
    60 
    65 template <typename T>
    61 template<typename T> static inline bool HASBIT(T x, int y)
    66 static inline bool HASBIT(T x, int y)
       
    67 {
    62 {
    68 	return (x & (((T)1) << y)) != 0;
    63 	return (x & ((T)1 << y)) != 0;
    69 }
    64 }
    70 
    65 
    71 template <typename T>
    66 template<typename T> static inline T SETBIT(T& x, int y)
    72 static inline T SETBIT(T& x, int y)
       
    73 {
    67 {
    74 	return x |= (((T)1) << y);
    68 	return x |= (T)1 << y;
    75 }
    69 }
    76 
    70 
    77 template <typename T>
    71 template<typename T> static inline T CLRBIT(T& x, int y)
    78 static inline T CLRBIT(T& x, int y)
       
    79 {
    72 {
    80 	return x &= ~(((T)1) << y);
    73 	return x &= ~((T)1 << y);
    81 }
    74 }
    82 
    75 
    83 template <typename T>
    76 template<typename T> static inline T TOGGLEBIT(T& x, int y)
    84 static inline T TOGGLEBIT(T& x, int y)
       
    85 {
    77 {
    86 	return x ^= (((T)1) << y);
    78 	return x ^= (T)1 << y;
    87 }
    79 }
    88 
    80 
    89 
    81 
    90 // checking more bits. Maybe unneccessary, but easy to use
    82 // checking more bits. Maybe unneccessary, but easy to use
    91 #define HASBITS(x,y) ((x) & (y))
    83 #define HASBITS(x,y) ((x) & (y))
   152 	for (_i = 0; _b != 0; _i++, _b >>= 1) \
   144 	for (_i = 0; _b != 0; _i++, _b >>= 1) \
   153 		if (_b & 1)
   145 		if (_b & 1)
   154 
   146 
   155 #define abs myabs
   147 #define abs myabs
   156 
   148 
   157 static inline int intxchg_(int *a, int b) { int t = *a; *a = b; return t; }
       
   158 #define intswap(a,b) ((b) = intxchg_(&(a), (b)))
       
   159 static inline int uintxchg_(uint *a, uint b) { uint t = *a; *a = b; return t; }
       
   160 #define uintswap(a,b) ((b) = uintxchg_(&(a), (b)))
       
   161 
       
   162 
   149 
   163 static inline uint16 ReadLE16Aligned(const void* x)
   150 static inline uint16 ReadLE16Aligned(const void* x)
   164 {
   151 {
   165 	return FROM_LE16(*(const uint16*)x);
   152 	return FROM_LE16(*(const uint16*)x);
   166 }
   153 }
   187  * @note n must be a power of 2
   174  * @note n must be a power of 2
   188  */
   175  */
   189 #define ALIGN(x, n) (((x) + (n) - 1) & ~((n) - 1))
   176 #define ALIGN(x, n) (((x) + (n) - 1) & ~((n) - 1))
   190 
   177 
   191 /** return the largest value that can be entered in a variable.
   178 /** return the largest value that can be entered in a variable.
   192  *  known to work for uint32.
       
   193  *  used by TGP to set the max value of the _patches.generation_seed in its definition
       
   194  */
   179  */
   195 #define MAX_UVALUE(type) ((type)~(type)0)
   180 #define MAX_UVALUE(type) ((type)~(type)0)
   196 
   181 
   197 #endif /* MACROS_H */
   182 #endif /* MACROS_H */