FD.io VPP  v16.06
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 {
176  uword n;
177  count_leading_zeros (n, x);
178  return BITS (uword) - n - 1;
179 }
180 #else
182 {
183  uword a = x, b = BITS(uword)/2, c = 0, r = 0;
184 
185  /* Reduce x to 4 bit result. */
186 #define _ \
187 { \
188  c = a >> b; \
189  if (c) a = c; \
190  if (c) r += b; \
191  b /= 2; \
192 }
193 
194  if (BITS (uword) > 32) _;
195  _; _; _;
196 #undef _
197 
198  /* Do table lookup on 4 bit partial. */
199  if (BITS (uword) > 32)
200  {
201  const u64 table = 0x3333333322221104LL;
202  uword t = (table >> (4*a)) & 0xf;
203  r = t < 4 ? r + t : ~0;
204  }
205  else
206  {
207  const u32 table = 0x22221104;
208  uword t = (a & 8) ? 3 : ((table >> (4*a)) & 0xf);
209  r = t < 4 ? r + t : ~0;
210  }
211 
212  return r;
213 }
214 #endif
215 
217 {
218  uword l = min_log2 (x);
219  if (x > ((uword) 1 << l))
220  l++;
221  return l;
222 }
223 
225 {
226  if (BITS (uword) == 64)
227  return min_log2 (x);
228  else
229  {
230  uword l, y;
231  y = x;
232  l = 0;
233  if (y == 0) {
234  l += 32;
235  x >>= 32;
236  }
237  l += min_log2 (x);
238  return l;
239  }
240 }
241 
243 { return ((uword) 1 << x) - (uword) 1; }
244 
246 {
247  word y = (word) 1 << min_log2 (x);
248  if (x > y) y *= 2;
249  return y;
250 }
251 
253 { return 0 == (x & (x - 1)); }
254 
256 {
257  return (x + pow2 - 1) &~ (pow2 - 1);
258 }
259 
261 {
262  return (x + pow2 - 1) &~ (pow2 - 1);
263 }
264 
266 { return x & -x; }
267 
269 {
270  uword result;
271 #ifdef count_trailing_zeros
272  count_trailing_zeros (result, x);
273 #else
274  result = min_log2 (first_set (x));
275 #endif
276  return result;
277 }
278 
280 { return (int) x; }
281 
283 { return (word) (x + .5); }
284 
286 { return f * flt_round_nearest (x / f); }
287 
288 #define clib_max(x,y) \
289 ({ \
290  __typeof__ (x) _x = (x); \
291  __typeof__ (y) _y = (y); \
292  _x > _y ? _x : _y; \
293 })
294 
295 #define clib_min(x,y) \
296 ({ \
297  __typeof__ (x) _x = (x); \
298  __typeof__ (y) _y = (y); \
299  _x < _y ? _x : _y; \
300 })
301 
302 #define clib_abs(x) \
303 ({ \
304  __typeof__ (x) _x = (x); \
305  _x < 0 ? -_x : _x; \
306 })
307 
308 /* Standard standalone-only function declarations. */
309 #ifndef CLIB_UNIX
310 void clib_standalone_init (void * memory, uword memory_bytes);
311 
312 void qsort (void * base, uword n, uword size,
313  int (*) (const void *, const void *));
314 #endif
315 
316 /* Stack backtrace. */
317 uword
318 clib_backtrace (uword * callers,
319  uword max_callers,
320  uword n_frames_to_skip);
321 
322 #endif /* included_clib_h */
always_inline uword log2_first_set(uword x)
Definition: clib.h:268
always_inline uword max_pow2(uword x)
Definition: clib.h:245
always_inline uword round_pow2(uword x, uword pow2)
Definition: clib.h:255
a
Definition: bitmap.h:393
always_inline uword max_log2(uword x)
Definition: clib.h:216
add_epi add_epi sub_epi sub_epi adds_epu subs_epu i16x8 y
Definition: vector_sse2.h:231
vhost_user_memory_t memory
Definition: vhost-user.h:79
always_inline uword first_set(uword x)
Definition: clib.h:265
#define always_inline
Definition: clib.h:84
unsigned long u64
Definition: types.h:89
uword clib_backtrace(uword *callers, uword max_callers, uword n_frames_to_skip)
Definition: backtrace.c:235
always_inline u64 min_log2_u64(u64 x)
Definition: clib.h:224
always_inline u64 round_pow2_u64(u64 x, u64 pow2)
Definition: clib.h:260
always_inline uword is_pow2(uword x)
Definition: clib.h:252
unsigned int u32
Definition: types.h:88
u32 size
Definition: vhost-user.h:74
always_inline f64 flt_round_to_multiple(f64 x, f64 f)
Definition: clib.h:285
always_inline f64 flt_round_down(f64 x)
Definition: clib.h:279
u64 uword
Definition: types.h:112
i64 word
Definition: types.h:111
void qsort(void *base, uword n, uword size, int(*compar)(const void *, const void *))
Definition: qsort.c:55
double f64
Definition: types.h:140
always_inline word flt_round_nearest(f64 x)
Definition: clib.h:282
always_inline uword min_log2(uword x)
Definition: clib.h:181
always_inline uword pow2_mask(uword x)
Definition: clib.h:242
#define BITS(x)
Definition: clib.h:58