FD.io VPP  v17.07-30-g839fa73
Vector Packet Processing
pipeline.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16  * pipeline.h: software pipeline infrastructure
17  *
18  * Copyright (c) 2010 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39 
40 #ifndef included_clib_pipeline_h
41 #define included_clib_pipeline_h
42 
43 #define clib_pipeline_stage(F,TYPE,ARG,I,BODY) \
44  always_inline void F##_inline (void * _, u32 I) \
45  { TYPE ARG = _; { BODY; } } \
46  never_inline void F##_no_inline (TYPE ARG, u32 I) \
47  { F##_inline (ARG, I); }
48 
49 #define clib_pipeline_stage_static(F,TYPE,ARG,I,BODY) \
50  static_always_inline void F##_inline (void * _, u32 I) \
51  { TYPE ARG = _; { BODY; } } \
52  never_inline void F##_no_inline (TYPE ARG, u32 I) \
53  { F##_inline (ARG, I); }
54 
55 #define clib_pipeline_stage_no_inline(F,TYPE,ARG,I,BODY) \
56  never_inline void F##_no_inline (void * _, u32 I) \
57  { TYPE ARG = _; { BODY; } } \
58  never_inline void F##_inline (TYPE ARG, u32 I) \
59  { F##_no_inline (ARG, I); }
60 
61 #define _clib_pipeline_var(v) _clib_pipeline_##v
62 
63 #define clib_pipeline_stage_execute(F,A,I,S) \
64  F##_##S (A, _clib_pipeline_var(i) - (I))
65 
66 #define clib_pipeline_main_stage(F,A,I) \
67  clib_pipeline_stage_execute (F, A, I, inline)
68 #define clib_pipeline_init_stage(F,A,I) \
69  if (_clib_pipeline_var(i) >= (I)) clib_pipeline_stage_execute (F, A, I, no_inline)
70 #define clib_pipeline_exit_stage(F,A,I) \
71  if (_clib_pipeline_var(i) >= (I) && _clib_pipeline_var(i) - (I) < _clib_pipeline_var(n_vectors)) \
72  clib_pipeline_stage_execute (F, A, I, no_inline)
73 
74 #define clib_pipeline_init_loop \
75  for (_clib_pipeline_var(i) = 0; \
76  _clib_pipeline_var(i) < \
77  clib_min (_clib_pipeline_var(n_stages) - 1, \
78  _clib_pipeline_var(n_vectors)); \
79  _clib_pipeline_var(i)++)
80 
81 #define clib_pipeline_main_loop \
82  for (; _clib_pipeline_var(i) < _clib_pipeline_var(n_vectors); \
83  _clib_pipeline_var(i)++)
84 
85 #define clib_pipeline_exit_loop \
86  for (; _clib_pipeline_var(i) < (_clib_pipeline_var(n_vectors) \
87  + _clib_pipeline_var(n_stages) - 1); \
88  _clib_pipeline_var(i)++)
89 
90 #define clib_pipeline_run_2_stage(N,ARG,STAGE0,STAGE1) \
91 do { \
92  uword _clib_pipeline_var(n_vectors) = (N); \
93  uword _clib_pipeline_var(n_stages) = 2; \
94  uword _clib_pipeline_var(i); \
95  \
96  clib_pipeline_init_loop \
97  { \
98  clib_pipeline_init_stage (STAGE0, ARG, 0); \
99  } \
100  \
101  clib_pipeline_main_loop \
102  { \
103  clib_pipeline_main_stage (STAGE0, ARG, 0); \
104  clib_pipeline_main_stage (STAGE1, ARG, 1); \
105  } \
106  \
107  clib_pipeline_exit_loop \
108  { \
109  clib_pipeline_exit_stage (STAGE1, ARG, 1); \
110  } \
111 } while (0)
112 
113 #define clib_pipeline_run_3_stage(N,ARG,STAGE0,STAGE1,STAGE2) \
114 do { \
115  uword _clib_pipeline_var(n_vectors) = (N); \
116  uword _clib_pipeline_var(n_stages) = 3; \
117  uword _clib_pipeline_var(i); \
118  \
119  clib_pipeline_init_loop \
120  { \
121  clib_pipeline_init_stage (STAGE0, ARG, 0); \
122  clib_pipeline_init_stage (STAGE1, ARG, 1); \
123  } \
124  \
125  clib_pipeline_main_loop \
126  { \
127  clib_pipeline_main_stage (STAGE0, ARG, 0); \
128  clib_pipeline_main_stage (STAGE1, ARG, 1); \
129  clib_pipeline_main_stage (STAGE2, ARG, 2); \
130  } \
131  \
132  clib_pipeline_exit_loop \
133  { \
134  clib_pipeline_exit_stage (STAGE1, ARG, 1); \
135  clib_pipeline_exit_stage (STAGE2, ARG, 2); \
136  } \
137 } while (0)
138 
139 #define clib_pipeline_run_4_stage(N,ARG,STAGE0,STAGE1,STAGE2,STAGE3) \
140 do { \
141  uword _clib_pipeline_var(n_vectors) = (N); \
142  uword _clib_pipeline_var(n_stages) = 4; \
143  uword _clib_pipeline_var(i); \
144  \
145  clib_pipeline_init_loop \
146  { \
147  clib_pipeline_init_stage (STAGE0, ARG, 0); \
148  clib_pipeline_init_stage (STAGE1, ARG, 1); \
149  clib_pipeline_init_stage (STAGE2, ARG, 2); \
150  } \
151  \
152  clib_pipeline_main_loop \
153  { \
154  clib_pipeline_main_stage (STAGE0, ARG, 0); \
155  clib_pipeline_main_stage (STAGE1, ARG, 1); \
156  clib_pipeline_main_stage (STAGE2, ARG, 2); \
157  clib_pipeline_main_stage (STAGE3, ARG, 3); \
158  } \
159  \
160  clib_pipeline_exit_loop \
161  { \
162  clib_pipeline_exit_stage (STAGE1, ARG, 1); \
163  clib_pipeline_exit_stage (STAGE2, ARG, 2); \
164  clib_pipeline_exit_stage (STAGE3, ARG, 3); \
165  } \
166 } while (0)
167 
168 #endif /* included_clib_pipeline_h */
169 
170 /*
171  * fd.io coding-style-patch-verification: ON
172  *
173  * Local Variables:
174  * eval: (c-set-style "gnu")
175  * End:
176  */