native 0.0.1
Vectors, masks and wide register packs for C++26
Loading...
Searching...
No Matches
native.x86.features.ccm
1// SPDX-FileCopyrightText: 2024 Edward Kmett <ekmett@gmail.com>
2// SPDX-License-Identifier: BSD-2-Clause OR Apache-2.0
3module;
4#include <array>
5#include <cstdint>
6#include <string_view>
7#include <hint.h>
8#if defined(_MSC_VER)
9#include <intrin.h>
10#else
11#include <cpuid.h>
12#endif
13
14import native.isa;
15
16namespace native {
22
25 struct [[nodiscard]] cpuid_t {
26 std::int32_t eax, ebx, ecx, edx;
27 };
28
34 cpuid_t cpuid(std::int32_t eax, std::int32_t ecx) noexcept;
35
39 enum class [[nodiscard]] cpu_vendor : std::uint8_t {
40 intel = 0,
41 amd = 1,
43 };
44
45
54 std::uint64_t xcr0 = 0;
56 bool xcr0_observed = false;
59 std::uint32_t max_basic_leaf = 0;
60 std::uint32_t leaf1_ecx = 0, leaf1_edx = 0, leaf7_ebx = 0;
63 std::uint32_t max_leaf7_subleaf = 0;
66 std::uint32_t leaf7_1_eax = 0;
68 std::uint32_t leaf7_edx = 0;
72 std::uint32_t leaf7_ecx = 0;
75 std::uint32_t max_extended_leaf = 0, extended1_ecx = 0;
78 std::uint32_t leaf7_1_edx = 0;
79 };
80
83 std::array<char,13> vendor_id{};
86 };
87
90
92 extern const enum cpu_vendor cpu_vendor;
93}
94
95namespace native {
96 namespace detail {
97 constexpr std::array<char,13> vendor_identifier(cpuid_t leaf) noexcept {
98 std::array<char,13> result{};
99 std::uint32_t words[]{std::uint32_t(leaf.ebx),std::uint32_t(leaf.edx),std::uint32_t(leaf.ecx)};
100 for(unsigned word=0;word!=3;++word)
101 for(unsigned byte=0;byte!=4;++byte)
102 result[4*word+byte]=char((words[word]>>(8*byte))&0xff);
103 return result;
104 }
105 constexpr enum cpu_vendor identify_vendor(std::array<char,13> const & id) noexcept {
106 std::string_view name{id.data(),12};
107 if(name=="GenuineIntel") return cpu_vendor::intel;
108 if(name=="AuthenticAMD") return cpu_vendor::amd;
109 return cpu_vendor::unknown;
110 }
111 static_assert(identify_vendor(vendor_identifier({0,0x756e6547,0x6c65746e,0x49656e69}))==cpu_vendor::intel);
112 static_assert(identify_vendor(vendor_identifier({0,0x68747541,0x444d4163,0x69746e65}))==cpu_vendor::amd);
113 static_assert(identify_vendor(vendor_identifier({}))==cpu_vendor::unknown);
114 }
115
116 cpuid_t cpuid(std::int32_t eax, std::int32_t ecx) noexcept {
117#if defined(_MSC_VER)
118 int registers[4];
119 __cpuidex(registers, eax, ecx);
120 return {registers[0], registers[1], registers[2], registers[3]};
121#else
122 cpuid_t result;
123 __cpuid_count(eax, ecx, result.eax, result.ebx, result.ecx, result.edx);
124 return result;
125#endif
126 }
127
128
130 x86_capabilities result;
131 auto identity=cpuid(0,0);
132 result.raw.max_basic_leaf = std::uint32_t(identity.eax);
133 result.vendor_id=detail::vendor_identifier(identity);
134 result.vendor=detail::identify_vendor(result.vendor_id);
135 if (result.raw.max_basic_leaf >= 1) {
136 auto leaf = cpuid(1, 0);
137 result.raw.leaf1_ecx = std::uint32_t(leaf.ecx);
138 result.raw.leaf1_edx = std::uint32_t(leaf.edx);
139 constexpr auto xsave = (1u << 26) | (1u << 27);
140 if ((result.raw.leaf1_ecx & xsave) == xsave) {
141#if defined(_MSC_VER)
142 result.xcr0 = _xgetbv(0);
143#else
144 std::uint32_t low, high;
145 __asm__("xgetbv" : "=a"(low), "=d"(high) : "c"(0));
146 result.xcr0 = std::uint64_t(low) | (std::uint64_t(high) << 32);
147#endif
148 result.xcr0_observed = true;
149 }
150 }
151 if (result.raw.max_basic_leaf >= 7) {
152 auto leaf = cpuid(7, 0);
153 result.raw.leaf7_ebx = std::uint32_t(leaf.ebx);
154 result.raw.leaf7_ecx = std::uint32_t(leaf.ecx);
155 result.raw.leaf7_edx = std::uint32_t(leaf.edx);
156 result.raw.max_leaf7_subleaf = std::uint32_t(leaf.eax);
157 if (result.raw.max_leaf7_subleaf >= 1) {
158 auto subleaf = cpuid(7, 1);
159 result.raw.leaf7_1_eax = std::uint32_t(subleaf.eax);
160 result.raw.leaf7_1_edx = std::uint32_t(subleaf.edx);
161 }
162 }
163 result.raw.max_extended_leaf=std::uint32_t(cpuid(std::int32_t(0x80000000u),0).eax);
164 if(result.raw.max_extended_leaf>=0x80000001u)
165 result.raw.extended1_ecx=std::uint32_t(cpuid(std::int32_t(0x80000001u),0).ecx);
166 auto features=detail::decode_x86_features(result.raw);
167 result.present=features.present;
168 result.observed=features.observed;
169 return result;
170 }
171
172 const enum cpu_vendor cpu_vendor = [] static noexcept {
173 return detail::identify_vendor(detail::vendor_identifier(cpuid(0,0)));
174 }();
175}
176
177export module native.x86.features;
178export import native.isa;
179
180export namespace native {
181 using ::native::cpuid_t;
182 using ::native::cpuid;
183 using ::native::cpu_vendor;
184 using ::native::x86_capabilities;
186}
cpuid_t cpuid(std::int32_t eax, std::int32_t ecx) noexcept
Architecture-tagged vectors, register packs and supporting value types. Native arithmetic follows its...
x86_capabilities observe_x86_capabilities() noexcept
Capture CPUID and, only with XSAVE and OSXSAVE, XCR0. No ISA profile required.
cpu_vendor
Recognized CPUID vendor strings; vendor identity alone does not imply features.
@ intel
CPUID reports GenuineIntel.
@ amd
CPUID reports AuthenticAMD.
@ unknown
Any other vendor string.
A structural set of one family's features, without prerequisite closure.
Definition isa.h:65
CPUID query inputs retained for diagnostics, not a second admission source.
std::uint32_t leaf7_edx
CPUID.7.0 EDX; bit 23 reports AVX512_FP16. Zero without leaf 7.
enum cpu_vendor vendor
Classification of vendor_id; it does not establish instruction support.
std::array< char, 13 > vendor_id
Exact CPUID vendor identifier, followed by a zero terminator.
std::uint64_t xcr0
OS-enabled vector state. Used only when xcr0_observed is true.
isa< x86 > present
Features whose queries succeeded and reported support; no compiler closure.
bool xcr0_observed
True after XSAVE/OSXSAVE permit XGETBV and its result was captured.
raw_observations raw
Diagnostic snapshot; editing it does not update present/observed or XCR0.
isa< x86 > observed
Features whose CPUID leaves were available, whether present or absent.