1 /**
2 * Copyright (C) Mellanox Technologies Ltd. 2001-2012.  ALL RIGHTS RESERVED.
3 *
4 * See file LICENSE for terms.
5 */
6 
7 #ifndef UCS_PREPROCESSOR_H
8 #define UCS_PREPROCESSOR_H
9 
10 /* Convert token to string */
11 #define UCS_PP_QUOTE(x)                 # x
12 
13 /* Paste two expanded tokens */
14 #define __UCS_TOKENPASTE_HELPER(x, y)   x ## y
15 #define UCS_PP_TOKENPASTE(x, y)         __UCS_TOKENPASTE_HELPER(x, y)
16 
17 /* Unique value generator */
18 #ifdef __COUNTER__
19 #  define UCS_PP_UNIQUE_ID __COUNTER__
20 #else
21 #  define UCS_PP_UNIQUE_ID __LINE__
22 #endif
23 
24 /* Creating unique identifiers, used for macros */
25 #define UCS_PP_APPEND_UNIQUE_ID(x)      UCS_PP_TOKENPASTE(x, UCS_PP_UNIQUE_ID)
26 
27 /* Convert to string */
28 #define _UCS_PP_MAKE_STRING(x) #x
29 #define UCS_PP_MAKE_STRING(x) _UCS_PP_MAKE_STRING(x)
30 
31 /*
32  * Count number of macro arguments
33  * e.g UCS_PP_NUM_ARGS(a,b) will expand to: 2
34  */
35 #define UCS_PP_MAX_ARGS 20
36 #define _UCS_PP_NUM_ARGS(_0,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,N,...) \
37     N
38 #define UCS_PP_NUM_ARGS(...) \
39     _UCS_PP_NUM_ARGS(, ## __VA_ARGS__,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0)
40 
41 
42 /* Expand macro for each argument in the list
43  * e.g
44  * UCS_PP_FOREACH(macro, arg, a, b, c) will expand to: macro(arg, a) macro(arg, b) macro(arg, c)
45  * UCS_PP_FOREACH_SEP(macro, arg, a, b, c) will expand to: macro(arg, a), macro(arg, b), macro(arg, c)
46  * UCS_PP_ZIP((a, b, c), (1, 2, 3)) will expand to: (a, 1), (b, 2), (c, 3)
47  */
48 #define UCS_PP_FOREACH(_macro, _arg, ...) \
49     UCS_PP_TOKENPASTE(_UCS_PP_FOREACH_, UCS_PP_NUM_ARGS(__VA_ARGS__))(_macro, _arg, __VA_ARGS__)
50 #define UCS_PP_FOREACH_SEP(_macro, _arg, ...) \
51     UCS_PP_TOKENPASTE(_UCS_PP_FOREACH_SEP_, UCS_PP_NUM_ARGS(__VA_ARGS__))(_macro, _arg, __VA_ARGS__)
52 #define UCS_PP_ZIP(_l1, _l2) \
53     UCS_PP_TOKENPASTE(_UCS_PP_ZIP_, UCS_PP_NUM_ARGS _l1)(_l1, _l2)
54 
55 #define _UCS_PP_FOREACH_0(_macro , _arg, ...)
56 #define _UCS_PP_FOREACH_1(_macro , _arg, _arg1, ...)  _macro(_arg, _arg1) _UCS_PP_FOREACH_0 (_macro, _arg, __VA_ARGS__)
57 #define _UCS_PP_FOREACH_2(_macro , _arg, _arg1, ...)  _macro(_arg, _arg1) _UCS_PP_FOREACH_1 (_macro, _arg, __VA_ARGS__)
58 #define _UCS_PP_FOREACH_3(_macro , _arg, _arg1, ...)  _macro(_arg, _arg1) _UCS_PP_FOREACH_2 (_macro, _arg, __VA_ARGS__)
59 #define _UCS_PP_FOREACH_4(_macro , _arg, _arg1, ...)  _macro(_arg, _arg1) _UCS_PP_FOREACH_3 (_macro, _arg, __VA_ARGS__)
60 #define _UCS_PP_FOREACH_5(_macro , _arg, _arg1, ...)  _macro(_arg, _arg1) _UCS_PP_FOREACH_4 (_macro, _arg, __VA_ARGS__)
61 #define _UCS_PP_FOREACH_6(_macro , _arg, _arg1, ...)  _macro(_arg, _arg1) _UCS_PP_FOREACH_5 (_macro, _arg, __VA_ARGS__)
62 #define _UCS_PP_FOREACH_7(_macro , _arg, _arg1, ...)  _macro(_arg, _arg1) _UCS_PP_FOREACH_6 (_macro, _arg, __VA_ARGS__)
63 #define _UCS_PP_FOREACH_8(_macro , _arg, _arg1, ...)  _macro(_arg, _arg1) _UCS_PP_FOREACH_7 (_macro, _arg, __VA_ARGS__)
64 #define _UCS_PP_FOREACH_9(_macro , _arg, _arg1, ...)  _macro(_arg, _arg1) _UCS_PP_FOREACH_8 (_macro, _arg, __VA_ARGS__)
65 #define _UCS_PP_FOREACH_10(_macro, _arg, _arg1, ...)  _macro(_arg, _arg1) _UCS_PP_FOREACH_9 (_macro, _arg, __VA_ARGS__)
66 #define _UCS_PP_FOREACH_11(_macro, _arg, _arg1, ...)  _macro(_arg, _arg1) _UCS_PP_FOREACH_10(_macro, _arg, __VA_ARGS__)
67 #define _UCS_PP_FOREACH_12(_macro, _arg, _arg1, ...)  _macro(_arg, _arg1) _UCS_PP_FOREACH_11(_macro, _arg, __VA_ARGS__)
68 #define _UCS_PP_FOREACH_13(_macro, _arg, _arg1, ...)  _macro(_arg, _arg1) _UCS_PP_FOREACH_12(_macro, _arg, __VA_ARGS__)
69 #define _UCS_PP_FOREACH_14(_macro, _arg, _arg1, ...)  _macro(_arg, _arg1) _UCS_PP_FOREACH_13(_macro, _arg, __VA_ARGS__)
70 #define _UCS_PP_FOREACH_15(_macro, _arg, _arg1, ...)  _macro(_arg, _arg1) _UCS_PP_FOREACH_14(_macro, _arg, __VA_ARGS__)
71 #define _UCS_PP_FOREACH_16(_macro, _arg, _arg1, ...)  _macro(_arg, _arg1) _UCS_PP_FOREACH_15(_macro, _arg, __VA_ARGS__)
72 #define _UCS_PP_FOREACH_17(_macro, _arg, _arg1, ...)  _macro(_arg, _arg1) _UCS_PP_FOREACH_16(_macro, _arg, __VA_ARGS__)
73 #define _UCS_PP_FOREACH_18(_macro, _arg, _arg1, ...)  _macro(_arg, _arg1) _UCS_PP_FOREACH_17(_macro, _arg, __VA_ARGS__)
74 #define _UCS_PP_FOREACH_19(_macro, _arg, _arg1, ...)  _macro(_arg, _arg1) _UCS_PP_FOREACH_18(_macro, _arg, __VA_ARGS__)
75 #define _UCS_PP_FOREACH_20(_macro, _arg, _arg1, ...)  _macro(_arg, _arg1) _UCS_PP_FOREACH_19(_macro, _arg, __VA_ARGS__)
76 
77 #define _UCS_PP_FOREACH_SEP_0(_macro , _arg, _arg1, ...)
78 #define _UCS_PP_FOREACH_SEP_1(_macro , _arg, _arg1, ...)  _macro(_arg, _arg1)
79 #define _UCS_PP_FOREACH_SEP_2(_macro , _arg, _arg1, ...)  _macro(_arg, _arg1), _UCS_PP_FOREACH_SEP_1 (_macro, _arg, __VA_ARGS__)
80 #define _UCS_PP_FOREACH_SEP_3(_macro , _arg, _arg1, ...)  _macro(_arg, _arg1), _UCS_PP_FOREACH_SEP_2 (_macro, _arg, __VA_ARGS__)
81 #define _UCS_PP_FOREACH_SEP_4(_macro , _arg, _arg1, ...)  _macro(_arg, _arg1), _UCS_PP_FOREACH_SEP_3 (_macro, _arg, __VA_ARGS__)
82 #define _UCS_PP_FOREACH_SEP_5(_macro , _arg, _arg1, ...)  _macro(_arg, _arg1), _UCS_PP_FOREACH_SEP_4 (_macro, _arg, __VA_ARGS__)
83 #define _UCS_PP_FOREACH_SEP_6(_macro , _arg, _arg1, ...)  _macro(_arg, _arg1), _UCS_PP_FOREACH_SEP_5 (_macro, _arg, __VA_ARGS__)
84 #define _UCS_PP_FOREACH_SEP_7(_macro , _arg, _arg1, ...)  _macro(_arg, _arg1), _UCS_PP_FOREACH_SEP_6 (_macro, _arg, __VA_ARGS__)
85 #define _UCS_PP_FOREACH_SEP_8(_macro , _arg, _arg1, ...)  _macro(_arg, _arg1), _UCS_PP_FOREACH_SEP_7 (_macro, _arg, __VA_ARGS__)
86 #define _UCS_PP_FOREACH_SEP_9(_macro , _arg, _arg1, ...)  _macro(_arg, _arg1), _UCS_PP_FOREACH_SEP_8 (_macro, _arg, __VA_ARGS__)
87 #define _UCS_PP_FOREACH_SEP_10(_macro, _arg, _arg1, ...)  _macro(_arg, _arg1), _UCS_PP_FOREACH_SEP_9 (_macro, _arg, __VA_ARGS__)
88 #define _UCS_PP_FOREACH_SEP_11(_macro, _arg, _arg1, ...)  _macro(_arg, _arg1), _UCS_PP_FOREACH_SEP_10(_macro, _arg, __VA_ARGS__)
89 #define _UCS_PP_FOREACH_SEP_12(_macro, _arg, _arg1, ...)  _macro(_arg, _arg1), _UCS_PP_FOREACH_SEP_11(_macro, _arg, __VA_ARGS__)
90 #define _UCS_PP_FOREACH_SEP_13(_macro, _arg, _arg1, ...)  _macro(_arg, _arg1), _UCS_PP_FOREACH_SEP_12(_macro, _arg, __VA_ARGS__)
91 #define _UCS_PP_FOREACH_SEP_14(_macro, _arg, _arg1, ...)  _macro(_arg, _arg1), _UCS_PP_FOREACH_SEP_13(_macro, _arg, __VA_ARGS__)
92 #define _UCS_PP_FOREACH_SEP_15(_macro, _arg, _arg1, ...)  _macro(_arg, _arg1), _UCS_PP_FOREACH_SEP_14(_macro, _arg, __VA_ARGS__)
93 #define _UCS_PP_FOREACH_SEP_16(_macro, _arg, _arg1, ...)  _macro(_arg, _arg1), _UCS_PP_FOREACH_SEP_15(_macro, _arg, __VA_ARGS__)
94 #define _UCS_PP_FOREACH_SEP_17(_macro, _arg, _arg1, ...)  _macro(_arg, _arg1), _UCS_PP_FOREACH_SEP_16(_macro, _arg, __VA_ARGS__)
95 #define _UCS_PP_FOREACH_SEP_18(_macro, _arg, _arg1, ...)  _macro(_arg, _arg1), _UCS_PP_FOREACH_SEP_17(_macro, _arg, __VA_ARGS__)
96 #define _UCS_PP_FOREACH_SEP_19(_macro, _arg, _arg1, ...)  _macro(_arg, _arg1), _UCS_PP_FOREACH_SEP_18(_macro, _arg, __VA_ARGS__)
97 #define _UCS_PP_FOREACH_SEP_20(_macro, _arg, _arg1, ...)  _macro(_arg, _arg1), _UCS_PP_FOREACH_SEP_19(_macro, _arg, __VA_ARGS__)
98 
99 #define _UCS_PP_ZIP_0(_l1, _l2)
100 #define _UCS_PP_ZIP_1(_l1, _l2)       _UCS_PP_ZIP_0(_l1, _l2)  (UCS_PP_TUPLE_0 _l1, UCS_PP_TUPLE_0 _l2)
101 #define _UCS_PP_ZIP_2(_l1, _l2)       _UCS_PP_ZIP_1(_l1, _l2), (UCS_PP_TUPLE_1 _l1, UCS_PP_TUPLE_1 _l2)
102 #define _UCS_PP_ZIP_3(_l1, _l2)       _UCS_PP_ZIP_2(_l1, _l2), (UCS_PP_TUPLE_2 _l1, UCS_PP_TUPLE_2 _l2)
103 #define _UCS_PP_ZIP_4(_l1, _l2)       _UCS_PP_ZIP_3(_l1, _l2), (UCS_PP_TUPLE_3 _l1, UCS_PP_TUPLE_3 _l2)
104 #define _UCS_PP_ZIP_5(_l1, _l2)       _UCS_PP_ZIP_4(_l1, _l2), (UCS_PP_TUPLE_4 _l1, UCS_PP_TUPLE_4 _l2)
105 #define _UCS_PP_ZIP_6(_l1, _l2)       _UCS_PP_ZIP_5(_l1, _l2), (UCS_PP_TUPLE_5 _l1, UCS_PP_TUPLE_5 _l2)
106 #define _UCS_PP_ZIP_7(_l1, _l2)       _UCS_PP_ZIP_6(_l1, _l2), (UCS_PP_TUPLE_6 _l1, UCS_PP_TUPLE_6 _l2)
107 #define _UCS_PP_ZIP_8(_l1, _l2)       _UCS_PP_ZIP_7(_l1, _l2), (UCS_PP_TUPLE_7 _l1, UCS_PP_TUPLE_7 _l2)
108 #define _UCS_PP_ZIP_9(_l1, _l2)       _UCS_PP_ZIP_8(_l1, _l2), (UCS_PP_TUPLE_8 _l1, UCS_PP_TUPLE_8 _l2)
109 #define _UCS_PP_ZIP_10(_l1, _l2)      _UCS_PP_ZIP_9(_l1, _l2), (UCS_PP_TUPLE_9 _l1, UCS_PP_TUPLE_9 _l2)
110 
111 
112 /* Extract elements from tuples
113  */
114 #define UCS_PP_TUPLE_0(_0, ...)                                            _0
115 #define UCS_PP_TUPLE_1(_0, _1, ...)                                        _1
116 #define UCS_PP_TUPLE_2(_0, _1, _2, ...)                                    _2
117 #define UCS_PP_TUPLE_3(_0, _1, _2, _3, ...)                                _3
118 #define UCS_PP_TUPLE_4(_0, _1, _2, _3, _4, ...)                            _4
119 #define UCS_PP_TUPLE_5(_0, _1, _2, _3, _4, _5, ...)                        _5
120 #define UCS_PP_TUPLE_6(_0, _1, _2, _3, _4, _5, _6, ...)                    _6
121 #define UCS_PP_TUPLE_7(_0, _1, _2, _3, _4, _5, _6, _7, ...)                _7
122 #define UCS_PP_TUPLE_8(_0, _1, _2, _3, _4, _5, _6, _7, _8, ...)            _8
123 #define UCS_PP_TUPLE_9(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, ...)        _9
124 #define UCS_PP_TUPLE_10(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, ...)  _10
125 #define UCS_PP_TUPLE_BREAK(...)                                            __VA_ARGS__
126 
127 
128 /* Sequence of numbers
129  */
130 #define _UCS_PP_SEQ_0
131 #define _UCS_PP_SEQ_1     _UCS_PP_SEQ_0   0
132 #define _UCS_PP_SEQ_2     _UCS_PP_SEQ_1 , 1
133 #define _UCS_PP_SEQ_3     _UCS_PP_SEQ_2 , 2
134 #define _UCS_PP_SEQ_4     _UCS_PP_SEQ_3 , 3
135 #define _UCS_PP_SEQ_5     _UCS_PP_SEQ_4 , 4
136 #define _UCS_PP_SEQ_6     _UCS_PP_SEQ_5 , 5
137 #define _UCS_PP_SEQ_7     _UCS_PP_SEQ_6 , 6
138 #define _UCS_PP_SEQ_8     _UCS_PP_SEQ_7 , 7
139 #define _UCS_PP_SEQ_9     _UCS_PP_SEQ_8 , 8
140 #define _UCS_PP_SEQ_10    _UCS_PP_SEQ_9 , 9
141 #define _UCS_PP_SEQ_11    _UCS_PP_SEQ_10, 10
142 #define _UCS_PP_SEQ_12    _UCS_PP_SEQ_11, 11
143 #define _UCS_PP_SEQ_13    _UCS_PP_SEQ_12, 12
144 #define _UCS_PP_SEQ_14    _UCS_PP_SEQ_13, 13
145 #define _UCS_PP_SEQ_15    _UCS_PP_SEQ_14, 14
146 #define _UCS_PP_SEQ_16    _UCS_PP_SEQ_15, 15
147 #define _UCS_PP_SEQ_17    _UCS_PP_SEQ_16, 16
148 #define _UCS_PP_SEQ_18    _UCS_PP_SEQ_17, 17
149 #define _UCS_PP_SEQ_19    _UCS_PP_SEQ_18, 18
150 #define _UCS_PP_SEQ_20    _UCS_PP_SEQ_19, 19
151 #define _UCS_PP_SEQ(_n)   _UCS_PP_SEQ_##_n
152 #define UCS_PP_SEQ(_n)    _UCS_PP_SEQ(_n)
153 
154 #endif
155