aboutsummaryrefslogtreecommitdiff
path: root/src/build_cpuid.cpp
diff options
context:
space:
mode:
authorJeroen van Rijn <Kelimion@users.noreply.github.com>2024-11-07 15:09:24 +0100
committerGitHub <noreply@github.com>2024-11-07 15:09:24 +0100
commit1b16ddbb3e30ad09b6a21bd2ee69fa228bf6045a (patch)
tree43c100590c3b630021c895891f941f060bba9c2a /src/build_cpuid.cpp
parentfd442b8678baa63be60c3c555d6063386e1d7453 (diff)
parent3bfe675a68ad8448195d3b4729ee293b547d8761 (diff)
Merge pull request #4464 from Kelimion/should_use_native
Suggest `-microarch:native` if `popcnt` instruction is missing.
Diffstat (limited to 'src/build_cpuid.cpp')
-rw-r--r--src/build_cpuid.cpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/build_cpuid.cpp b/src/build_cpuid.cpp
new file mode 100644
index 000000000..b7ba5dcdf
--- /dev/null
+++ b/src/build_cpuid.cpp
@@ -0,0 +1,35 @@
+#if !defined(GB_COMPILER_MSVC)
+ #if defined(GB_CPU_X86)
+ #include <cpuid.h>
+ #endif
+#endif
+
+gb_internal void odin_cpuid(int leaf, int result[]) {
+ #if defined(GB_CPU_ARM) || defined(GB_CPU_RISCV)
+ return;
+
+ #elif defined(GB_CPU_X86)
+
+ #if defined(GB_COMPILER_MSVC)
+ __cpuid(result, leaf);
+ #else
+ __get_cpuid(leaf, (unsigned int*)&result[0], (unsigned int*)&result[1], (unsigned int*)&result[2], (unsigned int*)&result[3]);
+ #endif
+
+ #endif
+}
+
+gb_internal bool should_use_march_native() {
+ #if !defined(GB_CPU_X86)
+ return false;
+
+ #else
+
+ int cpu[4];
+ odin_cpuid(0x1, &cpu[0]); // Get feature information in ECX + EDX
+
+ bool have_popcnt = cpu[2] & (1 << 23); // bit 23 in ECX = popcnt
+ return !have_popcnt;
+
+ #endif
+} \ No newline at end of file