Loading...
1/* SPDX-License-Identifier: MIT */
2/*
3 * Copyright © 2023 Intel Corporation
4 */
5
6#ifndef _XE_RTP_HELPERS_
7#define _XE_RTP_HELPERS_
8
9#ifndef _XE_RTP_INCLUDE_PRIVATE_HELPERS
10#error "This header is supposed to be included by xe_rtp.h only"
11#endif
12
13#include "xe_args.h"
14
15/*
16 * Helper macros - not to be used outside this header.
17 */
18#define _XE_ESC(...) __VA_ARGS__
19
20#define _XE_TUPLE_TAIL(...) (DROP_FIRST_ARG(__VA_ARGS__))
21
22#define _XE_RTP_CONCAT(a, b) CONCATENATE(XE_RTP_, CONCATENATE(a, b))
23
24#define __XE_RTP_PASTE_SEP_COMMA ,
25#define __XE_RTP_PASTE_SEP_BITWISE_OR |
26
27/*
28 * XE_RTP_PASTE_FOREACH - Paste XE_RTP_<@prefix_> on each element of the tuple
29 * @args, with the end result separated by @sep_. @sep must be one of the
30 * previously declared macros __XE_RTP_PASTE_SEP_*, or declared with such
31 * prefix.
32 *
33 * Examples:
34 *
35 * 1) XE_RTP_PASTE_FOREACH(TEST_, COMMA, (FOO, BAR))
36 * expands to:
37 *
38 * XE_RTP_TEST_FOO , XE_RTP_TEST_BAR
39 *
40 * 2) XE_RTP_PASTE_FOREACH(TEST2_, COMMA, (FOO))
41 * expands to:
42 *
43 * XE_RTP_TEST2_FOO
44 *
45 * 3) XE_RTP_PASTE_FOREACH(TEST3, BITWISE_OR, (FOO, BAR))
46 * expands to:
47 *
48 * XE_RTP_TEST3_FOO | XE_RTP_TEST3_BAR
49 *
50 * 4) #define __XE_RTP_PASTE_SEP_MY_SEP BANANA
51 * XE_RTP_PASTE_FOREACH(TEST_, MY_SEP, (FOO, BAR))
52 * expands to:
53 *
54 * XE_RTP_TEST_FOO BANANA XE_RTP_TEST_BAR
55 */
56#define XE_RTP_PASTE_FOREACH(prefix_, sep_, args_) _XE_RTP_CONCAT(PASTE_, COUNT_ARGS args_)(prefix_, sep_, args_)
57#define XE_RTP_PASTE_1(prefix_, sep_, args_) _XE_RTP_CONCAT(prefix_, FIRST_ARG args_)
58#define XE_RTP_PASTE_2(prefix_, sep_, args_) _XE_RTP_CONCAT(prefix_, FIRST_ARG args_) __XE_RTP_PASTE_SEP_ ## sep_ XE_RTP_PASTE_1(prefix_, sep_, _XE_TUPLE_TAIL args_)
59#define XE_RTP_PASTE_3(prefix_, sep_, args_) _XE_RTP_CONCAT(prefix_, FIRST_ARG args_) __XE_RTP_PASTE_SEP_ ## sep_ XE_RTP_PASTE_2(prefix_, sep_, _XE_TUPLE_TAIL args_)
60#define XE_RTP_PASTE_4(prefix_, sep_, args_) _XE_RTP_CONCAT(prefix_, FIRST_ARG args_) __XE_RTP_PASTE_SEP_ ## sep_ XE_RTP_PASTE_3(prefix_, sep_, _XE_TUPLE_TAIL args_)
61#define XE_RTP_PASTE_5(prefix_, sep_, args_) _XE_RTP_CONCAT(prefix_, FIRST_ARG args_) __XE_RTP_PASTE_SEP_ ## sep_ XE_RTP_PASTE_4(prefix_, sep_, _XE_TUPLE_TAIL args_)
62#define XE_RTP_PASTE_6(prefix_, sep_, args_) _XE_RTP_CONCAT(prefix_, FIRST_ARG args_) __XE_RTP_PASTE_SEP_ ## sep_ XE_RTP_PASTE_5(prefix_, sep_, _XE_TUPLE_TAIL args_)
63#define XE_RTP_PASTE_7(prefix_, sep_, args_) _XE_RTP_CONCAT(prefix_, FIRST_ARG args_) __XE_RTP_PASTE_SEP_ ## sep_ XE_RTP_PASTE_6(prefix_, sep_, _XE_TUPLE_TAIL args_)
64#define XE_RTP_PASTE_8(prefix_, sep_, args_) _XE_RTP_CONCAT(prefix_, FIRST_ARG args_) __XE_RTP_PASTE_SEP_ ## sep_ XE_RTP_PASTE_7(prefix_, sep_, _XE_TUPLE_TAIL args_)
65#define XE_RTP_PASTE_9(prefix_, sep_, args_) _XE_RTP_CONCAT(prefix_, FIRST_ARG args_) __XE_RTP_PASTE_SEP_ ## sep_ XE_RTP_PASTE_8(prefix_, sep_, _XE_TUPLE_TAIL args_)
66#define XE_RTP_PASTE_10(prefix_, sep_, args_) _XE_RTP_CONCAT(prefix_, FIRST_ARG args_) __XE_RTP_PASTE_SEP_ ## sep_ XE_RTP_PASTE_9(prefix_, sep_, _XE_TUPLE_TAIL args_)
67#define XE_RTP_PASTE_11(prefix_, sep_, args_) _XE_RTP_CONCAT(prefix_, FIRST_ARG args_) __XE_RTP_PASTE_SEP_ ## sep_ XE_RTP_PASTE_10(prefix_, sep_, _XE_TUPLE_TAIL args_)
68#define XE_RTP_PASTE_12(prefix_, sep_, args_) _XE_RTP_CONCAT(prefix_, FIRST_ARG args_) __XE_RTP_PASTE_SEP_ ## sep_ XE_RTP_PASTE_11(prefix_, sep_, _XE_TUPLE_TAIL args_)
69
70/*
71 * XE_RTP_DROP_CAST - Drop cast to convert a compound statement to a initializer
72 *
73 * Example:
74 *
75 * #define foo(a_) ((struct foo){ .a = a_ })
76 * XE_RTP_DROP_CAST(foo(10))
77 * expands to:
78 *
79 * { .a = 10 }
80 */
81#define XE_RTP_DROP_CAST(...) _XE_ESC(DROP_FIRST_ARG _XE_ESC __VA_ARGS__)
82
83#endif
1/* SPDX-License-Identifier: MIT */
2/*
3 * Copyright © 2023 Intel Corporation
4 */
5
6#ifndef _XE_RTP_HELPERS_
7#define _XE_RTP_HELPERS_
8
9#ifndef _XE_RTP_INCLUDE_PRIVATE_HELPERS
10#error "This header is supposed to be included by xe_rtp.h only"
11#endif
12
13/*
14 * Helper macros - not to be used outside this header.
15 */
16#define _XE_ESC(...) __VA_ARGS__
17#define _XE_COUNT_ARGS(...) _XE_ESC(__XE_COUNT_ARGS(__VA_ARGS__, 5, 4, 3, 2, 1,))
18#define __XE_COUNT_ARGS(_, _5, _4, _3, _2, X_, ...) X_
19
20#define _XE_FIRST(...) _XE_ESC(__XE_FIRST(__VA_ARGS__,))
21#define __XE_FIRST(x_, ...) x_
22#define _XE_TUPLE_TAIL(...) _XE_ESC(__XE_TUPLE_TAIL(__VA_ARGS__))
23#define __XE_TUPLE_TAIL(x_, ...) (__VA_ARGS__)
24
25#define _XE_DROP_FIRST(x_, ...) __VA_ARGS__
26
27#define _XE_RTP_CONCAT(a, b) __XE_RTP_CONCAT(a, b)
28#define __XE_RTP_CONCAT(a, b) XE_RTP_ ## a ## b
29
30#define __XE_RTP_PASTE_SEP_COMMA ,
31#define __XE_RTP_PASTE_SEP_BITWISE_OR |
32
33/*
34 * XE_RTP_PASTE_FOREACH - Paste XE_RTP_<@prefix_> on each element of the tuple
35 * @args, with the end result separated by @sep_. @sep must be one of the
36 * previously declared macros __XE_RTP_PASTE_SEP_*, or declared with such
37 * prefix.
38 *
39 * Examples:
40 *
41 * 1) XE_RTP_PASTE_FOREACH(TEST_, COMMA, (FOO, BAR))
42 * expands to:
43 *
44 * XE_RTP_TEST_FOO , XE_RTP_TEST_BAR
45 *
46 * 2) XE_RTP_PASTE_FOREACH(TEST2_, COMMA, (FOO))
47 * expands to:
48 *
49 * XE_RTP_TEST2_FOO
50 *
51 * 3) XE_RTP_PASTE_FOREACH(TEST3, BITWISE_OR, (FOO, BAR))
52 * expands to:
53 *
54 * XE_RTP_TEST3_FOO | XE_RTP_TEST3_BAR
55 *
56 * 4) #define __XE_RTP_PASTE_SEP_MY_SEP BANANA
57 * XE_RTP_PASTE_FOREACH(TEST_, MY_SEP, (FOO, BAR))
58 * expands to:
59 *
60 * XE_RTP_TEST_FOO BANANA XE_RTP_TEST_BAR
61 */
62#define XE_RTP_PASTE_FOREACH(prefix_, sep_, args_) _XE_ESC(_XE_RTP_CONCAT(PASTE_, _XE_COUNT_ARGS args_)(prefix_, sep_, args_))
63#define XE_RTP_PASTE_1(prefix_, sep_, args_) _XE_RTP_CONCAT(prefix_, _XE_FIRST args_)
64#define XE_RTP_PASTE_2(prefix_, sep_, args_) _XE_RTP_CONCAT(prefix_, _XE_FIRST args_) __XE_RTP_PASTE_SEP_ ## sep_ XE_RTP_PASTE_1(prefix_, sep_, _XE_TUPLE_TAIL args_)
65#define XE_RTP_PASTE_3(prefix_, sep_, args_) _XE_RTP_CONCAT(prefix_, _XE_FIRST args_) __XE_RTP_PASTE_SEP_ ## sep_ XE_RTP_PASTE_2(prefix_, sep_, _XE_TUPLE_TAIL args_)
66#define XE_RTP_PASTE_4(prefix_, sep_, args_) _XE_RTP_CONCAT(prefix_, _XE_FIRST args_) __XE_RTP_PASTE_SEP_ ## sep_ XE_RTP_PASTE_3(prefix_, sep_, _XE_TUPLE_TAIL args_)
67
68/*
69 * XE_RTP_DROP_CAST - Drop cast to convert a compound statement to a initializer
70 *
71 * Example:
72 *
73 * #define foo(a_) ((struct foo){ .a = a_ })
74 * XE_RTP_DROP_CAST(foo(10))
75 * expands to:
76 *
77 * { .a = 10 }
78 */
79#define XE_RTP_DROP_CAST(...) _XE_ESC(_XE_DROP_FIRST _XE_ESC __VA_ARGS__)
80
81#endif