FD.io VPP  v19.08.3-2-gbabecb413
Vector Packet Processing
clib.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  Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus
17 
18  Permission is hereby granted, free of charge, to any person obtaining
19  a copy of this software and associated documentation files (the
20  "Software"), to deal in the Software without restriction, including
21  without limitation the rights to use, copy, modify, merge, publish,
22  distribute, sublicense, and/or sell copies of the Software, and to
23  permit persons to whom the Software is furnished to do so, subject to
24  the following conditions:
25 
26  The above copyright notice and this permission notice shall be
27  included in all copies or substantial portions of the Software.
28 
29  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 */
37 
38 #ifndef included_clib_h
39 #define included_clib_h
40 
41 #include <stddef.h>
42 #include <vppinfra/config.h>
43 
44 /* Standalone means to not assume we are running on a Unix box. */
45 #if ! defined (CLIB_STANDALONE) && ! defined (CLIB_LINUX_KERNEL)
46 #define CLIB_UNIX
47 #endif
48 
49 #include <vppinfra/types.h>
50 #include <vppinfra/atomics.h>
51 
52 /* Global DEBUG flag. Setting this to 1 or 0 turns off
53  ASSERT (see vppinfra/error.h) & other debugging code. */
54 #ifndef CLIB_DEBUG
55 #define CLIB_DEBUG 0
56 #endif
57 
58 #ifndef NULL
59 #define NULL ((void *) 0)
60 #endif
61 
62 #define BITS(x) (8*sizeof(x))
63 #define ARRAY_LEN(x) (sizeof (x)/sizeof (x[0]))
64 
65 #define _STRUCT_FIELD(t,f) (((t *) 0)->f)
66 #define STRUCT_OFFSET_OF(t,f) offsetof(t, f)
67 #define STRUCT_BIT_OFFSET_OF(t,f) (BITS(u8) * STRUCT_OFFSET_OF (t, f))
68 #define STRUCT_SIZE_OF(t,f) (sizeof (_STRUCT_FIELD (t, f)))
69 #define STRUCT_BITS_OF(t,f) (BITS (_STRUCT_FIELD (t, f)))
70 #define STRUCT_ARRAY_LEN(t,f) ARRAY_LEN (_STRUCT_FIELD (t, f))
71 #define STRUCT_MARK(mark) u8 mark[0]
72 #define STRUCT_MARK_PTR(v, f) &(v)->f
73 
74 /* Stride in bytes between struct array elements. */
75 #define STRUCT_STRIDE_OF(t,f) \
76  ( ((uword) & (((t *) 0)[1].f)) \
77  - ((uword) & (((t *) 0)[0].f)))
78 
79 #define STRUCT_OFFSET_OF_VAR(v,f) ((uword) (&(v)->f) - (uword) (v))
80 
81 /* Used to pack structure elements. */
82 #define CLIB_PACKED(x) x __attribute__ ((packed))
83 #define CLIB_UNUSED(x) x __attribute__ ((unused))
84 
85 /* similar to CLIB_CACHE_LINE_ALIGN_MARK() but with arbitrary alignment */
86 #define CLIB_ALIGN_MARK(name, alignment) u8 name[0] __attribute__((aligned(alignment)))
87 
88 /* Make a string from the macro's argument */
89 #define CLIB_STRING_MACRO(x) #x
90 
91 #define __clib_unused __attribute__ ((unused))
92 #define __clib_weak __attribute__ ((weak))
93 #define __clib_packed __attribute__ ((packed))
94 #define __clib_constructor __attribute__ ((constructor))
95 
96 #define never_inline __attribute__ ((__noinline__))
97 
98 #if CLIB_DEBUG > 0
99 #define always_inline static inline
100 #define static_always_inline static inline
101 #else
102 #define always_inline static inline __attribute__ ((__always_inline__))
103 #define static_always_inline static inline __attribute__ ((__always_inline__))
104 #endif
105 
106 
107 /* Reserved (unused) structure element with address offset between
108  from and to. */
109 #define CLIB_PAD_FROM_TO(from,to) u8 pad_##from[(to) - (from)]
110 
111 /* Hints to compiler about hot/cold code. */
112 #define PREDICT_FALSE(x) __builtin_expect((x),0)
113 #define PREDICT_TRUE(x) __builtin_expect((x),1)
114 
115 /* Full memory barrier (read and write). */
116 #define CLIB_MEMORY_BARRIER() __sync_synchronize ()
117 
118 #if __x86_64__
119 #define CLIB_MEMORY_STORE_BARRIER() __builtin_ia32_sfence ()
120 #else
121 #define CLIB_MEMORY_STORE_BARRIER() __sync_synchronize ()
122 #endif
123 
124 /* Arranges for function to be called before main. */
125 #define INIT_FUNCTION(decl) \
126  decl __attribute ((constructor)); \
127  decl
128 
129 /* Arranges for function to be called before exit. */
130 #define EXIT_FUNCTION(decl) \
131  decl __attribute ((destructor)); \
132  decl
133 
134 /* Use __builtin_clz if available. */
135 #if uword_bits == 64
136 #define count_leading_zeros(x) __builtin_clzll (x)
137 #define count_trailing_zeros(x) __builtin_ctzll (x)
138 #else
139 #define count_leading_zeros(x) __builtin_clzl (x)
140 #define count_trailing_zeros(x) __builtin_ctzl (x)
141 #endif
142 
143 #if defined (count_leading_zeros)
146 {
147  uword n;
148  n = count_leading_zeros (x);
149  return BITS (uword) - n - 1;
150 }
151 #else
153 min_log2 (uword x)
154 {
155  uword a = x, b = BITS (uword) / 2, c = 0, r = 0;
156 
157  /* Reduce x to 4 bit result. */
158 #define _ \
159 { \
160  c = a >> b; \
161  if (c) a = c; \
162  if (c) r += b; \
163  b /= 2; \
164 }
165 
166  if (BITS (uword) > 32)
167  _;
168  _;
169  _;
170  _;
171 #undef _
172 
173  /* Do table lookup on 4 bit partial. */
174  if (BITS (uword) > 32)
175  {
176  const u64 table = 0x3333333322221104LL;
177  uword t = (table >> (4 * a)) & 0xf;
178  r = t < 4 ? r + t : ~0;
179  }
180  else
181  {
182  const u32 table = 0x22221104;
183  uword t = (a & 8) ? 3 : ((table >> (4 * a)) & 0xf);
184  r = t < 4 ? r + t : ~0;
185  }
186 
187  return r;
188 }
189 #endif
190 
193 {
194  uword l = min_log2 (x);
195  if (x > ((uword) 1 << l))
196  l++;
197  return l;
198 }
199 
202 {
203  if (BITS (uword) == 64)
204  return min_log2 (x);
205  else
206  {
207  uword l, y;
208  y = x;
209  l = 0;
210  if (y == 0)
211  {
212  l += 32;
213  x >>= 32;
214  }
215  l += min_log2 (x);
216  return l;
217  }
218 }
219 
222 {
223  return ((uword) 1 << x) - (uword) 1;
224 }
225 
228 {
229  word y = (word) 1 << min_log2 (x);
230  if (x > y)
231  y *= 2;
232  return y;
233 }
234 
237 {
238  return 0 == (x & (x - 1));
239 }
240 
243 {
244  return (x) & ~(pow2 - 1);
245 }
246 
249 {
250  return (x + pow2 - 1) & ~(pow2 - 1);
251 }
252 
255 {
256  return (x + pow2 - 1) & ~(pow2 - 1);
257 }
258 
261 {
262  return x & -x;
263 }
264 
267 {
268  uword result;
269 #ifdef count_trailing_zeros
270  result = count_trailing_zeros (x);
271 #else
272  result = min_log2 (first_set (x));
273 #endif
274  return result;
275 }
276 
279 {
280  return (int) x;
281 }
282 
285 {
286  return (word) (x + .5);
287 }
288 
291 {
292  return f * flt_round_nearest (x / f);
293 }
294 
295 #define clib_max(x,y) \
296 ({ \
297  __typeof__ (x) _x = (x); \
298  __typeof__ (y) _y = (y); \
299  _x > _y ? _x : _y; \
300 })
301 
302 #define clib_min(x,y) \
303 ({ \
304  __typeof__ (x) _x = (x); \
305  __typeof__ (y) _y = (y); \
306  _x < _y ? _x : _y; \
307 })
308 
309 #define clib_abs(x) \
310 ({ \
311  __typeof__ (x) _x = (x); \
312  _x < 0 ? -_x : _x; \
313 })
314 
315 /* Standard standalone-only function declarations. */
316 #ifndef CLIB_UNIX
317 void clib_standalone_init (void *memory, uword memory_bytes);
318 
319 void qsort (void *base, uword n, uword size,
320  int (*)(const void *, const void *));
321 #endif
322 
323 /* Stack backtrace. */
324 uword
325 clib_backtrace (uword * callers, uword max_callers, uword n_frames_to_skip);
326 
327 #endif /* included_clib_h */
328 
329 /*
330  * fd.io coding-style-patch-verification: ON
331  *
332  * Local Variables:
333  * eval: (c-set-style "gnu")
334  * End:
335  */
vhost_user_memory_t memory
Definition: vhost_user.h:148
a
Definition: bitmap.h:538
static uword log2_first_set(uword x)
Definition: clib.h:266
#define count_leading_zeros(x)
Definition: clib.h:139
unsigned long u64
Definition: types.h:89
#define count_trailing_zeros(x)
Definition: clib.h:140
static uword min_log2(uword x)
Definition: clib.h:145
double f64
Definition: types.h:142
i64 word
Definition: types.h:111
#define always_inline
Definition: clib.h:99
static uword pow2_mask(uword x)
Definition: clib.h:221
unsigned int u32
Definition: types.h:88
static u64 min_log2_u64(u64 x)
Definition: clib.h:201
uword clib_backtrace(uword *callers, uword max_callers, uword n_frames_to_skip)
Definition: backtrace.c:226
svmdb_client_t * c
static f64 flt_round_to_multiple(f64 x, f64 f)
Definition: clib.h:290
static uword max_pow2(uword x)
Definition: clib.h:227
static u64 round_pow2_u64(u64 x, u64 pow2)
Definition: clib.h:254
static uword round_pow2(uword x, uword pow2)
Definition: clib.h:248
static uword first_set(uword x)
Definition: clib.h:260
static uword is_pow2(uword x)
Definition: clib.h:236
static word flt_round_nearest(f64 x)
Definition: clib.h:284
void qsort(void *base, uword n, uword size, int(*compar)(const void *, const void *))
Definition: qsort.c:56
static uword max_log2(uword x)
Definition: clib.h:192
u64 uword
Definition: types.h:112
static f64 flt_round_down(f64 x)
Definition: clib.h:278
static uword round_down_pow2(uword x, uword pow2)
Definition: clib.h:242
#define BITS(x)
Definition: clib.h:62