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 IS_ALIGNED(x, byte_count) ((uintptr_t)(const void *)(x) % (byte_count) == 0) 00255 00257 #define UINT8_LOG2(x) \ 00258 ((x) < 2 ? 0 : \ 00259 ((x) < 4 ? 1 : \ 00260 ((x) < 8 ? 2 : \ 00261 ((x) < 16 ? 3 : \ 00262 ((x) < 32 ? 4 : \ 00263 ((x) < 64 ? 5 : \ 00264 ((x) < 128 ? 6 : 7))))))) 00265 00267 #define UINT16_LOG2(x) \ 00268 ((x < 256) ? UINT8_LOG2(x) : UINT8_LOG2((x) >> 8) + 8) 00269 00271 #define UINT32_LOG2(x) \ 00272 ((x < 65536UL) ? UINT16_LOG2(x) : UINT16_LOG2((x) >> 16) + 16) 00273 00274 #if COMPILER_VARIADIC_MACROS 00275 00276 #define PP_COUNT(...) \ 00277 PP_COUNT__(__VA_ARGS__,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0) 00278 #define PP_COUNT__(a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,count,...) \ 00279 count 00280 #endif 00281 00282 #if COMPILER_VARIADIC_MACROS 00283 00329 #define BIT_EXTRACT_FLAG_0(bit, value) bit 00330 #define BIT_EXTRACT_FLAG_1(bit, value) BV(bit) 00331 #define BIT_EXTRACT_VALUE__(bit, value) value 00332 00333 #define BIT_MASK_SINGLE__(use_bv, index, max, arg) \ 00334 ((index < max) ? (PP_CAT(BIT_EXTRACT_FLAG_, use_bv) arg) : 0) \ 00335 /* */ 00336 00337 #define BIT_MASK_IF_SINGLE__(use_bv, index, max, arg) \ 00338 (((index < max) && (BIT_EXTRACT_VALUE__ arg)) ? (PP_CAT(BIT_EXTRACT_FLAG_, use_bv) arg) : 0) \ 00339 /* */ 00340 00341 #define BIT_ITER__2(macro, use_bv, max, a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15, ...) \ 00342 (macro(use_bv, 0, max, a0) | \ 00343 macro(use_bv, 1, max, a1) | \ 00344 macro(use_bv, 2, max, a2) | \ 00345 macro(use_bv, 3, max, a3) | \ 00346 macro(use_bv, 4, max, a4) | \ 00347 macro(use_bv, 5, max, a5) | \ 00348 macro(use_bv, 6, max, a6) | \ 00349 macro(use_bv, 7, max, a7) | \ 00350 macro(use_bv, 8, max, a8) | \ 00351 macro(use_bv, 9, max, a9) | \ 00352 macro(use_bv, 10, max, a10) | \ 00353 macro(use_bv, 11, max, a11) | \ 00354 macro(use_bv, 12, max, a12) | \ 00355 macro(use_bv, 13, max, a13) | \ 00356 macro(use_bv, 14, max, a14) | \ 00357 macro(use_bv, 15, max, a15)) \ 00358 /* */ 00359 00360 #define BIT_ITER__(macro, use_bv, ...) \ 00361 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)) \ 00362 /* */ 00363 00364 #define BIT_MASKS__(use_bv, ...) \ 00365 BIT_ITER__(BIT_MASK_SINGLE__, use_bv, __VA_ARGS__) 00366 /* */ 00367 00368 #define BIT_MASKS_CONDITIONAL__(use_bv, ...) \ 00369 BIT_ITER__(BIT_MASK_IF_SINGLE__, use_bv, __VA_ARGS__) 00370 /* */ 00371 00372 #define BIT_CHANGE__(reg, use_bv, ...) \ 00373 ((reg) = ((reg) & ~BIT_MASKS__(use_bv, __VA_ARGS__)) | BIT_MASKS_CONDITIONAL__(use_bv, __VA_ARGS__)) \ 00374 /* */ 00375 00376 #define BIT_CHANGE(reg, ...) BIT_CHANGE__(reg, 0, __VA_ARGS__) 00377 #define BIT_CHANGE_BV(reg, ...) BIT_CHANGE__(reg, 1, __VA_ARGS__) 00378 00379 #endif /* COMPILER_VARIADIC_MACROS */ 00380 00385 #define ROTR(var, rot) (((var) >> (rot)) | ((var) << ((sizeof(var) * 8) - (rot)))) 00386 #define ROTL(var, rot) (((var) << (rot)) | ((var) >> ((sizeof(var) * 8) - (rot)))) 00387 /*\}*/ 00388 00393 #define MAKE_ID(a,b,c,d) \ 00394 ( ((uint32_t)(a) << 24) \ 00395 | ((uint32_t)(b) << 16) \ 00396 | ((uint32_t)(c) << 8) \ 00397 | ((uint32_t)(d) << 0) ) 00398 00402 typedef uint32_t id_t; 00403 00407 INLINE bool is_aligned(const void *addr, size_t size) 00408 { 00409 return ((size_t)addr & (size - 1)) == 0; 00410 } 00411 00415 #define BCD_TO_INT_32BIT(bcd) \ 00416 ((uint32_t )((bcd) & 0xf) * 1 + \ 00417 (((bcd) >> 4) & 0xf) * 10 + \ 00418 (((bcd) >> 8) & 0xf) * 100 + \ 00419 (((bcd) >> 12) & 0xf) * 1000 + \ 00420 (((bcd) >> 16) & 0xf) * 10000 + \ 00421 (((bcd) >> 20) & 0xf) * 100000 + \ 00422 (((bcd) >> 24) & 0xf) * 1000000 + \ 00423 (((bcd) >> 28) & 0xf) * 10000000) \ 00424 00425 00432 #define UNSTUFF_BITS(resp, start, size) \ 00433 ({ \ 00434 const uint32_t __size = size; \ 00435 const uint32_t __mask = (__size < 32 ? 1 << __size : 0) - 1; \ 00436 const uint32_t __off = 3 - ((start) / 32); \ 00437 const uint32_t __shft = (start) & 31; \ 00438 uint32_t __res; \ 00439 \ 00440 __res = resp[__off] >> __shft; \ 00441 if (__size + __shft > 32) \ 00442 __res |= resp[__off-1] << ((32 - __shft) % 32); \ 00443 __res & __mask; \ 00444 }) 00445 00446 //defgroup macros 00448 00449 00460 #define IP_ADDR_TO_INT_TUPLE(addr) \ 00461 (int)((addr) >> 0 & 0xff), \ 00462 (int)((addr) >> 8 & 0xff), \ 00463 (int)((addr) >> 16 & 0xff), \ 00464 (int)((addr) >> 24 & 0xff) 00465 00466 #endif /* MACROS_H */ 00467
![(please configure the [header_logo] section in trac.ini)](/chrome/site/bertos_logo.png)