macros.h

Go to the documentation of this file.
00001 
00039 #ifndef CFG_MACROS_H
00040 #define CFG_MACROS_H
00041 
00042 #include <cfg/compiler.h>
00043 
00044 /* avr-gcc does not seem to support libstdc++ */
00045 #if defined(__cplusplus) && !CPU_AVR
00046     /* Type-generic macros implemented with template functions. */
00047     #include <algorithm>
00048 
00049     template<class T> inline T ABS(T n) { return n >= 0 ? n : -n; }
00050     #define MIN(a,b)   std::min(a, b)
00051     #define MAX(a,b)   std::max(a, b)
00052     #define SWAP(a,b)  std::swap(a, b)
00053 #elif (COMPILER_STATEMENT_EXPRESSIONS && COMPILER_TYPEOF)
00054     /* Type-generic macros implemented with statement expressions. */
00055     #define ABS(n) ({ \
00056         typeof(n) _n = (n); \
00057         (_n < 0) ? -_n : _n; \
00058     })
00059     #define MIN(a,b) ({ \
00060         typeof(a) _a = (a); \
00061         typeof(b) _b = (b); \
00062         ASSERT_TYPE_EQUAL(_a, _b); \
00063  \
00073         ((typeof(_a))((_a < _b) ? _a : _b)); \
00074     })
00075     #define MAX(a,b) ({ \
00076         typeof(a) _a = (a); \
00077         typeof(b) _b = (b); \
00078         ASSERT_TYPE_EQUAL(_a, _b); \
00079  \
00089         ((typeof(_a))((_a > _b) ? _a : _b)); \
00090     })
00091 #else /* !(COMPILER_STATEMENT_EXPRESSIONS && COMPILER_TYPEOF) */
00092     /* Buggy macros for inferior compilers.  */
00093     #define ABS(a)      (((a) < 0) ? -(a) : (a))
00094     #define MIN(a,b)    (((a) < (b)) ? (a) : (b))
00095     #define MAX(a,b)    (((a) > (b)) ? (a) : (b))
00096 #endif /* !(COMPILER_STATEMENT_EXPRESSIONS && COMPILER_TYPEOF) */
00097 
00099 #define MINMAX(min,x,max)  (MIN(MAX(min, x), max))
00100 
00101 #ifdef __cplusplus
00102     /* Use standard implementation from <algorithm> */
00103     #define SWAP(a,b)  std::swap(a, b)
00104 #elif COMPILER_TYPEOF
00105 
00110     #define SWAP(a, b) \
00111         do { \
00112             typeof(a) tmp; \
00113             ASSERT_TYPE_EQUAL(a, b); \
00114             tmp = (a); \
00115             (a) = (b); \
00116             (b) = tmp; \
00117         } while (0)
00118 #else /* !COMPILER_TYPEOF */
00119     /* Sub-optimal implementation that only works with integral types. */
00120     #define SWAP(a, b) \
00121         do { \
00122             (a) ^= (b); \
00123             (b) ^= (a); \
00124             (a) ^= (b); \
00125         } while (0)
00126 
00127 #endif /* COMPILER_TYPEOF */
00128 
00132 #define SHUFFLE(array, len) \
00133     do { \
00134         int i, j; \
00135         for (i = (len) - 1; i > 0; i--) \
00136         { \
00137             j = ((i + 1) * (rand() / (RAND_MAX + 1.0))); \
00138             SWAP((array)[i], (array)[j]); \
00139         } \
00140     } while (0)
00141 
00147 #define SWAP_T(a, b, T) \
00148     do { \
00149         T tmp; \
00150         ASSERT_TYPE_IS(a, T); \
00151         ASSERT_TYPE_IS(b, T); \
00152         tmp = (a); \
00153         (a) = (b); \
00154         (b) = tmp; \
00155     } while (0)
00156 
00157 
00158 #ifndef BV
00159 
00160     #define BV(x)  (1<<(x))
00161 #endif
00162 
00164 #define BV32(x)  ((uint32_t)1<<(x))
00165 
00167 #define BV16(x)  ((uint16_t)1<<(x))
00168 
00170 #define BV8(x)  ((uint8_t)1<<(x))
00171 
00177 #define DIV_ROUND(dividend, divisor)  (((dividend) + (divisor) / 2) / (divisor))
00178 
00180 #define ROUND_UP2(x, pad) (((x) + ((pad) - 1)) & ~((pad) - 1))
00181 
00189 #define ROUND_DOWN(x, base)    ( (x) - ((x) % (base)) )
00190 #define ROUND_UP(x, base)      ( ((x) + (base) - 1) - (((x) + (base) - 1) % (base)) )
00191 #define ROUND_NEAREST(x, base) ( ((x) + (base) / 2) - (((x) + (base) / 2) % (base)) )
00192 /* \} */
00193 
00195 #define IS_POW2(x)     (!(bool)((x) & ((x)-1)))
00196 
00198 #define UINT8_LOG2(x) \
00199     ((x) < 2 ? 0 : \
00200      ((x) < 4 ? 1 : \
00201       ((x) < 8 ? 2 : \
00202        ((x) < 16 ? 3 : \
00203         ((x) < 32 ? 4 : \
00204          ((x) < 64 ? 5 : \
00205           ((x) < 128 ? 6 : 7)))))))
00206 
00208 #define UINT16_LOG2(x) \
00209     ((x < 256) ? UINT8_LOG2(x) : UINT8_LOG2((x) >> 8) + 8)
00210 
00212 #define UINT32_LOG2(x) \
00213     ((x < 65536UL) ? UINT16_LOG2(x) : UINT16_LOG2((x) >> 16) + 16)
00214 
00215 #if COMPILER_VARIADIC_MACROS
00216 
00217     #define PP_COUNT(...) \
00218         PP_COUNT__(__VA_ARGS__,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0)
00219     #define PP_COUNT__(a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,count,...) \
00220         count
00221 #endif
00222 
00223 #if COMPILER_VARIADIC_MACROS
00224 
00270     #define BIT_EXTRACT_FLAG_0(bit, value)  bit
00271     #define BIT_EXTRACT_FLAG_1(bit, value)  BV(bit)
00272     #define BIT_EXTRACT_VALUE__(bit, value) value
00273 
00274     #define BIT_MASK_SINGLE__(use_bv, index, max, arg) \
00275         ((index < max) ? (PP_CAT(BIT_EXTRACT_FLAG_, use_bv) arg) : 0) \
00276         
00277 
00278     #define BIT_MASK_IF_SINGLE__(use_bv, index, max, arg) \
00279         (((index < max) && (BIT_EXTRACT_VALUE__ arg)) ? (PP_CAT(BIT_EXTRACT_FLAG_, use_bv) arg) : 0) \
00280         
00281 
00282     #define BIT_ITER__2(macro, use_bv, max, a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15, ...) \
00283         (macro(use_bv, 0, max, a0) | \
00284         macro(use_bv, 1, max, a1) | \
00285         macro(use_bv, 2, max, a2) | \
00286         macro(use_bv, 3, max, a3) | \
00287         macro(use_bv, 4, max, a4) | \
00288         macro(use_bv, 5, max, a5) | \
00289         macro(use_bv, 6, max, a6) | \
00290         macro(use_bv, 7, max, a7) | \
00291         macro(use_bv, 8, max, a8) | \
00292         macro(use_bv, 9, max, a9) | \
00293         macro(use_bv, 10, max, a10) | \
00294         macro(use_bv, 11, max, a11) | \
00295         macro(use_bv, 12, max, a12) | \
00296         macro(use_bv, 13, max, a13) | \
00297         macro(use_bv, 14, max, a14) | \
00298         macro(use_bv, 15, max, a15)) \
00299         
00300 
00301     #define BIT_ITER__(macro, use_bv, ...) \
00302         BIT_ITER__2(macro, use_bv, PP_COUNT(__VA_ARGS__), __VA_ARGS__, (0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1)) \
00303         
00304 
00305     #define BIT_MASKS__(use_bv, ...) \
00306         BIT_ITER__(BIT_MASK_SINGLE__, use_bv, __VA_ARGS__)
00307         
00308 
00309     #define BIT_MASKS_CONDITIONAL__(use_bv, ...) \
00310         BIT_ITER__(BIT_MASK_IF_SINGLE__, use_bv, __VA_ARGS__)
00311         
00312 
00313     #define BIT_CHANGE__(reg, use_bv, ...) \
00314         ((reg) = ((reg) & ~BIT_MASKS__(use_bv, __VA_ARGS__)) | BIT_MASKS_CONDITIONAL__(use_bv, __VA_ARGS__)) \
00315         
00316 
00317     #define BIT_CHANGE(reg, ...)        BIT_CHANGE__(reg, 0, __VA_ARGS__)
00318     #define BIT_CHANGE_BV(reg, ...)     BIT_CHANGE__(reg, 1, __VA_ARGS__)
00319 
00320 #endif /* COMPILER_VARIADIC_MACROS */
00321 
00326 #define ROTR(var, rot) (((var) >> (rot)) | ((var) << ((sizeof(var) * 8) - (rot))))
00327 #define ROTL(var, rot) (((var) << (rot)) | ((var) >> ((sizeof(var) * 8) - (rot))))
00328 /*\}*/
00329 
00334 #define MAKE_ID(a,b,c,d) \
00335     ( ((uint32_t)(a) << 24) \
00336     | ((uint32_t)(b) << 16) \
00337     | ((uint32_t)(c) <<  8) \
00338     | ((uint32_t)(d) <<  0) )
00339 
00343 typedef uint32_t id_t;
00344 
00345 #endif /* MACROS_H */
00346