Debian Codegraph @

common/mptCPU.h

raw
1 /*
2 * mptCPU.h
3 * --------
4 * Purpose: CPU feature detection.
5 * Notes : (currently none)
6 * Authors: OpenMPT Devs
7 * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
8 */
9
10
11 #pragma once
12
13 #include "openmpt/all/BuildSettings.hpp"
14
15 #include "mpt/base/macros.hpp"
16 #ifdef MPT_ENABLE_ARCH_INTRINSICS
17 #include "mpt/arch/arch.hpp"
18 #endif
19
20
21 OPENMPT_NAMESPACE_BEGIN
22
23
24 namespace CPU
25 {
26
27
28 #ifdef MPT_ENABLE_ARCH_INTRINSICS
29
30
31 #if defined(MODPLUG_TRACKER) && !defined(MPT_BUILD_WINESUPPORT)
32
33
34 namespace detail
35 {
36
37 inline MPT_CONSTINIT mpt::arch::current::feature_flags EnabledFeatures;
38 inline MPT_CONSTINIT mpt::arch::current::mode_flags EnabledModes;
39
40 } // namespace detail
41
42 inline void EnableAvailableFeatures() noexcept
43 {
44 CPU::detail::EnabledFeatures = mpt::arch::get_cpu_info().get_features();
45 CPU::detail::EnabledModes = mpt::arch::get_cpu_info().get_modes();
46 }
47
48 // enabled processor features for inline asm and intrinsics
49 [[nodiscard]] MPT_FORCEINLINE mpt::arch::current::feature_flags GetEnabledFeatures() noexcept
50 {
51 return CPU::detail::EnabledFeatures;
52 }
53 [[nodiscard]] MPT_FORCEINLINE mpt::arch::current::mode_flags GetEnabledModes() noexcept
54 {
55 return CPU::detail::EnabledModes;
56 }
57
58 struct Info
59 {
60 [[nodiscard]] MPT_FORCEINLINE bool HasFeatureSet(mpt::arch::current::feature_flags features) const noexcept
61 {
62 return features == (GetEnabledFeatures() & features);
63 }
64 [[nodiscard]] MPT_FORCEINLINE bool HasModesEnabled(mpt::arch::current::mode_flags modes) const noexcept
65 {
66 return modes == (GetEnabledModes() & modes);
67 }
68 };
69
70
71 #else // !MODPLUG_TRACKER
72
73
74 struct Info
75 {
76 private:
77 const mpt::arch::flags_cache m_flags{mpt::arch::get_cpu_info()};
78 public:
79 [[nodiscard]] MPT_FORCEINLINE bool HasFeatureSet(mpt::arch::current::feature_flags features) const noexcept
80 {
81 return m_flags[features];
82 }
83 [[nodiscard]] MPT_FORCEINLINE bool HasModesEnabled(mpt::arch::current::mode_flags modes) const noexcept
84 {
85 return m_flags[modes];
86 }
87 };
88
89
90 #endif // MODPLUG_TRACKER
91
92
93 namespace feature = mpt::arch::current::feature;
94 namespace mode = mpt::arch::current::mode;
95
96 [[nodiscard]] MPT_FORCEINLINE bool HasFeatureSet(mpt::arch::current::feature_flags features) noexcept
97 {
98 return CPU::Info{}.HasFeatureSet(features);
99 }
100
101 [[nodiscard]] MPT_FORCEINLINE bool HasModesEnabled(mpt::arch::current::mode_flags modes) noexcept
102 {
103 return CPU::Info{}.HasModesEnabled(modes);
104 }
105
106
107 #endif // MPT_ENABLE_ARCH_INTRINSICS
108
109
110 } // namespace CPU
111
112
113 OPENMPT_NAMESPACE_END