diff options
author | David Herrmann <dh.herrmann@gmail.com> | 2014-08-28 14:45:38 +0200 |
---|---|---|
committer | David Herrmann <dh.herrmann@gmail.com> | 2014-08-28 14:45:38 +0200 |
commit | 667a0377fb25ddb0c3efbc43d186ffd4c097ce41 (patch) | |
tree | ca22fac8400cc1ab67826bb545ae6f5907e78afd /src/shared | |
parent | d974ad0524942882f489914013d08ab16d147170 (diff) |
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 <dh.herrmann@gmail.com>
Date: Fri Aug 22 14:41:37 2014 +0200
shared: make container_of() use unique variable names
Diffstat (limited to 'src/shared')
-rw-r--r-- | src/shared/macro.h | 53 |
1 files changed, 30 insertions, 23 deletions
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 { \ |