xref: /linux/drivers/gpu/drm/xe/xe_args.h (revision a3a02a52bcfcbcc4a637d4b68bf1bc391c9fad02)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Copyright © 2024 Intel Corporation
4  */
5 
6 #ifndef _XE_ARGS_H_
7 #define _XE_ARGS_H_
8 
9 #include <linux/args.h>
10 
11 /*
12  * Why don't the following macros have the XE prefix?
13  *
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.
16  */
17 
18 /**
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
22  *
23  * This macro allows calling macros which names might generated or we want to
24  * make sure it's arguments will be correctly expanded.
25  *
26  * Example:
27  *
28  *	#define foo	X,Y,Z,Q
29  *	#define bar	COUNT_ARGS(foo)
30  *	#define buz	CALL_ARGS(COUNT_ARGS, foo)
31  *
32  *	With above definitions bar expands to 1 while buz expands to 4.
33  */
34 #define CALL_ARGS(f, args...)		__CALL_ARGS(f, args)
35 #define __CALL_ARGS(f, args...)		f(args)
36 
37 /**
38  * DROP_FIRST_ARG - Returns all arguments except the first one.
39  * @args: arguments
40  *
41  * This helper macro allows manipulation the argument list before passing it
42  * to the next level macro.
43  *
44  * Example:
45  *
46  *	#define foo	X,Y,Z,Q
47  *	#define bar	CALL_ARGS(COUNT_ARGS, DROP_FIRST_ARG(foo))
48  *
49  *	With above definitions bar expands to 3.
50  */
51 #define DROP_FIRST_ARG(args...)		__DROP_FIRST_ARG(args)
52 #define __DROP_FIRST_ARG(a, b...)	b
53 
54 /**
55  * FIRST_ARG - Returns the first argument.
56  * @args: arguments
57  *
58  * This helper macro allows manipulation the argument list before passing it
59  * to the next level macro.
60  *
61  * Example:
62  *
63  *	#define foo	X,Y,Z,Q
64  *	#define bar	FIRST_ARG(foo)
65  *
66  *	With above definitions bar expands to X.
67  */
68 #define FIRST_ARG(args...)		__FIRST_ARG(args)
69 #define __FIRST_ARG(a, b...)		a
70 
71 /**
72  * LAST_ARG - Returns the last argument.
73  * @args: arguments
74  *
75  * This helper macro allows manipulation the argument list before passing it
76  * to the next level macro.
77  *
78  * Like COUNT_ARGS() this macro works up to 12 arguments.
79  *
80  * Example:
81  *
82  *	#define foo	X,Y,Z,Q
83  *	#define bar	LAST_ARG(foo)
84  *
85  *	With above definitions bar expands to Q.
86  */
87 #define LAST_ARG(args...)		__LAST_ARG(args)
88 #define __LAST_ARG(args...)		PICK_ARG(COUNT_ARGS(args), args)
89 
90 /**
91  * PICK_ARG - Returns the n-th argument.
92  * @n: argument number to be returned
93  * @args: arguments
94  *
95  * This helper macro allows manipulation the argument list before passing it
96  * to the next level macro.
97  *
98  * Like COUNT_ARGS() this macro supports n up to 12.
99  * Specialized macros PICK_ARG1() to PICK_ARG12() are also available.
100  *
101  * Example:
102  *
103  *	#define foo	X,Y,Z,Q
104  *	#define bar	PICK_ARG(2, foo)
105  *	#define buz	PICK_ARG3(foo)
106  *
107  *	With above definitions bar expands to Y and buz expands to Z.
108  */
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))
123 
124 /**
125  * ARGS_SEP_COMMA - Definition of a comma character.
126  *
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.
130  *
131  * Example:
132  *
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)))
136  *
137  *	With above definitions bar expands to
138  *		"X", "Y", "Z", "Q"
139  *	and buz expands to 4.
140  */
141 #define ARGS_SEP_COMMA			,
142 
143 #endif
144