|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
From 667a0377fb25ddb0c3efbc43d186ffd4c097ce41 Mon Sep 17 00:00:00 2001
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
From: David Herrmann <dh.herrmann@gmail.com>
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
Date: Thu, 28 Aug 2014 14:45:38 +0200
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
Subject: [PATCH] macro: use unique variable names for math-macros
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
Similar to container_of(), we now use unique variable names for the bascic
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
math macros MAX, MIN, CLAMP, LESS_BY. Furthermore, unit tests are added to
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
verify they work as expected.
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
For a rationale, see:
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
commit fb835651aff79a1e7fc5795086c9b26e59a8e6ca
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
Author: David Herrmann <dh.herrmann@gmail.com>
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
Date: Fri Aug 22 14:41:37 2014 +0200
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
shared: make container_of() use unique variable names
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
---
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
src/shared/macro.h | 53 +++++++++++++++++++++++++++++-----------------------
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
src/test/test-util.c | 17 +++++++++++++++++
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
2 files changed, 47 insertions(+), 23 deletions(-)
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
diff --git a/src/shared/macro.h b/src/shared/macro.h
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
index e6734804bd..9ee332c8df 100644
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
--- a/src/shared/macro.h
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+++ b/src/shared/macro.h
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
@@ -134,12 +134,13 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
})
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
#undef MAX
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
-#define MAX(a,b) \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+#define MAX(a, b) __MAX(UNIQ, (a), UNIQ, (b))
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+#define __MAX(aq, a, bq, b) \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
__extension__ ({ \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- const typeof(a) _a = (a); \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- const typeof(b) _b = (b); \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- _a > _b ? _a : _b; \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- })
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ const typeof(a) UNIQ_T(A, aq) = (a); \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ const typeof(b) UNIQ_T(B, bq) = (b); \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ UNIQ_T(A,aq) > UNIQ_T(B,bq) ? UNIQ_T(A,aq) : UNIQ_T(B,bq); \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ })
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
/* evaluates to (void) if _A or _B are not constant or of different types */
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
#define CONST_MAX(_A, _B) \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
@@ -160,12 +161,13 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
})
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
#undef MIN
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
-#define MIN(a,b) \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+#define MIN(a, b) __MIN(UNIQ, (a), UNIQ, (b))
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+#define __MIN(aq, a, bq, b) \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
__extension__ ({ \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- const typeof(a) _a = (a); \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- const typeof(b) _b = (b); \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- _a < _b ? _a : _b; \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- })
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ const typeof(a) UNIQ_T(A, aq) = (a); \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ const typeof(b) UNIQ_T(B, bq) = (b); \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ UNIQ_T(A,aq) < UNIQ_T(B,bq) ? UNIQ_T(A,aq) : UNIQ_T(B,bq); \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ })
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
#define MIN3(x,y,z) \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
__extension__ ({ \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
@@ -173,22 +175,27 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
MIN(_c, z); \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
})
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
-#define LESS_BY(A,B) \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+#define LESS_BY(a, b) __LESS_BY(UNIQ, (a), UNIQ, (b))
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+#define __LESS_BY(aq, a, bq, b) \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
__extension__ ({ \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- const typeof(A) _A = (A); \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- const typeof(B) _B = (B); \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- _A > _B ? _A - _B : 0; \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- })
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ const typeof(a) UNIQ_T(A, aq) = (a); \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ const typeof(b) UNIQ_T(B, bq) = (b); \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ UNIQ_T(A,aq) > UNIQ_T(B,bq) ? UNIQ_T(A,aq) - UNIQ_T(B,bq) : 0; \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ })
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
-#ifndef CLAMP
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
-#define CLAMP(x, low, high) \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+#undef CLAMP
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+#define CLAMP(x, low, high) __CLAMP(UNIQ, (x), UNIQ, (low), UNIQ, (high))
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+#define __CLAMP(xq, x, lowq, low, highq, high) \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
__extension__ ({ \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- const typeof(x) _x = (x); \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- const typeof(low) _low = (low); \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- const typeof(high) _high = (high); \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- ((_x > _high) ? _high : ((_x < _low) ? _low : _x)); \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- })
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
-#endif
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ const typeof(x) UNIQ_T(X,xq) = (x); \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ const typeof(low) UNIQ_T(LOW,lowq) = (low); \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ const typeof(high) UNIQ_T(HIGH,highq) = (high); \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ UNIQ_T(X,xq) > UNIQ_T(HIGH,highq) ? \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ UNIQ_T(HIGH,highq) : \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ UNIQ_T(X,xq) < UNIQ_T(LOW,lowq) ? \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ UNIQ_T(LOW,lowq) : \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ UNIQ_T(X,xq); \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ })
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
#define assert_se(expr) \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
do { \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
diff --git a/src/test/test-util.c b/src/test/test-util.c
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
index 795f3a1b3d..72a8a6b130 100644
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
--- a/src/test/test-util.c
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+++ b/src/test/test-util.c
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
@@ -94,6 +94,23 @@ static void test_max(void) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
assert_cc(MAXSIZE(char[3], uint16_t) == 3);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
assert_cc(MAXSIZE(char[3], uint32_t) == 4);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
assert_cc(MAXSIZE(char, long) == sizeof(long));
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ assert_se(MAX(-5, 5) == 5);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ assert_se(MAX(5, 5) == 5);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ assert_se(MAX(MAX(1, MAX(2, MAX(3, 4))), 5) == 5);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ assert_se(MAX(MAX(1, MAX(2, MAX(3, 2))), 1) == 3);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ assert_se(MAX(MIN(1, MIN(2, MIN(3, 4))), 5) == 5);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ assert_se(MAX(MAX(1, MIN(2, MIN(3, 2))), 1) == 2);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ assert_se(LESS_BY(8, 4) == 4);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ assert_se(LESS_BY(8, 8) == 0);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ assert_se(LESS_BY(4, 8) == 0);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ assert_se(LESS_BY(16, LESS_BY(8, 4)) == 12);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ assert_se(LESS_BY(4, LESS_BY(8, 4)) == 0);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ assert_se(CLAMP(-5, 0, 1) == 0);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ assert_se(CLAMP(5, 0, 1) == 1);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ assert_se(CLAMP(5, -10, 1) == 1);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ assert_se(CLAMP(5, -10, 10) == 5);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ assert_se(CLAMP(CLAMP(0, -10, 10), CLAMP(-5, 10, 20), CLAMP(100, -5, 20)) == 10);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
static void test_container_of(void) {
|