FD.io VPP  v16.09
Vector Packet Processing
cpu.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 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 #ifndef included_clib_cpu_h
17 #define included_clib_cpu_h
18 
19 #include <vppinfra/format.h>
20 
21 /*
22  * multiarchitecture support. Adding new entry will produce
23  * new graph node function variant optimized for specific cpu
24  * microarchitecture.
25  * Order is important for runtime selection, as 1st match wins...
26  */
27 
28 #if __x86_64__ && CLIB_DEBUG == 0
29 #define foreach_march_variant(macro, x) \
30  macro(avx2, x, "arch=core-avx2")
31 #else
32 #define foreach_march_variant(macro, x)
33 #endif
34 
35 
36 #if __GNUC__ > 4 && !__clang__
37 #define CLIB_CPU_OPTIMIZED __attribute__ ((optimize ("tree-vectorize")))
38 #else
39 #define CLIB_CPU_OPTIMIZED
40 #endif
41 
42 
43 #define CLIB_MULTIARCH_ARCH_CHECK(arch, fn, tgt) \
44  if (clib_cpu_supports_ ## arch()) \
45  return & fn ## _ ##arch;
46 
47 #define CLIB_MULTIARCH_SELECT_FN(fn,...) \
48  __VA_ARGS__ void * fn ## _multiarch_select(void) \
49 { \
50  foreach_march_variant(CLIB_MULTIARCH_ARCH_CHECK, fn) \
51  return & fn; \
52 }
53 
54 #if __x86_64__
55 #include "cpuid.h"
56 
57 #define foreach_x86_64_flags \
58 _ (sse3, 1, ecx, 0) \
59 _ (ssse3, 1, ecx, 9) \
60 _ (sse41, 1, ecx, 19) \
61 _ (sse42, 1, ecx, 20) \
62 _ (avx, 1, ecx, 28) \
63 _ (avx2, 7, ebx, 5) \
64 _ (avx512f, 7, ebx, 16) \
65 _ (aes, 1, ecx, 25) \
66 _ (sha, 7, ebx, 29)
67 
68 static inline int
69 clib_get_cpuid (const u32 lev, u32 * eax, u32 * ebx, u32 * ecx, u32 * edx)
70 {
71  if ((u32) __get_cpuid_max (0x80000000 & lev, 0) < lev)
72  return 0;
73  if (lev == 7)
74  __cpuid_count (lev, 0, *eax, *ebx, *ecx, *edx);
75  else
76  __cpuid (lev, *eax, *ebx, *ecx, *edx);
77  return 1;
78 }
79 
80 
81 #define _(flag, func, reg, bit) \
82 static inline int \
83 clib_cpu_supports_ ## flag() \
84 { \
85  u32 __attribute__((unused)) eax, ebx = 0, ecx = 0, edx = 0; \
86  clib_get_cpuid (func, &eax, &ebx, &ecx, &edx); \
87  \
88  return ((reg & (1 << bit)) != 0); \
89 }
91 #undef _
92 #endif
96 
97 #endif
98 
99 /*
100  * fd.io coding-style-patch-verification: ON
101  *
102  * Local Variables:
103  * eval: (c-set-style "gnu")
104  * End:
105  */
foreach_x86_64_flags format_function_t format_cpu_uarch
Definition: cpu.h:93
format_function_t format_cpu_flags
Definition: cpu.h:95
static int clib_get_cpuid(const u32 lev, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx)
Definition: cpu.h:69
#define foreach_x86_64_flags
Definition: cpu.h:57
unsigned int u32
Definition: types.h:88
u8 *( format_function_t)(u8 *s, va_list *args)
Definition: format.h:48
format_function_t format_cpu_model_name
Definition: cpu.h:94