From 667a0377fb25ddb0c3efbc43d186ffd4c097ce41 Mon Sep 17 00:00:00 2001 From: David Herrmann Date: Thu, 28 Aug 2014 14:45:38 +0200 Subject: macro: use unique variable names for math-macros Similar to container_of(), we now use unique variable names for the bascic math macros MAX, MIN, CLAMP, LESS_BY. Furthermore, unit tests are added to verify they work as expected. For a rationale, see: commit fb835651aff79a1e7fc5795086c9b26e59a8e6ca Author: David Herrmann Date: Fri Aug 22 14:41:37 2014 +0200 shared: make container_of() use unique variable names --- src/shared/macro.h | 53 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 23 deletions(-) (limited to 'src/shared') diff --git a/src/shared/macro.h b/src/shared/macro.h index e6734804bd..9ee332c8df 100644 --- a/src/shared/macro.h +++ b/src/shared/macro.h @@ -134,12 +134,13 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) { }) #undef MAX -#define MAX(a,b) \ +#define MAX(a, b) __MAX(UNIQ, (a), UNIQ, (b)) +#define __MAX(aq, a, bq, b) \ __extension__ ({ \ - const typeof(a) _a = (a); \ - const typeof(b) _b = (b); \ - _a > _b ? _a : _b; \ - }) + const typeof(a) UNIQ_T(A, aq) = (a); \ + const typeof(b) UNIQ_T(B, bq) = (b); \ + UNIQ_T(A,aq) > UNIQ_T(B,bq) ? UNIQ_T(A,aq) : UNIQ_T(B,bq); \ + }) /* evaluates to (void) if _A or _B are not constant or of different types */ #define CONST_MAX(_A, _B) \ @@ -160,12 +161,13 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) { }) #undef MIN -#define MIN(a,b) \ +#define MIN(a, b) __MIN(UNIQ, (a), UNIQ, (b)) +#define __MIN(aq, a, bq, b) \ __extension__ ({ \ - const typeof(a) _a = (a); \ - const typeof(b) _b = (b); \ - _a < _b ? _a : _b; \ - }) + const typeof(a) UNIQ_T(A, aq) = (a); \ + const typeof(b) UNIQ_T(B, bq) = (b); \ + UNIQ_T(A,aq) < UNIQ_T(B,bq) ? UNIQ_T(A,aq) : UNIQ_T(B,bq); \ + }) #define MIN3(x,y,z) \ __extension__ ({ \ @@ -173,22 +175,27 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) { MIN(_c, z); \ }) -#define LESS_BY(A,B) \ +#define LESS_BY(a, b) __LESS_BY(UNIQ, (a), UNIQ, (b)) +#define __LESS_BY(aq, a, bq, b) \ __extension__ ({ \ - const typeof(A) _A = (A); \ - const typeof(B) _B = (B); \ - _A > _B ? _A - _B : 0; \ - }) + const typeof(a) UNIQ_T(A, aq) = (a); \ + const typeof(b) UNIQ_T(B, bq) = (b); \ + UNIQ_T(A,aq) > UNIQ_T(B,bq) ? UNIQ_T(A,aq) - UNIQ_T(B,bq) : 0; \ + }) -#ifndef CLAMP -#define CLAMP(x, low, high) \ +#undef CLAMP +#define CLAMP(x, low, high) __CLAMP(UNIQ, (x), UNIQ, (low), UNIQ, (high)) +#define __CLAMP(xq, x, lowq, low, highq, high) \ __extension__ ({ \ - const typeof(x) _x = (x); \ - const typeof(low) _low = (low); \ - const typeof(high) _high = (high); \ - ((_x > _high) ? _high : ((_x < _low) ? _low : _x)); \ - }) -#endif + const typeof(x) UNIQ_T(X,xq) = (x); \ + const typeof(low) UNIQ_T(LOW,lowq) = (low); \ + const typeof(high) UNIQ_T(HIGH,highq) = (high); \ + UNIQ_T(X,xq) > UNIQ_T(HIGH,highq) ? \ + UNIQ_T(HIGH,highq) : \ + UNIQ_T(X,xq) < UNIQ_T(LOW,lowq) ? \ + UNIQ_T(LOW,lowq) : \ + UNIQ_T(X,xq); \ + }) #define assert_se(expr) \ do { \ -- cgit v1.2.3-54-g00ecf