X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=cfg%2Fmacros.h;h=17ee7887759914cca7814d090cfff253e2998fbd;hb=e40b7f1ecfc7be6267192d54b0650960dda84d6d;hp=85fb97af6f960dcbc5a781eafcd89e272379deda;hpb=6bf26ad78b32851d9dd56fbf68356bcbdded590e;p=bertos.git diff --git a/cfg/macros.h b/cfg/macros.h index 85fb97af..17ee7887 100755 --- a/cfg/macros.h +++ b/cfg/macros.h @@ -1,8 +1,8 @@ -/*! +/** * \file * * * \brief Common and handy function macros @@ -14,6 +14,30 @@ /*#* *#* $Log$ + *#* Revision 1.9 2006/07/19 12:56:25 bernie + *#* Convert to new Doxygen style. + *#* + *#* Revision 1.8 2006/03/13 02:06:55 bernie + *#* ROUND_UP2: Rename from ROUND2. + *#* + *#* Revision 1.7 2006/02/20 14:34:58 bernie + *#* Use portable type checking. + *#* + *#* Revision 1.6 2006/02/10 12:36:57 bernie + *#* Pacify IAR warnings for side-effects. + *#* + *#* Revision 1.5 2005/11/04 16:20:01 bernie + *#* Fix reference to README.devlib in header. + *#* + *#* Revision 1.4 2005/07/03 15:19:09 bernie + *#* Doxygen fix. + *#* + *#* Revision 1.3 2005/06/27 21:23:32 bernie + *#* ROUND_DOWN, ROUND_UP, ROUND_NEAREST: New macros. + *#* + *#* Revision 1.2 2005/04/11 19:10:27 bernie + *#* Include top-level headers from cfg/ subdir. + *#* *#* Revision 1.1 2005/04/11 19:04:13 bernie *#* Move top-level headers to cfg/ subdir. *#* @@ -67,7 +91,7 @@ #ifndef MACROS_H #define MACROS_H -#include +#include /* avr-gcc does not seem to support libstdc++ */ #if defined(__cplusplus) && !CPU_AVR @@ -87,13 +111,13 @@ #define MIN(a,b) ({ \ typeof(a) _a = (a); \ typeof(b) _b = (b); \ - (void)(&_a == &_b); /* ensure same type */ \ + ASSERT_TYPE_EQUAL(_a, _b); \ (_a < _b) ? _a : _b; \ }) #define MAX(a,b) ({ \ typeof(a) _a = (a); \ typeof(b) _b = (b); \ - (void)(&_a == &_b); /* ensure same type */ \ + ASSERT_TYPE_EQUAL(_a, _b); \ (_a > _b) ? _a : _b; \ }) #else /* !(COMPILER_STATEMENT_EXPRESSIONS && COMPILER_TYPEOF) */ @@ -103,43 +127,64 @@ #define MAX(a,b) (((a) > (b)) ? (a) : (b)) #endif /* !(COMPILER_STATEMENT_EXPRESSIONS && COMPILER_TYPEOF) */ -/*! Bound \a x between \a min and \a max. */ +/** Bound \a x between \a min and \a max. */ #define MINMAX(min,x,max) (MIN(MAX(min, x), max)) #ifdef __cplusplus /* Use standard implementation from */ #define SWAP(a,b) std::swap(a, b) #elif COMPILER_TYPEOF - /*! + /** * Type-generic macro to swap \a a with \a b. * * \note Arguments are evaluated multiple times. */ #define SWAP(a, b) \ do { \ - (void)(&(a) == &(b)); /* type check */ \ typeof(a) tmp; \ + ASSERT_TYPE_EQUAL(a, b); \ tmp = (a); \ (a) = (b); \ (b) = tmp; \ } while (0) #else /* !COMPILER_TYPEOF */ /* Sub-optimal implementation that only works with integral types. */ - #define SWAP(a, b) ((a) ^= (b) ^= (a) ^= (b)) + #define SWAP(a, b) \ + do { \ + (a) ^= (b); \ + (b) ^= (a); \ + (a) ^= (b); \ + } while (0) + #endif /* COMPILER_TYPEOF */ #ifndef BV - /*! Convert a bit value to a binary flag. */ + /** Convert a bit value to a binary flag. */ #define BV(x) (1<<(x)) #endif -/*! Round up \a x to an even multiple of the 2's power \a pad */ -#define ROUND2(x, pad) (((x) + ((pad) - 1)) & ~((pad) - 1)) +/** Round up \a x to an even multiple of the 2's power \a pad. */ +#define ROUND_UP2(x, pad) (((x) + ((pad) - 1)) & ~((pad) - 1)) + +/* OBSOLETE */ +#define ROUND2 ROUND_UP2 + +/** + * \name Integer round macros. + * + * Round \a x to a multiple of \a base. + * \note If \a x is signed these macros generate a lot of code. + * \{ + */ +#define ROUND_DOWN(x, base) ( (x) - ((x) % (base)) ) +#define ROUND_UP(x, base) ( ((x) + (base) - 1) - (((x) + (base) - 1) % (base)) ) +#define ROUND_NEAREST(x, base) ( ((x) + (base) / 2) - (((x) + (base) / 2) % (base)) ) +/* \} */ -//! Check if \a x is an integer power of 2 +/** Check if \a x is an integer power of 2. */ #define IS_POW2(x) (!(bool)((x) & ((x)-1))) -/*! Calculate a compile-time log2 for a uint8_t */ +/** Calculate a compile-time log2 for a uint8_t */ #define UINT8_LOG2(x) \ ((x) < 2 ? 0 : \ ((x) < 4 ? 1 : \ @@ -149,16 +194,16 @@ ((x) < 64 ? 5 : \ ((x) < 128 ? 6 : 7))))))) -/*! Calculate a compile-time log2 for a uint16_t */ +/** Calculate a compile-time log2 for a uint16_t */ #define UINT16_LOG2(x) \ ((x < 256) ? UINT8_LOG2(x) : UINT8_LOG2((x) >> 8) + 8) -/*! Calculate a compile-time log2 for a uint32_t */ +/** Calculate a compile-time log2 for a uint32_t */ #define UINT32_LOG2(x) \ ((x < 65536UL) ? UINT16_LOG2(x) : UINT16_LOG2((x) >> 16) + 16) #if COMPILER_VARIADIC_MACROS - /*! Count the number of arguments (up to 16). */ + /** Count the number of arguments (up to 16). */ #define PP_COUNT(...) \ PP_COUNT__(__VA_ARGS__,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0) #define PP_COUNT__(a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,count,...) \ @@ -166,7 +211,7 @@ #endif #if COMPILER_VARIADIC_MACROS - /*! + /** * \def BIT_CHANGE(reg, (mask, value), ...) * * This macro allows for efficient and compact bit toggling in a hardware @@ -205,7 +250,7 @@ * for use with C90 compilers, through Boost Preprocessor. */ - /*! + /** * \def BIT_CHANGE_BV(reg, (bit, value), ...) * * Similar to BIT_CHANGE(), but get bits instead of masks (and applies BV() to convert