/src/build-dir/_deps/simdjson-src/src/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 SIMDJSON_INTERNAL_ISADETECTION_H |
47 | | #define SIMDJSON_INTERNAL_ISADETECTION_H |
48 | | |
49 | | #include "simdjson/internal/instruction_set.h" |
50 | | |
51 | | #include <cstdint> |
52 | | #include <cstdlib> |
53 | | #if defined(_MSC_VER) |
54 | | #include <intrin.h> |
55 | | #elif defined(HAVE_GCC_GET_CPUID) && defined(USE_GCC_GET_CPUID) |
56 | | #include <cpuid.h> |
57 | | #endif |
58 | | #if defined(__loongarch__) && defined(__linux__) |
59 | | #include <sys/auxv.h> |
60 | | #endif |
61 | | |
62 | | namespace simdjson { |
63 | | namespace internal { |
64 | | |
65 | | #if defined(__PPC64__) |
66 | | |
67 | | static inline uint32_t detect_supported_architectures() { |
68 | | return instruction_set::ALTIVEC; |
69 | | } |
70 | | |
71 | | #elif defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC) |
72 | | |
73 | | static inline uint32_t detect_supported_architectures() { |
74 | | return instruction_set::NEON; |
75 | | } |
76 | | |
77 | | #elif defined(__x86_64__) || defined(_M_AMD64) // x64 |
78 | | |
79 | | |
80 | | namespace { |
81 | | // Can be found on Intel ISA Reference for CPUID |
82 | | constexpr uint32_t cpuid_avx2_bit = 1 << 5; ///< @private Bit 5 of EBX for EAX=0x7 |
83 | | constexpr uint32_t cpuid_bmi1_bit = 1 << 3; ///< @private bit 3 of EBX for EAX=0x7 |
84 | | constexpr uint32_t cpuid_bmi2_bit = 1 << 8; ///< @private bit 8 of EBX for EAX=0x7 |
85 | | constexpr uint32_t cpuid_avx512f_bit = 1 << 16; ///< @private bit 16 of EBX for EAX=0x7 |
86 | | constexpr uint32_t cpuid_avx512dq_bit = 1 << 17; ///< @private bit 17 of EBX for EAX=0x7 |
87 | | constexpr uint32_t cpuid_avx512ifma_bit = 1 << 21; ///< @private bit 21 of EBX for EAX=0x7 |
88 | | constexpr uint32_t cpuid_avx512pf_bit = 1 << 26; ///< @private bit 26 of EBX for EAX=0x7 |
89 | | constexpr uint32_t cpuid_avx512er_bit = 1 << 27; ///< @private bit 27 of EBX for EAX=0x7 |
90 | | constexpr uint32_t cpuid_avx512cd_bit = 1 << 28; ///< @private bit 28 of EBX for EAX=0x7 |
91 | | constexpr uint32_t cpuid_avx512bw_bit = 1 << 30; ///< @private bit 30 of EBX for EAX=0x7 |
92 | | constexpr uint32_t cpuid_avx512vl_bit = 1U << 31; ///< @private bit 31 of EBX for EAX=0x7 |
93 | | constexpr uint32_t cpuid_avx512vbmi2_bit = 1 << 6; ///< @private bit 6 of ECX for EAX=0x7 |
94 | | constexpr uint64_t cpuid_avx256_saved = uint64_t(1) << 2; ///< @private bit 2 = AVX |
95 | | constexpr uint64_t cpuid_avx512_saved = uint64_t(7) << 5; ///< @private bits 5,6,7 = opmask, ZMM_hi256, hi16_ZMM |
96 | | constexpr uint32_t cpuid_sse42_bit = 1 << 20; ///< @private bit 20 of ECX for EAX=0x1 |
97 | | constexpr uint32_t cpuid_osxsave = (uint32_t(1) << 26) | (uint32_t(1) << 27); ///< @private bits 26+27 of ECX for EAX=0x1 |
98 | | constexpr uint32_t cpuid_pclmulqdq_bit = 1 << 1; ///< @private bit 1 of ECX for EAX=0x1 |
99 | | } |
100 | | |
101 | | |
102 | | |
103 | | static inline void cpuid(uint32_t *eax, uint32_t *ebx, uint32_t *ecx, |
104 | 0 | uint32_t *edx) { |
105 | | #if defined(_MSC_VER) |
106 | | int cpu_info[4]; |
107 | | __cpuidex(cpu_info, *eax, *ecx); |
108 | | *eax = cpu_info[0]; |
109 | | *ebx = cpu_info[1]; |
110 | | *ecx = cpu_info[2]; |
111 | | *edx = cpu_info[3]; |
112 | | #elif defined(HAVE_GCC_GET_CPUID) && defined(USE_GCC_GET_CPUID) |
113 | | uint32_t level = *eax; |
114 | | __get_cpuid(level, eax, ebx, ecx, edx); |
115 | | #else |
116 | 0 | uint32_t a = *eax, b, c = *ecx, d; |
117 | 0 | asm volatile("cpuid\n\t" : "+a"(a), "=b"(b), "+c"(c), "=d"(d)); |
118 | 0 | *eax = a; |
119 | 0 | *ebx = b; |
120 | 0 | *ecx = c; |
121 | 0 | *edx = d; |
122 | 0 | #endif |
123 | 0 | } |
124 | | |
125 | | |
126 | 0 | static inline uint64_t xgetbv() { |
127 | | #if defined(_MSC_VER) |
128 | | return _xgetbv(0); |
129 | | #else |
130 | 0 | uint32_t xcr0_lo, xcr0_hi; |
131 | 0 | asm volatile("xgetbv\n\t" : "=a" (xcr0_lo), "=d" (xcr0_hi) : "c" (0)); |
132 | 0 | return xcr0_lo | (uint64_t(xcr0_hi) << 32); |
133 | 0 | #endif |
134 | 0 | } |
135 | | |
136 | 0 | static inline uint32_t detect_supported_architectures() { |
137 | 0 | uint32_t eax, ebx, ecx, edx; |
138 | 0 | uint32_t host_isa = 0x0; |
139 | | |
140 | | // EBX for EAX=0x1 |
141 | 0 | eax = 0x1; |
142 | 0 | ecx = 0x0; |
143 | 0 | cpuid(&eax, &ebx, &ecx, &edx); |
144 | |
|
145 | 0 | if (ecx & cpuid_sse42_bit) { |
146 | 0 | host_isa |= instruction_set::SSE42; |
147 | 0 | } else { |
148 | 0 | return host_isa; // everything after is redundant |
149 | 0 | } |
150 | | |
151 | 0 | if (ecx & cpuid_pclmulqdq_bit) { |
152 | 0 | host_isa |= instruction_set::PCLMULQDQ; |
153 | 0 | } |
154 | | |
155 | |
|
156 | 0 | if ((ecx & cpuid_osxsave) != cpuid_osxsave) { |
157 | 0 | return host_isa; |
158 | 0 | } |
159 | | |
160 | | // xgetbv for checking if the OS saves registers |
161 | 0 | uint64_t xcr0 = xgetbv(); |
162 | |
|
163 | 0 | if ((xcr0 & cpuid_avx256_saved) == 0) { |
164 | 0 | return host_isa; |
165 | 0 | } |
166 | | |
167 | | // ECX for EAX=0x7 |
168 | 0 | eax = 0x7; |
169 | 0 | ecx = 0x0; |
170 | 0 | cpuid(&eax, &ebx, &ecx, &edx); |
171 | 0 | if (ebx & cpuid_avx2_bit) { |
172 | 0 | host_isa |= instruction_set::AVX2; |
173 | 0 | } |
174 | 0 | if (ebx & cpuid_bmi1_bit) { |
175 | 0 | host_isa |= instruction_set::BMI1; |
176 | 0 | } |
177 | |
|
178 | 0 | if (ebx & cpuid_bmi2_bit) { |
179 | 0 | host_isa |= instruction_set::BMI2; |
180 | 0 | } |
181 | |
|
182 | 0 | if (!((xcr0 & cpuid_avx512_saved) == cpuid_avx512_saved)) { |
183 | 0 | return host_isa; |
184 | 0 | } |
185 | | |
186 | 0 | if (ebx & cpuid_avx512f_bit) { |
187 | 0 | host_isa |= instruction_set::AVX512F; |
188 | 0 | } |
189 | |
|
190 | 0 | if (ebx & cpuid_avx512dq_bit) { |
191 | 0 | host_isa |= instruction_set::AVX512DQ; |
192 | 0 | } |
193 | |
|
194 | 0 | if (ebx & cpuid_avx512ifma_bit) { |
195 | 0 | host_isa |= instruction_set::AVX512IFMA; |
196 | 0 | } |
197 | |
|
198 | 0 | if (ebx & cpuid_avx512pf_bit) { |
199 | 0 | host_isa |= instruction_set::AVX512PF; |
200 | 0 | } |
201 | |
|
202 | 0 | if (ebx & cpuid_avx512er_bit) { |
203 | 0 | host_isa |= instruction_set::AVX512ER; |
204 | 0 | } |
205 | |
|
206 | 0 | if (ebx & cpuid_avx512cd_bit) { |
207 | 0 | host_isa |= instruction_set::AVX512CD; |
208 | 0 | } |
209 | |
|
210 | 0 | if (ebx & cpuid_avx512bw_bit) { |
211 | 0 | host_isa |= instruction_set::AVX512BW; |
212 | 0 | } |
213 | |
|
214 | 0 | if (ebx & cpuid_avx512vl_bit) { |
215 | 0 | host_isa |= instruction_set::AVX512VL; |
216 | 0 | } |
217 | |
|
218 | 0 | if (ecx & cpuid_avx512vbmi2_bit) { |
219 | 0 | host_isa |= instruction_set::AVX512VBMI2; |
220 | 0 | } |
221 | |
|
222 | 0 | return host_isa; |
223 | 0 | } |
224 | | |
225 | | #elif defined(__loongarch__) |
226 | | |
227 | | static inline uint32_t detect_supported_architectures() { |
228 | | uint32_t host_isa = instruction_set::DEFAULT; |
229 | | #if defined(__linux__) |
230 | | uint64_t hwcap = 0; |
231 | | hwcap = getauxval(AT_HWCAP); |
232 | | if (hwcap & HWCAP_LOONGARCH_LSX) { |
233 | | host_isa |= instruction_set::LSX; |
234 | | } |
235 | | if (hwcap & HWCAP_LOONGARCH_LASX) { |
236 | | host_isa |= instruction_set::LASX; |
237 | | } |
238 | | #endif |
239 | | return host_isa; |
240 | | } |
241 | | |
242 | | #elif SIMDJSON_IS_RISCV64 |
243 | | |
244 | | static inline uint32_t detect_supported_architectures() { |
245 | | uint32_t host_isa = instruction_set::DEFAULT; |
246 | | #if SIMDJSON_IS_RVV_VLS |
247 | | host_isa |= instruction_set::RVV_VLS; |
248 | | #endif |
249 | | return host_isa; |
250 | | } |
251 | | |
252 | | #else // fallback |
253 | | |
254 | | |
255 | | static inline uint32_t detect_supported_architectures() { |
256 | | return instruction_set::DEFAULT; |
257 | | } |
258 | | |
259 | | |
260 | | #endif // end SIMD extension detection code |
261 | | |
262 | | } // namespace internal |
263 | | } // namespace simdjson |
264 | | |
265 | | #endif // SIMDJSON_INTERNAL_ISADETECTION_H |