Skip to content

Instantly share code, notes, and snippets.

@obfusk
Created July 4, 2023 23:06
Show Gist options
  • Save obfusk/b3c47a35271e166a0dd0fc133e020003 to your computer and use it in GitHub Desktop.
Save obfusk/b3c47a35271e166a0dd0fc133e020003 to your computer and use it in GitHub Desktop.
avx detection
#include <stdio.h>
// https://en.wikipedia.org/wiki/CPUID
static void cpuid(int cpu_info[4], int info_type) {
__asm__ volatile("cpuid \n\t"
: "=a"(cpu_info[0]),
"=b"(cpu_info[1]),
"=c"(cpu_info[3]),
"=d"(cpu_info[2])
: "a"(info_type), "c"(0));
}
int main() {
int cpu_info[5] = {0};
int cpu_info7[4] = {0};
cpuid(cpu_info, 0);
int len = cpu_info[0];
printf("vendor=%s\n", (char*) &cpu_info[1]);
if (len > 0) {
cpuid(cpu_info, 1);
if (len >= 7) {
cpuid(cpu_info7, 7);
}
}
int has_avx = (cpu_info[3] & (1 << 28)) != 0;
int has_avx2 = (cpu_info7[1] & (1 << 5)) != 0;
printf("avx=%d\n", has_avx);
printf("avx2=%d\n", has_avx2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment