FD.io VPP  v16.09
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 /* Standalone means to not assume we are running on a Unix box. */
42 #if ! defined (CLIB_STANDALONE) && ! defined (CLIB_LINUX_KERNEL)
43 #define CLIB_UNIX
44 #endif
45 
46 #include <vppinfra/types.h>
47 
48 /* Global DEBUG flag. Setting this to 1 or 0 turns off
49  ASSERT (see vppinfra/error.h) & other debugging code. */
50 #ifndef CLIB_DEBUG
51 #define CLIB_DEBUG 0
52 #endif
53 
54 #ifndef NULL
55 #define NULL ((void *) 0)
56 #endif
57 
58 #define BITS(x) (8*sizeof(x))
59 #define ARRAY_LEN(x) (sizeof (x)/sizeof (x[0]))
60 
61 #define _STRUCT_FIELD(t,f) (((t *) 0)->f)
62 #define STRUCT_OFFSET_OF(t,f) ((uword) & _STRUCT_FIELD (t, f))
63 #define STRUCT_BIT_OFFSET_OF(t,f) (BITS(u8) * (uword) & _STRUCT_FIELD (t, f))
64 #define STRUCT_SIZE_OF(t,f) (sizeof (_STRUCT_FIELD (t, f)))
65 #define STRUCT_BITS_OF(t,f) (BITS (_STRUCT_FIELD (t, f)))
66 #define STRUCT_ARRAY_LEN(t,f) ARRAY_LEN (_STRUCT_FIELD (t, f))
67 #define STRUCT_MARK(mark) u8 mark[0]
68 #define STRUCT_MARK_PTR(v, f) &(v)->f
69 
70 /* Stride in bytes between struct array elements. */
71 #define STRUCT_STRIDE_OF(t,f) \
72  ( ((uword) & (((t *) 0)[1].f)) \
73  - ((uword) & (((t *) 0)[0].f)))
74 
75 #define STRUCT_OFFSET_OF_VAR(v,f) ((uword) (&(v)->f) - (uword) (v))
76 
77 /* Used to pack structure elements. */
78 #define CLIB_PACKED(x) x __attribute__ ((packed))
79 #define CLIB_UNUSED(x) x __attribute__ ((unused))
80 
81 #define never_inline __attribute__ ((__noinline__))
82 
83 #if CLIB_DEBUG > 0
84 #define always_inline static inline
85 #define static_always_inline static inline
86 #else
87 #define always_inline static inline __attribute__ ((__always_inline__))
88 #define static_always_inline static inline __attribute__ ((__always_inline__))
89 #endif
90 
91 
92 /* Reserved (unused) structure element with address offset between
93  from and to. */
94 #define CLIB_PAD_FROM_TO(from,to) u8 pad_##from[(to) - (from)]
95 
96 /* Hints to compiler about hot/cold code. */
97 #define PREDICT_FALSE(x) __builtin_expect((x),0)
98 #define PREDICT_TRUE(x) __builtin_expect((x),1)
99 
100 /* Full memory barrier (read and write). */
101 #define CLIB_MEMORY_BARRIER() __sync_synchronize ()
102 
103 /* Arranges for function to be called before main. */
104 #define INIT_FUNCTION(decl) \
105  decl __attribute ((constructor)); \
106  decl
107 
108 /* Arranges for function to be called before exit. */
109 #define EXIT_FUNCTION(decl) \
110  decl __attribute ((destructor)); \
111  decl
112 
113 /* Use __builtin_clz if available. */
114 #ifdef __GNUC__
115 #include <features.h>
116 #if __GNUC_PREREQ(3, 4)
117 #if uword_bits == 64
118 #define count_leading_zeros(count,x) count = __builtin_clzll (x)
119 #define count_trailing_zeros(count,x) count = __builtin_ctzll (x)
120 #else
121 #define count_leading_zeros(count,x) count = __builtin_clzl (x)
122 #define count_trailing_zeros(count,x) count = __builtin_ctzl (x)
123 #endif
124 #endif
125 #endif
126 
127 #ifndef count_leading_zeros
128 
129 /* Misc. integer arithmetic functions. */
130 #if defined (i386)
131 #define count_leading_zeros(count, x) \
132  do { \
133  word _clz; \
134  __asm__ ("bsrl %1,%0" \
135  : "=r" (_clz) : "rm" ((word) (x)));\
136  (count) = _clz ^ 31; \
137  } while (0)
138 
139 #define count_trailing_zeros(count, x) \
140  __asm__ ("bsfl %1,%0" : "=r" (count) : "rm" ((word)(x)))
141 #endif /* i386 */
142 
143 #if defined (__alpha__) && defined (HAVE_CIX)
144 #define count_leading_zeros(count, x) \
145  __asm__ ("ctlz %1,%0" \
146  : "=r" ((word) (count)) \
147  : "r" ((word) (x)))
148 #define count_trailing_zeros(count, x) \
149  __asm__ ("cttz %1,%0" \
150  : "=r" ((word) (count)) \
151  : "r" ((word) (x)))
152 #endif /* alpha && HAVE_CIX */
153 
154 #if __mips >= 4
155 
156 /* Select between 32/64 opcodes. */
157 #if uword_bits == 32
158 #define count_leading_zeros(_count, _x) \
159  __asm__ ("clz %[count],%[x]" \
160  : [count] "=r" ((word) (_count)) \
161  : [x] "r" ((word) (_x)))
162 #else
163 #define count_leading_zeros(_count, _x) \
164  __asm__ ("dclz %[count],%[x]" \
165  : [count] "=r" ((word) (_count)) \
166  : [x] "r" ((word) (_x)))
167 #endif
168 
169 #endif /* __mips >= 4 */
170 
171 #endif /* count_leading_zeros */
172 
173 #if defined (count_leading_zeros)
175 min_log2 (uword x)
176 {
177  uword n;
178  count_leading_zeros (n, x);
179  return BITS (uword) - n - 1;
180 }
181 #else
184 {
185  uword a = x, b = BITS (uword) / 2, c = 0, r = 0;
186 
187  /* Reduce x to 4 bit result. */
188 #define _ \
189 { \
190  c = a >> b; \
191  if (c) a = c; \
192  if (c) r += b; \
193  b /= 2; \
194 }
195 
196  if (BITS (uword) > 32)
197  _;
198  _;
199  _;
200  _;
201 #undef _
202 
203  /* Do table lookup on 4 bit partial. */
204  if (BITS (uword) > 32)
205  {
206  const u64 table = 0x3333333322221104LL;
207  uword t = (table >> (4 * a)) & 0xf;
208  r = t < 4 ? r + t : ~0;
209  }
210  else
211  {
212  const u32 table = 0x22221104;
213  uword t = (a & 8) ? 3 : ((table >> (4 * a)) & 0xf);
214  r = t < 4 ? r + t : ~0;
215  }
216 
217  return r;
218 }
219 #endif
220 
223 {
224  uword l = min_log2 (x);
225  if (x > ((uword) 1 << l))
226  l++;
227  return l;
228 }
229 
232 {
233  if (BITS (uword) == 64)
234  return min_log2 (x);
235  else
236  {
237  uword l, y;
238  y = x;
239  l = 0;
240  if (y == 0)
241  {
242  l += 32;
243  x >>= 32;
244  }
245  l += min_log2 (x);
246  return l;
247  }
248 }
249 
252 {
253  return ((uword) 1 << x) - (uword) 1;
254 }
255 
258 {
259  word y = (word) 1 << min_log2 (x);
260  if (x > y)
261  y *= 2;
262  return y;
263 }
264 
267 {
268  return 0 == (x & (x - 1));
269 }
270 
273 {
274  return (x + pow2 - 1) & ~(pow2 - 1);
275 }
276 
279 {
280  return (x + pow2 - 1) & ~(pow2 - 1);
281 }
282 
285 {
286  return x & -x;
287 }
288 
291 {
292  uword result;
293 #ifdef count_trailing_zeros
294  count_trailing_zeros (result, x);
295 #else
296  result = min_log2 (first_set (x));
297 #endif
298  return result;
299 }
300 
303 {
304  return (int) x;
305 }
306 
309 {
310  return (word) (x + .5);
311 }
312 
315 {
316  return f * flt_round_nearest (x / f);
317 }
318 
319 #define clib_max(x,y) \
320 ({ \
321  __typeof__ (x) _x = (x); \
322  __typeof__ (y) _y = (y); \
323  _x > _y ? _x : _y; \
324 })
325 
326 #define clib_min(x,y) \
327 ({ \
328  __typeof__ (x) _x = (x); \
329  __typeof__ (y) _y = (y); \
330  _x < _y ? _x : _y; \
331 })
332 
333 #define clib_abs(x) \
334 ({ \
335  __typeof__ (x) _x = (x); \
336  _x < 0 ? -_x : _x; \
337 })
338 
339 /* Standard standalone-only function declarations. */
340 #ifndef CLIB_UNIX
341 void clib_standalone_init (void *memory, uword memory_bytes);
342 
343 void qsort (void *base, uword n, uword size,
344  int (*)(const void *, const void *));
345 #endif
346 
347 /* Stack backtrace. */
348 uword
349 clib_backtrace (uword * callers, uword max_callers, uword n_frames_to_skip);
350 
351 #endif /* included_clib_h */
352 
353 /*
354  * fd.io coding-style-patch-verification: ON
355  *
356  * Local Variables:
357  * eval: (c-set-style "gnu")
358  * End:
359  */
a
Definition: bitmap.h:516
static uword log2_first_set(uword x)
Definition: clib.h:290
add_epi add_epi sub_epi sub_epi adds_epu subs_epu i16x8 y
Definition: vector_sse2.h:299
static uword min_log2(uword x)
Definition: clib.h:183
vhost_user_memory_t memory
Definition: vhost-user.h:83
#define always_inline
Definition: clib.h:84
static uword pow2_mask(uword x)
Definition: clib.h:251
unsigned long u64
Definition: types.h:89
static u64 min_log2_u64(u64 x)
Definition: clib.h:231
uword clib_backtrace(uword *callers, uword max_callers, uword n_frames_to_skip)
Definition: backtrace.c:232
svmdb_client_t * c
static f64 flt_round_to_multiple(f64 x, f64 f)
Definition: clib.h:314
static uword max_pow2(uword x)
Definition: clib.h:257
static u64 round_pow2_u64(u64 x, u64 pow2)
Definition: clib.h:278
static uword round_pow2(uword x, uword pow2)
Definition: clib.h:272
static uword first_set(uword x)
Definition: clib.h:284
unsigned int u32
Definition: types.h:88
static uword is_pow2(uword x)
Definition: clib.h:266
u64 uword
Definition: types.h:112
i64 word
Definition: types.h:111
static word flt_round_nearest(f64 x)
Definition: clib.h:308
void qsort(void *base, uword n, uword size, int(*compar)(const void *, const void *))
Definition: qsort.c:56
double f64
Definition: types.h:142
static uword max_log2(uword x)
Definition: clib.h:222
static f64 flt_round_down(f64 x)
Definition: clib.h:302
#define BITS(x)
Definition: clib.h:58