| 1 |
|
| 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 | * IF_ARGS() - Make selection based on optional argument list.
|
| 126 | * @then: token to return if arguments are present
|
| 127 | * @else: token to return if arguments are empty
|
| 128 | * @...: arguments to check (optional)
|
| 129 | *
|
| 130 | * This macro allows to select a token based on the presence of the argument list.
|
| 131 | *
|
| 132 | * Example:
|
| 133 | *
|
| 134 | * #define foo X, Y
|
| 135 | * #define bar IF_ARGS(Z, Q, foo)
|
| 136 | * #define buz IF_ARGS(Z, Q, DROP_FIRST_ARG(FIRST_ARG(foo)))
|
| 137 | *
|
| 138 | * With above definitions bar expands to Z while buz expands to Q.
|
| 139 |
|
| 140 | #if defined(CONFIG_CC_IS_CLANG) || GCC_VERSION >= 100100
|
| 141 | #define IF_ARGS(then, else, ...) FIRST_ARG(__VA_OPT__(then,) else)
|
| 142 | #else
|
| 143 | #define IF_ARGS(then, else, ...) _IF_ARGS(then, else, CALL_ARGS(FIRST_ARG, __VA_ARGS__))
|
| 144 | #define _IF_ARGS(then, else, ...) __IF_ARGS(then, else, CALL_ARGS(COUNT_ARGS, __VA_ARGS__))
|
| 145 | #define __IF_ARGS(then, else, n) ___IF_ARGS(then, else, CALL_ARGS(CONCATENATE, ___IF_ARG, n))
|
| 146 | #define ___IF_ARGS(then, else, if) CALL_ARGS(if, then, else)
|
| 147 | #define ___IF_ARG1(then, else) then
|
| 148 | #define ___IF_ARG0(then, else) else
|
| 149 | #endif
|
| 150 |
|
| 151 | |
| 152 | * ARGS_SEP_COMMA - Definition of a comma character.
|
| 153 | *
|
| 154 | * This definition can be used in cases where any intermediate macro expects
|
| 155 | * fixed number of arguments, but we want to pass more arguments which can
|
| 156 | * be properly evaluated only by the next level macro.
|
| 157 | *
|
| 158 | * Example:
|
| 159 | *
|
| 160 | * #define foo(f) f(X) f(Y) f(Z) f(Q)
|
| 161 | * #define bar DROP_FIRST_ARG(foo(ARGS_SEP_COMMA __stringify))
|
| 162 | * #define buz CALL_ARGS(COUNT_ARGS, DROP_FIRST_ARG(foo(ARGS_SEP_COMMA)))
|
| 163 | *
|
| 164 | * With above definitions bar expands to
|
| 165 | * "X", "Y", "Z", "Q"
|
| 166 | * and buz expands to 4.
|
| 167 |
|
| 168 | #define ARGS_SEP_COMMA ,
|
| 169 |
|
| 170 | #endif
|