Skip to content

Commit

Permalink
Don't try to use AVX512 without OS support
Browse files Browse the repository at this point in the history
AVX512 instructions require OS support as well as CPU support. Bits 5,
6, and 7 of XCR0 should be enabled to run with AVX512 support. This
fixes SIGILL on OpenBSD, for example.

openbsd/ports@bb29df3
  • Loading branch information
silby committed Feb 23, 2024
1 parent e182733 commit 94360df
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion cbits/measure_off.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@
bool has_avx512_vl_bw() {
#if (__GNUC__ >= 7 || __GNUC__ == 6 && __GNUC_MINOR__ >= 3) || defined(__clang_major__)
uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
uint64_t xcr0;
__get_cpuid_count(7, 0, &eax, &ebx, &ecx, &edx);
// https://en.wikipedia.org/wiki/CPUID#EAX=7,_ECX=0:_Extended_Features
// __asm__("xgetbv\n\t" : "=a" (xcr0) : "c" (0));
const bool has_avx512_bw = ebx & (1 << 30);
const bool has_avx512_vl = ebx & (1 << 31);
// XCR0 bits 5, 6, and 7
const bool avx512_os_enabled = (xcr0 & 0xE0) == 0xE0;
// printf("cpuid=%d=cpuid\n", has_avx512_bw && has_avx512_vl);
return has_avx512_bw && has_avx512_vl;
return has_avx512_bw && has_avx512_vl && avx512_os_enabled;
#else
return false;
#endif
Expand Down

0 comments on commit 94360df

Please sign in to comment.