/home/runner/work/LabNeura/LabNeura/src/labneura/backends/common/cpu_features.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #include "labneura/backends/common/cpu_features.h" |
2 | | #include "cpu_features_internal.h" |
3 | | |
4 | | #include <atomic> |
5 | | |
6 | | #if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_IX86)) |
7 | | #include <intrin.h> |
8 | | #elif defined(__x86_64__) || defined(__i386__) |
9 | | #include <cpuid.h> |
10 | | #endif |
11 | | |
12 | | #if defined(__linux__) && defined(__arm__) && !defined(__aarch64__) |
13 | | #include <asm/hwcap.h> |
14 | | #include <sys/auxv.h> |
15 | | #endif |
16 | | |
17 | | namespace labneura { |
18 | | |
19 | | namespace { |
20 | | |
21 | | #if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_IX86)) |
22 | | void cpuid(unsigned int leaf, unsigned int subleaf, int registers[4]) { |
23 | | __cpuidex(registers, static_cast<int>(leaf), static_cast<int>(subleaf)); |
24 | | } |
25 | | |
26 | | unsigned long long xcr0() { |
27 | | return _xgetbv(0); |
28 | | } |
29 | | #elif defined(__x86_64__) || defined(__i386__) |
30 | | bool cpuid(unsigned int leaf, unsigned int subleaf, unsigned int& eax, |
31 | 4 | unsigned int& ebx, unsigned int& ecx, unsigned int& edx) { |
32 | 4 | return __get_cpuid_count(leaf, subleaf, &eax, &ebx, &ecx, &edx) != 0; |
33 | 4 | } |
34 | | |
35 | 2 | unsigned long long xcr0() { |
36 | 2 | unsigned int low; |
37 | 2 | unsigned int high; |
38 | 2 | __asm__ volatile("xgetbv" : "=a"(low), "=d"(high) : "c"(0)); |
39 | 2 | return (static_cast<unsigned long long>(high) << 32) | low; |
40 | 2 | } |
41 | | #endif |
42 | | |
43 | 1 | bool detect_sse41() { |
44 | | #if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_IX86)) |
45 | | int registers[4]; |
46 | | cpuid(1, 0, registers); |
47 | | return (registers[2] & (1 << 19)) != 0; |
48 | | #elif defined(__x86_64__) || defined(__i386__) |
49 | | unsigned int eax, ebx, ecx, edx; |
50 | 1 | return cpuid(1, 0, eax, ebx, ecx, edx) && (ecx & bit_SSE4_1) != 0; |
51 | | #else |
52 | | return false; |
53 | | #endif |
54 | 1 | } |
55 | | |
56 | 1 | bool detect_avx2() { |
57 | | #if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_IX86)) |
58 | | int registers[4]; |
59 | | cpuid(1, 0, registers); |
60 | | constexpr int avx = 1 << 28; |
61 | | constexpr int osxsave = 1 << 27; |
62 | | if ((registers[2] & (avx | osxsave)) != (avx | osxsave) || |
63 | | (xcr0() & 0x6) != 0x6) { |
64 | | return false; |
65 | | } |
66 | | cpuid(7, 0, registers); |
67 | | return (registers[1] & (1 << 5)) != 0; |
68 | | #elif defined(__x86_64__) || defined(__i386__) |
69 | | unsigned int eax, ebx, ecx, edx; |
70 | 1 | constexpr unsigned int avx = 1U << 28; |
71 | 1 | constexpr unsigned int osxsave = 1U << 27; |
72 | 1 | if (!cpuid(1, 0, eax, ebx, ecx, edx) || |
73 | 1 | (ecx & (avx | osxsave)) != (avx | osxsave) || |
74 | 1 | (xcr0() & 0x6U) != 0x6U) { |
75 | 0 | return false; |
76 | 0 | } |
77 | 1 | return cpuid(7, 0, eax, ebx, ecx, edx) && |
78 | 1 | (ebx & bit_AVX2) != 0; |
79 | | #else |
80 | | return false; |
81 | | #endif |
82 | 1 | } |
83 | | |
84 | 1 | bool detect_f16c() { |
85 | | #if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_IX86)) |
86 | | int registers[4]; |
87 | | cpuid(1, 0, registers); |
88 | | constexpr int avx = 1 << 28; |
89 | | constexpr int osxsave = 1 << 27; |
90 | | constexpr int f16c = 1 << 29; |
91 | | return (registers[2] & (avx | osxsave | f16c)) == (avx | osxsave | f16c) && |
92 | | (xcr0() & 0x6) == 0x6; |
93 | | #elif defined(__x86_64__) || defined(__i386__) |
94 | | unsigned int eax, ebx, ecx, edx; |
95 | 1 | constexpr unsigned int avx = 1U << 28; |
96 | 1 | constexpr unsigned int osxsave = 1U << 27; |
97 | 1 | constexpr unsigned int f16c = 1U << 29; |
98 | 1 | return cpuid(1, 0, eax, ebx, ecx, edx) && |
99 | 1 | (ecx & (avx | osxsave | f16c)) == (avx | osxsave | f16c) && |
100 | 1 | (xcr0() & 0x6U) == 0x6U; |
101 | | #else |
102 | | return false; |
103 | | #endif |
104 | 1 | } |
105 | | |
106 | 1 | bool detect_neon() { |
107 | | #if defined(__aarch64__) || defined(_M_ARM64) |
108 | | return true; |
109 | | #elif defined(__linux__) && defined(__arm__) |
110 | | return (getauxval(AT_HWCAP) & HWCAP_NEON) != 0; |
111 | | #elif defined(__ARM_NEON) || defined(__ARM_NEON__) |
112 | | return true; |
113 | | #else |
114 | 1 | return false; |
115 | 1 | #endif |
116 | 1 | } |
117 | | |
118 | | struct DetectedFeatures { |
119 | | CPUFeatures features; |
120 | | bool neon; |
121 | | }; |
122 | | |
123 | | std::atomic<std::size_t> g_detection_runs{0}; |
124 | | |
125 | | // Detection runs once per process (thread-safe function-local static). |
126 | 8.16k | const DetectedFeatures& cached_features() { |
127 | 8.16k | static const DetectedFeatures detected = [] { |
128 | 1 | g_detection_runs.fetch_add(1, std::memory_order_relaxed); |
129 | 1 | return DetectedFeatures{{detect_sse41(), detect_avx2(), detect_f16c()}, |
130 | 1 | detect_neon()}; |
131 | 1 | }(); |
132 | 8.16k | return detected; |
133 | 8.16k | } |
134 | | |
135 | | } // namespace |
136 | | |
137 | | namespace detail { |
138 | | |
139 | 0 | std::size_t cpu_feature_detection_count() { |
140 | 0 | return g_detection_runs.load(std::memory_order_relaxed); |
141 | 0 | } |
142 | | |
143 | | } // namespace detail |
144 | | |
145 | 4.08k | CPUFeatures detect_cpu_features() { |
146 | 4.08k | return cached_features().features; |
147 | 4.08k | } |
148 | | |
149 | 0 | bool cpu_supports_avx2() { |
150 | 0 | return cached_features().features.avx2; |
151 | 0 | } |
152 | | |
153 | 1 | bool cpu_supports_f16c() { |
154 | 1 | return cached_features().features.f16c; |
155 | 1 | } |
156 | | |
157 | 4.08k | bool cpu_supports_neon() { |
158 | 4.08k | return cached_features().neon; |
159 | 4.08k | } |
160 | | |
161 | | } // namespace labneura |