From 71e0310b33a94b339ed1e51fe3e9da19ab7f3510 Mon Sep 17 00:00:00 2001 From: Andrew Cooper Date: Fri, 27 May 2016 10:42:53 +0100 Subject: [PATCH] Introduce typesafe min()/max() helpers Signed-off-by: Andrew Cooper --- include/xtf/lib.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/include/xtf/lib.h b/include/xtf/lib.h index ddb9f98..6bece8e 100644 --- a/include/xtf/lib.h +++ b/include/xtf/lib.h @@ -30,6 +30,22 @@ void __noreturn panic(const char *fmt, ...) __printf(1, 2); #define BUILD_BUG_ON(cond) \ _Static_assert(!cond, "!(" #cond ")") +#define min(a, b) \ + ({ \ + const typeof(a) _a = (a); \ + const typeof(b) _b = (b); \ + (void)(&_a == &_b); \ + _a < _b ? _a : _b; \ + }) + +#define max(a, b) \ + ({ \ + const typeof(a) _a = (a); \ + const typeof(b) _b = (b); \ + (void)(&_a == &_b); \ + _a > _b ? _a : _b; \ + }) + void heapsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *), void (*swap)(void *, void *)); -- 2.39.5