Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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)) |
Dave Barach | dbf19ca | 2016-03-15 10:21:54 +0100 | [diff] [blame] | 67 | #define STRUCT_MARK(mark) u8 mark[0] |
| 68 | #define STRUCT_MARK_PTR(v, f) &(v)->f |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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)) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 79 | #define CLIB_UNUSED(x) x __attribute__ ((unused)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 174 | always_inline uword |
| 175 | min_log2 (uword x) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 176 | { |
| 177 | uword n; |
| 178 | count_leading_zeros (n, x); |
| 179 | return BITS (uword) - n - 1; |
| 180 | } |
| 181 | #else |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 182 | always_inline uword |
| 183 | min_log2 (uword x) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 184 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 185 | uword a = x, b = BITS (uword) / 2, c = 0, r = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 196 | if (BITS (uword) > 32) |
| 197 | _; |
| 198 | _; |
| 199 | _; |
| 200 | _; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 201 | #undef _ |
| 202 | |
| 203 | /* Do table lookup on 4 bit partial. */ |
| 204 | if (BITS (uword) > 32) |
| 205 | { |
| 206 | const u64 table = 0x3333333322221104LL; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 207 | uword t = (table >> (4 * a)) & 0xf; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 208 | r = t < 4 ? r + t : ~0; |
| 209 | } |
| 210 | else |
| 211 | { |
| 212 | const u32 table = 0x22221104; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 213 | uword t = (a & 8) ? 3 : ((table >> (4 * a)) & 0xf); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 214 | r = t < 4 ? r + t : ~0; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 215 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 216 | |
| 217 | return r; |
| 218 | } |
| 219 | #endif |
| 220 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 221 | always_inline uword |
| 222 | max_log2 (uword x) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 223 | { |
| 224 | uword l = min_log2 (x); |
| 225 | if (x > ((uword) 1 << l)) |
| 226 | l++; |
| 227 | return l; |
| 228 | } |
| 229 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 230 | always_inline u64 |
| 231 | min_log2_u64 (u64 x) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 232 | { |
| 233 | if (BITS (uword) == 64) |
| 234 | return min_log2 (x); |
| 235 | else |
| 236 | { |
| 237 | uword l, y; |
| 238 | y = x; |
| 239 | l = 0; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 240 | if (y == 0) |
| 241 | { |
| 242 | l += 32; |
| 243 | x >>= 32; |
| 244 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 245 | l += min_log2 (x); |
| 246 | return l; |
| 247 | } |
| 248 | } |
| 249 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 250 | always_inline uword |
| 251 | pow2_mask (uword x) |
| 252 | { |
| 253 | return ((uword) 1 << x) - (uword) 1; |
| 254 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 255 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 256 | always_inline uword |
| 257 | max_pow2 (uword x) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 258 | { |
| 259 | word y = (word) 1 << min_log2 (x); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 260 | if (x > y) |
| 261 | y *= 2; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 262 | return y; |
| 263 | } |
| 264 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 265 | always_inline uword |
| 266 | is_pow2 (uword x) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 267 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 268 | return 0 == (x & (x - 1)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 271 | always_inline uword |
| 272 | round_pow2 (uword x, uword pow2) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 273 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 274 | return (x + pow2 - 1) & ~(pow2 - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 277 | always_inline u64 |
| 278 | round_pow2_u64 (u64 x, u64 pow2) |
| 279 | { |
| 280 | return (x + pow2 - 1) & ~(pow2 - 1); |
| 281 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 282 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 283 | always_inline uword |
| 284 | first_set (uword x) |
| 285 | { |
| 286 | return x & -x; |
| 287 | } |
| 288 | |
| 289 | always_inline uword |
| 290 | log2_first_set (uword x) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 301 | always_inline f64 |
| 302 | flt_round_down (f64 x) |
| 303 | { |
| 304 | return (int) x; |
| 305 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 306 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 307 | always_inline word |
| 308 | flt_round_nearest (f64 x) |
| 309 | { |
| 310 | return (word) (x + .5); |
| 311 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 312 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 313 | always_inline f64 |
| 314 | flt_round_to_multiple (f64 x, f64 f) |
| 315 | { |
| 316 | return f * flt_round_nearest (x / f); |
| 317 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 341 | void clib_standalone_init (void *memory, uword memory_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 342 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 343 | void qsort (void *base, uword n, uword size, |
| 344 | int (*)(const void *, const void *)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 345 | #endif |
| 346 | |
| 347 | /* Stack backtrace. */ |
| 348 | uword |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 349 | clib_backtrace (uword * callers, uword max_callers, uword n_frames_to_skip); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 350 | |
| 351 | #endif /* included_clib_h */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 352 | |
| 353 | /* |
| 354 | * fd.io coding-style-patch-verification: ON |
| 355 | * |
| 356 | * Local Variables: |
| 357 | * eval: (c-set-style "gnu") |
| 358 | * End: |
| 359 | */ |