/work/include/simdutf/internal/isadetection.h
Line | Count | Source |
1 | | /* From |
2 | | https://github.com/endorno/pytorch/blob/master/torch/lib/TH/generic/simd/simd.h |
3 | | Highly modified. |
4 | | |
5 | | Copyright (c) 2016- Facebook, Inc (Adam Paszke) |
6 | | Copyright (c) 2014- Facebook, Inc (Soumith Chintala) |
7 | | Copyright (c) 2011-2014 Idiap Research Institute (Ronan Collobert) |
8 | | Copyright (c) 2012-2014 Deepmind Technologies (Koray Kavukcuoglu) |
9 | | Copyright (c) 2011-2012 NEC Laboratories America (Koray Kavukcuoglu) |
10 | | Copyright (c) 2011-2013 NYU (Clement Farabet) |
11 | | Copyright (c) 2006-2010 NEC Laboratories America (Ronan Collobert, Leon Bottou, |
12 | | Iain Melvin, Jason Weston) Copyright (c) 2006 Idiap Research Institute |
13 | | (Samy Bengio) Copyright (c) 2001-2004 Idiap Research Institute (Ronan Collobert, |
14 | | Samy Bengio, Johnny Mariethoz) |
15 | | |
16 | | All rights reserved. |
17 | | |
18 | | Redistribution and use in source and binary forms, with or without |
19 | | modification, are permitted provided that the following conditions are met: |
20 | | |
21 | | 1. Redistributions of source code must retain the above copyright |
22 | | notice, this list of conditions and the following disclaimer. |
23 | | |
24 | | 2. Redistributions in binary form must reproduce the above copyright |
25 | | notice, this list of conditions and the following disclaimer in the |
26 | | documentation and/or other materials provided with the distribution. |
27 | | |
28 | | 3. Neither the names of Facebook, Deepmind Technologies, NYU, NEC Laboratories |
29 | | America and IDIAP Research Institute nor the names of its contributors may be |
30 | | used to endorse or promote products derived from this software without |
31 | | specific prior written permission. |
32 | | |
33 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
34 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
35 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
36 | | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
37 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
38 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
39 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
40 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
41 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
42 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
43 | | POSSIBILITY OF SUCH DAMAGE. |
44 | | */ |
45 | | |
46 | | #ifndef SIMDutf_INTERNAL_ISADETECTION_H |
47 | | #define SIMDutf_INTERNAL_ISADETECTION_H |
48 | | |
49 | | #include <cstdint> |
50 | | #include <cstdlib> |
51 | | #if defined(_MSC_VER) |
52 | | #include <intrin.h> |
53 | | #elif (defined(HAVE_GCC_GET_CPUID) && defined(USE_GCC_GET_CPUID)) || \ |
54 | | defined(__FILC__) |
55 | | #include <cpuid.h> |
56 | | #endif |
57 | | |
58 | | #ifdef __FILC__ |
59 | | #include <stdfil.h> |
60 | | #endif |
61 | | |
62 | | #include "simdutf/portability.h" |
63 | | |
64 | | // RISC-V ISA detection utilities |
65 | | #if SIMDUTF_IS_RISCV64 && defined(__linux__) |
66 | | #include <unistd.h> // for syscall |
67 | | // We define these ourselves, for backwards compatibility |
68 | | struct simdutf_riscv_hwprobe { |
69 | | int64_t key; |
70 | | uint64_t value; |
71 | | }; |
72 | | #define simdutf_riscv_hwprobe(...) syscall(258, __VA_ARGS__) |
73 | | #define SIMDUTF_RISCV_HWPROBE_KEY_IMA_EXT_0 4 |
74 | | #define SIMDUTF_RISCV_HWPROBE_IMA_V (1 << 2) |
75 | | #define SIMDUTF_RISCV_HWPROBE_EXT_ZVBB (1 << 17) |
76 | | #endif // SIMDUTF_IS_RISCV64 && defined(__linux__) |
77 | | |
78 | | #if defined(__loongarch__) && defined(__linux__) |
79 | | #include <sys/auxv.h> |
80 | | // bits/hwcap.h |
81 | | // #define HWCAP_LOONGARCH_LSX (1 << 4) |
82 | | // #define HWCAP_LOONGARCH_LASX (1 << 5) |
83 | | #endif |
84 | | #if defined(__aarch64__) && defined(__linux__) |
85 | | #include <sys/auxv.h> |
86 | | #endif |
87 | | #if (defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC)) && \ |
88 | | defined(_WIN32) && !defined(_WINDOWS_) |
89 | | // We avoid including <windows.h> (macro pollution); this matches the |
90 | | // declaration in the Windows SDK (BOOL WINAPI |
91 | | // IsProcessorFeaturePresent(DWORD)). |
92 | | extern "C" __declspec(dllimport) int __stdcall IsProcessorFeaturePresent( |
93 | | unsigned long ProcessorFeature); |
94 | | #endif |
95 | | |
96 | | namespace simdutf { |
97 | | namespace internal { |
98 | | |
99 | | enum instruction_set { |
100 | | DEFAULT = 0x0, |
101 | | NEON = 0x1, |
102 | | AVX2 = 0x4, |
103 | | SSE42 = 0x8, |
104 | | PCLMULQDQ = 0x10, |
105 | | BMI1 = 0x20, |
106 | | BMI2 = 0x40, |
107 | | ALTIVEC = 0x80, |
108 | | AVX512F = 0x100, |
109 | | AVX512DQ = 0x200, |
110 | | AVX512IFMA = 0x400, |
111 | | AVX512PF = 0x800, |
112 | | AVX512ER = 0x1000, |
113 | | AVX512CD = 0x2000, |
114 | | AVX512BW = 0x4000, |
115 | | AVX512VL = 0x8000, |
116 | | AVX512VBMI2 = 0x10000, |
117 | | AVX512VPOPCNTDQ = 0x2000, |
118 | | RVV = 0x4000, |
119 | | ZVBB = 0x8000, |
120 | | LSX = 0x40000, |
121 | | LASX = 0x80000, |
122 | | SVE = 0x100000, |
123 | | SVE2 = 0x200000, |
124 | | }; |
125 | | |
126 | | #if defined(__PPC64__) |
127 | | |
128 | | static inline uint32_t detect_supported_architectures() { |
129 | | return instruction_set::ALTIVEC; |
130 | | } |
131 | | |
132 | | #elif SIMDUTF_IS_RISCV64 |
133 | | |
134 | | static inline uint32_t detect_supported_architectures() { |
135 | | uint32_t host_isa = instruction_set::DEFAULT; |
136 | | #if SIMDUTF_IS_RVV |
137 | | host_isa |= instruction_set::RVV; |
138 | | #endif |
139 | | #if SIMDUTF_IS_ZVBB |
140 | | host_isa |= instruction_set::ZVBB; |
141 | | #endif |
142 | | #if defined(__linux__) |
143 | | simdutf_riscv_hwprobe probes[] = {{SIMDUTF_RISCV_HWPROBE_KEY_IMA_EXT_0, 0}}; |
144 | | long ret = simdutf_riscv_hwprobe(&probes, sizeof probes / sizeof *probes, 0, |
145 | | nullptr, 0); |
146 | | if (ret == 0) { |
147 | | uint64_t extensions = probes[0].value; |
148 | | if (extensions & SIMDUTF_RISCV_HWPROBE_IMA_V) |
149 | | host_isa |= instruction_set::RVV; |
150 | | if (extensions & SIMDUTF_RISCV_HWPROBE_EXT_ZVBB) |
151 | | host_isa |= instruction_set::ZVBB; |
152 | | } |
153 | | #endif |
154 | | #if defined(RUN_IN_SPIKE_SIMULATOR) |
155 | | // Proxy Kernel does not implement yet hwprobe syscall |
156 | | host_isa |= instruction_set::RVV; |
157 | | #endif |
158 | | return host_isa; |
159 | | } |
160 | | |
161 | | #elif defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC) |
162 | | |
163 | | #if defined(__linux__) |
164 | | // The kernel advertises SVE in AT_HWCAP and SVE2 in AT_HWCAP2. Older |
165 | | // headers may not define these constants, so we provide the kernel's values |
166 | | // (we deliberately do not include <asm/hwcap.h>, which is not available on |
167 | | // all toolchains, e.g., musl without linux-headers). |
168 | | #ifndef AT_HWCAP2 |
169 | | #define AT_HWCAP2 26 |
170 | | #endif |
171 | | #ifndef HWCAP_SVE |
172 | | #define HWCAP_SVE (1 << 22) |
173 | | #endif |
174 | | #ifndef HWCAP2_SVE2 |
175 | | #define HWCAP2_SVE2 (1 << 1) |
176 | | #endif |
177 | | #endif // __linux__ |
178 | | |
179 | | #if defined(_WIN32) |
180 | | // Only recent Windows SDKs define these processor features. |
181 | | #ifndef PF_ARM_SVE_INSTRUCTIONS_AVAILABLE |
182 | | #define PF_ARM_SVE_INSTRUCTIONS_AVAILABLE 46 |
183 | | #endif |
184 | | #ifndef PF_ARM_SVE2_INSTRUCTIONS_AVAILABLE |
185 | | #define PF_ARM_SVE2_INSTRUCTIONS_AVAILABLE 47 |
186 | | #endif |
187 | | #endif // _WIN32 |
188 | | |
189 | | static inline uint32_t detect_supported_architectures() { |
190 | | // NEON is mandatory on AArch64. |
191 | | uint32_t host_isa = instruction_set::NEON; |
192 | | #if defined(__linux__) |
193 | | unsigned long hwcap = getauxval(AT_HWCAP); |
194 | | unsigned long hwcap2 = getauxval(AT_HWCAP2); |
195 | | if (hwcap & HWCAP_SVE) { |
196 | | host_isa |= instruction_set::SVE; |
197 | | // We only claim SVE2 when SVE is also present. Before Linux 6.14, the |
198 | | // kernel set HWCAP2_SVE2 on processors implementing SME(2) but not SVE, |
199 | | // because SVE2 instructions are available in streaming mode. Our SVE2 |
200 | | // code runs in non-streaming mode and needs actual SVE. |
201 | | if (hwcap2 & HWCAP2_SVE2) { |
202 | | host_isa |= instruction_set::SVE2; |
203 | | } |
204 | | } |
205 | | #elif defined(_WIN32) |
206 | | if (IsProcessorFeaturePresent(PF_ARM_SVE_INSTRUCTIONS_AVAILABLE)) { |
207 | | host_isa |= instruction_set::SVE; |
208 | | // As on Linux, require SVE before claiming SVE2. |
209 | | if (IsProcessorFeaturePresent(PF_ARM_SVE2_INSTRUCTIONS_AVAILABLE)) { |
210 | | host_isa |= instruction_set::SVE2; |
211 | | } |
212 | | } |
213 | | #endif |
214 | | // On other systems (e.g., macOS, where Apple Silicon has no SVE), we only |
215 | | // report NEON. |
216 | | return host_isa; |
217 | | } |
218 | | |
219 | | #elif defined(__x86_64__) || defined(_M_AMD64) // x64 |
220 | | |
221 | | namespace { |
222 | | namespace cpuid_bit { |
223 | | // Can be found on Intel ISA Reference for CPUID |
224 | | |
225 | | // EAX = 0x01 |
226 | | constexpr uint32_t pclmulqdq = uint32_t(1) |
227 | | << 1; ///< @private bit 1 of ECX for EAX=0x1 |
228 | | constexpr uint32_t sse42 = uint32_t(1) |
229 | | << 20; ///< @private bit 20 of ECX for EAX=0x1 |
230 | | constexpr uint32_t osxsave = |
231 | | (uint32_t(1) << 26) | |
232 | | (uint32_t(1) << 27); ///< @private bits 26+27 of ECX for EAX=0x1 |
233 | | |
234 | | // EAX = 0x7f (Structured Extended Feature Flags), ECX = 0x00 (Sub-leaf) |
235 | | // See: "Table 3-8. Information Returned by CPUID Instruction" |
236 | | namespace ebx { |
237 | | constexpr uint32_t bmi1 = uint32_t(1) << 3; |
238 | | constexpr uint32_t avx2 = uint32_t(1) << 5; |
239 | | constexpr uint32_t bmi2 = uint32_t(1) << 8; |
240 | | constexpr uint32_t avx512f = uint32_t(1) << 16; |
241 | | constexpr uint32_t avx512dq = uint32_t(1) << 17; |
242 | | constexpr uint32_t avx512ifma = uint32_t(1) << 21; |
243 | | constexpr uint32_t avx512cd = uint32_t(1) << 28; |
244 | | constexpr uint32_t avx512bw = uint32_t(1) << 30; |
245 | | constexpr uint32_t avx512vl = uint32_t(1) << 31; |
246 | | } // namespace ebx |
247 | | |
248 | | namespace ecx { |
249 | | constexpr uint32_t avx512vbmi = uint32_t(1) << 1; |
250 | | constexpr uint32_t avx512vbmi2 = uint32_t(1) << 6; |
251 | | constexpr uint32_t avx512vnni = uint32_t(1) << 11; |
252 | | constexpr uint32_t avx512bitalg = uint32_t(1) << 12; |
253 | | constexpr uint32_t avx512vpopcnt = uint32_t(1) << 14; |
254 | | } // namespace ecx |
255 | | namespace edx { |
256 | | constexpr uint32_t avx512vp2intersect = uint32_t(1) << 8; |
257 | | } |
258 | | namespace xcr0_bit { |
259 | | constexpr uint64_t avx256_saved = uint64_t(1) << 2; ///< @private bit 2 = AVX |
260 | | constexpr uint64_t avx512_saved = |
261 | | uint64_t(7) << 5; ///< @private bits 5,6,7 = opmask, ZMM_hi256, hi16_ZMM |
262 | | } // namespace xcr0_bit |
263 | | } // namespace cpuid_bit |
264 | | } // namespace |
265 | | |
266 | | static inline void cpuid(uint32_t *eax, uint32_t *ebx, uint32_t *ecx, |
267 | 0 | uint32_t *edx) { |
268 | 0 | #if defined(_MSC_VER) |
269 | 0 | int cpu_info[4]; |
270 | 0 | __cpuidex(cpu_info, *eax, *ecx); |
271 | 0 | *eax = cpu_info[0]; |
272 | 0 | *ebx = cpu_info[1]; |
273 | 0 | *ecx = cpu_info[2]; |
274 | 0 | *edx = cpu_info[3]; |
275 | 0 | #elif (defined(HAVE_GCC_GET_CPUID) && defined(USE_GCC_GET_CPUID)) || \ |
276 | 0 | defined(__FILC__) |
277 | 0 | uint32_t level = *eax; |
278 | 0 | __get_cpuid(level, eax, ebx, ecx, edx); |
279 | 0 | #else |
280 | 0 | uint32_t a = *eax, b, c = *ecx, d; |
281 | 0 | asm volatile("cpuid\n\t" : "+a"(a), "=b"(b), "+c"(c), "=d"(d)); |
282 | 0 | *eax = a; |
283 | 0 | *ebx = b; |
284 | 0 | *ecx = c; |
285 | 0 | *edx = d; |
286 | 0 | #endif |
287 | 0 | } |
288 | | |
289 | 0 | static inline uint64_t xgetbv() { |
290 | 0 | #if defined(_MSC_VER) |
291 | 0 | return _xgetbv(0); |
292 | 0 | #elif defined(__FILC__) |
293 | 0 | return zxgetbv(); |
294 | 0 | #else |
295 | 0 | uint32_t xcr0_lo, xcr0_hi; |
296 | 0 | asm volatile("xgetbv\n\t" : "=a"(xcr0_lo), "=d"(xcr0_hi) : "c"(0)); |
297 | 0 | return xcr0_lo | ((uint64_t)xcr0_hi << 32); |
298 | 0 | #endif |
299 | 0 | } |
300 | | |
301 | 0 | static inline uint32_t detect_supported_architectures() { |
302 | 0 | uint32_t eax; |
303 | 0 | uint32_t ebx = 0; |
304 | 0 | uint32_t ecx = 0; |
305 | 0 | uint32_t edx = 0; |
306 | 0 | uint32_t host_isa = 0x0; |
307 | 0 |
|
308 | 0 | // EBX for EAX=0x1 |
309 | 0 | eax = 0x1; |
310 | 0 | cpuid(&eax, &ebx, &ecx, &edx); |
311 | 0 |
|
312 | 0 | if (ecx & cpuid_bit::sse42) { |
313 | 0 | host_isa |= instruction_set::SSE42; |
314 | 0 | } |
315 | 0 |
|
316 | 0 | if (ecx & cpuid_bit::pclmulqdq) { |
317 | 0 | host_isa |= instruction_set::PCLMULQDQ; |
318 | 0 | } |
319 | 0 |
|
320 | 0 | if ((ecx & cpuid_bit::osxsave) != cpuid_bit::osxsave) { |
321 | 0 | return host_isa; |
322 | 0 | } |
323 | 0 |
|
324 | 0 | // xgetbv for checking if the OS saves registers |
325 | 0 | uint64_t xcr0 = xgetbv(); |
326 | 0 |
|
327 | 0 | if ((xcr0 & cpuid_bit::xcr0_bit::avx256_saved) == 0) { |
328 | 0 | return host_isa; |
329 | 0 | } |
330 | 0 | // ECX for EAX=0x7 |
331 | 0 | eax = 0x7; |
332 | 0 | ecx = 0x0; // Sub-leaf = 0 |
333 | 0 | cpuid(&eax, &ebx, &ecx, &edx); |
334 | 0 | if (ebx & cpuid_bit::ebx::avx2) { |
335 | 0 | host_isa |= instruction_set::AVX2; |
336 | 0 | } |
337 | 0 | if (ebx & cpuid_bit::ebx::bmi1) { |
338 | 0 | host_isa |= instruction_set::BMI1; |
339 | 0 | } |
340 | 0 | if (ebx & cpuid_bit::ebx::bmi2) { |
341 | 0 | host_isa |= instruction_set::BMI2; |
342 | 0 | } |
343 | 0 | if (!((xcr0 & cpuid_bit::xcr0_bit::avx512_saved) == |
344 | 0 | cpuid_bit::xcr0_bit::avx512_saved)) { |
345 | 0 | return host_isa; |
346 | 0 | } |
347 | 0 | if (ebx & cpuid_bit::ebx::avx512f) { |
348 | 0 | host_isa |= instruction_set::AVX512F; |
349 | 0 | } |
350 | 0 | if (ebx & cpuid_bit::ebx::avx512bw) { |
351 | 0 | host_isa |= instruction_set::AVX512BW; |
352 | 0 | } |
353 | 0 | if (ebx & cpuid_bit::ebx::avx512cd) { |
354 | 0 | host_isa |= instruction_set::AVX512CD; |
355 | 0 | } |
356 | 0 | if (ebx & cpuid_bit::ebx::avx512dq) { |
357 | 0 | host_isa |= instruction_set::AVX512DQ; |
358 | 0 | } |
359 | 0 | if (ebx & cpuid_bit::ebx::avx512vl) { |
360 | 0 | host_isa |= instruction_set::AVX512VL; |
361 | 0 | } |
362 | 0 | if (ecx & cpuid_bit::ecx::avx512vbmi2) { |
363 | 0 | host_isa |= instruction_set::AVX512VBMI2; |
364 | 0 | } |
365 | 0 | if (ecx & cpuid_bit::ecx::avx512vpopcnt) { |
366 | 0 | host_isa |= instruction_set::AVX512VPOPCNTDQ; |
367 | 0 | } |
368 | 0 | return host_isa; |
369 | 0 | } |
370 | | #elif defined(__loongarch__) |
371 | | |
372 | | static inline uint32_t detect_supported_architectures() { |
373 | | uint32_t host_isa = instruction_set::DEFAULT; |
374 | | #if defined(__linux__) |
375 | | uint64_t hwcap = 0; |
376 | | hwcap = getauxval(AT_HWCAP); |
377 | | if (hwcap & HWCAP_LOONGARCH_LSX) { |
378 | | host_isa |= instruction_set::LSX; |
379 | | } |
380 | | if (hwcap & HWCAP_LOONGARCH_LASX) { |
381 | | host_isa |= instruction_set::LASX; |
382 | | } |
383 | | #endif |
384 | | return host_isa; |
385 | | } |
386 | | #else // fallback |
387 | | |
388 | | // includes 32-bit ARM. |
389 | | static inline uint32_t detect_supported_architectures() { |
390 | | return instruction_set::DEFAULT; |
391 | | } |
392 | | |
393 | | #endif // end SIMD extension detection code |
394 | | |
395 | | } // namespace internal |
396 | | } // namespace simdutf |
397 | | |
398 | | #endif // SIMDutf_INTERNAL_ISADETECTION_H |