Debian Codegraph @

src/mpt/arch/x86_amd64.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_ARCH_X86_AMD64_HPP
4 #define MPT_ARCH_X86_AMD64_HPP
5
6
7 #include "mpt/arch/feature_flags.hpp"
8 #include "mpt/base/bit.hpp"
9 #include "mpt/base/detect.hpp"
10 #include "mpt/base/integer.hpp"
11 #include "mpt/base/macros.hpp"
12 #include "mpt/base/namespace.hpp"
13 #include "mpt/base/utility.hpp"
14 #include "mpt/osinfo/windows_version.hpp"
15
16 #include <algorithm>
17 #include <array>
18 #if MPT_ARCH_X86 || MPT_ARCH_AMD64
19 #if MPT_COMPILER_GCC
20 #include <atomic>
21 #endif
22 #endif
23 #include <optional>
24 #include <string>
25 #include <string_view>
26
27 #include <cstddef>
28
29 #if MPT_OS_DJGPP
30 #include <dpmi.h>
31 #include <pc.h>
32 #endif
33
34 #if MPT_OS_WINDOWS
35 #include <windows.h>
36 #endif
37
38 #if MPT_ARCH_X86 || MPT_ARCH_AMD64
39 #if MPT_COMPILER_MSVC
40 #include <intrin.h>
41 #endif
42 #if MPT_COMPILER_MSVC
43 #include <immintrin.h>
44 #elif MPT_COMPILER_GCC || MPT_COMPILER_CLANG
45 #include <cpuid.h>
46 #include <x86intrin.h>
47 #endif
48 #endif
49
50 // skip assumed features for amd64
51 #define MPT_ARCH_X86_AMD64_FAST_DETECT 0
52
53
54
55 namespace mpt {
56 inline namespace MPT_INLINE_NS {
57
58
59
60 namespace arch {
61
62
63
64 namespace x86 {
65
66
67
68 using feature_flags = mpt::arch::basic_feature_flags<uint32>;
69
70
71 using mode_flags = mpt::arch::basic_feature_flags<uint8>;
72
73
74
75 // clang-format off
76
77 namespace feature {
78 inline constexpr feature_flags none = feature_flags{};
79 inline constexpr feature_flags intel386 = feature_flags{ 0x0000'0001 };
80 inline constexpr feature_flags fpu = feature_flags{ 0x0000'0002 };
81 inline constexpr feature_flags fsin = feature_flags{ 0x0000'0004 };
82 inline constexpr feature_flags intel486 = feature_flags{ 0x0000'0008 }; // XADD, BSWAP, CMPXCHG
83 inline constexpr feature_flags cpuid = feature_flags{ 0x0000'0010 };
84 inline constexpr feature_flags tsc = feature_flags{ 0x0000'0020 };
85 inline constexpr feature_flags cx8 = feature_flags{ 0x0000'0040 };
86 inline constexpr feature_flags cmov = feature_flags{ 0x0000'0080 };
87 inline constexpr feature_flags mmx = feature_flags{ 0x0000'0100 };
88 inline constexpr feature_flags mmxext = feature_flags{ 0x0000'0200 };
89 inline constexpr feature_flags x3dnow = feature_flags{ 0x0000'0400 };
90 inline constexpr feature_flags x3dnowext = feature_flags{ 0x0000'0800 };
91 inline constexpr feature_flags x3dnowprefetch = feature_flags{ 0x0000'1000 };
92 inline constexpr feature_flags fxsr = feature_flags{ 0x0000'2000 };
93 inline constexpr feature_flags sse = feature_flags{ 0x0000'4000 };
94 inline constexpr feature_flags sse2 = feature_flags{ 0x0000'8000 };
95 inline constexpr feature_flags sse3 = feature_flags{ 0x0001'0000 };
96 inline constexpr feature_flags ssse3 = feature_flags{ 0x0002'0000 };
97 inline constexpr feature_flags sse4_1 = feature_flags{ 0x0004'0000 };
98 inline constexpr feature_flags sse4_2 = feature_flags{ 0x0008'0000 };
99 inline constexpr feature_flags xsave = feature_flags{ 0x0010'0000 };
100 inline constexpr feature_flags avx = feature_flags{ 0x0020'0000 };
101 inline constexpr feature_flags avx2 = feature_flags{ 0x0040'0000 };
102 inline constexpr feature_flags cx16 = feature_flags{ 0x0080'0000 };
103 inline constexpr feature_flags lahf = feature_flags{ 0x0100'0000 };
104 inline constexpr feature_flags popcnt = feature_flags{ 0x0200'0000 };
105 inline constexpr feature_flags bmi1 = feature_flags{ 0x0400'0000 };
106 inline constexpr feature_flags bmi2 = feature_flags{ 0x0800'0000 };
107 inline constexpr feature_flags f16c = feature_flags{ 0x1000'0000 };
108 inline constexpr feature_flags fma = feature_flags{ 0x2000'0000 };
109 inline constexpr feature_flags lzcnt = feature_flags{ 0x4000'0000 };
110 inline constexpr feature_flags movbe = feature_flags{ 0x8000'0000 };
111 } // namespace feature
112
113 namespace mode {
114 inline constexpr mode_flags base = mode_flags{ 0x00 };
115 inline constexpr mode_flags xmm128sse = mode_flags{ 0x01 };
116 inline constexpr mode_flags ymm256avx = mode_flags{ 0x02 };
117 } // namespace mode
118
119 namespace featureset {
120 inline constexpr feature_flags intel386 = feature::intel386;
121 inline constexpr feature_flags intel486SX = featureset::intel386 | feature::intel486;
122 inline constexpr feature_flags intel486DX = featureset::intel486SX | feature::fpu | feature::fsin;
123 inline constexpr feature_flags intel586 = featureset::intel486DX | feature::cpuid | feature::tsc | feature::cx8;
124 inline constexpr feature_flags intel586_mmx = featureset::intel586 | feature::mmx;
125 inline constexpr feature_flags intel686 = featureset::intel586 | feature::cmov;
126 inline constexpr feature_flags intel686_mmx = featureset::intel686 | feature::mmx;
127 inline constexpr feature_flags intel686_sse = featureset::intel686_mmx | feature::mmxext | feature::fxsr | feature::sse;
128 inline constexpr feature_flags intel686_sse2 = featureset::intel686_sse | feature::sse2;
129 inline constexpr feature_flags intel786 = featureset::intel686_sse2;
130 inline constexpr feature_flags amd64 = featureset::intel686_sse2;
131 inline constexpr feature_flags amd64_v2 = featureset::amd64 | feature::cx16 | feature::lahf | feature::popcnt | feature::sse3 | feature::ssse3 | feature::sse4_1 | feature::sse4_2;
132 inline constexpr feature_flags amd64_v3 = featureset::amd64_v2 | feature::xsave | feature::avx | feature::avx2 | feature::bmi1 | feature::bmi2 | feature::f16c | feature::fma | feature::lzcnt | feature::movbe;
133 inline constexpr feature_flags msvc_x86_1998 = featureset::intel386 | feature::fpu | feature::fsin;
134 inline constexpr feature_flags msvc_x86_2005 = featureset::intel486DX;
135 inline constexpr feature_flags msvc_x86_2008 = featureset::intel586;
136 inline constexpr feature_flags msvc_x86_sse = featureset::intel686_sse;
137 inline constexpr feature_flags msvc_x86_sse2 = featureset::intel686_sse2;
138 inline constexpr feature_flags msvc_x86_avx = featureset::intel686_sse2 | feature::x3dnowprefetch | feature::xsave | feature::avx;
139 inline constexpr feature_flags msvc_x86_avx2 = featureset::intel686_sse2 | feature::x3dnowprefetch | feature::xsave | feature::avx | feature::avx2 | feature::fma | feature::bmi1;
140 inline constexpr feature_flags msvc_amd64 = featureset::amd64;
141 inline constexpr feature_flags msvc_amd64_avx = featureset::amd64 | feature::xsave | feature::avx;
142 inline constexpr feature_flags msvc_amd64_avx2 = featureset::amd64 | feature::xsave | feature::avx | feature::avx2 | feature::fma | feature::bmi1;
143 } // namespace featureset
144
145 namespace modeset {
146 inline constexpr mode_flags intel386 = mode::base;
147 inline constexpr mode_flags intel486SX = mode::base;
148 inline constexpr mode_flags intel486DX = mode::base;
149 inline constexpr mode_flags intel586 = mode::base;
150 inline constexpr mode_flags intel586_mmx = mode::base;
151 inline constexpr mode_flags intel686 = mode::base;
152 inline constexpr mode_flags intel686_mmx = mode::base;
153 inline constexpr mode_flags intel686_sse = mode::base | mode::xmm128sse;
154 inline constexpr mode_flags intel686_sse2 = mode::base | mode::xmm128sse;
155 inline constexpr mode_flags intel786 = mode::base | mode::xmm128sse;
156 inline constexpr mode_flags amd64 = mode::base | mode::xmm128sse;
157 inline constexpr mode_flags amd64_v2 = mode::base | mode::xmm128sse;
158 inline constexpr mode_flags amd64_v3 = mode::base | mode::xmm128sse | mode::ymm256avx;
159 inline constexpr mode_flags msvc_x86_1998 = mode::base;
160 inline constexpr mode_flags msvc_x86_2005 = mode::base;
161 inline constexpr mode_flags msvc_x86_2008 = mode::base;
162 inline constexpr mode_flags msvc_x86_sse = mode::base | mode::xmm128sse;
163 inline constexpr mode_flags msvc_x86_sse2 = mode::base | mode::xmm128sse;
164 inline constexpr mode_flags msvc_x86_avx = mode::base | mode::xmm128sse | mode::ymm256avx;
165 inline constexpr mode_flags msvc_x86_avx2 = mode::base | mode::xmm128sse | mode::ymm256avx;
166 inline constexpr mode_flags msvc_amd64 = mode::base | mode::xmm128sse;
167 inline constexpr mode_flags msvc_amd64_avx = mode::base | mode::xmm128sse | mode::ymm256avx;
168 inline constexpr mode_flags msvc_amd64_avx2 = mode::base | mode::xmm128sse | mode::ymm256avx;
169 } // namespace modeset
170
171 // clang-format on
172
173
174 enum class vendor : uint8 {
175 unknown = 0,
176 AMD,
177 Centaur,
178 Cyrix,
179 Intel,
180 Transmeta,
181 NSC,
182 NexGen,
183 Rise,
184 SiS,
185 UMC,
186 VIA,
187 DMnP,
188 Zhaoxin,
189 Hygon,
190 Elbrus,
191 MiSTer,
192 bhyve,
193 KVM,
194 QEMU,
195 HyperV,
196 Parallels,
197 VMWare,
198 Xen,
199 ACRN,
200 QNX,
201 }; // enum class vendor
202
203
204
205 // clang-format off
206 [[nodiscard]] MPT_CONSTEVAL feature_flags assumed_features() noexcept {
207 feature_flags flags{};
208 #if MPT_ARCH_X86 || MPT_ARCH_AMD64
209 #ifdef MPT_ARCH_X86_I386
210 flags |= feature::intel386;
211 #endif
212 #ifdef MPT_ARCH_X86_FPU
213 flags |= feature::fpu;
214 #endif
215 #ifdef MPT_ARCH_X86_FSIN
216 flags |= feature::fsin;
217 #endif
218 #ifdef MPT_ARCH_X86_I486
219 flags |= feature::intel486;
220 #endif
221 #ifdef MPT_ARCH_X86_CPUID
222 flags |= feature::cpuid;
223 #endif
224 #ifdef MPT_ARCH_X86_TSC
225 flags |= feature::tsc;
226 #endif
227 #ifdef MPT_ARCH_X86_CX8
228 flags |= feature::cx8;
229 #endif
230 #ifdef MPT_ARCH_X86_CMOV
231 flags |= feature::cmov;
232 #endif
233 #ifdef MPT_ARCH_X86_MMX
234 flags |= feature::mmx;
235 #endif
236 #ifdef MPT_ARCH_X86_MMXEXT
237 flags |= feature::mmxext;
238 #endif
239 #ifdef MPT_ARCH_X86_3DNOW
240 flags |= feature::x3dnow;
241 #endif
242 #ifdef MPT_ARCH_X86_3DNOWEXT
243 flags |= feature::x3dnowext;
244 #endif
245 #ifdef MPT_ARCH_X86_3DNOWPREFETCH
246 flags |= feature::x3dnowprefetch;
247 #endif
248 #ifdef MPT_ARCH_X86_FXSR
249 flags |= feature::fxsr;
250 #endif
251 #ifdef MPT_ARCH_X86_SSE
252 flags |= feature::sse;
253 #endif
254 #ifdef MPT_ARCH_X86_SSE2
255 flags |= feature::sse2;
256 #endif
257 #ifdef MPT_ARCH_X86_SSE3
258 flags |= feature::sse3;
259 #endif
260 #ifdef MPT_ARCH_X86_SSSE3
261 flags |= feature::ssse3;
262 #endif
263 #ifdef MPT_ARCH_X86_SSE4_1
264 flags |= feature::sse4_1;
265 #endif
266 #ifdef MPT_ARCH_X86_SSE4_2
267 flags |= feature::sse4_2;
268 #endif
269 #ifdef MPT_ARCH_X86_XSAVE
270 flags |= feature::xsave;
271 #endif
272 #ifdef MPT_ARCH_X86_AVX
273 flags |= feature::avx;
274 #endif
275 #ifdef MPT_ARCH_X86_AVX2
276 flags |= feature::avx2;
277 #endif
278 #ifdef MPT_ARCH_X86_CX16
279 flags |= feature::cx16;
280 #endif
281 #ifdef MPT_ARCH_X86_LAHF
282 flags |= feature::lahf;
283 #endif
284 #ifdef MPT_ARCH_X86_POPCNT
285 flags |= feature::popcnt;
286 #endif
287 #ifdef MPT_ARCH_X86_BMI1
288 flags |= feature::bmi1;
289 #endif
290 #ifdef MPT_ARCH_X86_BMI2
291 flags |= feature::bmi2;
292 #endif
293 #ifdef MPT_ARCH_X86_F16C
294 flags |= feature::f16c;
295 #endif
296 #ifdef MPT_ARCH_X86_FMA
297 flags |= feature::fma;
298 #endif
299 #ifdef MPT_ARCH_X86_LZCNT
300 flags |= feature::lzcnt;
301 #endif
302 #ifdef MPT_ARCH_X86_MOVBE
303 flags |= feature::movbe;
304 #endif
305 #endif // MPT_ARCH_X86 || MPT_ARCH_AMD64
306 return flags;
307 }
308 // clang-format on
309
310
311 // clang-format off
312 [[nodiscard]] MPT_CONSTEVAL mode_flags assumed_modes() noexcept {
313 mode_flags flags{};
314 #if MPT_ARCH_X86 || MPT_ARCH_AMD64
315 #ifdef MPT_ARCH_X86_SSE
316 flags |= mode::xmm128sse;
317 #endif
318 #ifdef MPT_ARCH_X86_AVX
319 flags |= mode::ymm256avx;
320 #endif
321 #endif // MPT_ARCH_X86 || MPT_ARCH_AMD64
322 return flags;
323 }
324 // clang-format on
325
326
327
328 template <std::size_t N>
329 struct fixed_string {
330 std::array<char, N> m_data = {};
331 [[nodiscard]] constexpr const char & operator[](std::size_t i) const noexcept {
332 return m_data[i];
333 }
334 [[nodiscard]] constexpr char & operator[](std::size_t i) noexcept {
335 return m_data[i];
336 }
337 [[nodiscard]] constexpr std::size_t size() const noexcept {
338 return m_data.size();
339 }
340 [[nodiscard]] constexpr const char * data() const noexcept {
341 return m_data.data();
342 }
343 [[nodiscard]] constexpr char * data() noexcept {
344 return m_data.data();
345 }
346 [[nodiscard]] constexpr const char * begin() const noexcept {
347 return m_data.data();
348 }
349 [[nodiscard]] constexpr char * begin() noexcept {
350 return m_data.data();
351 }
352 [[nodiscard]] constexpr const char * end() const noexcept {
353 return m_data.data() + m_data.size();
354 }
355 [[nodiscard]] constexpr char * end() noexcept {
356 return m_data.data() + m_data.size();
357 }
358 [[nodiscard]] constexpr operator std::string_view() const noexcept {
359 #if MPT_CXX_AT_LEAST(20) && !defined(MPT_COMPILER_QUIRK_NO_STRING_VIEW_ITERATOR_CTOR)
360 return std::string_view(m_data.begin(), std::find(m_data.begin(), m_data.end(), '\0'));
361 #else
362 return std::string_view(m_data.data(), std::find(m_data.begin(), m_data.end(), '\0') - m_data.begin());
363 #endif
364 }
365 template <std::size_t M>
366 [[nodiscard]] friend MPT_CONSTEXPR20_FUN auto operator+(fixed_string<N> a, fixed_string<M> b) -> fixed_string<N + M> {
367 fixed_string<N + M> result;
368 std::copy(a.begin(), a.end(), result.data() + 0);
369 std::copy(b.begin(), b.end(), result.data() + N);
370 return result;
371 }
372 [[nodiscard]] inline explicit operator std::string() const {
373 return std::string(m_data.begin(), std::find(m_data.begin(), m_data.end(), '\0'));
374 }
375 };
376
377
378 struct cpu_info {
379
380 private:
381
382 feature_flags Features{};
383 mode_flags Modes{};
384 uint32 CPUID = 0;
385 vendor Vendor = vendor::unknown;
386 uint16 Family = 0;
387 uint8 Model = 0;
388 uint8 Stepping = 0;
389 fixed_string<12> VendorID;
390 fixed_string<48> BrandID;
391 bool Virtualized = false;
392 fixed_string<12> HypervisorVendor;
393 fixed_string<4> HypervisorInterface;
394 #if !MPT_ARCH_AMD64
395 bool LongMode = false;
396 #endif // !MPT_ARCH_AMD64
397
398 public:
399
400 [[nodiscard]] MPT_CONSTEXPRINLINE bool operator[](feature_flags query_features) const noexcept {
401 return ((Features & query_features) == query_features);
402 }
403
404 [[nodiscard]] MPT_CONSTEXPRINLINE bool has_features(feature_flags query_features) const noexcept {
405 return ((Features & query_features) == query_features);
406 }
407
408 [[nodiscard]] MPT_CONSTEXPRINLINE feature_flags get_features() const noexcept {
409 return Features;
410 }
411
412 [[nodiscard]] MPT_CONSTEXPRINLINE bool operator[](mode_flags query_modes) const noexcept {
413 return ((Modes & query_modes) == query_modes);
414 }
415
416 [[nodiscard]] MPT_CONSTEXPRINLINE bool enabled_modes(mode_flags query_modes) const noexcept {
417 return ((Modes & query_modes) == query_modes);
418 }
419
420 [[nodiscard]] MPT_CONSTEXPRINLINE mode_flags get_modes() const noexcept {
421 return Modes;
422 }
423
424 [[nodiscard]] MPT_CONSTEXPRINLINE uint32 get_cpuid() const noexcept {
425 return CPUID;
426 }
427
428 [[nodiscard]] MPT_CONSTEXPRINLINE vendor get_vendor() const noexcept {
429 return Vendor;
430 }
431
432 [[nodiscard]] MPT_CONSTEXPRINLINE uint16 get_family() const noexcept {
433 return Family;
434 }
435
436 [[nodiscard]] MPT_CONSTEXPRINLINE uint8 get_model() const noexcept {
437 return Model;
438 }
439
440 [[nodiscard]] MPT_CONSTEXPRINLINE uint8 get_stepping() const noexcept {
441 return Stepping;
442 }
443
444 [[nodiscard]] inline std::string get_vendor_string() const {
445 return std::string(VendorID);
446 }
447
448 [[nodiscard]] inline std::string get_brand_string() const {
449 return std::string(BrandID);
450 }
451
452 [[nodiscard]] MPT_CONSTEXPRINLINE bool is_virtual() const noexcept {
453 return Virtualized;
454 }
455
456 [[nodiscard]] MPT_CONSTEXPRINLINE bool can_long_mode() const noexcept {
457 #if !MPT_ARCH_AMD64
458 return LongMode;
459 #else // MPT_ARCH_AMD64
460 return true;
461 #endif // !MPT_ARCH_AMD64
462 }
463
464 private:
465
466 #if MPT_ARCH_X86 || MPT_ARCH_AMD64
467
468 struct cpuid_result {
469
470 uint32 a = 0;
471 uint32 b = 0;
472 uint32 c = 0;
473 uint32 d = 0;
474
475 [[nodiscard]] MPT_CONSTEXPR20_FUN fixed_string<4> as_text4() const noexcept {
476 fixed_string<4> result;
477 result[0 + 0] = static_cast<char>((a >> 0) & 0xff);
478 result[0 + 1] = static_cast<char>((a >> 8) & 0xff);
479 result[0 + 2] = static_cast<char>((a >> 16) & 0xff);
480 result[0 + 3] = static_cast<char>((a >> 24) & 0xff);
481 return result;
482 }
483
484 [[nodiscard]] MPT_CONSTEXPR20_FUN fixed_string<12> as_text12bcd() const noexcept {
485 fixed_string<12> result;
486 result[0 + 0] = static_cast<char>((b >> 0) & 0xff);
487 result[0 + 1] = static_cast<char>((b >> 8) & 0xff);
488 result[0 + 2] = static_cast<char>((b >> 16) & 0xff);
489 result[0 + 3] = static_cast<char>((b >> 24) & 0xff);
490 result[4 + 0] = static_cast<char>((c >> 0) & 0xff);
491 result[4 + 1] = static_cast<char>((c >> 8) & 0xff);
492 result[4 + 2] = static_cast<char>((c >> 16) & 0xff);
493 result[4 + 3] = static_cast<char>((c >> 24) & 0xff);
494 result[8 + 0] = static_cast<char>((d >> 0) & 0xff);
495 result[8 + 1] = static_cast<char>((d >> 8) & 0xff);
496 result[8 + 2] = static_cast<char>((d >> 16) & 0xff);
497 result[8 + 3] = static_cast<char>((d >> 24) & 0xff);
498 return result;
499 }
500
501 [[nodiscard]] MPT_CONSTEXPR20_FUN fixed_string<12> as_text12bdc() const noexcept {
502 fixed_string<12> result;
503 result[0 + 0] = static_cast<char>((b >> 0) & 0xff);
504 result[0 + 1] = static_cast<char>((b >> 8) & 0xff);
505 result[0 + 2] = static_cast<char>((b >> 16) & 0xff);
506 result[0 + 3] = static_cast<char>((b >> 24) & 0xff);
507 result[4 + 0] = static_cast<char>((d >> 0) & 0xff);
508 result[4 + 1] = static_cast<char>((d >> 8) & 0xff);
509 result[4 + 2] = static_cast<char>((d >> 16) & 0xff);
510 result[4 + 3] = static_cast<char>((d >> 24) & 0xff);
511 result[8 + 0] = static_cast<char>((c >> 0) & 0xff);
512 result[8 + 1] = static_cast<char>((c >> 8) & 0xff);
513 result[8 + 2] = static_cast<char>((c >> 16) & 0xff);
514 result[8 + 3] = static_cast<char>((c >> 24) & 0xff);
515 return result;
516 }
517
518 [[nodiscard]] MPT_CONSTEXPR20_FUN fixed_string<16> as_text16() const noexcept {
519 fixed_string<16> result;
520 result[0 + 0] = static_cast<char>((a >> 0) & 0xff);
521 result[0 + 1] = static_cast<char>((a >> 8) & 0xff);
522 result[0 + 2] = static_cast<char>((a >> 16) & 0xff);
523 result[0 + 3] = static_cast<char>((a >> 24) & 0xff);
524 result[4 + 0] = static_cast<char>((b >> 0) & 0xff);
525 result[4 + 1] = static_cast<char>((b >> 8) & 0xff);
526 result[4 + 2] = static_cast<char>((b >> 16) & 0xff);
527 result[4 + 3] = static_cast<char>((b >> 24) & 0xff);
528 result[8 + 0] = static_cast<char>((c >> 0) & 0xff);
529 result[8 + 1] = static_cast<char>((c >> 8) & 0xff);
530 result[8 + 2] = static_cast<char>((c >> 16) & 0xff);
531 result[8 + 3] = static_cast<char>((c >> 24) & 0xff);
532 result[12 + 0] = static_cast<char>((d >> 0) & 0xff);
533 result[12 + 1] = static_cast<char>((d >> 8) & 0xff);
534 result[12 + 2] = static_cast<char>((d >> 16) & 0xff);
535 result[12 + 3] = static_cast<char>((d >> 24) & 0xff);
536 return result;
537 }
538 };
539
540 #if MPT_COMPILER_MSVC || MPT_COMPILER_GCC || MPT_COMPILER_CLANG
541
542 [[nodiscard]] static cpuid_result cpuid(uint32 function) noexcept {
543
544 #if MPT_COMPILER_MSVC
545
546 cpuid_result result;
547 int CPUInfo[4]{};
548 __cpuid(CPUInfo, function);
549 result.a = CPUInfo[0];
550 result.b = CPUInfo[1];
551 result.c = CPUInfo[2];
552 result.d = CPUInfo[3];
553 return result;
554
555 #elif MPT_COMPILER_GCC || (MPT_COMPILER_CLANG && !MPT_ARCH_AMD64) || MPT_CLANG_AT_LEAST(13, 0, 0)
556 // <https://reviews.llvm.org/D101338>
557
558 cpuid_result result;
559 unsigned int regeax{};
560 unsigned int regebx{};
561 unsigned int regecx{};
562 unsigned int regedx{};
563 __cpuid(function, regeax, regebx, regecx, regedx);
564 result.a = regeax;
565 result.b = regebx;
566 result.c = regecx;
567 result.d = regedx;
568 return result;
569
570 #elif MPT_COMPILER_CLANG && MPT_ARCH_AMD64
571 // <https://reviews.llvm.org/D101338>
572
573 cpuid_result result;
574 unsigned int a{};
575 unsigned int b{};
576 unsigned int c{};
577 unsigned int d{};
578 // clang-format off
579 __asm__ __volatile__ (
580 "xchgq %%rbx,%q1 \n\t"
581 "cpuid \n\t"
582 "xchgq %%rbx,%q1 \n\t"
583 : "=a" (a), "=r" (b), "=c" (c), "=d" (d)
584 : "0" (function));
585 // clang-format on
586 result.a = a;
587 result.b = b;
588 result.c = c;
589 result.d = d;
590 return result;
591
592 #elif 0
593
594 cpuid_result result;
595 unsigned int a{};
596 unsigned int b{};
597 unsigned int c{};
598 unsigned int d{};
599 // clang-format off
600 __asm__ __volatile__ (
601 "cpuid \n\t"
602 : "=a" (a), "=b" (b), "=c" (c), "=d" (d)
603 : "0" (function));
604 // clang-format on
605 result.a = a;
606 result.b = b;
607 result.c = c;
608 result.d = d;
609 return result;
610
611 #else // MPT_COMPILER
612
613 return cpuid_result result{};
614
615 #endif // MPT_COMPILER
616 }
617
618 [[nodiscard]] static cpuid_result cpuidex(uint32 function_a, uint32 function_c) noexcept {
619
620 #if MPT_COMPILER_MSVC
621
622 cpuid_result result;
623 int CPUInfo[4]{};
624 __cpuidex(CPUInfo, function_a, function_c);
625 result.a = CPUInfo[0];
626 result.b = CPUInfo[1];
627 result.c = CPUInfo[2];
628 result.d = CPUInfo[3];
629 return result;
630
631 #elif MPT_COMPILER_GCC || (MPT_COMPILER_CLANG && !MPT_ARCH_AMD64) || MPT_CLANG_AT_LEAST(13, 0, 0)
632 // <https://reviews.llvm.org/D101338>
633
634 cpuid_result result;
635 unsigned int regeax{};
636 unsigned int regebx{};
637 unsigned int regecx{};
638 unsigned int regedx{};
639 __cpuid_count(function_a, function_c, regeax, regebx, regecx, regedx);
640 result.a = regeax;
641 result.b = regebx;
642 result.c = regecx;
643 result.d = regedx;
644 return result;
645
646 #elif MPT_COMPILER_CLANG && MPT_ARCH_AMD64
647 // <https://reviews.llvm.org/D101338>
648
649 cpuid_result result;
650 unsigned int a{};
651 unsigned int b{};
652 unsigned int c{};
653 unsigned int d{};
654 // clang-format off
655 __asm__ __volatile__ (
656 "xchgq %%rbx,%q1 \n\t"
657 "cpuid \n\t"
658 "xchgq %%rbx,%q1 \n\t"
659 : "=a" (a), "=r" (b), "=c" (c), "=d" (d)
660 : "0" (function_a), "2" (function_c));
661 // clang-format on
662 result.a = a;
663 result.b = b;
664 result.c = c;
665 result.d = d;
666 return result;
667
668 #elif 0
669
670 cpuid_result result;
671 unsigned int a{};
672 unsigned int b{};
673 unsigned int c{};
674 unsigned int d{};
675 // clang-format off
676 __asm__ __volatile__ (
677 "cpuid \n\t"
678 : "=a" (a), "=b" (b), "=c" (c), "=d" (d)
679 : "0" (function_a), "2" (function_c));
680 // clang-format on
681 result.a = a;
682 result.b = b;
683 result.c = c;
684 result.d = d;
685 return result;
686
687 #else // MPT_COMPILER
688
689 return cpuid_result result{};
690
691 #endif // MPT_COMPILER
692 }
693
694 #if MPT_MODE_KERNEL
695
696 [[nodiscard]] static uint32 read_cr4() noexcept {
697 return __readcr4();
698 }
699
700 #endif // MPT_MODE_KERNEL
701
702 [[nodiscard]] static uint64 read_xcr(uint32 num) noexcept {
703
704 #if MPT_COMPILER_MSVC
705
706 return _xgetbv(num);
707
708 #elif MPT_COMPILER_GCC || MPT_COMPILER_CLANG
709
710 uint32 param_ecx = num;
711 uint32 result_eax = 0;
712 uint32 result_edx = 0;
713 // clang-format off
714 __asm__ __volatile__("xgetbv" : "=a" (result_eax), "=d" (result_edx) : "c" (param_ecx));
715 // clang-format on
716 return static_cast<uint64>(result_eax) + (static_cast<uint64>(result_edx) << 32);
717
718 #else
719
720 return _xgetbv(num);
721
722 #endif
723 }
724
725 #endif // MPT_COMPILER_MSVC || MPT_COMPILER_GCC || MPT_COMPILER_CLANG
726
727 private:
728
729 #if MPT_ARCH_X86 || !MPT_ARCH_X86_AMD64_FAST_DETECT
730
731 #if MPT_OS_DJGPP
732
733 [[nodiscard]] static uint8 detect_cpu_level() noexcept {
734
735 #if 0
736 uint8 result = 0;
737 __dpmi_version_ret dpmi_version{};
738 if (__dpmi_get_version(&dpmi_version) == 0) {
739 result = dpmi_version.cpu;
740 }
741 return result;
742 #else
743 uint8 result = 0;
744 __dpmi_regs regs{};
745 regs.x.ax = 0x0400;
746 if (__dpmi_int(0x31, &regs) == 0) {
747 unsigned int cf = (regs.x.flags >> 0) & 1u;
748 if (cf == 0) {
749 result = regs.h.cl;
750 }
751 }
752 return result;
753 #endif
754 }
755
756 #endif // MPT_OS_DJGPP
757
758 [[nodiscard]] static uint8 detect_fpu_level() noexcept {
759
760 #if MPT_OS_DJGPP
761
762 #if 1
763 uint8 result = 0;
764 int coprocessor_status = __dpmi_get_coprocessor_status();
765 if (coprocessor_status == -1) {
766 // error = __dpmi_error
767 return 0;
768 }
769 result = (static_cast<unsigned int>(coprocessor_status) & 0x00f0u) >> 4;
770 return result;
771 #else
772 uint8 result = 0;
773 __dpmi_regs regs{};
774 regs.x.ax = 0x0e00;
775 if (__dpmi_int(0x31, &regs) == 0) {
776 unsigned int cf = (regs.x.flags >> 0) & 1u;
777 if (cf == 0) {
778 result = (regs.x.ax & 0x00f0u) >> 4;
779 }
780 }
781 return result;
782 #endif
783
784 #elif MPT_OS_WINDOWS
785
786 uint8 result = 0;
787 #if MPT_WINNT_AT_LEAST(MPT_WIN_NT4)
788 if (mpt::osinfo::windows::Version::Current().IsAtLeast(mpt::osinfo::windows::Version::Win2000)) {
789 if (IsProcessorFeaturePresent(PF_FLOATING_POINT_EMULATED) == 0) {
790 result = 3;
791 }
792 } else {
793 // logic is inverted on NT4
794 if (IsProcessorFeaturePresent(PF_FLOATING_POINT_EMULATED) != 0) {
795 result = 3;
796 }
797 }
798 #else
799 if ((assumed_features() & feature::fpu) && (assumed_features() & feature::fsin)) {
800 result = 3;
801 } else if (assumed_features() & feature::fpu) {
802 result = 2;
803 } else {
804 result = 0;
805 }
806 #endif
807 return result;
808
809 #elif MPT_COMPILER_MSVC && MPT_MODE_KERNEL
810
811 uint8 result = 0;
812 const std::size_t cr0 = __readcr0();
813 if (!(cr0 & (1u << 2))) { // EM
814 result = 2;
815 if (cr0 & (1u << 4)) { // ET
816 result = 3;
817 }
818 }
819 return result;
820
821 #else
822
823 uint8 result = 0;
824 if ((assumed_features() & feature::fpu) && (assumed_features() & feature::fsin)) {
825 result = 3;
826 } else if (assumed_features() & feature::fpu) {
827 result = 2;
828 } else {
829 result = 0;
830 }
831 return result;
832
833 #endif
834 }
835
836 #if MPT_COMPILER_MSVC || MPT_COMPILER_GCC || MPT_COMPILER_CLANG
837
838 [[nodiscard]] static bool can_toggle_eflags(std::size_t mask) noexcept {
839 std::size_t eflags_old = __readeflags();
840 std::size_t eflags_flipped = eflags_old ^ mask;
841 __writeeflags(eflags_flipped);
842 std::size_t eflags_testchanged = __readeflags();
843 __writeeflags(eflags_old);
844 return ((eflags_testchanged ^ eflags_old) & mask) == mask;
845 }
846
847 #endif
848
849 [[nodiscard]] static bool can_toggle_eflags_ac() noexcept {
850 #if MPT_COMPILER_MSVC || MPT_COMPILER_GCC || MPT_COMPILER_CLANG
851 return can_toggle_eflags(0x0004'0000);
852 #else // MPT_COMPILER
853 return (assumed_features() & feature::intel486) != 0;
854 #endif // MPT_COMPILER
855 }
856
857 [[nodiscard]] static bool can_toggle_eflags_id() noexcept {
858 #if MPT_COMPILER_MSVC || MPT_COMPILER_GCC || MPT_COMPILER_CLANG
859 return can_toggle_eflags(0x0020'0000);
860 #else // MPT_COMPILER
861 return (assumed_features() & feature::tsc) != 0;
862 #endif // MPT_COMPILER
863 }
864
865 [[nodiscard]] MPT_NOINLINE static bool detect_nexgen() noexcept {
866
867 #if MPT_ARCH_X86 && MPT_COMPILER_MSVC
868
869 uint8 result = 0;
870 // clang-format off
871 _asm {
872 mov eax, 0x5555
873 xor edx, edx
874 mov ecx, 2
875 clc
876 div ecx
877 jz found
878 jmp done
879 found:
880 mov result, 1
881 jmp done
882 done:
883 }
884 // clang-format on
885 return (result != 0);
886
887 #elif MPT_ARCH_X86 && (MPT_COMPILER_GCC || MPT_COMPILER_CLANG)
888
889 unsigned int result = 0;
890 // clang-format off
891 __asm__ __volatile(
892 "movl $0x5555, %%eax\n"
893 "xorl %%edx, %%edx\n"
894 "movl $2, %%ecx\n"
895 "clc\n"
896 "divl %%ecx\n"
897 "jz detect_nexgen_found\n"
898 "movl $0, %%eax\n"
899 "jmp detect_nexgen_done\n"
900 "detect_nexgen_found:\n"
901 "movl $0, %%eax\n"
902 "jmp detect_nexgen_done\n"
903 "detect_nexgen_done:\n"
904 : "=a" (result)
905 :
906 : "%ebx", "%ecx"
907 );
908 // clang-format on
909 return (result != 0);
910
911 #else
912
913 // assume false
914 return false;
915
916 #endif
917 }
918
919 [[nodiscard]] MPT_NOINLINE static bool detect_cyrix() noexcept {
920
921 #if MPT_ARCH_X86 && MPT_COMPILER_MSVC
922
923 uint8 result = 0;
924 // clang-format off
925 _asm {
926 xor ax, ax
927 sahf
928 mov ax, 5
929 mov bx, 2
930 div bl
931 lahf
932 cmp ah, 2
933 jne not_cyrix
934 mov result, 1
935 not_cyrix:
936 }
937 // clang-format on
938 return (result != 0);
939
940 #elif MPT_ARCH_X86 && (MPT_COMPILER_GCC || MPT_COMPILER_CLANG)
941
942 unsigned int result = 0;
943 // clang-format off
944 __asm__ __volatile(
945 "xor %%ax, %%ax\n"
946 "sahf\n"
947 "movw $5, %%ax\n"
948 "movw $2, %%bx\n"
949 "divb %%bl\n"
950 "lahf\n"
951 "cmpb $2, %%ah\n"
952 "jne detect_cyrix_done_not_cyrix\n"
953 "movl $1, %%eax\n"
954 "jmp detect_cyrix_done\n"
955 "detect_cyrix_done_not_cyrix:\n"
956 "movl $0, %%eax\n"
957 "jmp detect_cyrix_done\n"
958 "detect_cyrix_done:\n"
959 : "=a" (result)
960 :
961 : "%ebx"
962 );
963 // clang-format on
964 return (result != 0);
965
966 #else
967
968 // assume false
969 return false;
970
971 #endif
972 }
973
974 [[nodiscard]] static uint16 read_cyrix_id() noexcept {
975
976 #if MPT_OS_DJGPP
977
978 uint16 result = 0;
979 outportb(0x22, 0x00);
980 result |= static_cast<uint16>(inportb(0x23)) << 8;
981 outportb(0x22, 0x01);
982 result |= static_cast<uint16>(inportb(0x23)) << 0;
983 outportb(0x22, 0x00);
984 inportb(0x23);
985 return result;
986
987 #elif MPT_COMPILER_MSVC && MPT_MODE_KERNEL
988
989 uint16 result = 0;
990 __outbyte(0x22, 0x00);
991 result |= static_cast<uint16>(__inbyte(0x23)) << 8;
992 __outbyte(0x22, 0x01);
993 result |= static_cast<uint16>(__inbyte(0x23)) << 0;
994 __outbyte(0x22, 0x00);
995 __inbyte(0x23);
996 return result;
997
998 #else
999
1000 return 0x00'00;
1001
1002 #endif
1003 }
1004
1005 #endif // MPT_ARCH_X86
1006
1007 #endif // MPT_ARCH_X86 || MPT_ARCH_AMD64
1008
1009 public:
1010
1011 cpu_info() noexcept {
1012
1013 #if MPT_ARCH_X86 || MPT_ARCH_AMD64
1014
1015 #if MPT_COMPILER_MSVC || MPT_COMPILER_GCC || MPT_COMPILER_CLANG
1016
1017 #if MPT_ARCH_X86 || !MPT_ARCH_X86_AMD64_FAST_DETECT
1018
1019 Features |= featureset::intel386;
1020
1021 if (can_toggle_eflags_ac()) {
1022 Features |= feature::intel486;
1023 }
1024 if (can_toggle_eflags_id()) {
1025 Features |= feature::cpuid;
1026 }
1027 if (!Features.supports(feature::cpuid)) {
1028 // without cpuid
1029 if (!Features.supports(feature::intel486)) {
1030 // 386
1031 const uint8 fpu_level = detect_fpu_level();
1032 if (fpu_level >= 2) {
1033 Features |= feature::fpu;
1034 }
1035 if (fpu_level >= 3) {
1036 Features |= feature::fsin;
1037 }
1038 if (detect_nexgen()) {
1039 Vendor = vendor::NexGen;
1040 } else {
1041 Vendor = vendor::unknown;
1042 }
1043 } else {
1044 // 486+
1045 const uint8 fpu_level = detect_fpu_level();
1046 if (fpu_level >= 2) {
1047 Features |= feature::fpu;
1048 }
1049 if (fpu_level >= 3) {
1050 Features |= feature::fsin;
1051 }
1052 if (detect_cyrix()) {
1053 Vendor = vendor::Cyrix;
1054 uint16 id = read_cyrix_id();
1055 if (id <= 0x07'00) {
1056 // Cx486SLC / Cx486DLC
1057 Family = 3;
1058 Model = static_cast<uint8>((id & 0xff'00) >> 8);
1059 Stepping = static_cast<uint8>(id & 0x00'ff);
1060 } else if ((0x10'00 <= id) && (id <= 0x13'00)) {
1061 // Cx486S
1062 Family = 4;
1063 Model = static_cast<uint8>((id & 0xff'00) >> 8);
1064 Stepping = static_cast<uint8>(id & 0x00'ff);
1065 } else if ((0x1a'00 <= id) && (id <= 0x1f'00)) {
1066 // Cx486DX
1067 Family = 4;
1068 Model = static_cast<uint8>((id & 0xff'00) >> 8);
1069 Stepping = static_cast<uint8>(id & 0x00'ff);
1070 } else if ((0x28'00 <= id) && (id <= 0x2e'00)) {
1071 // Cx5x86
1072 Family = 4;
1073 Model = static_cast<uint8>((id & 0xff'00) >> 8);
1074 Stepping = static_cast<uint8>(id & 0x00'ff);
1075 } else if ((0x40'00 <= id) && (id <= 0x4f'00)) {
1076 // MediaGx / MediaGXm
1077 Family = 4;
1078 Model = 4;
1079 Stepping = static_cast<uint8>(id & 0x00'ff);
1080 } else if ((0x30'00 <= id) && (id <= 0x34'00)) {
1081 // Cx6x86 / Cx6x86L
1082 Family = 5;
1083 Model = 2;
1084 Stepping = static_cast<uint8>(id & 0x00'ff);
1085 if ((id & 0x00'ff) > 0x21) {
1086 // Cx6x86L
1087 Features |= feature::cx8;
1088 }
1089 } else if ((0x50'00 <= id) && (id <= 0x5f'00)) {
1090 // Cx6x86MX
1091 Family = 6;
1092 Model = 0;
1093 Stepping = static_cast<uint8>(id & 0x00'ff);
1094 Features |= feature::cx8;
1095 Features |= feature::tsc;
1096 Features |= feature::mmx;
1097 Features |= feature::cmov;
1098 }
1099 } else {
1100 Vendor = vendor::unknown;
1101 }
1102 }
1103 }
1104
1105 #elif MPT_ARCH_AMD64
1106
1107 Features |= featureset::amd64;
1108 Modes |= modeset::amd64;
1109
1110 #endif // MPT_ARCH
1111
1112 bool have_osxsave = false;
1113
1114 if (Features.supports(feature::cpuid)) {
1115 // with cpuid
1116 cpuid_result VendorString = cpuid(0x0000'0000u);
1117 VendorID = VendorString.as_text12bdc();
1118 if (VendorID == std::string_view(" ")) {
1119 Vendor = vendor::unknown;
1120 } else if (VendorID == std::string_view("AMDisbetter!")) {
1121 Vendor = vendor::AMD;
1122 } else if (VendorID == std::string_view("AuthenticAMD")) {
1123 Vendor = vendor::AMD;
1124 } else if (VendorID == std::string_view("CentaurHauls")) {
1125 Vendor = vendor::Centaur;
1126 } else if (VendorID == std::string_view("CyrixInstead")) {
1127 Vendor = vendor::Cyrix;
1128 } else if (VendorID == std::string_view("GenuineIntel")) {
1129 Vendor = vendor::Intel;
1130 } else if (VendorID == std::string_view("TransmetaCPU")) {
1131 Vendor = vendor::Transmeta;
1132 } else if (VendorID == std::string_view("GenuineTMx86")) {
1133 Vendor = vendor::Transmeta;
1134 } else if (VendorID == std::string_view("Geode by NSC")) {
1135 Vendor = vendor::NSC;
1136 } else if (VendorID == std::string_view("NexGenDriven")) {
1137 Vendor = vendor::NexGen;
1138 } else if (VendorID == std::string_view("RiseRiseRise")) {
1139 Vendor = vendor::Rise;
1140 } else if (VendorID == std::string_view("SiS SiS SiS ")) {
1141 Vendor = vendor::SiS;
1142 } else if (VendorID == std::string_view("UMC UMC UMC ")) {
1143 Vendor = vendor::UMC;
1144 } else if (VendorID == std::string_view("VIA VIA VIA ")) {
1145 Vendor = vendor::VIA;
1146 } else if (VendorID == std::string_view("Vortex86 SoC")) {
1147 Vendor = vendor::DMnP;
1148 } else if (VendorID == std::string_view(" Shanghai ")) {
1149 Vendor = vendor::Zhaoxin;
1150 } else if (VendorID == std::string_view("HygonGenuine")) {
1151 Vendor = vendor::Hygon;
1152 } else if (VendorID == std::string_view("E2K MACHINE")) {
1153 Vendor = vendor::Elbrus;
1154 } else if (VendorID == std::string_view("MiSTer AO486")) {
1155 Vendor = vendor::MiSTer;
1156 } else if (VendorID == std::string_view("bhyve bhyve ")) {
1157 Vendor = vendor::bhyve;
1158 } else if (VendorID == std::string_view(" KVMKVMKVM ")) {
1159 Vendor = vendor::KVM;
1160 } else if (VendorID == std::string_view("TCGTCGTCGTCG")) {
1161 Vendor = vendor::QEMU;
1162 } else if (VendorID == std::string_view("Microsoft Hv")) {
1163 Vendor = vendor::HyperV;
1164 } else if (VendorID == std::string_view(" lrpepyh vr")) {
1165 Vendor = vendor::Parallels;
1166 } else if (VendorID == std::string_view("VMwareVMware")) {
1167 Vendor = vendor::VMWare;
1168 } else if (VendorID == std::string_view("XenVMMXenVMM")) {
1169 Vendor = vendor::Xen;
1170 } else if (VendorID == std::string_view("ACRNACRNACRN")) {
1171 Vendor = vendor::ACRN;
1172 } else if (VendorID == std::string_view(" QNXQVMBSQG ")) {
1173 Vendor = vendor::QNX;
1174 }
1175 // Cyrix 6x86 and 6x86MX do not specify the value returned in eax.
1176 // They both support 0x00000001u however.
1177 if ((VendorString.a >= 0x0000'0001u) || (Vendor == vendor::Cyrix)) {
1178 cpuid_result StandardFeatureFlags = cpuid(0x0000'0001u);
1179 CPUID = StandardFeatureFlags.a;
1180 // clang-format off
1181 uint32 BaseStepping = (StandardFeatureFlags.a >> 0) & 0x0f;
1182 uint32 BaseModel = (StandardFeatureFlags.a >> 4) & 0x0f;
1183 uint32 BaseFamily = (StandardFeatureFlags.a >> 8) & 0x0f;
1184 uint32 ExtModel = (StandardFeatureFlags.a >> 16) & 0x0f;
1185 uint32 ExtFamily = (StandardFeatureFlags.a >> 20) & 0xff;
1186 // clang-format on
1187 if (BaseFamily == 0xf) {
1188 Family = static_cast<uint16>(ExtFamily + BaseFamily);
1189 } else {
1190 Family = static_cast<uint16>(BaseFamily);
1191 }
1192 if ((Vendor == vendor::AMD) && (BaseFamily == 0xf)) {
1193 Model = static_cast<uint8>((ExtModel << 4) | (BaseModel << 0));
1194 } else if ((Vendor == vendor::Centaur) && (BaseFamily >= 0x6)) {
1195 // Newer Zhaoxin CPUs use extended family also with BaseFamily=7.
1196 Model = static_cast<uint8>((ExtModel << 4) | (BaseModel << 0));
1197 } else if ((BaseFamily == 0x6) || (BaseFamily == 0xf)) {
1198 Model = static_cast<uint8>((ExtModel << 4) | (BaseModel << 0));
1199 } else {
1200 Model = static_cast<uint8>(BaseModel);
1201 }
1202 Stepping = static_cast<uint8>(BaseStepping);
1203 // clang-format off
1204 Features |= (StandardFeatureFlags.d & (1u << 0)) ? (feature::fpu | feature::fsin) : feature::none;
1205 Features |= (StandardFeatureFlags.d & (1u << 4)) ? (feature::tsc) : feature::none;
1206 Features |= (StandardFeatureFlags.d & (1u << 8)) ? (feature::cx8) : feature::none;
1207 Features |= (StandardFeatureFlags.d & (1u << 15)) ? (feature::cmov) : feature::none;
1208 Features |= (StandardFeatureFlags.d & (1u << 23)) ? (feature::mmx) : feature::none;
1209 Features |= (StandardFeatureFlags.d & (1u << 24)) ? (feature::fxsr) : feature::none;
1210 Features |= (StandardFeatureFlags.d & (1u << 25)) ? (feature::sse | feature::mmxext) : feature::none;
1211 Features |= (StandardFeatureFlags.d & (1u << 26)) ? (feature::sse2) : feature::none;
1212 Features |= (StandardFeatureFlags.c & (1u << 0)) ? (feature::sse3) : feature::none;
1213 Features |= (StandardFeatureFlags.c & (1u << 9)) ? (feature::ssse3) : feature::none;
1214 Features |= (StandardFeatureFlags.c & (1u << 12)) ? (feature::fma) : feature::none;
1215 Features |= (StandardFeatureFlags.c & (1u << 13)) ? (feature::cx16) : feature::none;
1216 Features |= (StandardFeatureFlags.c & (1u << 19)) ? (feature::sse4_1) : feature::none;
1217 Features |= (StandardFeatureFlags.c & (1u << 20)) ? (feature::sse4_2) : feature::none;
1218 Features |= (StandardFeatureFlags.c & (1u << 22)) ? (feature::movbe) : feature::none;
1219 Features |= (StandardFeatureFlags.c & (1u << 23)) ? (feature::popcnt) : feature::none;
1220 Features |= (StandardFeatureFlags.c & (1u << 26)) ? (feature::xsave) : feature::none;
1221 Features |= (StandardFeatureFlags.c & (1u << 28)) ? (feature::avx) : feature::none;
1222 Features |= (StandardFeatureFlags.c & (1u << 29)) ? (feature::f16c) : feature::none;
1223 // clang-format on
1224 if (StandardFeatureFlags.c & (1u << 27)) {
1225 have_osxsave = true;
1226 }
1227 if (StandardFeatureFlags.c & (1u << 31)) {
1228 Virtualized = true;
1229 }
1230 }
1231 if ((VendorString.a >= 0x0000'0007u) && Features.supports(feature::avx)) {
1232 // Some Skylake Celerons wrongly report BMI1 even though they do not support it.
1233 // They also do not support AVX.
1234 // Guard BMI1/BMI2 with AVX, as all known CPUs with BMI1 also support AVX.
1235 // See <https://mastodon.gamedev.place/@rygorous/115375820395748540>.
1236 cpuid_result ExtendedFeatures = cpuidex(0x0000'0007u, 0x0000'0000u);
1237 // clang-format off
1238 Features |= (ExtendedFeatures.b & (1u << 3)) ? (feature::bmi1) : feature::none;
1239 Features |= (ExtendedFeatures.b & (1u << 5)) ? (feature::avx2) : feature::none;
1240 Features |= (ExtendedFeatures.b & (1u << 8)) ? (feature::bmi2) : feature::none;
1241 // clang-format on
1242 }
1243 // 3DNow! manual recommends to just execute 0x8000'0000u.
1244 // It is totally unknown how earlier CPUs from other vendors
1245 // would behave.
1246 // Thus we only execute 0x80000000u on other vendors CPUs for the earliest
1247 // that we found it documented for and that actually supports 3DNow!.
1248 bool ecpuid = false;
1249 bool x3dnowknown = false;
1250 if (Vendor == vendor::Intel) {
1251 ecpuid = true;
1252 } else if (Vendor == vendor::AMD) {
1253 if ((Family > 5) || ((Family == 5) && (Model >= 8))) {
1254 // >= K6-2 (K6 = Family 5, K6-2 = Model 8)
1255 // Not sure if earlier AMD CPUs support 0x80000000u.
1256 // AMD 5k86 and AMD K5 manuals do not mention it.
1257 ecpuid = true;
1258 x3dnowknown = true;
1259 }
1260 } else if (Vendor == vendor::Centaur) {
1261 // Centaur (IDT WinChip or VIA C3)
1262 if (Family == 5) {
1263 // IDT
1264 if (Model >= 8) {
1265 // >= WinChip 2
1266 ecpuid = true;
1267 x3dnowknown = true;
1268 }
1269 } else if (Family >= 6) {
1270 // VIA
1271 if ((Family >= 7) || ((Family == 6) && (Model >= 7))) {
1272 // >= C3 Samuel 2
1273 ecpuid = true;
1274 x3dnowknown = true;
1275 }
1276 }
1277 } else if (Vendor == vendor::Cyrix) {
1278 // Cyrix
1279 // 6x86 : 5.2.x
1280 // 6x86L : 5.2.x
1281 // MediaGX : 4.4.x
1282 // 6x86MX : 6.0.x
1283 // MII : 6.0.x
1284 // MediaGXm: 5.4.x
1285 // well, doh ...
1286 if ((Family == 5) && (Model >= 4)) {
1287 // Cyrix MediaGXm
1288 ecpuid = true;
1289 x3dnowknown = true;
1290 }
1291 } else if (Vendor == vendor::NSC) {
1292 // National Semiconductor
1293 if ((Family > 5) || ((Family == 5) && (Model >= 5))) {
1294 // >= Geode GX2
1295 ecpuid = true;
1296 x3dnowknown = true;
1297 }
1298 } else {
1299 // Intel specification allows 0x8000'0000u on earlier CPUs,
1300 // thus we execute it on unknown vendors.
1301 ecpuid = true;
1302 }
1303 if (ecpuid) {
1304 cpuid_result ExtendedVendorString = cpuid(0x8000'0000u);
1305 if ((ExtendedVendorString.a & 0xffff'0000u) == 0x8000'0000u) {
1306 if (ExtendedVendorString.a >= 0x8000'0001u) {
1307 // clang-format off
1308 cpuid_result ExtendedFeatureFlags = cpuid(0x8000'0001u);
1309 #if !MPT_ARCH_AMD64
1310 if (ExtendedFeatureFlags.d & (1u << 29)) {
1311 LongMode = true;
1312 }
1313 #endif // !MPT_ARCH_AMD64
1314 Features |= (ExtendedFeatureFlags.c & (1u << 0)) ? (feature::lahf) : feature::none;
1315 Features |= (ExtendedFeatureFlags.c & (1u << 5)) ? (feature::lzcnt) : feature::none;
1316 if (x3dnowknown) {
1317 Features |= (ExtendedFeatureFlags.d & (1u << 31)) ? (feature::x3dnow) : feature::none;
1318 }
1319 if (Vendor == vendor::AMD) {
1320 Features |= (ExtendedFeatureFlags.d & (1u << 22)) ? (feature::mmxext) : feature::none;
1321 Features |= (ExtendedFeatureFlags.d & (1u << 30)) ? (feature::x3dnowext) : feature::none;
1322 Features |= (ExtendedFeatureFlags.c & (1u << 5)) ? (feature::popcnt) : feature::none;
1323 Features |= (ExtendedFeatureFlags.c & (1u << 8)) ? (feature::x3dnowprefetch) : feature::none;
1324 }
1325 // clang-format on
1326 }
1327 if (ExtendedVendorString.a >= 0x8000'0004u) {
1328 BrandID = cpuid(0x8000'0002u).as_text16() + cpuid(0x8000'0003u).as_text16() + cpuid(0x8000'0004u).as_text16();
1329 }
1330 }
1331 }
1332
1333 #if MPT_ARCH_AMD64
1334
1335 // clang-format off
1336 Modes |= mode::xmm128sse;
1337 const bool have_xsave =
1338 #ifdef MPT_ARCH_X86_XSAVE
1339 true;
1340 #else
1341 Features.supports(feature::xsave);
1342 #endif
1343 MPT_MAYBE_CONSTANT_IF (have_xsave && have_osxsave) {
1344 const uint64 xcr0 = read_xcr(0x0000'0000u);
1345 Modes |= (xcr0 & (1ull << 2)) ? mode::ymm256avx : mode::base;
1346 }
1347 // clang-format on
1348
1349 #else // !MPT_ARCH_AMD64
1350
1351 // clang-format off
1352 const bool have_xsave =
1353 #ifdef MPT_ARCH_X86_XSAVE
1354 true;
1355 #else
1356 Features.supports(feature::xsave);
1357 #endif
1358 MPT_MAYBE_CONSTANT_IF (have_xsave && have_osxsave) {
1359 const uint64 xcr0 = read_xcr(0x0000'0000u);
1360 Modes |= (xcr0 & (1ull << 1)) ? mode::xmm128sse : mode::base;
1361 Modes |= (xcr0 & (1ull << 2)) ? mode::ymm256avx : mode::base;
1362 } else {
1363 const bool have_fxsr =
1364 #ifdef MPT_ARCH_X86_FXSR
1365 true;
1366 #else
1367 Features.supports(feature::fxsr);
1368 #endif
1369 MPT_MAYBE_CONSTANT_IF (have_fxsr) {
1370 #if MPT_MODE_KERNEL
1371 const uint32 cr4 = read_cr4();
1372 Modes |= (cr4 & (1u << 9)) ? mode::xmm128sse : mode::base;
1373 #else // !MPT_MODE_KERNEL
1374 // There is no way for user-mode code to check for SSE enabled in CR4.
1375 // Assume based on FXSR and SSE and platform heuristics.
1376 // Avoid assumption on DOS.
1377 #if MPT_OS_WINDOWS
1378 // get from platform API
1379 #if MPT_OS_WINDOWS_WINNT
1380 #ifdef PF_XMMI_INSTRUCTIONS_AVAILABLE
1381 Modes |= (IsProcessorFeaturePresent(PF_XMMI_INSTRUCTIONS_AVAILABLE) != 0) ? (mode::xmm128sse) : mode::base;
1382 #endif
1383 #elif MPT_WIN9X_AT_LEAST(MPT_WIN_WIN98)
1384 const bool have_sse =
1385 #ifdef MPT_ARCH_X86_SSE
1386 true;
1387 #else
1388 Features.supports(feature::sse);
1389 #endif
1390 MPT_MAYBE_CONSTANT_IF (have_sse) {
1391 Modes |= mode::xmm128sse;
1392 }
1393 #else // MPT_OS_WINDOWS
1394 // nothing
1395 #endif // MPT_OS_WINDOWS
1396 #elif MPT_OS_DJGPP || MPT_OS_UNKNOWN
1397 // avoid SSE on DOS and unknown systems,
1398 // however if we directly targeting >= SSE, then assume it's activated because the process will crash otherwise anyway
1399 #ifdef MPT_ARCH_X86_SSE
1400 Modes |= mode::xmm128sse;
1401 #endif
1402 #else // MPT_OS
1403 // assume based on FXSR and SSE
1404 const bool have_sse =
1405 #ifdef MPT_ARCH_X86_SSE
1406 true;
1407 #else
1408 Features.supports(feature::sse);
1409 #endif
1410 MPT_MAYBE_CONSTANT_IF (have_sse) {
1411 Modes |= mode::xmm128sse;
1412 }
1413 #endif // MPT_OS
1414 #endif // MPT_MODE_KERNEL
1415 }
1416 }
1417 // clang-format on
1418
1419 #endif // MPT_ARCH_AMD64
1420
1421 if (Virtualized) {
1422 cpuid_result HypervisorVendorID = cpuid(0x4000'0000u);
1423 if (HypervisorVendorID.a >= 0x4000'0000u) {
1424 HypervisorVendor = HypervisorVendorID.as_text12bcd();
1425 if (HypervisorVendor == std::string_view("Microsoft Hv")) {
1426 if (HypervisorVendorID.a >= 0x4000'0001u) {
1427 cpuid_result HypervisorInterfaceID = cpuid(0x4000'0001u);
1428 HypervisorInterface = HypervisorInterfaceID.as_text4();
1429 }
1430 } else if (HypervisorVendor == std::string_view("KVMKVMKVM")) {
1431 // nothing
1432 }
1433 }
1434 }
1435 }
1436
1437 #elif MPT_OS_WINDOWS
1438
1439 #if MPT_ARCH_X86 || !MPT_ARCH_X86_AMD64_FAST_DETECT
1440
1441 SYSTEM_INFO si = {};
1442 GetSystemInfo(&si);
1443 switch (si.wProcessorArchitecture) {
1444 case PROCESSOR_ARCHITECTURE_INTEL:
1445 case PROCESSOR_ARCHITECTURE_AMD64:
1446 case PROCESSOR_ARCHITECTURE_IA32_ON_WIN64:
1447 case PROCESSOR_ARCHITECTURE_IA32_ON_ARM64:
1448 switch (si.dwProcessorType) {
1449 case PROCESSOR_INTEL_386:
1450 Family = si.wProcessorLevel;
1451 if (((si.wProcessorRevision & 0xff00) >> 8) == 0xff) {
1452 Model = ((si.wProcessorRevision & 0x00f0) >> 4) - 0xa;
1453 Stepping = ((si.wProcessorRevision & 0x000f) >> 0);
1454 } else {
1455 Model = ((si.wProcessorRevision & 0xff00) >> 8) + static_cast<unsigned char>('A');
1456 Stepping = ((si.wProcessorRevision & 0x00ff) >> 0);
1457 }
1458 Model = (si.wProcessorRevision & 0xff00) >> 8;
1459 Stepping = (si.wProcessorRevision & 0x00ff) >> 0;
1460 Features |= featureset::intel386;
1461 break;
1462 case PROCESSOR_INTEL_486:
1463 Family = si.wProcessorLevel;
1464 if (((si.wProcessorRevision & 0xff00) >> 8) == 0xff) {
1465 Model = ((si.wProcessorRevision & 0x00f0) >> 4) - 0xa;
1466 Stepping = ((si.wProcessorRevision & 0x000f) >> 0);
1467 } else {
1468 Model = ((si.wProcessorRevision & 0xff00) >> 8) + static_cast<unsigned char>('A');
1469 Stepping = ((si.wProcessorRevision & 0x00ff) >> 0);
1470 }
1471 Model = (si.wProcessorRevision & 0xff00) >> 8;
1472 Stepping = (si.wProcessorRevision & 0x00ff) >> 0;
1473 Features |= featureset::intel486SX;
1474 break;
1475 case PROCESSOR_INTEL_PENTIUM:
1476 Family = si.wProcessorLevel;
1477 Model = (si.wProcessorRevision & 0xff00) >> 8;
1478 Stepping = (si.wProcessorRevision & 0x00ff) >> 0;
1479 // rely on IsProcessorFeaturePresent() for > 486 features
1480 // Features |= featureset::intel586;
1481 Features |= featureset::intel486SX;
1482 break;
1483 }
1484 break;
1485 }
1486 Features |= featureset::intel386;
1487 const uint8 fpu_level = detect_fpu_level();
1488 if (fpu_level >= 2) {
1489 Features |= feature::fpu;
1490 }
1491 if (fpu_level >= 3) {
1492 Features |= feature::fsin;
1493 }
1494
1495 #elif MPT_ARCH_AMD64
1496
1497 Features |= featureset::amd64;
1498
1499 #endif // MPT_ARCH
1500
1501 #if MPT_OS_WINDOWS_WINNT
1502 // clang-format off
1503 Features |= (IsProcessorFeaturePresent(PF_RDTSC_INSTRUCTION_AVAILABLE) != 0) ? (feature::tsc | feature::intel486) : feature::none;
1504 Features |= (IsProcessorFeaturePresent(PF_COMPARE_EXCHANGE_DOUBLE) != 0) ? (feature::cx8 | feature::intel486) : feature::none;
1505 Features |= (IsProcessorFeaturePresent(PF_MMX_INSTRUCTIONS_AVAILABLE) != 0) ? (feature::mmx | feature::intel486) : feature::none;
1506 Features |= (IsProcessorFeaturePresent(PF_3DNOW_INSTRUCTIONS_AVAILABLE) != 0) ? (feature::x3dnow) : feature::none;
1507 Features |= (IsProcessorFeaturePresent(PF_XMMI_INSTRUCTIONS_AVAILABLE) != 0) ? (feature::sse | feature::mmxext | feature::fxsr | feature::cmov) : feature::none;
1508 Features |= (IsProcessorFeaturePresent(PF_XMMI64_INSTRUCTIONS_AVAILABLE) != 0) ? (feature::sse2) : feature::none;
1509 Features |= (IsProcessorFeaturePresent(PF_SSE3_INSTRUCTIONS_AVAILABLE) != 0) ? (feature::sse3) : feature::none;
1510 Features |= (IsProcessorFeaturePresent(PF_SSSE3_INSTRUCTIONS_AVAILABLE) != 0) ? (feature::ssse3) : feature::none;
1511 Features |= (IsProcessorFeaturePresent(PF_SSE4_1_INSTRUCTIONS_AVAILABLE) != 0) ? (feature::sse4_1) : feature::none;
1512 Features |= (IsProcessorFeaturePresent(PF_SSE4_2_INSTRUCTIONS_AVAILABLE) != 0) ? (feature::sse4_1) : feature::none;
1513 Features |= (IsProcessorFeaturePresent(PF_AVX_INSTRUCTIONS_AVAILABLE) != 0) ? (feature::avx | feature::xsave) : feature::none;
1514 Features |= (IsProcessorFeaturePresent(PF_AVX2_INSTRUCTIONS_AVAILABLE) != 0) ? (feature::avx2 | feature::fma | feature::bmi1) : feature::none;
1515 Modes |= (IsProcessorFeaturePresent(PF_XMMI_INSTRUCTIONS_AVAILABLE) != 0) ? (mode::xmm128sse) : mode::base;
1516 Modes |= (IsProcessorFeaturePresent(PF_XMMI64_INSTRUCTIONS_AVAILABLE) != 0) ? (mode::xmm128sse) : mode::base;
1517 Modes |= (IsProcessorFeaturePresent(PF_SSE3_INSTRUCTIONS_AVAILABLE) != 0) ? (mode::xmm128sse) : mode::base;
1518 Modes |= (IsProcessorFeaturePresent(PF_SSSE3_INSTRUCTIONS_AVAILABLE) != 0) ? (mode::xmm128sse) : mode::base;
1519 Modes |= (IsProcessorFeaturePresent(PF_SSE4_1_INSTRUCTIONS_AVAILABLE) != 0) ? (mode::xmm128sse) : mode::base;
1520 Modes |= (IsProcessorFeaturePresent(PF_SSE4_2_INSTRUCTIONS_AVAILABLE) != 0) ? (mode::xmm128sse) : mode::base;
1521 Modes |= (IsProcessorFeaturePresent(PF_AVX_INSTRUCTIONS_AVAILABLE) != 0) ? (mode::xmm128sse | mode::ymm256avx) : mode::base;
1522 Modes |= (IsProcessorFeaturePresent(PF_AVX2_INSTRUCTIONS_AVAILABLE) != 0) ? (mode::xmm128sse | mode::ymm256avx) : mode::base;
1523 // clang-format on
1524 #endif
1525
1526 #elif MPT_OS_DJGPP
1527
1528 const uint8 cpu_level = detect_cpu_level();
1529 Features |= (cpu_level >= 3) ? featureset::intel386 : feature::none;
1530 Features |= (cpu_level >= 4) ? featureset::intel486SX : feature::none;
1531 const uint8 fpu_level = detect_fpu_level();
1532 Features |= (fpu_level >= 2) ? feature::fpu : feature::none;
1533 Features |= (fpu_level >= 3) ? feature::fsin : feature::none;
1534
1535 #endif
1536
1537 #endif // MPT_ARCH_X86 || MPT_ARCH_AMD64
1538 }
1539 };
1540
1541
1542
1543 struct floating_point {
1544
1545 public:
1546
1547 static inline constexpr uint16 FCW_IM = (1 << 0);
1548 static inline constexpr uint16 FCW_DM = (1 << 1);
1549 static inline constexpr uint16 FCW_ZM = (1 << 2);
1550 static inline constexpr uint16 FCW_OM = (1 << 3);
1551 static inline constexpr uint16 FCW_UM = (1 << 4);
1552 static inline constexpr uint16 FCW_PM = (1 << 5);
1553 static inline constexpr uint16 FCW_PC = 0x0300;
1554 static inline constexpr uint16 FCW_RC = 0x0c00;
1555 static inline constexpr uint16 FCW_X = (1 << 12);
1556
1557 static inline constexpr uint32 MXCSR_IE = (1 << 0);
1558 static inline constexpr uint32 MXCSR_DE = (1 << 1);
1559 static inline constexpr uint32 MXCSR_ZE = (1 << 2);
1560 static inline constexpr uint32 MXCSR_OE = (1 << 3);
1561 static inline constexpr uint32 MXCSR_UE = (1 << 4);
1562 static inline constexpr uint32 MXCSR_PE = (1 << 5);
1563 static inline constexpr uint32 MXCSR_DAZ = (1 << 6);
1564 static inline constexpr uint32 MXCSR_IM = (1 << 7);
1565 static inline constexpr uint32 MXCSR_DM = (1 << 8);
1566 static inline constexpr uint32 MXCSR_ZM = (1 << 9);
1567 static inline constexpr uint32 MXCSR_OM = (1 << 10);
1568 static inline constexpr uint32 MXCSR_UM = (1 << 11);
1569 static inline constexpr uint32 MXCSR_PM = (1 << 12);
1570 static inline constexpr uint32 MXCSR_RC = (1 << 13) | (1 << 14);
1571 static inline constexpr uint32 MXCSR_FTZ = (1 << 15);
1572
1573 enum class precision : uint8 {
1574 single24 = 0,
1575 reserved = 1,
1576 double53 = 2,
1577 extended64 = 3,
1578 };
1579
1580 enum class rounding : uint8 {
1581 nearest = 0,
1582 down = 1,
1583 up = 2,
1584 zero = 3,
1585 };
1586
1587 struct alignas(16) fxsave_state {
1588 uint16 fcw;
1589 uint16 fsw;
1590 uint16 ftw;
1591 uint16 fop;
1592 uint32 fip;
1593 uint32 fcs;
1594 uint32 foo;
1595 uint32 fos;
1596 uint32 mxcsr;
1597 uint32 mxcsr_mask;
1598 uint32 st_space[32];
1599 uint32 xmm_space[32];
1600 uint8 padding[224];
1601 };
1602 static_assert(sizeof(fxsave_state) == 512);
1603
1604 struct control_state {
1605 uint8 x87_level = 0;
1606 uint16 x87fcw = 0; // default 0x37f (glibc) / 0x27f (msvc)
1607 uint32 mxcsr_mask = 0;
1608 uint32 mxcsr = 0; // default: 0x00001f80
1609 };
1610
1611 #if MPT_ARCH_X86
1612
1613 #if MPT_COMPILER_MSVC
1614
1615 [[nodiscard]] static MPT_FORCEINLINE uint16 get_x87fcw() noexcept {
1616 uint16 tmp = 0;
1617 // clang-format off
1618 _asm {
1619 fwait
1620 fnstcw tmp
1621 }
1622 // clang-format on
1623 return tmp;
1624 }
1625
1626 static MPT_FORCEINLINE void set_x87fcw(uint16 fcw) noexcept {
1627 // clang-format off
1628 _asm {
1629 fldcw fcw
1630 }
1631 // clang-format on
1632 }
1633
1634 [[nodiscard]] static MPT_FORCEINLINE uint32 get_mxcsr() noexcept {
1635 return _mm_getcsr();
1636 }
1637
1638 static MPT_FORCEINLINE void set_mxcsr(uint32 csr) noexcept {
1639 _mm_setcsr(csr);
1640 }
1641
1642 static MPT_FORCEINLINE void fxsave(fxsave_state * state) noexcept {
1643 _fxsave(state);
1644 }
1645
1646 static MPT_FORCEINLINE void fxrstor(const fxsave_state * state) noexcept {
1647 _fxrstor(state);
1648 }
1649
1650 #elif MPT_COMPILER_GCC || MPT_COMPILER_CLANG
1651
1652 [[nodiscard]] static MPT_FORCEINLINE uint16 get_x87fcw() noexcept {
1653 typedef unsigned int fpu_control_t __attribute__((__mode__(__HI__)));
1654 fpu_control_t tmp = 0;
1655 // clang-format off
1656 __asm__ __volatile__("fwait" "\n" "fnstcw %0" : "=m" (*&tmp));
1657 // clang-format on
1658 return static_cast<uint16>(tmp);
1659 }
1660
1661 static MPT_FORCEINLINE void set_x87fcw(uint16 fcw) noexcept {
1662 typedef unsigned int fpu_control_t __attribute__((__mode__(__HI__)));
1663 fpu_control_t tmp = fcw;
1664 // clang-format off
1665 __asm__ __volatile__("fldcw %0" : : "m" (*&tmp));
1666 // clang-format on
1667 }
1668
1669 [[nodiscard]] static MPT_FORCEINLINE uint32 get_mxcsr() noexcept {
1670 #ifdef MPT_ARCH_X86_SSE
1671 return __builtin_ia32_stmxcsr();
1672 #else
1673 uint32 csr = 0;
1674 // clang-format off
1675 __asm__ __volatile__("stmxcsr %0" : "=m" (csr));
1676 // clang-format on
1677 return csr;
1678 #endif
1679 }
1680
1681 static MPT_FORCEINLINE void set_mxcsr(uint32 csr) noexcept {
1682 #ifdef MPT_ARCH_X86_SSE
1683 #if MPT_COMPILER_GCC
1684 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55752
1685 std::atomic_thread_fence(std::memory_order_seq_cst);
1686 #endif
1687 __builtin_ia32_ldmxcsr(csr);
1688 #if MPT_COMPILER_GCC
1689 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55752
1690 std::atomic_thread_fence(std::memory_order_seq_cst);
1691 #endif
1692 #else
1693 // clang-format off
1694 __asm__ __volatile__("ldmxcsr %0" : : "m" (csr));
1695 // clang-format on
1696 #endif
1697 }
1698
1699 static MPT_FORCEINLINE void fxsave(fxsave_state * state) noexcept {
1700 #ifdef MPT_ARCH_X86_FXSR
1701 __builtin_ia32_fxsave(state);
1702 #else
1703 // clang-format off
1704 __asm__ __volatile__("fxsave %0" : : "m" (*state));
1705 // clang-format on
1706 #endif
1707 }
1708
1709 static MPT_FORCEINLINE void fxrstor(const fxsave_state * state) noexcept {
1710 #ifdef MPT_ARCH_X86_FXSR
1711 __builtin_ia32_fxrstor(const_cast<fxsave_state *>(state));
1712 #else
1713 // clang-format off
1714 __asm__ __volatile__("fxrstor %0" : : "m" (*state));
1715 // clang-format on
1716 #endif
1717 }
1718
1719 #endif // MPT_COMPILER
1720
1721 static MPT_FORCEINLINE bool have_fxsr() noexcept {
1722 #ifdef MPT_ARCH_X86_FXSR
1723 return true;
1724 #else
1725 return cpu_info{}[mpt::arch::x86::feature::fxsr];
1726 #endif
1727 }
1728
1729 static MPT_FORCEINLINE bool have_sse() noexcept {
1730 #ifdef MPT_ARCH_X86_SSE
1731 return true;
1732 #else
1733 const cpu_info cpu_info;
1734 return cpu_info[mpt::arch::x86::feature::sse] && cpu_info[mpt::arch::x86::mode::xmm128sse];
1735 #endif
1736 }
1737
1738 static MPT_FORCEINLINE uint8 get_fpu_level() noexcept {
1739 #ifdef MPT_ARCH_X86_FSIN
1740 return 3;
1741 #elif defined(MPT_ARCH_X86_FPU)
1742 return cpu_info{}[mpt::arch::x86::feature::fsin] ? 3 : 2;
1743 #else
1744 cpu_info tmp{};
1745 return tmp[mpt::arch::x86::feature::fsin] ? 3 : tmp[mpt::arch::x86::feature::fpu] ? 2
1746 : 0;
1747 #endif
1748 }
1749
1750 static MPT_FORCEINLINE control_state get_state() noexcept {
1751 control_state result;
1752 #ifdef MPT_ARCH_X86_FXSR
1753 fxsave_state tmp = {};
1754 fxsave(&tmp);
1755 result.x87_level = 3;
1756 result.x87fcw = tmp.fcw;
1757 result.mxcsr_mask = tmp.mxcsr_mask;
1758 result.mxcsr = tmp.mxcsr;
1759 #else
1760 if (have_fxsr()) {
1761 fxsave_state tmp = {};
1762 fxsave(&tmp);
1763 result.x87_level = 3;
1764 result.x87fcw = tmp.fcw;
1765 result.mxcsr_mask = tmp.mxcsr_mask;
1766 result.mxcsr = tmp.mxcsr;
1767 } else {
1768 result.x87_level = get_fpu_level();
1769 if (result.x87_level > 0) {
1770 result.x87fcw = get_x87fcw();
1771 }
1772 }
1773 #endif
1774 return result;
1775 }
1776
1777 static MPT_FORCEINLINE void set_state(control_state state) noexcept {
1778 #ifdef MPT_ARCH_X86_SSE
1779 if (state.x87_level) {
1780 set_x87fcw(state.x87fcw);
1781 }
1782 if (state.mxcsr_mask) {
1783 set_mxcsr(state.mxcsr);
1784 }
1785 #else
1786 if (have_sse()) {
1787 if (state.x87_level) {
1788 set_x87fcw(state.x87fcw);
1789 }
1790 if (state.mxcsr_mask) {
1791 set_mxcsr(state.mxcsr);
1792 }
1793 } else {
1794 #ifdef MPT_ARCH_X86_FXSR
1795 fxsave_state tmp = {};
1796 fxsave(&tmp);
1797 if (state.x87_level) {
1798 tmp.fcw = state.x87fcw;
1799 }
1800 if (state.mxcsr_mask) {
1801 tmp.mxcsr = state.mxcsr;
1802 }
1803 fxrstor(&tmp);
1804 #else
1805 if (have_fxsr()) {
1806 fxsave_state tmp = {};
1807 fxsave(&tmp);
1808 if (state.x87_level) {
1809 tmp.fcw = state.x87fcw;
1810 }
1811 if (state.mxcsr_mask) {
1812 tmp.mxcsr = state.mxcsr;
1813 }
1814 fxrstor(&tmp);
1815 } else {
1816 if (state.x87_level) {
1817 set_x87fcw(state.x87fcw);
1818 }
1819 }
1820 #endif
1821 }
1822 #endif
1823 }
1824
1825 #elif MPT_ARCH_AMD64
1826
1827 #if MPT_COMPILER_MSVC
1828
1829 [[nodiscard]] static MPT_FORCEINLINE uint16 get_x87fcw() noexcept {
1830 fxsave_state state = {};
1831 fxsave(&state);
1832 return state.fcw;
1833 }
1834
1835 static MPT_FORCEINLINE void set_x87fcw(uint16 fcw) noexcept {
1836 fxsave_state state = {};
1837 fxsave(&state);
1838 state.fcw = fcw;
1839 fxrstor(&state);
1840 }
1841
1842 [[nodiscard]] static MPT_FORCEINLINE uint32 get_mxcsr() noexcept {
1843 return _mm_getcsr();
1844 }
1845
1846 static MPT_FORCEINLINE void set_mxcsr(uint32 csr) noexcept {
1847 _mm_setcsr(csr);
1848 }
1849
1850 static MPT_FORCEINLINE void fxsave(fxsave_state * state) noexcept {
1851 _fxsave(state);
1852 }
1853
1854 static MPT_FORCEINLINE void fxrstor(const fxsave_state * state) noexcept {
1855 _fxrstor(state);
1856 }
1857
1858 static MPT_FORCEINLINE bool have_fxsr() noexcept {
1859 return true;
1860 }
1861
1862 static MPT_FORCEINLINE control_state get_state() noexcept {
1863 control_state result;
1864 fxsave_state tmp = {};
1865 fxsave(&tmp);
1866 result.x87_level = 3;
1867 result.x87fcw = tmp.fcw;
1868 result.mxcsr_mask = tmp.mxcsr_mask;
1869 result.mxcsr = tmp.mxcsr;
1870 return result;
1871 }
1872
1873 static MPT_FORCEINLINE void set_state(control_state state) noexcept {
1874 fxsave_state tmp = {};
1875 fxsave(&tmp);
1876 tmp.fcw = state.x87fcw;
1877 tmp.mxcsr_mask = state.mxcsr_mask;
1878 tmp.mxcsr = state.mxcsr;
1879 fxrstor(&tmp);
1880 }
1881
1882 #elif MPT_COMPILER_GCC || MPT_COMPILER_CLANG
1883
1884 [[nodiscard]] static MPT_FORCEINLINE uint16 get_x87fcw() noexcept {
1885 typedef unsigned int fpu_control_t __attribute__((__mode__(__HI__)));
1886 fpu_control_t tmp = 0;
1887 // clang-format off
1888 __asm__ __volatile__("fwait" "\n" "fnstcw %0" : "=m" (*&tmp));
1889 // clang-format on
1890 return static_cast<uint16>(tmp);
1891 }
1892
1893 static MPT_FORCEINLINE void set_x87fcw(uint16 fcw) noexcept {
1894 typedef unsigned int fpu_control_t __attribute__((__mode__(__HI__)));
1895 fpu_control_t tmp = fcw;
1896 // clang-format off
1897 __asm__ __volatile__("fldcw %0" : : "m" (*&tmp));
1898 // clang-format on
1899 }
1900
1901 [[nodiscard]] static MPT_FORCEINLINE uint32 get_mxcsr() noexcept {
1902 return __builtin_ia32_stmxcsr();
1903 }
1904
1905 static MPT_FORCEINLINE void set_mxcsr(uint32 csr) noexcept {
1906 #if MPT_COMPILER_GCC
1907 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55752
1908 std::atomic_thread_fence(std::memory_order_seq_cst);
1909 #endif
1910 __builtin_ia32_ldmxcsr(csr);
1911 #if MPT_COMPILER_GCC
1912 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55752
1913 std::atomic_thread_fence(std::memory_order_seq_cst);
1914 #endif
1915 }
1916
1917 static MPT_FORCEINLINE void fxsave(fxsave_state * state) noexcept {
1918 __builtin_ia32_fxsave(state);
1919 }
1920
1921 static MPT_FORCEINLINE void fxrstor(const fxsave_state * state) noexcept {
1922 __builtin_ia32_fxrstor(const_cast<fxsave_state *>(state));
1923 }
1924
1925 static MPT_FORCEINLINE bool have_fxsr() noexcept {
1926 return true;
1927 }
1928
1929 static MPT_FORCEINLINE control_state get_state() noexcept {
1930 control_state result;
1931 result.x87_level = 3;
1932 result.x87fcw = get_x87fcw();
1933 result.mxcsr_mask = 0x0000'ffff;
1934 result.mxcsr = get_mxcsr();
1935 return result;
1936 }
1937
1938 static MPT_FORCEINLINE void set_state(control_state state) noexcept {
1939 set_x87fcw(state.x87fcw);
1940 set_mxcsr(state.mxcsr);
1941 }
1942
1943 #endif // MPT_COMPILER
1944
1945 class guard {
1946
1947 private:
1948
1949 const control_state m_oldstate;
1950
1951 public:
1952
1953 MPT_FORCEINLINE guard(std::optional<rounding> rounding, std::optional<bool> denormals_as_zero, std::optional<precision> precision, std::optional<bool> infinity_projective) noexcept
1954 : m_oldstate(get_state()) {
1955 control_state state = m_oldstate;
1956 if (rounding) {
1957 if (state.x87_level) {
1958 state.x87fcw = (state.x87fcw & ~FCW_RC) | (mpt::to_underlying(*rounding) << mpt::countr_zero(FCW_RC));
1959 }
1960 if ((state.mxcsr_mask & MXCSR_RC) == MXCSR_RC) {
1961 state.mxcsr = (state.mxcsr & ~MXCSR_RC) | (mpt::to_underlying(*rounding) << mpt::countr_zero(MXCSR_RC));
1962 }
1963 }
1964 if (denormals_as_zero) {
1965 if (state.mxcsr_mask) {
1966 state.mxcsr = (state.mxcsr & ~(MXCSR_FTZ | MXCSR_DAZ)) | ((*denormals_as_zero) ? ((MXCSR_FTZ | MXCSR_DAZ) & state.mxcsr_mask) : 0);
1967 }
1968 }
1969 if (precision) {
1970 if (state.x87_level) {
1971 state.x87fcw = (state.x87fcw & ~FCW_PC) | (mpt::to_underlying(*precision) << mpt::countr_zero(FCW_PC));
1972 }
1973 }
1974 if (infinity_projective) {
1975 if (state.x87_level <= 2) {
1976 state.x87fcw = (state.x87fcw & ~FCW_X) | ((*infinity_projective) ? 0 : FCW_X);
1977 }
1978 }
1979 set_state(state);
1980 }
1981
1982 MPT_FORCEINLINE ~guard() {
1983 set_state(m_oldstate);
1984 }
1985 };
1986
1987 #endif // MPT_ARCH
1988 };
1989
1990
1991
1992 } // namespace x86
1993
1994 namespace amd64 = x86;
1995
1996
1997
1998 } // namespace arch
1999
2000
2001
2002 } // namespace MPT_INLINE_NS
2003 } // namespace mpt
2004
2005
2006
2007 #endif // MPT_ARCH_X86_AMD64_HPP