/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 | | |
85 | | namespace simdutf { |
86 | | namespace internal { |
87 | | |
88 | | enum instruction_set { |
89 | | DEFAULT = 0x0, |
90 | | NEON = 0x1, |
91 | | AVX2 = 0x4, |
92 | | SSE42 = 0x8, |
93 | | PCLMULQDQ = 0x10, |
94 | | BMI1 = 0x20, |
95 | | BMI2 = 0x40, |
96 | | ALTIVEC = 0x80, |
97 | | AVX512F = 0x100, |
98 | | AVX512DQ = 0x200, |
99 | | AVX512IFMA = 0x400, |
100 | | AVX512PF = 0x800, |
101 | | AVX512ER = 0x1000, |
102 | | AVX512CD = 0x2000, |
103 | | AVX512BW = 0x4000, |
104 | | AVX512VL = 0x8000, |
105 | | AVX512VBMI2 = 0x10000, |
106 | | AVX512VPOPCNTDQ = 0x2000, |
107 | | RVV = 0x4000, |
108 | | ZVBB = 0x8000, |
109 | | LSX = 0x40000, |
110 | | LASX = 0x80000, |
111 | | }; |
112 | | |
113 | | #if defined(__PPC64__) |
114 | | |
115 | | static inline uint32_t detect_supported_architectures() { |
116 | | return instruction_set::ALTIVEC; |
117 | | } |
118 | | |
119 | | #elif SIMDUTF_IS_RISCV64 |
120 | | |
121 | | static inline uint32_t detect_supported_architectures() { |
122 | | uint32_t host_isa = instruction_set::DEFAULT; |
123 | | #if SIMDUTF_IS_RVV |
124 | | host_isa |= instruction_set::RVV; |
125 | | #endif |
126 | | #if SIMDUTF_IS_ZVBB |
127 | | host_isa |= instruction_set::ZVBB; |
128 | | #endif |
129 | | #if defined(__linux__) |
130 | | simdutf_riscv_hwprobe probes[] = {{SIMDUTF_RISCV_HWPROBE_KEY_IMA_EXT_0, 0}}; |
131 | | long ret = simdutf_riscv_hwprobe(&probes, sizeof probes / sizeof *probes, 0, |
132 | | nullptr, 0); |
133 | | if (ret == 0) { |
134 | | uint64_t extensions = probes[0].value; |
135 | | if (extensions & SIMDUTF_RISCV_HWPROBE_IMA_V) |
136 | | host_isa |= instruction_set::RVV; |
137 | | if (extensions & SIMDUTF_RISCV_HWPROBE_EXT_ZVBB) |
138 | | host_isa |= instruction_set::ZVBB; |
139 | | } |
140 | | #endif |
141 | | #if defined(RUN_IN_SPIKE_SIMULATOR) |
142 | | // Proxy Kernel does not implement yet hwprobe syscall |
143 | | host_isa |= instruction_set::RVV; |
144 | | #endif |
145 | | return host_isa; |
146 | | } |
147 | | |
148 | | #elif defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC) |
149 | | |
150 | | static inline uint32_t detect_supported_architectures() { |
151 | | return instruction_set::NEON; |
152 | | } |
153 | | |
154 | | #elif defined(__x86_64__) || defined(_M_AMD64) // x64 |
155 | | |
156 | | namespace { |
157 | | namespace cpuid_bit { |
158 | | // Can be found on Intel ISA Reference for CPUID |
159 | | |
160 | | // EAX = 0x01 |
161 | | constexpr uint32_t pclmulqdq = uint32_t(1) |
162 | | << 1; ///< @private bit 1 of ECX for EAX=0x1 |
163 | | constexpr uint32_t sse42 = uint32_t(1) |
164 | | << 20; ///< @private bit 20 of ECX for EAX=0x1 |
165 | | constexpr uint32_t osxsave = |
166 | | (uint32_t(1) << 26) | |
167 | | (uint32_t(1) << 27); ///< @private bits 26+27 of ECX for EAX=0x1 |
168 | | |
169 | | // EAX = 0x7f (Structured Extended Feature Flags), ECX = 0x00 (Sub-leaf) |
170 | | // See: "Table 3-8. Information Returned by CPUID Instruction" |
171 | | namespace ebx { |
172 | | constexpr uint32_t bmi1 = uint32_t(1) << 3; |
173 | | constexpr uint32_t avx2 = uint32_t(1) << 5; |
174 | | constexpr uint32_t bmi2 = uint32_t(1) << 8; |
175 | | constexpr uint32_t avx512f = uint32_t(1) << 16; |
176 | | constexpr uint32_t avx512dq = uint32_t(1) << 17; |
177 | | constexpr uint32_t avx512ifma = uint32_t(1) << 21; |
178 | | constexpr uint32_t avx512cd = uint32_t(1) << 28; |
179 | | constexpr uint32_t avx512bw = uint32_t(1) << 30; |
180 | | constexpr uint32_t avx512vl = uint32_t(1) << 31; |
181 | | } // namespace ebx |
182 | | |
183 | | namespace ecx { |
184 | | constexpr uint32_t avx512vbmi = uint32_t(1) << 1; |
185 | | constexpr uint32_t avx512vbmi2 = uint32_t(1) << 6; |
186 | | constexpr uint32_t avx512vnni = uint32_t(1) << 11; |
187 | | constexpr uint32_t avx512bitalg = uint32_t(1) << 12; |
188 | | constexpr uint32_t avx512vpopcnt = uint32_t(1) << 14; |
189 | | } // namespace ecx |
190 | | namespace edx { |
191 | | constexpr uint32_t avx512vp2intersect = uint32_t(1) << 8; |
192 | | } |
193 | | namespace xcr0_bit { |
194 | | constexpr uint64_t avx256_saved = uint64_t(1) << 2; ///< @private bit 2 = AVX |
195 | | constexpr uint64_t avx512_saved = |
196 | | uint64_t(7) << 5; ///< @private bits 5,6,7 = opmask, ZMM_hi256, hi16_ZMM |
197 | | } // namespace xcr0_bit |
198 | | } // namespace cpuid_bit |
199 | | } // namespace |
200 | | |
201 | | static inline void cpuid(uint32_t *eax, uint32_t *ebx, uint32_t *ecx, |
202 | 0 | uint32_t *edx) { |
203 | 0 | #if defined(_MSC_VER) |
204 | 0 | int cpu_info[4]; |
205 | 0 | __cpuidex(cpu_info, *eax, *ecx); |
206 | 0 | *eax = cpu_info[0]; |
207 | 0 | *ebx = cpu_info[1]; |
208 | 0 | *ecx = cpu_info[2]; |
209 | 0 | *edx = cpu_info[3]; |
210 | 0 | #elif (defined(HAVE_GCC_GET_CPUID) && defined(USE_GCC_GET_CPUID)) || \ |
211 | 0 | defined(__FILC__) |
212 | 0 | uint32_t level = *eax; |
213 | 0 | __get_cpuid(level, eax, ebx, ecx, edx); |
214 | 0 | #else |
215 | 0 | uint32_t a = *eax, b, c = *ecx, d; |
216 | 0 | asm volatile("cpuid\n\t" : "+a"(a), "=b"(b), "+c"(c), "=d"(d)); |
217 | 0 | *eax = a; |
218 | 0 | *ebx = b; |
219 | 0 | *ecx = c; |
220 | 0 | *edx = d; |
221 | 0 | #endif |
222 | 0 | } |
223 | | |
224 | 0 | static inline uint64_t xgetbv() { |
225 | 0 | #if defined(_MSC_VER) |
226 | 0 | return _xgetbv(0); |
227 | 0 | #elif defined(__FILC__) |
228 | 0 | return zxgetbv(); |
229 | 0 | #else |
230 | 0 | uint32_t xcr0_lo, xcr0_hi; |
231 | 0 | asm volatile("xgetbv\n\t" : "=a"(xcr0_lo), "=d"(xcr0_hi) : "c"(0)); |
232 | 0 | return xcr0_lo | ((uint64_t)xcr0_hi << 32); |
233 | 0 | #endif |
234 | 0 | } |
235 | | |
236 | 0 | static inline uint32_t detect_supported_architectures() { |
237 | 0 | uint32_t eax; |
238 | 0 | uint32_t ebx = 0; |
239 | 0 | uint32_t ecx = 0; |
240 | 0 | uint32_t edx = 0; |
241 | 0 | uint32_t host_isa = 0x0; |
242 | 0 |
|
243 | 0 | // EBX for EAX=0x1 |
244 | 0 | eax = 0x1; |
245 | 0 | cpuid(&eax, &ebx, &ecx, &edx); |
246 | 0 |
|
247 | 0 | if (ecx & cpuid_bit::sse42) { |
248 | 0 | host_isa |= instruction_set::SSE42; |
249 | 0 | } |
250 | 0 |
|
251 | 0 | if (ecx & cpuid_bit::pclmulqdq) { |
252 | 0 | host_isa |= instruction_set::PCLMULQDQ; |
253 | 0 | } |
254 | 0 |
|
255 | 0 | if ((ecx & cpuid_bit::osxsave) != cpuid_bit::osxsave) { |
256 | 0 | return host_isa; |
257 | 0 | } |
258 | 0 |
|
259 | 0 | // xgetbv for checking if the OS saves registers |
260 | 0 | uint64_t xcr0 = xgetbv(); |
261 | 0 |
|
262 | 0 | if ((xcr0 & cpuid_bit::xcr0_bit::avx256_saved) == 0) { |
263 | 0 | return host_isa; |
264 | 0 | } |
265 | 0 | // ECX for EAX=0x7 |
266 | 0 | eax = 0x7; |
267 | 0 | ecx = 0x0; // Sub-leaf = 0 |
268 | 0 | cpuid(&eax, &ebx, &ecx, &edx); |
269 | 0 | if (ebx & cpuid_bit::ebx::avx2) { |
270 | 0 | host_isa |= instruction_set::AVX2; |
271 | 0 | } |
272 | 0 | if (ebx & cpuid_bit::ebx::bmi1) { |
273 | 0 | host_isa |= instruction_set::BMI1; |
274 | 0 | } |
275 | 0 | if (ebx & cpuid_bit::ebx::bmi2) { |
276 | 0 | host_isa |= instruction_set::BMI2; |
277 | 0 | } |
278 | 0 | if (!((xcr0 & cpuid_bit::xcr0_bit::avx512_saved) == |
279 | 0 | cpuid_bit::xcr0_bit::avx512_saved)) { |
280 | 0 | return host_isa; |
281 | 0 | } |
282 | 0 | if (ebx & cpuid_bit::ebx::avx512f) { |
283 | 0 | host_isa |= instruction_set::AVX512F; |
284 | 0 | } |
285 | 0 | if (ebx & cpuid_bit::ebx::avx512bw) { |
286 | 0 | host_isa |= instruction_set::AVX512BW; |
287 | 0 | } |
288 | 0 | if (ebx & cpuid_bit::ebx::avx512cd) { |
289 | 0 | host_isa |= instruction_set::AVX512CD; |
290 | 0 | } |
291 | 0 | if (ebx & cpuid_bit::ebx::avx512dq) { |
292 | 0 | host_isa |= instruction_set::AVX512DQ; |
293 | 0 | } |
294 | 0 | if (ebx & cpuid_bit::ebx::avx512vl) { |
295 | 0 | host_isa |= instruction_set::AVX512VL; |
296 | 0 | } |
297 | 0 | if (ecx & cpuid_bit::ecx::avx512vbmi2) { |
298 | 0 | host_isa |= instruction_set::AVX512VBMI2; |
299 | 0 | } |
300 | 0 | if (ecx & cpuid_bit::ecx::avx512vpopcnt) { |
301 | 0 | host_isa |= instruction_set::AVX512VPOPCNTDQ; |
302 | 0 | } |
303 | 0 | return host_isa; |
304 | 0 | } |
305 | | #elif defined(__loongarch__) |
306 | | |
307 | | static inline uint32_t detect_supported_architectures() { |
308 | | uint32_t host_isa = instruction_set::DEFAULT; |
309 | | #if defined(__linux__) |
310 | | uint64_t hwcap = 0; |
311 | | hwcap = getauxval(AT_HWCAP); |
312 | | if (hwcap & HWCAP_LOONGARCH_LSX) { |
313 | | host_isa |= instruction_set::LSX; |
314 | | } |
315 | | if (hwcap & HWCAP_LOONGARCH_LASX) { |
316 | | host_isa |= instruction_set::LASX; |
317 | | } |
318 | | #endif |
319 | | return host_isa; |
320 | | } |
321 | | #else // fallback |
322 | | |
323 | | // includes 32-bit ARM. |
324 | | static inline uint32_t detect_supported_architectures() { |
325 | | return instruction_set::DEFAULT; |
326 | | } |
327 | | |
328 | | #endif // end SIMD extension detection code |
329 | | |
330 | | } // namespace internal |
331 | | } // namespace simdutf |
332 | | |
333 | | #endif // SIMDutf_INTERNAL_ISADETECTION_H |