1 /* SPDX-License-Identifier: MIT */
3 * Copyright © 2024 Intel Corporation
9 #include <linux/args.h>
12 * Why don't the following macros have the XE prefix?
14 * Once we find more potential users outside of the Xe driver, we plan to move
15 * all of the following macros unchanged to linux/args.h.
19 * CALL_ARGS - Invoke a macro, but allow parameters to be expanded beforehand.
20 * @f: name of the macro to invoke
21 * @args: arguments for the macro
23 * This macro allows calling macros which names might generated or we want to
24 * make sure it's arguments will be correctly expanded.
29 * #define bar COUNT_ARGS(foo)
30 * #define buz CALL_ARGS(COUNT_ARGS, foo)
32 * With above definitions bar expands to 1 while buz expands to 4.
34 #define CALL_ARGS(f, args...) __CALL_ARGS(f, args)
35 #define __CALL_ARGS(f, args...) f(args)
38 * DROP_FIRST_ARG - Returns all arguments except the first one.
41 * This helper macro allows manipulation the argument list before passing it
42 * to the next level macro.
47 * #define bar CALL_ARGS(COUNT_ARGS, DROP_FIRST_ARG(foo))
49 * With above definitions bar expands to 3.
51 #define DROP_FIRST_ARG(args...) __DROP_FIRST_ARG(args)
52 #define __DROP_FIRST_ARG(a, b...) b
55 * FIRST_ARG - Returns the first argument.
58 * This helper macro allows manipulation the argument list before passing it
59 * to the next level macro.
64 * #define bar FIRST_ARG(foo)
66 * With above definitions bar expands to X.
68 #define FIRST_ARG(args...) __FIRST_ARG(args)
69 #define __FIRST_ARG(a, b...) a
72 * LAST_ARG - Returns the last argument.
75 * This helper macro allows manipulation the argument list before passing it
76 * to the next level macro.
78 * Like COUNT_ARGS() this macro works up to 12 arguments.
83 * #define bar LAST_ARG(foo)
85 * With above definitions bar expands to Q.
87 #define LAST_ARG(args...) __LAST_ARG(args)
88 #define __LAST_ARG(args...) PICK_ARG(COUNT_ARGS(args), args)
91 * PICK_ARG - Returns the n-th argument.
92 * @n: argument number to be returned
95 * This helper macro allows manipulation the argument list before passing it
96 * to the next level macro.
98 * Like COUNT_ARGS() this macro supports n up to 12.
99 * Specialized macros PICK_ARG1() to PICK_ARG12() are also available.
103 * #define foo X,Y,Z,Q
104 * #define bar PICK_ARG(2, foo)
105 * #define buz PICK_ARG3(foo)
107 * With above definitions bar expands to Y and buz expands to Z.
109 #define PICK_ARG(n, args...) __PICK_ARG(n, args)
110 #define __PICK_ARG(n, args...) CALL_ARGS(CONCATENATE(PICK_ARG, n), args)
111 #define PICK_ARG1(args...) FIRST_ARG(args)
112 #define PICK_ARG2(args...) PICK_ARG1(DROP_FIRST_ARG(args))
113 #define PICK_ARG3(args...) PICK_ARG2(DROP_FIRST_ARG(args))
114 #define PICK_ARG4(args...) PICK_ARG3(DROP_FIRST_ARG(args))
115 #define PICK_ARG5(args...) PICK_ARG4(DROP_FIRST_ARG(args))
116 #define PICK_ARG6(args...) PICK_ARG5(DROP_FIRST_ARG(args))
117 #define PICK_ARG7(args...) PICK_ARG6(DROP_FIRST_ARG(args))
118 #define PICK_ARG8(args...) PICK_ARG7(DROP_FIRST_ARG(args))
119 #define PICK_ARG9(args...) PICK_ARG8(DROP_FIRST_ARG(args))
120 #define PICK_ARG10(args...) PICK_ARG9(DROP_FIRST_ARG(args))
121 #define PICK_ARG11(args...) PICK_ARG10(DROP_FIRST_ARG(args))
122 #define PICK_ARG12(args...) PICK_ARG11(DROP_FIRST_ARG(args))
125 * ARGS_SEP_COMMA - Definition of a comma character.
127 * This definition can be used in cases where any intermediate macro expects
128 * fixed number of arguments, but we want to pass more arguments which can
129 * be properly evaluated only by the next level macro.
133 * #define foo(f) f(X) f(Y) f(Z) f(Q)
134 * #define bar DROP_FIRST_ARG(foo(ARGS_SEP_COMMA __stringify))
135 * #define buz CALL_ARGS(COUNT_ARGS, DROP_FIRST_ARG(foo(ARGS_SEP_COMMA)))
137 * With above definitions bar expands to
139 * and buz expands to 4.
141 #define ARGS_SEP_COMMA ,