]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
drm/xe: Introduce IF_ARGS macro utility
authorMichal Wajdeczko <michal.wajdeczko@intel.com>
Wed, 17 Dec 2025 22:40:18 +0000 (23:40 +0100)
committerMichal Wajdeczko <michal.wajdeczko@intel.com>
Wed, 17 Dec 2025 22:42:40 +0000 (23:42 +0100)
We want to extend our macro-based KLV list definitions with new
information about the version from which given KLV is supported.
Add utility IF_ARGS macro that can be used in code generators to
emit different code based on the presence of additional arguments.

Introduce macro itself and extend our kunit tests to cover it.
We will use this macro in next patch.

Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
Reviewed-by: Daniele Ceraolo Spurio <daniele.ceraolospurio@intel.com>
Acked-by: Matthew Brost <matthew.brost@intel.com>
Link: https://patch.msgid.link/20251217224018.3490-1-michal.wajdeczko@intel.com
drivers/gpu/drm/xe/tests/xe_args_test.c
drivers/gpu/drm/xe/xe_args.h

index f3fb23aa5d2e109eb3d55202dc144bc5b6f49625..2687a1b054dd7d94621293c50dff2f5d73ca1a24 100644 (file)
@@ -78,6 +78,24 @@ static void pick_arg_example(struct kunit *test)
 #undef buz
 }
 
+static void if_args_example(struct kunit *test)
+{
+       enum { Z = 1, Q };
+
+#define foo    X, Y
+#define bar    IF_ARGS(Z, Q, foo)
+#define buz    IF_ARGS(Z, Q, DROP_FIRST_ARG(FIRST_ARG(foo)))
+
+       KUNIT_EXPECT_EQ(test, bar, Z);
+       KUNIT_EXPECT_EQ(test, buz, Q);
+       KUNIT_EXPECT_STREQ(test, __stringify(bar), "Z");
+       KUNIT_EXPECT_STREQ(test, __stringify(buz), "Q");
+
+#undef foo
+#undef bar
+#undef buz
+}
+
 static void sep_comma_example(struct kunit *test)
 {
 #define foo(f) f(X) f(Y) f(Z) f(Q)
@@ -198,6 +216,40 @@ static void last_arg_test(struct kunit *test)
        KUNIT_EXPECT_STREQ(test, __stringify(LAST_ARG(MAX_ARGS)), "-12");
 }
 
+static void if_args_test(struct kunit *test)
+{
+       bool with_args = true;
+       bool no_args = false;
+       enum { X = 100 };
+
+       KUNIT_EXPECT_TRUE(test, IF_ARGS(true, false, FOO_ARGS));
+       KUNIT_EXPECT_FALSE(test, IF_ARGS(true, false, NO_ARGS));
+
+       KUNIT_EXPECT_TRUE(test, CONCATENATE(IF_ARGS(with, no, FOO_ARGS), _args));
+       KUNIT_EXPECT_FALSE(test, CONCATENATE(IF_ARGS(with, no, NO_ARGS), _args));
+
+       KUNIT_EXPECT_STREQ(test, __stringify(IF_ARGS(yes, no, FOO_ARGS)), "yes");
+       KUNIT_EXPECT_STREQ(test, __stringify(IF_ARGS(yes, no, NO_ARGS)), "no");
+
+       KUNIT_EXPECT_EQ(test, IF_ARGS(CALL_ARGS(COUNT_ARGS, FOO_ARGS), -1, FOO_ARGS), 4);
+       KUNIT_EXPECT_EQ(test, IF_ARGS(CALL_ARGS(COUNT_ARGS, FOO_ARGS), -1, NO_ARGS), -1);
+       KUNIT_EXPECT_EQ(test, IF_ARGS(CALL_ARGS(COUNT_ARGS, NO_ARGS), -1, FOO_ARGS), 0);
+       KUNIT_EXPECT_EQ(test, IF_ARGS(CALL_ARGS(COUNT_ARGS, NO_ARGS), -1, NO_ARGS), -1);
+
+       KUNIT_EXPECT_EQ(test,
+                       CALL_ARGS(FIRST_ARG,
+                                 CALL_ARGS(CONCATENATE, IF_ARGS(FOO, MAX, FOO_ARGS), _ARGS)), X);
+       KUNIT_EXPECT_EQ(test,
+                       CALL_ARGS(FIRST_ARG,
+                                 CALL_ARGS(CONCATENATE, IF_ARGS(FOO, MAX, NO_ARGS), _ARGS)), -1);
+       KUNIT_EXPECT_EQ(test,
+                       CALL_ARGS(COUNT_ARGS,
+                                 CALL_ARGS(CONCATENATE, IF_ARGS(FOO, MAX, FOO_ARGS), _ARGS)), 4);
+       KUNIT_EXPECT_EQ(test,
+                       CALL_ARGS(COUNT_ARGS,
+                                 CALL_ARGS(CONCATENATE, IF_ARGS(FOO, MAX, NO_ARGS), _ARGS)), 12);
+}
+
 static struct kunit_case args_tests[] = {
        KUNIT_CASE(count_args_test),
        KUNIT_CASE(call_args_example),
@@ -209,6 +261,8 @@ static struct kunit_case args_tests[] = {
        KUNIT_CASE(last_arg_example),
        KUNIT_CASE(last_arg_test),
        KUNIT_CASE(pick_arg_example),
+       KUNIT_CASE(if_args_example),
+       KUNIT_CASE(if_args_test),
        KUNIT_CASE(sep_comma_example),
        {}
 };
index 4dbc7e53c624815727ed1e474bd47b31e31dc046..f550b5e3b993a4c5ed229a6577bbf6f8a2515ecb 100644 (file)
 #define PICK_ARG11(args...)            PICK_ARG10(DROP_FIRST_ARG(args))
 #define PICK_ARG12(args...)            PICK_ARG11(DROP_FIRST_ARG(args))
 
+/**
+ * IF_ARGS() - Make selection based on optional argument list.
+ * @then: token to return if arguments are present
+ * @else: token to return if arguments are empty
+ * @...: arguments to check (optional)
+ *
+ * This macro allows to select a token based on the presence of the argument list.
+ *
+ * Example:
+ *
+ *     #define foo     X, Y
+ *     #define bar     IF_ARGS(Z, Q, foo)
+ *     #define buz     IF_ARGS(Z, Q, DROP_FIRST_ARG(FIRST_ARG(foo)))
+ *
+ *     With above definitions bar expands to Z while buz expands to Q.
+ */
+#if defined(CONFIG_CC_IS_CLANG) || GCC_VERSION >= 100100
+#define IF_ARGS(then, else, ...)       FIRST_ARG(__VA_OPT__(then,) else)
+#else
+#define IF_ARGS(then, else, ...)       _IF_ARGS(then, else, CALL_ARGS(FIRST_ARG, __VA_ARGS__))
+#define _IF_ARGS(then, else, ...)      __IF_ARGS(then, else, CALL_ARGS(COUNT_ARGS, __VA_ARGS__))
+#define __IF_ARGS(then, else, n)       ___IF_ARGS(then, else, CALL_ARGS(CONCATENATE, ___IF_ARG, n))
+#define ___IF_ARGS(then, else, if)     CALL_ARGS(if, then, else)
+#define ___IF_ARG1(then, else)         then
+#define ___IF_ARG0(then, else)         else
+#endif
+
 /**
  * ARGS_SEP_COMMA - Definition of a comma character.
  *