ycpp 0.1.3__cp312-abi3-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (142) hide show
  1. ycpp/__init__.py +110 -0
  2. ycpp/_array.py +318 -0
  3. ycpp/_awareness.py +268 -0
  4. ycpp/_doc.py +718 -0
  5. ycpp/_event.py +188 -0
  6. ycpp/_id_map.py +620 -0
  7. ycpp/_map.py +267 -0
  8. ycpp/_provider.py +180 -0
  9. ycpp/_snapshot.py +28 -0
  10. ycpp/_sticky_index.py +59 -0
  11. ycpp/_stream.py +91 -0
  12. ycpp/_sync.py +192 -0
  13. ycpp/_text.py +239 -0
  14. ycpp/_transaction.py +287 -0
  15. ycpp/_typed.py +122 -0
  16. ycpp/_undo.py +201 -0
  17. ycpp/_update.py +18 -0
  18. ycpp/_xml.py +452 -0
  19. ycpp/_ycpp.lib +0 -0
  20. ycpp/_ycpp.pyd +0 -0
  21. ycpp-0.1.3.data/headers/fast_float/ascii_number.h +766 -0
  22. ycpp-0.1.3.data/headers/fast_float/bigint.h +638 -0
  23. ycpp-0.1.3.data/headers/fast_float/constexpr_feature_detect.h +53 -0
  24. ycpp-0.1.3.data/headers/fast_float/decimal_to_binary.h +212 -0
  25. ycpp-0.1.3.data/headers/fast_float/digit_comparison.h +455 -0
  26. ycpp-0.1.3.data/headers/fast_float/fast_float.h +91 -0
  27. ycpp-0.1.3.data/headers/fast_float/fast_table.h +708 -0
  28. ycpp-0.1.3.data/headers/fast_float/float_common.h +1432 -0
  29. ycpp-0.1.3.data/headers/fast_float/parse_number.h +488 -0
  30. ycpp-0.1.3.data/headers/xsimd/arch/common/xsimd_common_arithmetic.hpp +267 -0
  31. ycpp-0.1.3.data/headers/xsimd/arch/common/xsimd_common_bit.hpp +230 -0
  32. ycpp-0.1.3.data/headers/xsimd/arch/common/xsimd_common_cast.hpp +43 -0
  33. ycpp-0.1.3.data/headers/xsimd/arch/common/xsimd_common_complex.hpp +112 -0
  34. ycpp-0.1.3.data/headers/xsimd/arch/common/xsimd_common_details.hpp +386 -0
  35. ycpp-0.1.3.data/headers/xsimd/arch/common/xsimd_common_logical.hpp +240 -0
  36. ycpp-0.1.3.data/headers/xsimd/arch/common/xsimd_common_math.hpp +2601 -0
  37. ycpp-0.1.3.data/headers/xsimd/arch/common/xsimd_common_memory.hpp +1006 -0
  38. ycpp-0.1.3.data/headers/xsimd/arch/common/xsimd_common_rounding.hpp +72 -0
  39. ycpp-0.1.3.data/headers/xsimd/arch/common/xsimd_common_swizzle.hpp +260 -0
  40. ycpp-0.1.3.data/headers/xsimd/arch/common/xsimd_common_trigo.hpp +996 -0
  41. ycpp-0.1.3.data/headers/xsimd/arch/utils/shifts.hpp +82 -0
  42. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_avx.hpp +2111 -0
  43. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_avx2.hpp +1591 -0
  44. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_avx512bw.hpp +724 -0
  45. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_avx512cd.hpp +28 -0
  46. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_avx512dq.hpp +270 -0
  47. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_avx512er.hpp +17 -0
  48. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_avx512f.hpp +2919 -0
  49. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_avx512ifma.hpp +17 -0
  50. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_avx512pf.hpp +17 -0
  51. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_avx512vbmi.hpp +76 -0
  52. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_avx512vbmi2.hpp +130 -0
  53. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_avx512vnni_avx512bw.hpp +17 -0
  54. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_avx512vnni_avx512vbmi2.hpp +17 -0
  55. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_avxvnni.hpp +17 -0
  56. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_common.hpp +26 -0
  57. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_common_fwd.hpp +127 -0
  58. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_constants.hpp +393 -0
  59. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_emulated.hpp +787 -0
  60. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_fma3_avx.hpp +93 -0
  61. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_fma3_avx2.hpp +46 -0
  62. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_fma3_sse.hpp +97 -0
  63. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_fma4.hpp +92 -0
  64. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_i8mm_neon64.hpp +17 -0
  65. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_isa.hpp +146 -0
  66. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_neon.hpp +3580 -0
  67. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_neon64.hpp +1614 -0
  68. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_rvv.hpp +1515 -0
  69. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_scalar.hpp +1210 -0
  70. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_sse2.hpp +2333 -0
  71. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_sse3.hpp +66 -0
  72. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_sse4_1.hpp +510 -0
  73. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_sse4_2.hpp +44 -0
  74. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_ssse3.hpp +186 -0
  75. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_sve.hpp +1186 -0
  76. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_vsx.hpp +916 -0
  77. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_vxe.hpp +787 -0
  78. ycpp-0.1.3.data/headers/xsimd/arch/xsimd_wasm.hpp +1819 -0
  79. ycpp-0.1.3.data/headers/xsimd/config/xsimd_arch.hpp +242 -0
  80. ycpp-0.1.3.data/headers/xsimd/config/xsimd_config.hpp +620 -0
  81. ycpp-0.1.3.data/headers/xsimd/config/xsimd_cpu_features.hpp +50 -0
  82. ycpp-0.1.3.data/headers/xsimd/config/xsimd_cpu_features_arm.hpp +132 -0
  83. ycpp-0.1.3.data/headers/xsimd/config/xsimd_cpu_features_ppc.hpp +54 -0
  84. ycpp-0.1.3.data/headers/xsimd/config/xsimd_cpu_features_riscv.hpp +95 -0
  85. ycpp-0.1.3.data/headers/xsimd/config/xsimd_cpu_features_s390x.hpp +56 -0
  86. ycpp-0.1.3.data/headers/xsimd/config/xsimd_cpu_features_x86.hpp +1044 -0
  87. ycpp-0.1.3.data/headers/xsimd/config/xsimd_cpuid.hpp +144 -0
  88. ycpp-0.1.3.data/headers/xsimd/config/xsimd_getauxval.hpp +159 -0
  89. ycpp-0.1.3.data/headers/xsimd/config/xsimd_macros.hpp +76 -0
  90. ycpp-0.1.3.data/headers/xsimd/math/xsimd_rem_pio2.hpp +707 -0
  91. ycpp-0.1.3.data/headers/xsimd/memory/xsimd_aligned_allocator.hpp +349 -0
  92. ycpp-0.1.3.data/headers/xsimd/memory/xsimd_alignment.hpp +102 -0
  93. ycpp-0.1.3.data/headers/xsimd/types/xsimd_all_registers.hpp +57 -0
  94. ycpp-0.1.3.data/headers/xsimd/types/xsimd_api.hpp +3106 -0
  95. ycpp-0.1.3.data/headers/xsimd/types/xsimd_avx2_register.hpp +44 -0
  96. ycpp-0.1.3.data/headers/xsimd/types/xsimd_avx512bw_register.hpp +51 -0
  97. ycpp-0.1.3.data/headers/xsimd/types/xsimd_avx512cd_register.hpp +51 -0
  98. ycpp-0.1.3.data/headers/xsimd/types/xsimd_avx512dq_register.hpp +51 -0
  99. ycpp-0.1.3.data/headers/xsimd/types/xsimd_avx512er_register.hpp +51 -0
  100. ycpp-0.1.3.data/headers/xsimd/types/xsimd_avx512f_register.hpp +77 -0
  101. ycpp-0.1.3.data/headers/xsimd/types/xsimd_avx512ifma_register.hpp +51 -0
  102. ycpp-0.1.3.data/headers/xsimd/types/xsimd_avx512pf_register.hpp +51 -0
  103. ycpp-0.1.3.data/headers/xsimd/types/xsimd_avx512vbmi2_register.hpp +51 -0
  104. ycpp-0.1.3.data/headers/xsimd/types/xsimd_avx512vbmi_register.hpp +51 -0
  105. ycpp-0.1.3.data/headers/xsimd/types/xsimd_avx512vnni_avx512bw_register.hpp +54 -0
  106. ycpp-0.1.3.data/headers/xsimd/types/xsimd_avx512vnni_avx512vbmi2_register.hpp +53 -0
  107. ycpp-0.1.3.data/headers/xsimd/types/xsimd_avx_register.hpp +64 -0
  108. ycpp-0.1.3.data/headers/xsimd/types/xsimd_avxvnni_register.hpp +44 -0
  109. ycpp-0.1.3.data/headers/xsimd/types/xsimd_batch.hpp +1665 -0
  110. ycpp-0.1.3.data/headers/xsimd/types/xsimd_batch_constant.hpp +571 -0
  111. ycpp-0.1.3.data/headers/xsimd/types/xsimd_batch_fwd.hpp +41 -0
  112. ycpp-0.1.3.data/headers/xsimd/types/xsimd_common_arch.hpp +49 -0
  113. ycpp-0.1.3.data/headers/xsimd/types/xsimd_emulated_register.hpp +80 -0
  114. ycpp-0.1.3.data/headers/xsimd/types/xsimd_fma3_avx2_register.hpp +50 -0
  115. ycpp-0.1.3.data/headers/xsimd/types/xsimd_fma3_avx_register.hpp +50 -0
  116. ycpp-0.1.3.data/headers/xsimd/types/xsimd_fma3_sse_register.hpp +50 -0
  117. ycpp-0.1.3.data/headers/xsimd/types/xsimd_fma4_register.hpp +50 -0
  118. ycpp-0.1.3.data/headers/xsimd/types/xsimd_i8mm_neon64_register.hpp +55 -0
  119. ycpp-0.1.3.data/headers/xsimd/types/xsimd_neon64_register.hpp +55 -0
  120. ycpp-0.1.3.data/headers/xsimd/types/xsimd_neon_register.hpp +130 -0
  121. ycpp-0.1.3.data/headers/xsimd/types/xsimd_register.hpp +96 -0
  122. ycpp-0.1.3.data/headers/xsimd/types/xsimd_rvv_register.hpp +491 -0
  123. ycpp-0.1.3.data/headers/xsimd/types/xsimd_sse2_register.hpp +59 -0
  124. ycpp-0.1.3.data/headers/xsimd/types/xsimd_sse3_register.hpp +49 -0
  125. ycpp-0.1.3.data/headers/xsimd/types/xsimd_sse4_1_register.hpp +48 -0
  126. ycpp-0.1.3.data/headers/xsimd/types/xsimd_sse4_2_register.hpp +48 -0
  127. ycpp-0.1.3.data/headers/xsimd/types/xsimd_ssse3_register.hpp +48 -0
  128. ycpp-0.1.3.data/headers/xsimd/types/xsimd_sve_register.hpp +216 -0
  129. ycpp-0.1.3.data/headers/xsimd/types/xsimd_traits.hpp +408 -0
  130. ycpp-0.1.3.data/headers/xsimd/types/xsimd_utils.hpp +462 -0
  131. ycpp-0.1.3.data/headers/xsimd/types/xsimd_vsx_register.hpp +77 -0
  132. ycpp-0.1.3.data/headers/xsimd/types/xsimd_vxe_register.hpp +86 -0
  133. ycpp-0.1.3.data/headers/xsimd/types/xsimd_wasm_register.hpp +59 -0
  134. ycpp-0.1.3.data/headers/xsimd/utils/bits.hpp +122 -0
  135. ycpp-0.1.3.data/headers/xsimd/utils/xsimd_type_traits.hpp +145 -0
  136. ycpp-0.1.3.data/headers/xsimd/xsimd.hpp +43 -0
  137. ycpp-0.1.3.dist-info/DELVEWHEEL +2 -0
  138. ycpp-0.1.3.dist-info/METADATA +15 -0
  139. ycpp-0.1.3.dist-info/RECORD +142 -0
  140. ycpp-0.1.3.dist-info/WHEEL +4 -0
  141. ycpp.libs/msvcp140-a4c2229bdc2a2a630acdc095b4d86008.dll +0 -0
  142. ycpp.libs/msvcp140_1-226498d4bfcb4f07974757b596ebec45.dll +0 -0
@@ -0,0 +1,1044 @@
1
+ /***************************************************************************
2
+ * Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and *
3
+ * Martin Renou *
4
+ * Copyright (c) QuantStack *
5
+ * Copyright (c) Serge Guelton *
6
+ * *
7
+ * Distributed under the terms of the BSD 3-Clause License. *
8
+ * *
9
+ * The full license is in the file LICENSE, distributed with this software. *
10
+ ****************************************************************************/
11
+
12
+ #ifndef XSIMD_CPU_FEATURES_X86_HPP
13
+ #define XSIMD_CPU_FEATURES_X86_HPP
14
+
15
+ #include <array>
16
+ #include <cassert>
17
+ #include <cstdint>
18
+ #include <cstring>
19
+ #include <type_traits>
20
+ #if __cplusplus >= 201703L
21
+ #include <string_view>
22
+ #endif
23
+
24
+ #include "../utils/bits.hpp"
25
+ #include "./xsimd_config.hpp"
26
+
27
+ #if XSIMD_TARGET_X86 && defined(_MSC_VER)
28
+ #include <intrin.h> // Contains the definition of __cpuidex
29
+ #endif
30
+
31
+ namespace xsimd
32
+ {
33
+ namespace detail
34
+ {
35
+ using x86_reg32_t = std::uint32_t;
36
+
37
+ using cpuid_reg_t = std::array<x86_reg32_t, 4>;
38
+
39
+ /**
40
+ * CPU Identification (CPUID) instruction results.
41
+ *
42
+ * The CPUID instruction provides detailed information about the processor,
43
+ * including supported instruction set extensions (SSE, AVX, AVX-512, etc.).
44
+ * This function is well defined on all architectures but will return all zeros
45
+ * on all non-x86 architectures.
46
+ *
47
+ * @param leaf The value inputted to the EAX register.
48
+ * @param subleaf The value inputted to the ECX register.
49
+ *
50
+ * @see https://en.wikipedia.org/wiki/CPUID
51
+ */
52
+ inline cpuid_reg_t x86_cpuid(int leaf, int subleaf = 0) noexcept;
53
+
54
+ inline x86_reg32_t x86_xcr0_low() noexcept;
55
+
56
+ /** A strongly type bitset for a 32 bits register. */
57
+ template <typename E>
58
+ using x86_reg32_bitset = utils::uint_bitset<E, x86_reg32_t>;
59
+
60
+ /** A wrapper to attach a register bitfield descriptor and its CPUID index. */
61
+ template <typename E, x86_reg32_t I>
62
+ struct x86_reg_id
63
+ {
64
+ static constexpr x86_reg32_t index = I;
65
+ using bits = E;
66
+
67
+ static_assert(index >= 0 && index < 4, "At most 4 register in CPUID");
68
+ };
69
+
70
+ /** Find the register id with index k. */
71
+ template <x86_reg32_t K, typename... reg_ids>
72
+ struct find_reg_k;
73
+
74
+ /** Find the register id with index k (empty / nothing found case). */
75
+ template <x86_reg32_t K, typename... reg_ids>
76
+ struct find_reg_k
77
+ {
78
+ using type = x86_reg_id<void, 0>;
79
+ };
80
+
81
+ /** Find the register id with index k (recursive case). */
82
+ template <x86_reg32_t K, typename reg_id_head, typename... reg_id_tail>
83
+ struct find_reg_k<K, reg_id_head, reg_id_tail...>
84
+ {
85
+ using type = std::conditional_t<
86
+ reg_id_head::index == K,
87
+ reg_id_head,
88
+ typename find_reg_k<K, reg_id_tail...>::type>;
89
+ };
90
+
91
+ /**
92
+ * A class with strongly typed bitfield for `CPUID` registers.
93
+ *
94
+ * The class stores a variable number of register (up to four) from the CPUID
95
+ * output. This is a space optimization to avoid storing many zeros in the
96
+ * final `x86_cpu_features`.
97
+ * As a result, some of the type aliases `eax`, `ebx`, `ecx`, `edx` may be `void`.
98
+ */
99
+ template <x86_reg32_t leaf_num, x86_reg32_t subleaf_num, typename... reg_ids>
100
+ class x86_cpuid_regs : private x86_reg32_bitset<typename reg_ids::bits>...
101
+ {
102
+ private:
103
+ static_assert(sizeof...(reg_ids) <= 4, "At most 4 register in CPUID");
104
+
105
+ /* Parse CPUINFO register value into individual bit components.*/
106
+ constexpr explicit x86_cpuid_regs(const cpuid_reg_t& regs) noexcept
107
+ : x86_reg32_bitset<typename reg_ids::bits>(regs[reg_ids::index])...
108
+ {
109
+ }
110
+
111
+ public:
112
+ static constexpr x86_reg32_t leaf = leaf_num;
113
+ static constexpr x86_reg32_t subleaf = subleaf_num;
114
+
115
+ using eax = typename find_reg_k<0, reg_ids...>::type::bits;
116
+ using ebx = typename find_reg_k<1, reg_ids...>::type::bits;
117
+ using ecx = typename find_reg_k<2, reg_ids...>::type::bits;
118
+ using edx = typename find_reg_k<3, reg_ids...>::type::bits;
119
+
120
+ inline static x86_cpuid_regs read()
121
+ {
122
+ return x86_cpuid_regs(detail::x86_cpuid(leaf, subleaf));
123
+ }
124
+
125
+ constexpr x86_cpuid_regs() noexcept = default;
126
+
127
+ // TODO(C++17) compact version for which this was designed.
128
+ // The else clause contains a very verbose port.
129
+ #if 0
130
+ using x86_reg32_bitset<typename reg_ids::bits>::all_bits_set...;
131
+ using x86_reg32_bitset<typename reg_ids::bits>::get_range...;
132
+ #else
133
+
134
+ private:
135
+ template <int N>
136
+ struct m_empty_reg
137
+ {
138
+ enum class type {};
139
+ };
140
+
141
+ using eax_or_empty = std::conditional_t<std::is_void<eax>::value, typename m_empty_reg<0>::type, eax>;
142
+ using ebx_or_empty = std::conditional_t<std::is_void<ebx>::value, typename m_empty_reg<1>::type, ebx>;
143
+ using ecx_or_empty = std::conditional_t<std::is_void<ecx>::value, typename m_empty_reg<2>::type, ecx>;
144
+ using edx_or_empty = std::conditional_t<std::is_void<edx>::value, typename m_empty_reg<3>::type, edx>;
145
+
146
+ public:
147
+ template <eax_or_empty... bits, typename T = eax, std::enable_if_t<!std::is_void<T>::value, int> = 0>
148
+ constexpr bool all_bits_set() const noexcept
149
+ {
150
+ return x86_reg32_bitset<eax>::template all_bits_set<bits...>();
151
+ }
152
+
153
+ template <eax_or_empty start, eax_or_empty end, typename T = eax, std::enable_if_t<!std::is_void<T>::value, int> = 0>
154
+ constexpr x86_reg32_t get_range() const noexcept
155
+ {
156
+ return x86_reg32_bitset<eax>::template get_range<start, end>();
157
+ }
158
+
159
+ template <ebx_or_empty... bits, typename T = ebx, std::enable_if_t<!std::is_void<T>::value, int> = 0>
160
+ constexpr bool all_bits_set() const noexcept
161
+ {
162
+ return x86_reg32_bitset<ebx>::template all_bits_set<bits...>();
163
+ }
164
+
165
+ template <ebx_or_empty start, ebx_or_empty end, typename T = ebx, std::enable_if_t<!std::is_void<T>::value, int> = 0>
166
+ constexpr x86_reg32_t get_range() const noexcept
167
+ {
168
+ return x86_reg32_bitset<ebx>::template get_range<start, end>();
169
+ }
170
+
171
+ template <ecx_or_empty... bits, typename T = ecx, std::enable_if_t<!std::is_void<T>::value, int> = 0>
172
+ constexpr bool all_bits_set() const noexcept
173
+ {
174
+ return x86_reg32_bitset<ecx>::template all_bits_set<bits...>();
175
+ }
176
+
177
+ template <ecx_or_empty start, ecx_or_empty end, typename T = ecx, std::enable_if_t<!std::is_void<T>::value, int> = 0>
178
+ constexpr x86_reg32_t get_range() const noexcept
179
+ {
180
+ return x86_reg32_bitset<ecx>::template get_range<start, end>();
181
+ }
182
+
183
+ template <edx_or_empty... bits, typename T = edx, std::enable_if_t<!std::is_void<T>::value, int> = 0>
184
+ constexpr bool all_bits_set() const noexcept
185
+ {
186
+ return x86_reg32_bitset<edx>::template all_bits_set<bits...>();
187
+ }
188
+
189
+ template <edx_or_empty start, edx_or_empty end, typename T = edx, std::enable_if_t<!std::is_void<T>::value, int> = 0>
190
+ constexpr x86_reg32_t get_range() const noexcept
191
+ {
192
+ return x86_reg32_bitset<edx>::template get_range<start, end>();
193
+ }
194
+
195
+ #endif // C++17
196
+ };
197
+
198
+ template <bool extended>
199
+ struct x86_cpuid_highest_func
200
+ {
201
+ private:
202
+ using x86_reg32_t = detail::x86_reg32_t;
203
+ using manufacturer_str = std::array<char, 3 * sizeof(x86_reg32_t)>;
204
+
205
+ public:
206
+ static constexpr x86_reg32_t leaf = extended ? 0x80000000 : 0x0;
207
+
208
+ inline static x86_cpuid_highest_func read()
209
+ {
210
+ auto regs = detail::x86_cpuid(0);
211
+ x86_cpuid_highest_func out {};
212
+ // Highest function parameter in EAX
213
+ out.m_highest_leaf = regs[0];
214
+
215
+ // Manufacturer string in EBX, EDX, ECX (in that order)
216
+ char* manuf = out.m_manufacturer_id.data();
217
+ std::memcpy(manuf + 0 * sizeof(x86_reg32_t), &regs[1], sizeof(x86_reg32_t));
218
+ std::memcpy(manuf + 1 * sizeof(x86_reg32_t), &regs[3], sizeof(x86_reg32_t));
219
+ std::memcpy(manuf + 2 * sizeof(x86_reg32_t), &regs[2], sizeof(x86_reg32_t));
220
+
221
+ return out;
222
+ }
223
+
224
+ constexpr x86_cpuid_highest_func() noexcept = default;
225
+
226
+ /**
227
+ * Highest available leaf in CPUID non-extended range.
228
+ *
229
+ * This is the highest function parameter (EAX) that can be passed to CPUID.
230
+ * This is valid in the specified range:
231
+ * - if `extended` is `false`, that is below `0x80000000`,
232
+ * - if `extended` is `true`, that is above `0x80000000`,
233
+ */
234
+ constexpr x86_reg32_t highest_leaf() const noexcept
235
+ {
236
+ return m_highest_leaf;
237
+ }
238
+
239
+ /**
240
+ * The manufacturer ID string in a static array.
241
+ *
242
+ * This raw character array is case specific and may contain both leading
243
+ * and trailing whitespaces.
244
+ * It cannot be assumed to be null terminated.
245
+ * This is not implemented for all manufacturer when `extended` is `true`.
246
+ */
247
+ constexpr manufacturer_str manufacturer_id_raw() const noexcept
248
+ {
249
+ return m_manufacturer_id;
250
+ }
251
+
252
+ #if __cplusplus >= 201703L
253
+ constexpr std::string_view manufacturer_id() const noexcept
254
+ {
255
+ return { m_manufacturer_id.data(), m_manufacturer_id.size() };
256
+ }
257
+ #endif
258
+
259
+ private:
260
+ manufacturer_str m_manufacturer_id {};
261
+ x86_reg32_t m_highest_leaf {};
262
+ };
263
+ }
264
+
265
+ /**
266
+ * Highest CPUID Function Parameter and Manufacturer ID (EAX=0).
267
+ *
268
+ * Returns the highest leaf value supported by CPUID in the standard range
269
+ * (below 0x80000000), and the processor manufacturer ID string.
270
+ *
271
+ * @see https://en.wikipedia.org/wiki/CPUID
272
+ */
273
+ using x86_cpuid_leaf0 = detail::x86_cpuid_highest_func<false>;
274
+
275
+ /**
276
+ * Known processor manufacturer ID strings returned by CPUID leaf 0.
277
+ *
278
+ * The 12-byte manufacturer ID is stored in EBX, EDX, ECX (in that order).
279
+ * Some strings are shared across physical CPUs, emulators, and virtual machines.
280
+ * Obscure, defunct, and soft-core CPUs are not represented; they map to `unknown`.
281
+ *
282
+ * @see https://en.wikipedia.org/wiki/CPUID
283
+ */
284
+ enum class x86_manufacturer
285
+ {
286
+ /**
287
+ * AMD ("AuthenticAMD", "AMD ISBETTER").
288
+ *
289
+ * "AMD ISBETTER" was used by early K5 engineering samples.
290
+ */
291
+ amd,
292
+ /**
293
+ * Intel ("GenuineIntel", "GenuineIotel").
294
+ *
295
+ * "GenuineIotel" is a rare typo variant seen on some chips.
296
+ */
297
+ intel,
298
+ /**
299
+ * VIA / Centaur ("CentaurHauls", "VIA VIA VIA ").
300
+ *
301
+ * Centaur Technology was acquired by VIA in 1999;
302
+ * older chips report "CentaurHauls", newer ones "VIA VIA VIA ".
303
+ */
304
+ via,
305
+ /** Zhaoxin (" Shanghai "). */
306
+ zhaoxin,
307
+ /** Hygon ("HygonGenuine"). */
308
+ hygon,
309
+ /**
310
+ * Transmeta ("TransmetaCPU", "GenuineTMx86").
311
+ *
312
+ * Two different ID strings were used across product lines.
313
+ */
314
+ transmeta,
315
+ /** MCST Elbrus ("E2K MACHINE "). */
316
+ elbrus,
317
+ /** Microsoft Virtual PC / x86-to-ARM ("Virtual CPU "). */
318
+ microsoft_vpc,
319
+ /** Unrecognized manufacturer ID string. */
320
+ unknown,
321
+ };
322
+
323
+ /**
324
+ * Parse a 12-byte CPUID manufacturer ID into an @ref x86_manufacturer value.
325
+ *
326
+ * The input is the raw character array returned by @ref x86_cpuid_leaf0::manufacturer_id_raw.
327
+ * Unrecognized strings map to @ref x86_manufacturer::unknown.
328
+ */
329
+ inline x86_manufacturer x86_parse_manufacturer(const std::array<char, 12>& id) noexcept
330
+ {
331
+ auto eq = [&id](const char(&s)[13]) noexcept -> bool
332
+ {
333
+ return std::memcmp(id.data(), s, 12) == 0;
334
+ };
335
+ if (eq("GenuineIntel") || eq("GenuineIotel"))
336
+ return x86_manufacturer::intel;
337
+ if (eq("AuthenticAMD") || eq("AMD ISBETTER"))
338
+ return x86_manufacturer::amd;
339
+ if (eq("CentaurHauls") || eq("VIA VIA VIA "))
340
+ return x86_manufacturer::via;
341
+ if (eq(" Shanghai "))
342
+ return x86_manufacturer::zhaoxin;
343
+ if (eq("HygonGenuine"))
344
+ return x86_manufacturer::hygon;
345
+ if (eq("TransmetaCPU") || eq("GenuineTMx86"))
346
+ return x86_manufacturer::transmeta;
347
+ if (eq("E2K MACHINE "))
348
+ return x86_manufacturer::elbrus;
349
+ if (eq("Virtual CPU "))
350
+ return x86_manufacturer::microsoft_vpc;
351
+ return x86_manufacturer::unknown;
352
+ };
353
+
354
+ /** Return a string representation of an @ref x86_manufacturer value. */
355
+ constexpr const char* x86_manufacturer_name(x86_manufacturer m) noexcept
356
+ {
357
+ switch (m)
358
+ {
359
+ case x86_manufacturer::intel:
360
+ return "intel";
361
+ case x86_manufacturer::amd:
362
+ return "amd";
363
+ case x86_manufacturer::via:
364
+ return "via";
365
+ case x86_manufacturer::zhaoxin:
366
+ return "zhaoxin";
367
+ case x86_manufacturer::hygon:
368
+ return "hygon";
369
+ case x86_manufacturer::transmeta:
370
+ return "transmeta";
371
+ case x86_manufacturer::elbrus:
372
+ return "elbrus";
373
+ case x86_manufacturer::microsoft_vpc:
374
+ return "microsoft_vpc";
375
+ case x86_manufacturer::unknown:
376
+ return "unknown";
377
+ }
378
+ return "invalid";
379
+ }
380
+
381
+ struct x86_cpuid_leaf1_traits
382
+ {
383
+ static constexpr detail::x86_reg32_t leaf = 1;
384
+ static constexpr detail::x86_reg32_t subleaf = 0;
385
+
386
+ enum class ecx
387
+ {
388
+ /* Streaming SIMD Extensions 3. */
389
+ sse3 = 0,
390
+ /* Supplemental Streaming SIMD Extensions 3. */
391
+ ssse3 = 9,
392
+ /* Fused multiply-add with 3 operands (FMA3). */
393
+ fma3 = 12,
394
+ /* Streaming SIMD Extensions 4.1. */
395
+ sse4_1 = 19,
396
+ /* Streaming SIMD Extensions 4.2. */
397
+ sse4_2 = 20,
398
+ /* Population count instruction (POPCNT). */
399
+ popcnt = 23,
400
+ /* OS has enabled XSAVE/XRSTOR for extended processor state management. */
401
+ osxsave = 27,
402
+ /* Advanced Vector Extensions (256-bit SIMD). */
403
+ avx = 28,
404
+ };
405
+ enum class edx
406
+ {
407
+ /* Streaming SIMD Extensions 2. */
408
+ sse2 = 26,
409
+ };
410
+
411
+ using regs_t = detail::x86_cpuid_regs<leaf, subleaf,
412
+ detail::x86_reg_id<ecx, 2>,
413
+ detail::x86_reg_id<edx, 3>>;
414
+ };
415
+
416
+ /**
417
+ * Processor Info and Feature Bits.
418
+ *
419
+ * Utility class that can read and parse the registers for the first leaf level
420
+ * of the CPUID instruction.
421
+ * This is well defined on all architectures but will return all false on all
422
+ * non-x86 architectures.
423
+ *
424
+ * @see https://en.wikipedia.org/wiki/CPUID
425
+ */
426
+ using x86_cpuid_leaf1 = typename x86_cpuid_leaf1_traits::regs_t;
427
+
428
+ struct x86_cpuid_leaf7_traits
429
+ {
430
+ static constexpr detail::x86_reg32_t leaf = 7;
431
+ static constexpr detail::x86_reg32_t subleaf = 0;
432
+
433
+ enum class eax
434
+ {
435
+ /* Start bit for the encoding of the highest subleaf available. */
436
+ highest_subleaf_start = 0,
437
+ /* End bit for the encoding of the highest subleaf available. */
438
+ highest_subleaf_end = 32,
439
+ };
440
+ enum class ebx
441
+ {
442
+ /* Bit Manipulation Instruction Set 1. */
443
+ bmi1 = 3,
444
+ /* Advanced Vector Extensions 2 (integer 256-bit SIMD). */
445
+ avx2 = 5,
446
+ /* Bit Manipulation Instruction Set 2. */
447
+ bmi2 = 8,
448
+ /* AVX-512 Foundation instructions. */
449
+ avx512f = 16,
450
+ /* AVX-512 Doubleword and Quadword instructions. */
451
+ avx512dq = 17,
452
+ /* AVX-512 Integer Fused Multiply-Add instructions. */
453
+ avx512ifma = 21,
454
+ /* AVX-512 Prefetch instructions. */
455
+ avx512pf = 26,
456
+ /* AVX-512 Exponential and Reciprocal instructions. */
457
+ avx512er = 27,
458
+ /* AVX-512 Conflict Detection instructions. */
459
+ avx512cd = 28,
460
+ /* AVX-512 Byte and Word instructions. */
461
+ avx512bw = 30,
462
+ };
463
+ enum class ecx
464
+ {
465
+ /* AVX-512 Vector Bit Manipulation instructions. */
466
+ avx512vbmi = 1,
467
+ /* AVX-512 Vector Bit Manipulation instructions 2. */
468
+ avx512vbmi2 = 6,
469
+ /* AVX-512 Vector Neural Network instructions. */
470
+ avx512vnni_bw = 11,
471
+ };
472
+
473
+ using regs_t = detail::x86_cpuid_regs<leaf, subleaf,
474
+ detail::x86_reg_id<eax, 0>,
475
+ detail::x86_reg_id<ebx, 1>,
476
+ detail::x86_reg_id<ecx, 2>>;
477
+ };
478
+
479
+ /**
480
+ * Extended Feature Bits (EAX=7, ECX=0).
481
+ *
482
+ * Utility class that can read and parse the registers for the extended
483
+ * feature bits leaf of the CPUID instruction.
484
+ * This is well defined on all architectures but will return all false on all
485
+ * non-x86 architectures.
486
+ *
487
+ * @see https://en.wikipedia.org/wiki/CPUID
488
+ */
489
+ using x86_cpuid_leaf7 = typename x86_cpuid_leaf7_traits::regs_t;
490
+
491
+ struct x86_cpuid_leaf7sub1_traits
492
+ {
493
+ static constexpr detail::x86_reg32_t leaf = 7;
494
+ static constexpr detail::x86_reg32_t subleaf = 1;
495
+
496
+ enum class eax
497
+ {
498
+ /* AVX (VEX-encoded) Vector Neural Network instructions. */
499
+ avxvnni = 4,
500
+ };
501
+
502
+ using regs_t = detail::x86_cpuid_regs<leaf, subleaf,
503
+ detail::x86_reg_id<eax, 0>>;
504
+ };
505
+
506
+ /**
507
+ * Extended Feature Bits (EAX=7, ECX=1).
508
+ *
509
+ * Utility class that can read and parse the registers for the extended
510
+ * feature bits, subleaf 1, of the CPUID instruction.
511
+ * This is well defined on all architectures but will return all false on all
512
+ * non-x86 architectures.
513
+ *
514
+ * @see https://en.wikipedia.org/wiki/CPUID
515
+ */
516
+ using x86_cpuid_leaf7sub1 = typename x86_cpuid_leaf7sub1_traits::regs_t;
517
+
518
+ /**
519
+ * Highest Extended CPUID Function Parameter (EAX=0x80000000).
520
+ *
521
+ * Returns the highest leaf value supported by CPUID in the extended range
522
+ * (at or above 0x80000000), and the processor manufacturer ID string.
523
+ *
524
+ * @see https://en.wikipedia.org/wiki/CPUID
525
+ */
526
+ using x86_cpuid_leaf80000000 = detail::x86_cpuid_highest_func<true>;
527
+
528
+ struct x86_cpuid_leaf80000001_traits
529
+ {
530
+ static constexpr detail::x86_reg32_t leaf = 0x80000001;
531
+ static constexpr detail::x86_reg32_t subleaf = 0;
532
+
533
+ enum class ecx
534
+ {
535
+ /* AMD Fused multiply-add with 4 operands (FMA4). */
536
+ fma4 = 16,
537
+ };
538
+
539
+ using regs_t = detail::x86_cpuid_regs<leaf, subleaf,
540
+ detail::x86_reg_id<ecx, 2>>;
541
+ };
542
+
543
+ /**
544
+ * Extended Processor Info and Feature Bits.
545
+ *
546
+ * Utility class that can read and parse the registers for the extended
547
+ * processor info leaf of the CPUID instruction.
548
+ * This is well defined on all architectures but will return all false on all
549
+ * non-x86 architectures.
550
+ *
551
+ * @see https://en.wikipedia.org/wiki/CPUID
552
+ */
553
+ using x86_cpuid_leaf80000001 = typename x86_cpuid_leaf80000001_traits::regs_t;
554
+
555
+ /*
556
+ * Extended Control Register 0 (XCR0).
557
+ *
558
+ * Operating systems can explicitly disable the usage of instruction set (such
559
+ * as SSE or AVX extensions) by setting an appropriate flag in XCR0 register.
560
+ * This utility parses such bit values.
561
+ *
562
+ * @see https://docs.kernel.org/admin-guide/hw-vuln/gather_data_sampling.html
563
+ */
564
+ class x86_xcr0
565
+ {
566
+ public:
567
+ enum class xcr0
568
+ {
569
+ /** x87 FPU/MMX support (must be 1). */
570
+ x87 = 0,
571
+ /** XSAVE support for MXCSR and XMM registers. */
572
+ sse = 1,
573
+ /** AVX enabled and XSAVE support for upper halves of YMM registers. */
574
+ avx = 2,
575
+ /** MPX enabled and XSAVE support for BND0-BND3 registers. */
576
+ bndreg = 3,
577
+ /** MPX enabled and XSAVE support for BNDCFGU and BNDSTATUS registers. */
578
+ bndcsr = 4,
579
+ /** AVX-512 enabled and XSAVE support for opmask registers k0-k7. */
580
+ opmask = 5,
581
+ /** AVX-512 enabled and XSAVE support for upper halves of lower ZMM registers. */
582
+ zmm_hi256 = 6,
583
+ /** AVX-512 enabled and XSAVE support for upper ZMM registers. */
584
+ hi16_zmm = 7,
585
+ /** Saving/restoring Intel Processor Trace state via XSAVE enabled.*/
586
+ processor_trace = 8,
587
+ /** XSAVE support for PKRU register. */
588
+ pkru = 9,
589
+ };
590
+
591
+ /**
592
+ * Create a default value with only SSE enabled.
593
+ *
594
+ * AVX and AVX512 strictly require OSXSAVE to be enabled by the OS.
595
+ * If OSXSAVE is disabled (e.g., via bcdedit /set xsavedisable 1), AVX state won't
596
+ * be preserved across context switches, so AVX cannot be used.
597
+ * SSE is therefore the only value safe to assume.
598
+ */
599
+ constexpr static x86_xcr0 safe_default() noexcept
600
+ {
601
+ x86_reg32_t low = {};
602
+ low = utils::make_bit_mask(static_cast<x86_reg32_t>(xcr0::sse));
603
+ return x86_xcr0(low);
604
+ }
605
+
606
+ /**
607
+ * Read the XCR0 register from the CPU if on the correct architecture.
608
+ *
609
+ * This is only safe to call if bit 18 of CR4.OSXSAVE has been set.
610
+ *
611
+ * @see cpu_id::osxsave
612
+ */
613
+ inline static x86_xcr0 read()
614
+ {
615
+ assert(x86_cpuid_leaf1::read().all_bits_set<x86_cpuid_leaf1::ecx::osxsave>());
616
+ return x86_xcr0(detail::x86_xcr0_low());
617
+ }
618
+
619
+ template <xcr0... bits>
620
+ constexpr bool all_bits_set() const noexcept
621
+ {
622
+ return m_low.all_bits_set<bits...>();
623
+ }
624
+
625
+ /** Create a value which return false to everything. */
626
+ constexpr x86_xcr0() noexcept = default;
627
+
628
+ private:
629
+ using x86_reg32_t = detail::x86_reg32_t;
630
+
631
+ using xcr0_reg_t = detail::x86_reg32_bitset<xcr0>;
632
+
633
+ /** Parse a XCR0 value into individual components. */
634
+ constexpr explicit x86_xcr0(x86_reg32_t low) noexcept
635
+ : m_low(low)
636
+ {
637
+ }
638
+
639
+ xcr0_reg_t m_low {};
640
+ };
641
+
642
+ /**
643
+ * Orchestrator for `CPUID` calls.
644
+ *
645
+ * This class orchestrate `CPUID` and `XCR0` calls so that they are made in the appropriate
646
+ * order. It also implements lazy calling and cache mechanism around those calls.
647
+ * Works on all platforms, and return all zeros on non `x86` platforms.
648
+ */
649
+ class x86_cpu_features_backend_cpuid
650
+ {
651
+ public:
652
+ x86_cpu_features_backend_cpuid() noexcept = default;
653
+
654
+ inline x86_xcr0 const& xcr0() const noexcept;
655
+ inline x86_cpuid_leaf0 const& leaf0() const;
656
+ inline x86_cpuid_leaf80000000 const& leaf80000000() const;
657
+ inline x86_cpuid_leaf1 const& leaf1() const;
658
+ inline x86_cpuid_leaf7 const& leaf7() const;
659
+ inline x86_cpuid_leaf7sub1 const& leaf7sub1() const;
660
+ inline x86_cpuid_leaf80000001 const& leaf80000001() const;
661
+
662
+ private:
663
+ enum class status
664
+ {
665
+ leaf0_valid = 0,
666
+ leaf1_valid = 1,
667
+ leaf7_valid = 2,
668
+ leaf7sub1_valid = 3,
669
+ leaf80000000_valid = 4,
670
+ leaf80000001_valid = 5,
671
+ xcr0_valid = 6,
672
+ };
673
+
674
+ using status_bitset = utils::uint_bitset<status, std::uint32_t>;
675
+
676
+ mutable x86_cpuid_leaf0 m_leaf0 {};
677
+ mutable x86_cpuid_leaf1 m_leaf1 {};
678
+ mutable x86_cpuid_leaf7 m_leaf7 {};
679
+ mutable x86_cpuid_leaf7sub1 m_leaf7sub1 {};
680
+ mutable x86_cpuid_leaf80000000 m_leaf80000000 {};
681
+ mutable x86_cpuid_leaf80000001 m_leaf80000001 {};
682
+ mutable x86_xcr0 m_xcr0 {};
683
+ mutable status_bitset m_status {};
684
+
685
+ inline bool osxsave() const noexcept;
686
+
687
+ /**
688
+ * Internal utility to lazily read and cache a CPUID leaf.
689
+ *
690
+ * @tparam status_id The status bit tracking whether this leaf has been read and cached.
691
+ * @tparam L The CPUID leaf type (e.g. x86_cpuid_leaf1, x86_cpuid_leaf7).
692
+ * @param leaf_cache A non-const reference to the class member that stores the leaf
693
+ * value. It must be non-const because this function may write to it on first
694
+ * call. It is passed explicitly (rather than accessed via `this`) to allow
695
+ * factoring the caching logic across different leaf members.
696
+ * @return A const reference to `leaf_cache`. The non-const input / const-ref output
697
+ * asymmetry is intentional: callers must not modify the cached value, but
698
+ * this function needs write access to populate it.
699
+ *
700
+ * On first call, checks whether the leaf number is within the range advertised as
701
+ * supported by CPUID (via leaf 0 for the standard range, leaf 0x80000000 for the
702
+ * extended range). If supported, reads the leaf from the CPU; otherwise leaves
703
+ * `leaf_cache` at its zero-initialized default (all feature bits false). Either
704
+ * way, `status_id` is set so subsequent calls return immediately.
705
+ */
706
+ template <status status_id, typename L>
707
+ inline auto const& safe_read_leaf(L& leaf_cache) const;
708
+ };
709
+
710
+ /**
711
+ * No-Op orchestrator for `CPUID` calls
712
+ *
713
+ * This does nothing and return zero-constructed objects on all calls.
714
+ * This is meant as an optimization on non `x86` platforms as the
715
+ * `x86_cpu_features_backend_cpuid` can be slightly large (hundred of bytes).
716
+ */
717
+ class x86_cpu_features_backend_noop
718
+ {
719
+ public:
720
+ constexpr x86_xcr0 xcr0() const noexcept { return {}; }
721
+ constexpr x86_cpuid_leaf0 leaf0() const { return {}; }
722
+ constexpr x86_cpuid_leaf80000000 leaf80000000() const { return {}; }
723
+ constexpr x86_cpuid_leaf1 leaf1() const { return {}; }
724
+ constexpr x86_cpuid_leaf7 leaf7() const { return {}; }
725
+ constexpr x86_cpuid_leaf7sub1 leaf7sub1() const { return {}; }
726
+ constexpr x86_cpuid_leaf80000001 leaf80000001() const { return {}; }
727
+ };
728
+
729
+ #if XSIMD_TARGET_X86
730
+ using x86_cpu_features_backend_default = x86_cpu_features_backend_cpuid;
731
+ #else
732
+ using x86_cpu_features_backend_default = x86_cpu_features_backend_noop;
733
+ #endif
734
+
735
+ /**
736
+ * An opiniated CPU feature detection utility for x86.
737
+ *
738
+ * These are high level features that combine multiple registers reads in sequence.
739
+ * Instead of looking directly at raw CPUID results, this utility also checks that
740
+ * permissions (e.g. OSXSAVE) are enabled, and otherwise return conservative defaults.
741
+ *
742
+ * This is well defined on all architectures. It will always return false on
743
+ * non-x86 architectures.
744
+ */
745
+ class x86_cpu_features : private x86_cpu_features_backend_default
746
+ {
747
+ public:
748
+ x86_cpu_features() noexcept = default;
749
+
750
+ inline bool sse_enabled() const noexcept
751
+ {
752
+ return xcr0().all_bits_set<x86_xcr0::xcr0::sse>();
753
+ }
754
+
755
+ inline bool avx_enabled() const noexcept
756
+ {
757
+ // Check both SSE and AVX bits even though AVX must imply SSE
758
+ return xcr0().all_bits_set<x86_xcr0::xcr0::sse, x86_xcr0::xcr0::avx>();
759
+ }
760
+
761
+ inline bool avx512_enabled() const noexcept
762
+ {
763
+ // Check all SSE, AVX, optmask, and AVX512 bits even though AVX512 must
764
+ // imply AVX, SSE, and masked operations.
765
+ return xcr0().all_bits_set<x86_xcr0::xcr0::sse, x86_xcr0::xcr0::avx, x86_xcr0::xcr0::opmask, x86_xcr0::xcr0::zmm_hi256>();
766
+ }
767
+
768
+ /**
769
+ * The manufacturer ID string in a static array.
770
+ *
771
+ * This raw character array is case specific and may contain both leading
772
+ * and trailing whitespaces.
773
+ * It cannot be assumed to be null terminated.
774
+ */
775
+ inline auto manufacturer_id_raw() const noexcept
776
+ {
777
+ return leaf0().manufacturer_id_raw();
778
+ }
779
+
780
+ #if __cplusplus >= 201703L
781
+ inline std::string_view manufacturer_id() const noexcept
782
+ {
783
+ return leaf0().manufacturer_id();
784
+ }
785
+ #endif
786
+
787
+ /** The manufacturer ID string parsed into known common vendors. */
788
+ inline x86_manufacturer known_manufacturer() const noexcept
789
+ {
790
+ return x86_parse_manufacturer(manufacturer_id_raw());
791
+ }
792
+
793
+ /**
794
+ * Indicates whether the OS has enabled extended state management.
795
+ *
796
+ * When true, the OS has set bit 18 (OSXSAVE) in the CR4 control register,
797
+ * enabling the XGETBV/XSETBV instructions to access XCR0 and support
798
+ * processor extended state management using XSAVE/XRSTOR.
799
+ *
800
+ * This value is read from CPUID leaf 0x1, ECX bit 27, which reflects
801
+ * the state of CR4.OSXSAVE.
802
+ */
803
+ inline bool osxsave() const noexcept { return leaf1().all_bits_set<x86_cpuid_leaf1::ecx::osxsave>(); }
804
+
805
+ inline bool sse2() const noexcept { return sse_enabled() && leaf1().all_bits_set<x86_cpuid_leaf1::edx::sse2>(); }
806
+
807
+ inline bool sse3() const noexcept { return sse_enabled() && leaf1().all_bits_set<x86_cpuid_leaf1::ecx::sse3>(); }
808
+
809
+ inline bool ssse3() const noexcept { return sse_enabled() && leaf1().all_bits_set<x86_cpuid_leaf1::ecx::ssse3>(); }
810
+
811
+ inline bool sse4_1() const noexcept { return sse_enabled() && leaf1().all_bits_set<x86_cpuid_leaf1::ecx::sse4_1>(); }
812
+
813
+ inline bool sse4_2() const noexcept { return sse_enabled() && leaf1().all_bits_set<x86_cpuid_leaf1::ecx::sse4_2>(); }
814
+
815
+ inline bool popcnt() const noexcept { return leaf1().all_bits_set<x86_cpuid_leaf1::ecx::popcnt>(); }
816
+
817
+ inline bool fma3() const noexcept { return sse_enabled() && leaf1().all_bits_set<x86_cpuid_leaf1::ecx::fma3>(); }
818
+
819
+ inline bool avx() const noexcept { return avx_enabled() && leaf1().all_bits_set<x86_cpuid_leaf1::ecx::avx>(); }
820
+
821
+ inline bool bmi1() const noexcept { return leaf7().all_bits_set<x86_cpuid_leaf7::ebx::bmi1>(); }
822
+
823
+ inline bool avx2() const noexcept { return avx_enabled() && leaf7().all_bits_set<x86_cpuid_leaf7::ebx::avx2>(); }
824
+
825
+ inline bool bmi2() const noexcept { return leaf7().all_bits_set<x86_cpuid_leaf7::ebx::bmi2>(); }
826
+
827
+ inline bool avx512f() const noexcept { return avx512_enabled() && leaf7().all_bits_set<x86_cpuid_leaf7::ebx::avx512f>(); }
828
+
829
+ inline bool avx512dq() const noexcept { return avx512_enabled() && leaf7().all_bits_set<x86_cpuid_leaf7::ebx::avx512dq>(); }
830
+
831
+ inline bool avx512ifma() const noexcept { return avx512_enabled() && leaf7().all_bits_set<x86_cpuid_leaf7::ebx::avx512ifma>(); }
832
+
833
+ inline bool avx512pf() const noexcept { return avx512_enabled() && leaf7().all_bits_set<x86_cpuid_leaf7::ebx::avx512pf>(); }
834
+
835
+ inline bool avx512er() const noexcept { return avx512_enabled() && leaf7().all_bits_set<x86_cpuid_leaf7::ebx::avx512er>(); }
836
+
837
+ inline bool avx512cd() const noexcept { return avx512_enabled() && leaf7().all_bits_set<x86_cpuid_leaf7::ebx::avx512cd>(); }
838
+
839
+ inline bool avx512bw() const noexcept { return avx512_enabled() && leaf7().all_bits_set<x86_cpuid_leaf7::ebx::avx512bw>(); }
840
+
841
+ inline bool avx512vbmi() const noexcept { return avx512_enabled() && leaf7().all_bits_set<x86_cpuid_leaf7::ecx::avx512vbmi>(); }
842
+
843
+ inline bool avx512vbmi2() const noexcept { return avx512_enabled() && leaf7().all_bits_set<x86_cpuid_leaf7::ecx::avx512vbmi2>(); }
844
+
845
+ inline bool avx512vnni_bw() const noexcept { return avx512_enabled() && leaf7().all_bits_set<x86_cpuid_leaf7::ecx::avx512vnni_bw>(); }
846
+
847
+ inline bool avxvnni() const noexcept { return avx_enabled() && leaf7sub1().all_bits_set<x86_cpuid_leaf7sub1::eax::avxvnni>(); }
848
+
849
+ inline bool fma4() const noexcept { return avx_enabled() && leaf80000001().all_bits_set<x86_cpuid_leaf80000001::ecx::fma4>(); }
850
+ };
851
+
852
+ /********************
853
+ * Implementation *
854
+ ********************/
855
+
856
+ template <x86_cpu_features_backend_cpuid::status status_id, typename L>
857
+ inline auto const& x86_cpu_features_backend_cpuid::safe_read_leaf(L& leaf_cache) const
858
+ {
859
+ // Check if already initialized
860
+ if (m_status.bit_is_set<status_id>())
861
+ {
862
+ return leaf_cache;
863
+ }
864
+
865
+ // Limit where we need to check leaf0 or leaf 80000000.
866
+ constexpr auto extended_threshold = x86_cpuid_leaf80000000::leaf;
867
+
868
+ // Check if it is safe to call CPUID with this value.
869
+ // First we identify if the leaf is in the regular or extended range.
870
+ // TODO(C++17): if constexpr
871
+ if (L::leaf < extended_threshold)
872
+ {
873
+ // Check leaf0 in regular range
874
+ if (L::leaf <= leaf0().highest_leaf())
875
+ {
876
+ leaf_cache = L::read();
877
+ }
878
+ }
879
+ else
880
+ {
881
+ // Check leaf80000000 in extended range
882
+ if (L::leaf <= leaf80000000().highest_leaf())
883
+ {
884
+ leaf_cache = L::read();
885
+ }
886
+ }
887
+
888
+ // Mark as valid in all cases, including if it was not read.
889
+ // In this case it will be filled with zeros (all false).
890
+ m_status.set_bit<status_id>();
891
+ return leaf_cache;
892
+ }
893
+
894
+ inline x86_xcr0 const& x86_cpu_features_backend_cpuid::xcr0() const noexcept
895
+ {
896
+ if (!m_status.bit_is_set<status::xcr0_valid>())
897
+ {
898
+ m_xcr0 = osxsave() ? x86_xcr0::read() : x86_xcr0::safe_default();
899
+ m_status.set_bit<status::xcr0_valid>();
900
+ }
901
+ return m_xcr0;
902
+ }
903
+
904
+ inline x86_cpuid_leaf0 const& x86_cpu_features_backend_cpuid::leaf0() const
905
+ {
906
+ if (!m_status.bit_is_set<status::leaf0_valid>())
907
+ {
908
+ m_leaf0 = x86_cpuid_leaf0::read();
909
+ m_status.set_bit<status::leaf0_valid>();
910
+ }
911
+ return m_leaf0;
912
+ }
913
+
914
+ inline x86_cpuid_leaf80000000 const& x86_cpu_features_backend_cpuid::leaf80000000() const
915
+ {
916
+ if (!m_status.bit_is_set<status::leaf80000000_valid>())
917
+ {
918
+ m_leaf80000000 = x86_cpuid_leaf80000000::read();
919
+ m_status.set_bit<status::leaf80000000_valid>();
920
+ }
921
+ return m_leaf80000000;
922
+ }
923
+
924
+ inline x86_cpuid_leaf1 const& x86_cpu_features_backend_cpuid::leaf1() const
925
+ {
926
+ return safe_read_leaf<status::leaf1_valid>(m_leaf1);
927
+ }
928
+
929
+ inline x86_cpuid_leaf7 const& x86_cpu_features_backend_cpuid::leaf7() const
930
+ {
931
+ return safe_read_leaf<status::leaf7_valid>(m_leaf7);
932
+ }
933
+
934
+ inline x86_cpuid_leaf7sub1 const& x86_cpu_features_backend_cpuid::leaf7sub1() const
935
+ {
936
+ // Check if already initialized
937
+ if (m_status.bit_is_set<status::leaf7sub1_valid>())
938
+ {
939
+ return m_leaf7sub1;
940
+ }
941
+
942
+ // Check if safe to call CPUID with this value as subleaf.
943
+ constexpr auto start = x86_cpuid_leaf7::eax::highest_subleaf_start;
944
+ constexpr auto end = x86_cpuid_leaf7::eax::highest_subleaf_end;
945
+ const auto highest_subleaf7 = leaf7().get_range<start, end>();
946
+ if (x86_cpuid_leaf7sub1::subleaf <= highest_subleaf7)
947
+ {
948
+ m_leaf7sub1 = x86_cpuid_leaf7sub1::read();
949
+ }
950
+
951
+ // Mark as valid in all cases, including if it was not read.
952
+ // In this case it will be filled with zeros (all false).
953
+ m_status.set_bit<status::leaf7sub1_valid>();
954
+ return m_leaf7sub1;
955
+ }
956
+
957
+ inline x86_cpuid_leaf80000001 const& x86_cpu_features_backend_cpuid::leaf80000001() const
958
+ {
959
+ return safe_read_leaf<status::leaf80000001_valid>(m_leaf80000001);
960
+ }
961
+
962
+ inline bool x86_cpu_features_backend_cpuid::osxsave() const noexcept
963
+ {
964
+ return leaf1().all_bits_set<x86_cpuid_leaf1::ecx::osxsave>();
965
+ }
966
+
967
+ namespace detail
968
+ {
969
+ #if XSIMD_TARGET_X86
970
+
971
+ inline cpuid_reg_t x86_cpuid(int leaf, int subleaf) noexcept
972
+ {
973
+ cpuid_reg_t reg = {};
974
+ #if defined(_MSC_VER)
975
+ int buf[4];
976
+ __cpuidex(buf, leaf, subleaf);
977
+ std::memcpy(reg.data(), buf, sizeof(buf));
978
+
979
+ // Intel compiler has long had support for `__cpuid`, but only recently for `__cpuidex`.
980
+ // Modern Clang and GCC also now support `__cpuidex`.
981
+ // It was decided to keep the inline ASM version for maximum compatibility, as the difference
982
+ // in ASM is negligible compared to the cost of CPUID.
983
+ // https://github.com/xtensor-stack/xsimd/pull/1278
984
+ #elif XSIMD_WITH_INLINE_ASM
985
+
986
+ #if defined(__i386__) && defined(__PIC__)
987
+ // %ebx may be the PIC register
988
+ __asm__("xchg{l}\t{%%}ebx, %1\n\t"
989
+ "cpuid\n\t"
990
+ "xchg{l}\t{%%}ebx, %1\n\t"
991
+ : "=a"(reg[0]), "=r"(reg[1]), "=c"(reg[2]), "=d"(reg[3])
992
+ : "0"(leaf), "2"(subleaf));
993
+
994
+ #else
995
+ __asm__("cpuid\n\t"
996
+ : "=a"(reg[0]), "=b"(reg[1]), "=c"(reg[2]), "=d"(reg[3])
997
+ : "0"(leaf), "2"(subleaf));
998
+ #endif
999
+ #endif
1000
+ return reg;
1001
+ }
1002
+
1003
+ inline x86_reg32_t x86_xcr0_low() noexcept
1004
+ {
1005
+ #if defined(_MSC_VER)
1006
+ #if _MSC_VER >= 1400
1007
+ return static_cast<x86_reg32_t>(_xgetbv(0));
1008
+ #else
1009
+ #error "_MSC_VER < 1400 is not supported"
1010
+ #endif
1011
+
1012
+ #elif XSIMD_WITH_INLINE_ASM
1013
+ x86_reg32_t xcr0 = {};
1014
+ __asm__(
1015
+ "xorl %%ecx, %%ecx\n"
1016
+ "xgetbv\n"
1017
+ : "=a"(xcr0)
1018
+ :
1019
+ #if defined(__i386__)
1020
+ : "ecx", "edx"
1021
+ #else
1022
+ : "rcx", "rdx"
1023
+ #endif
1024
+ );
1025
+ return xcr0;
1026
+ #endif
1027
+ }
1028
+
1029
+ #else // XSIMD_TARGET_X86
1030
+
1031
+ inline cpuid_reg_t x86_cpuid(int /* leaf */, int /* subleaf */) noexcept
1032
+ {
1033
+ return {}; // All bits to zero
1034
+ }
1035
+
1036
+ inline x86_reg32_t x86_xcr0_low() noexcept
1037
+ {
1038
+ return {}; // All bits to zero
1039
+ }
1040
+
1041
+ #endif // XSIMD_TARGET_X86
1042
+ }
1043
+ }
1044
+ #endif