mirror of
https://github.com/systemd/systemd.git
synced 2024-10-27 18:55:40 +03:00
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 fb835651af
Author: David Herrmann <dh.herrmann@gmail.com>
Date: Fri Aug 22 14:41:37 2014 +0200
shared: make container_of() use unique variable names
This commit is contained in:
parent
d974ad0524
commit
667a0377fb
@ -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 { \
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user