BeRTOS
macros.h
Go to the documentation of this file.
00001 
00042 #ifndef CFG_MACROS_H
00043 #define CFG_MACROS_H
00044 
00045 #include <cfg/compiler.h>
00046 
00047 /* avr-gcc does not seem to support libstdc++ */
00048 #if defined(__cplusplus) && !CPU_AVR
00049     /* Type-generic macros implemented with template functions. */
00050     #include <algorithm>
00051 
00052     template<class T> inline T ABS(T n) { return n >= 0 ? n : -n; }
00053     #define MIN(a,b)   std::min(a, b)
00054     #define MAX(a,b)   std::max(a, b)
00055     #define SWAP(a,b)  std::swap(a, b)
00056 #elif (COMPILER_STATEMENT_EXPRESSIONS && COMPILER_TYPEOF)
00057     /* Type-generic macros implemented with statement expressions. */
00058     #define ABS(n) ({ \
00059         typeof(n) _n = (n); \
00060         (_n < 0) ? -_n : _n; \
00061     })
00062     #define MIN(a,b) ({ \
00063         typeof(a) _a = (a); \
00064         typeof(b) _b = (b); \
00065         ASSERT_TYPE_EQUAL(_a, _b); \
00066  \
00076         ((typeof(_a))((_a < _b) ? _a : _b)); \
00077     })
00078     #define MAX(a,b) ({ \
00079         typeof(a) _a = (a); \
00080         typeof(b) _b = (b); \
00081         ASSERT_TYPE_EQUAL(_a, _b); \
00082  \
00092         ((typeof(_a))((_a > _b) ? _a : _b)); \
00093     })
00094 #else /* !(COMPILER_STATEMENT_EXPRESSIONS && COMPILER_TYPEOF) */
00095     /* Buggy macros for inferior compilers.  */
00096     #define ABS(a)      (((a) < 0) ? -(a) : (a))
00097     #define MIN(a,b)    (((a) < (b)) ? (a) : (b))
00098     #define MAX(a,b)    (((a) > (b)) ? (a) : (b))
00099 #endif /* !(COMPILER_STATEMENT_EXPRESSIONS && COMPILER_TYPEOF) */
00100 
00102 #define ALIGN_UP(value, align)  (((value) & ((align) - 1)) ? \
00103                 (((value) + ((align) - 1)) & ~((align) - 1)) : \
00104                 (value))
00105 
00107 #define MINMAX(min,x,max)  (MIN(MAX(min, x), max))
00108 
00109 #ifdef __cplusplus
00110     /* Use standard implementation from <algorithm> */
00111     #define SWAP(a,b)  std::swap(a, b)
00112 #elif COMPILER_TYPEOF
00113 
00118     #define SWAP(a, b) \
00119         do { \
00120             typeof(a) tmp; \
00121             ASSERT_TYPE_EQUAL(a, b); \
00122             tmp = (a); \
00123             (a) = (b); \
00124             (b) = tmp; \
00125         } while (0)
00126 #else /* !COMPILER_TYPEOF */
00127     /* Sub-optimal implementation that only works with integral types. */
00128     #define SWAP(a, b) \
00129         do { \
00130             (a) ^= (b); \
00131             (b) ^= (a); \
00132             (a) ^= (b); \
00133         } while (0)
00134 
00135 #endif /* COMPILER_TYPEOF */
00136 
00140 #define SHUFFLE(array, len) \
00141     do { \
00142         int i, j; \
00143         for (i = (len) - 1; i > 0; i--) \
00144         { \
00145             j = ((i + 1) * (rand() / (RAND_MAX + 1.0))); \
00146             SWAP((array)[i], (array)[j]); \
00147         } \
00148     } while (0)
00149 
00155 #define SWAP_T(a, b, T) \
00156     do { \
00157         T tmp; \
00158         ASSERT_TYPE_IS(a, T); \
00159         ASSERT_TYPE_IS(b, T); \
00160         tmp = (a); \
00161         (a) = (b); \
00162         (b) = tmp; \
00163     } while (0)
00164 
00169 #define REVERSE_UINT8(b) \
00170     ((uint8_t)((((b) * 0x0802UL & 0x22110UL) | ((b) * 0x8020UL & 0x88440UL)) * 0x10101UL >> 16))
00171 
00172 #ifndef BV
00173 
00174     #define BV(x)  (1<<(x))
00175 #endif
00176 
00178 #define BV32(x)  ((uint32_t)1<<(x))
00179 
00181 #define BV16(x)  ((uint16_t)1<<(x))
00182 
00184 #define BV8(x)  ((uint8_t)1<<(x))
00185 
00191 #define DIV_ROUND(dividend, divisor)  (((dividend) + (divisor) / 2) / (divisor))
00192 
00197 #define DIV_ROUNDUP(dividend, divisor)  (((dividend) + (divisor) - 1) / (divisor))
00198 
00199 
00232 #define INT_MULT(a, f, prec) (((a) * (long)((f) * (1 << (prec)) + 0.5)) >> (prec))
00233 
00234 
00236 #define ROUND_UP2(x, pad) (((x) + ((pad) - 1)) & ~((pad) - 1))
00237 
00245 #define ROUND_DOWN(x, base)    ( (x) - ((x) % (base)) )
00246 #define ROUND_UP(x, base)      ( ((x) + (base) - 1) - (((x) + (base) - 1) % (base)) )
00247 #define ROUND_NEAREST(x, base) ( ((x) + (base) / 2) - (((x) + (base) / 2) % (base)) )
00248 /* \} */
00249 
00251 #define IS_POW2(x)     (!(bool)((x) & ((x)-1)))
00252 
00254 #define UINT8_LOG2(x) \
00255     ((x) < 2 ? 0 : \
00256      ((x) < 4 ? 1 : \
00257       ((x) < 8 ? 2 : \
00258        ((x) < 16 ? 3 : \
00259         ((x) < 32 ? 4 : \
00260          ((x) < 64 ? 5 : \
00261           ((x) < 128 ? 6 : 7)))))))
00262 
00264 #define UINT16_LOG2(x) \
00265     ((x < 256) ? UINT8_LOG2(x) : UINT8_LOG2((x) >> 8) + 8)
00266 
00268 #define UINT32_LOG2(x) \
00269     ((x < 65536UL) ? UINT16_LOG2(x) : UINT16_LOG2((x) >> 16) + 16)
00270 
00271 #if COMPILER_VARIADIC_MACROS
00272 
00273     #define PP_COUNT(...) \
00274         PP_COUNT__(__VA_ARGS__,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0)
00275     #define PP_COUNT__(a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,count,...) \
00276         count
00277 #endif
00278 
00279 #if COMPILER_VARIADIC_MACROS
00280 
00326     #define BIT_EXTRACT_FLAG_0(bit, value)  bit
00327     #define BIT_EXTRACT_FLAG_1(bit, value)  BV(bit)
00328     #define BIT_EXTRACT_VALUE__(bit, value) value
00329 
00330     #define BIT_MASK_SINGLE__(use_bv, index, max, arg) \
00331         ((index < max) ? (PP_CAT(BIT_EXTRACT_FLAG_, use_bv) arg) : 0) \
00332         /* */
00333 
00334     #define BIT_MASK_IF_SINGLE__(use_bv, index, max, arg) \
00335         (((index < max) && (BIT_EXTRACT_VALUE__ arg)) ? (PP_CAT(BIT_EXTRACT_FLAG_, use_bv) arg) : 0) \
00336         /* */
00337 
00338     #define BIT_ITER__2(macro, use_bv, max, a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15, ...) \
00339         (macro(use_bv, 0, max, a0) | \
00340         macro(use_bv, 1, max, a1) | \
00341         macro(use_bv, 2, max, a2) | \
00342         macro(use_bv, 3, max, a3) | \
00343         macro(use_bv, 4, max, a4) | \
00344         macro(use_bv, 5, max, a5) | \
00345         macro(use_bv, 6, max, a6) | \
00346         macro(use_bv, 7, max, a7) | \
00347         macro(use_bv, 8, max, a8) | \
00348         macro(use_bv, 9, max, a9) | \
00349         macro(use_bv, 10, max, a10) | \
00350         macro(use_bv, 11, max, a11) | \
00351         macro(use_bv, 12, max, a12) | \
00352         macro(use_bv, 13, max, a13) | \
00353         macro(use_bv, 14, max, a14) | \
00354         macro(use_bv, 15, max, a15)) \
00355         /* */
00356 
00357     #define BIT_ITER__(macro, use_bv, ...) \
00358         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)) \
00359         /* */
00360 
00361     #define BIT_MASKS__(use_bv, ...) \
00362         BIT_ITER__(BIT_MASK_SINGLE__, use_bv, __VA_ARGS__)
00363         /* */
00364 
00365     #define BIT_MASKS_CONDITIONAL__(use_bv, ...) \
00366         BIT_ITER__(BIT_MASK_IF_SINGLE__, use_bv, __VA_ARGS__)
00367         /* */
00368 
00369     #define BIT_CHANGE__(reg, use_bv, ...) \
00370         ((reg) = ((reg) & ~BIT_MASKS__(use_bv, __VA_ARGS__)) | BIT_MASKS_CONDITIONAL__(use_bv, __VA_ARGS__)) \
00371         /* */
00372 
00373     #define BIT_CHANGE(reg, ...)        BIT_CHANGE__(reg, 0, __VA_ARGS__)
00374     #define BIT_CHANGE_BV(reg, ...)     BIT_CHANGE__(reg, 1, __VA_ARGS__)
00375 
00376 #endif /* COMPILER_VARIADIC_MACROS */
00377 
00382 #define ROTR(var, rot) (((var) >> (rot)) | ((var) << ((sizeof(var) * 8) - (rot))))
00383 #define ROTL(var, rot) (((var) << (rot)) | ((var) >> ((sizeof(var) * 8) - (rot))))
00384 /*\}*/
00385 
00390 #define MAKE_ID(a,b,c,d) \
00391     ( ((uint32_t)(a) << 24) \
00392     | ((uint32_t)(b) << 16) \
00393     | ((uint32_t)(c) <<  8) \
00394     | ((uint32_t)(d) <<  0) )
00395 
00399 typedef uint32_t id_t;
00400 
00404 INLINE bool is_aligned(const void *addr, size_t size)
00405 {
00406     return ((size_t)addr & (size - 1)) == 0;
00407 }
00408  //defgroup macros
00410 
00411 #endif /* MACROS_H */
00412