Skip to content

Instantly share code, notes, and snippets.

@zeha
Created December 6, 2019 19:22
Show Gist options
  • Save zeha/7bbcbb1dafd5b92a16d7ad1184eb6a18 to your computer and use it in GitHub Desktop.
Save zeha/7bbcbb1dafd5b92a16d7ad1184eb6a18 to your computer and use it in GitHub Desktop.
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#define CPUID_EBX_AVX2 0x00000020
#define CPUID_EBX_AVX512F 0x00010000
#define XCR0_SSE 0x00000002
#define XCR0_AVX 0x00000004
#define XCR0_OPMASK 0x00000020
#define XCR0_ZMM_HI256 0x00000040
#define XCR0_HI16_ZMM 0x00000080
static void
_cpuid(unsigned int cpu_info[4U], const unsigned int cpu_info_type)
{
__asm__ __volatile__("xchgq %%rbx, %q1; cpuid; xchgq %%rbx, %q1"
: "=a"(cpu_info[0]), "=&r"(cpu_info[1]),
"=c"(cpu_info[2]), "=d"(cpu_info[3])
: "0"(cpu_info_type), "2"(0U));
}
int main() {
unsigned int cpu_info[4];
unsigned int id;
uint32_t xcr0 = 0U;
_cpuid(cpu_info, 0x0);
__asm__ __volatile__(".byte 0x0f, 0x01, 0xd0" /* XGETBV */
: "=a"(xcr0)
: "c"((uint32_t) 0U)
: "%edx");
unsigned int cpu_info7[4];
_cpuid(cpu_info7, 0x00000007);
printf("claims avx512f? %s \n", (cpu_info7[1] & CPUID_EBX_AVX512F) == CPUID_EBX_AVX512F ? "yes":"no");
printf("%x \n", ((cpu_info7[1] & CPUID_EBX_AVX512F) ) );
printf("old code claims avx512f? %s \n", ((cpu_info7[1] & CPUID_EBX_AVX512F) != 0x0) ? "yes":"no");
if ((cpu_info7[1] & CPUID_EBX_AVX512F) == CPUID_EBX_AVX512F &&
(xcr0 & (XCR0_OPMASK | XCR0_ZMM_HI256 | XCR0_HI16_ZMM))
== (XCR0_OPMASK | XCR0_ZMM_HI256 | XCR0_HI16_ZMM)) {
printf("has avx512f\n");
}
printf("done\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment