]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
macro: add CONST_MIN() similar to CONST_MAX()
authorLennart Poettering <lennart@poettering.net>
Fri, 29 May 2020 21:27:59 +0000 (23:27 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 25 Jun 2020 13:00:09 +0000 (15:00 +0200)
src/basic/macro.h

index 78e4262e41dcf5ffc8d78a7bb479d5de1206ba9f..fd8772f377f04e36de3e612a274017f1d0f303e8 100644 (file)
@@ -286,6 +286,15 @@ static inline size_t GREEDY_ALLOC_ROUND_UP(size_t l) {
                 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_MIN(_A, _B) \
+        (__builtin_choose_expr(                                         \
+                __builtin_constant_p(_A) &&                             \
+                __builtin_constant_p(_B) &&                             \
+                __builtin_types_compatible_p(typeof(_A), typeof(_B)),   \
+                ((_A) < (_B)) ? (_A) : (_B),                            \
+                VOID_0))
+
 #define MIN3(x, y, z)                                   \
         ({                                              \
                 const typeof(x) _c = MIN(x, y);         \