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