X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=cfg%2Fmacros.h;h=081b20334cff9846f610a1a8c0b9338fc26fd2d1;hb=00d63661f45019b60d2ae51de2354c193a0b6997;hp=6dd62487dace13b6f4317414ec11194297b2297b;hpb=af9c555446161016fdd76c1cdff96ce76bb6cba2;p=bertos.git diff --git a/cfg/macros.h b/cfg/macros.h index 6dd62487..081b2033 100755 --- a/cfg/macros.h +++ b/cfg/macros.h @@ -2,7 +2,7 @@ * \file * * * \brief Common and handy function macros @@ -14,6 +14,21 @@ /*#* *#* $Log$ + *#* 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. *#* @@ -90,13 +105,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) */ @@ -120,15 +135,21 @@ */ #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 @@ -139,6 +160,18 @@ /*! Round up \a x to an even multiple of the 2's power \a pad */ #define ROUND2(x, pad) (((x) + ((pad) - 1)) & ~((pad) - 1)) +/*! + * \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 #define IS_POW2(x) (!(bool)((x) & ((x)-1)))