| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_ARCH_ARCH_HPP |
| 4 |
#define MPT_ARCH_ARCH_HPP |
| 5 |
|
| 6 |
|
| 7 |
#include "mpt/arch/feature_fence.hpp" |
| 8 |
#include "mpt/arch/feature_flags.hpp" |
| 9 |
#include "mpt/arch/x86_amd64.hpp" |
| 10 |
#include "mpt/base/detect.hpp" |
| 11 |
#include "mpt/base/integer.hpp" |
| 12 |
#include "mpt/base/macros.hpp" |
| 13 |
#include "mpt/base/namespace.hpp" |
| 14 |
|
| 15 |
#include <string> |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
namespace mpt { |
| 20 |
inline namespace MPT_INLINE_NS { |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
namespace arch { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
namespace unknown { |
| 29 |
|
| 30 |
template <typename tag> |
| 31 |
struct no_flags { |
| 32 |
constexpr no_flags() noexcept = default; |
| 33 |
constexpr no_flags operator~() noexcept { |
| 34 |
return no_flags{}; |
| 35 |
} |
| 36 |
constexpr no_flags & operator&=(no_flags) noexcept { |
| 37 |
return *this; |
| 38 |
} |
| 39 |
constexpr no_flags & operator|=(no_flags) noexcept { |
| 40 |
return *this; |
| 41 |
} |
| 42 |
constexpr no_flags & operator^=(no_flags) noexcept { |
| 43 |
return *this; |
| 44 |
} |
| 45 |
friend constexpr no_flags operator&(no_flags, no_flags) noexcept { |
| 46 |
return no_flags{}; |
| 47 |
} |
| 48 |
friend constexpr no_flags operator|(no_flags, no_flags) noexcept { |
| 49 |
return no_flags{}; |
| 50 |
} |
| 51 |
friend constexpr no_flags operator^(no_flags, no_flags) noexcept { |
| 52 |
return no_flags{}; |
| 53 |
} |
| 54 |
friend constexpr bool operator==(no_flags, no_flags) noexcept { |
| 55 |
return true; |
| 56 |
} |
| 57 |
friend constexpr bool operator!=(no_flags, no_flags) noexcept { |
| 58 |
return false; |
| 59 |
} |
| 60 |
constexpr bool supports(no_flags) noexcept { |
| 61 |
return true; |
| 62 |
} |
| 63 |
explicit constexpr operator bool() const noexcept { |
| 64 |
return true; |
| 65 |
} |
| 66 |
constexpr bool operator!() const noexcept { |
| 67 |
return false; |
| 68 |
} |
| 69 |
}; |
| 70 |
|
| 71 |
struct no_feature_flags_tag { |
| 72 |
}; |
| 73 |
|
| 74 |
struct no_mode_flags_tag { |
| 75 |
}; |
| 76 |
|
| 77 |
using feature_flags = mpt::arch::basic_feature_flags<no_flags<no_feature_flags_tag>>; |
| 78 |
using mode_flags = mpt::arch::basic_feature_flags<no_flags<no_mode_flags_tag>>; |
| 79 |
|
| 80 |
namespace feature { |
| 81 |
inline constexpr feature_flags none = feature_flags{}; |
| 82 |
} // namespace feature |
| 83 |
|
| 84 |
namespace mode { |
| 85 |
inline constexpr mode_flags none = mode_flags{}; |
| 86 |
} // namespace mode |
| 87 |
|
| 88 |
struct cpu_info { |
| 89 |
public: |
| 90 |
cpu_info() = default; |
| 91 |
public: |
| 92 |
MPT_CONSTEXPRINLINE bool operator[](feature_flags) const noexcept { |
| 93 |
return true; |
| 94 |
} |
| 95 |
MPT_CONSTEXPRINLINE bool has_features(feature_flags) const noexcept { |
| 96 |
return true; |
| 97 |
} |
| 98 |
MPT_CONSTEXPRINLINE feature_flags get_features() const noexcept { |
| 99 |
return {}; |
| 100 |
} |
| 101 |
MPT_CONSTEXPRINLINE bool operator[](mode_flags) const noexcept { |
| 102 |
return true; |
| 103 |
} |
| 104 |
MPT_CONSTEXPRINLINE bool enabled_modes(mode_flags) const noexcept { |
| 105 |
return true; |
| 106 |
} |
| 107 |
MPT_CONSTEXPRINLINE mode_flags get_modes() const noexcept { |
| 108 |
return {}; |
| 109 |
} |
| 110 |
}; |
| 111 |
|
| 112 |
[[nodiscard]] MPT_CONSTEVAL feature_flags assumed_features() noexcept { |
| 113 |
return {}; |
| 114 |
} |
| 115 |
|
| 116 |
[[nodiscard]] MPT_CONSTEVAL mode_flags assumed_modes() noexcept { |
| 117 |
return {}; |
| 118 |
} |
| 119 |
|
| 120 |
} // namespace unknown |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
#if MPT_ARCH_X86 |
| 125 |
namespace current = x86; |
| 126 |
#elif MPT_ARCH_AMD64 |
| 127 |
namespace current = amd64; |
| 128 |
#else |
| 129 |
namespace current = unknown; |
| 130 |
#endif |
| 131 |
|
| 132 |
using cpu_info = mpt::arch::current::cpu_info; |
| 133 |
|
| 134 |
inline const cpu_info & get_cpu_info() { |
| 135 |
static cpu_info info; |
| 136 |
return info; |
| 137 |
} |
| 138 |
|
| 139 |
namespace detail { |
| 140 |
|
| 141 |
struct info_initializer { |
| 142 |
inline info_initializer() noexcept { |
| 143 |
get_cpu_info(); |
| 144 |
} |
| 145 |
}; |
| 146 |
|
| 147 |
#if MPT_COMPILER_CLANG |
| 148 |
#pragma clang diagnostic push |
| 149 |
#pragma clang diagnostic ignored "-Wglobal-constructors" |
| 150 |
#endif // MPT_COMPILER_CLANG |
| 151 |
inline info_initializer g_info_initializer; |
| 152 |
#if MPT_COMPILER_CLANG |
| 153 |
#pragma clang diagnostic pop |
| 154 |
#endif // MPT_COMPILER_CLANG |
| 155 |
|
| 156 |
|
| 157 |
} // namespace detail |
| 158 |
|
| 159 |
struct flags_cache { |
| 160 |
private: |
| 161 |
const mpt::arch::current::feature_flags Features; |
| 162 |
const mpt::arch::current::mode_flags Modes; |
| 163 |
public: |
| 164 |
MPT_CONSTEXPRINLINE flags_cache(const mpt::arch::cpu_info & info) noexcept |
| 165 |
: Features(info.get_features()) |
| 166 |
, Modes(info.get_modes()) { |
| 167 |
return; |
| 168 |
} |
| 169 |
[[nodiscard]] MPT_CONSTEXPRINLINE bool operator[](mpt::arch::current::feature_flags query_features) const noexcept { |
| 170 |
return ((Features & query_features) == query_features); |
| 171 |
} |
| 172 |
[[nodiscard]] MPT_CONSTEXPRINLINE bool has_features(mpt::arch::current::feature_flags query_features) const noexcept { |
| 173 |
return ((Features & query_features) == query_features); |
| 174 |
} |
| 175 |
[[nodiscard]] MPT_CONSTEXPRINLINE mpt::arch::current::feature_flags get_features() const noexcept { |
| 176 |
return Features; |
| 177 |
} |
| 178 |
[[nodiscard]] MPT_CONSTEXPRINLINE bool operator[](mpt::arch::current::mode_flags query_modes) const noexcept { |
| 179 |
return ((Modes & query_modes) == query_modes); |
| 180 |
} |
| 181 |
[[nodiscard]] MPT_CONSTEXPRINLINE bool has_features(mpt::arch::current::mode_flags query_modes) const noexcept { |
| 182 |
return ((Modes & query_modes) == query_modes); |
| 183 |
} |
| 184 |
[[nodiscard]] MPT_CONSTEXPRINLINE mpt::arch::current::mode_flags get_modes() const noexcept { |
| 185 |
return Modes; |
| 186 |
} |
| 187 |
}; |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
} // namespace arch |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
} // namespace MPT_INLINE_NS |
| 196 |
} // namespace mpt |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
#endif // MPT_ARCH_ARCH_HPP |