lib/util: Implement min & max macros
authorJeremy Kerr <jk@ozlabs.org>
Mon, 21 Oct 2013 04:15:20 +0000 (12:15 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Wed, 13 Nov 2013 09:20:59 +0000 (17:20 +0800)
Standard type-safe min & max implementations.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
lib/util/util.h

index 30b472d47b6139c55d9b471d0e68a39fde076b20..8c08dc52eafd7e812949c78fba01e7f8b051a674 100644 (file)
 
 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
 
+#define min(a,b) ({                                    \
+               typeof(a) _min_a = (a);                 \
+               typeof(b) _min_b = (b);                 \
+               (void)(&_min_a == &_min_b);             \
+               _min_a < _min_b ? _min_a : _min_b;      \
+               })
+
+#define max(a,b) ({                                    \
+               typeof(a) _max_a = (a);                 \
+               typeof(b) _max_b = (b);                 \
+               (void)(&_max_a == &_max_b);             \
+               _max_a > _max_b ? _max_a : _max_b;      \
+               })
+
 #endif /* UTIL_H */