summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/shared/macro.h53
-rw-r--r--src/test/test-util.c17
2 files changed, 47 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 { \
diff --git a/src/test/test-util.c b/src/test/test-util.c
index 795f3a1b3d..72a8a6b130 100644
--- a/src/test/test-util.c
+++ b/src/test/test-util.c
@@ -94,6 +94,23 @@ static void test_max(void) {
assert_cc(MAXSIZE(char[3], uint16_t) == 3);
assert_cc(MAXSIZE(char[3], uint32_t) == 4);
assert_cc(MAXSIZE(char, long) == sizeof(long));
+
+ assert_se(MAX(-5, 5) == 5);
+ assert_se(MAX(5, 5) == 5);
+ assert_se(MAX(MAX(1, MAX(2, MAX(3, 4))), 5) == 5);
+ assert_se(MAX(MAX(1, MAX(2, MAX(3, 2))), 1) == 3);
+ assert_se(MAX(MIN(1, MIN(2, MIN(3, 4))), 5) == 5);
+ assert_se(MAX(MAX(1, MIN(2, MIN(3, 2))), 1) == 2);
+ assert_se(LESS_BY(8, 4) == 4);
+ assert_se(LESS_BY(8, 8) == 0);
+ assert_se(LESS_BY(4, 8) == 0);
+ assert_se(LESS_BY(16, LESS_BY(8, 4)) == 12);
+ assert_se(LESS_BY(4, LESS_BY(8, 4)) == 0);
+ assert_se(CLAMP(-5, 0, 1) == 0);
+ assert_se(CLAMP(5, 0, 1) == 1);
+ assert_se(CLAMP(5, -10, 1) == 1);
+ assert_se(CLAMP(5, -10, 10) == 5);
+ assert_se(CLAMP(CLAMP(0, -10, 10), CLAMP(-5, 10, 20), CLAMP(100, -5, 20)) == 10);
}
static void test_container_of(void) {