Skip to content

Commit

Permalink
fix and simplify cpu detection macros
Browse files Browse the repository at this point in the history
  • Loading branch information
SalvatorePreviti committed Mar 1, 2019
1 parent 524ea1a commit 959d372
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/cpuinfo/cpuinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,33 @@
#endif

#if defined(USE_INTRIN_H)
inline static uint64_t xgetbv(uint32_t xcr) {
static void _cpuid(uint32_t info[4U], const uint32_t info_type) {
info[0] = info[1] = info[2] = info[3] = 0;
__cpuid((int *)info, info_type);
}
static uint64_t xgetbv(uint32_t xcr) {
return _xgetbv(xcr);
}
#elif defined(USE_CPUID)
inline static uint64_t xgetbv(uint32_t xcr) {
uint32_t eax, edx;
static void _cpuid(uint32_t info[4U], const uint32_t info_type) {
info[0] = info[1] = info[2] = info[3] = 0u;
__asm__ __volatile__("xchgq %%rbx, %q1; cpuid; xchgq %%rbx, %q1" : "=a"(info[0]), "=&r"(info[1]), "=c"(info[2]), "=d"(info[3]) : "0"(info_type), "2"(0U));
}
static uint64_t xgetbv(uint32_t xcr) {
uint32_t eax = 0u;
uint32_t edx = 0u;
__asm__(".byte 0x0f, 0x01, 0xd0" : "=a"(eax), "=d"(edx) : "c"(xcr));
return (uint64_t)(edx) << 32 | eax;
}
#else
static void _cpuid(uint32_t info[4U], const uint32_t info_type) {
info[0] = info[1] = info[2] = info[3] = 0;
}
static uint64_t xgetbv(uint32_t) {
return 0;
}
#endif

inline static void _cpuid(unsigned int info[4U], const unsigned int info_type) {
info[0] = info[1] = info[2] = info[3] = 0;
#ifdef USE_INTRIN_H
__cpuid((int *)info, info_type);
#elif defined(USE_CPUID)
__asm__ __volatile__("xchgq %%rbx, %q1; cpuid; xchgq %%rbx, %q1" : "=a"(info[0]), "=&r"(info[1]), "=c"(info[2]), "=d"(info[3]) : "0"(info_type), "2"(0U));
#endif
}

const uint32_t AVX = 1u << 28;
const uint32_t OSXSAVE = 1u << 27;
const uint32_t XSAVE = 1u << 26;
Expand Down

1 comment on commit 959d372

@TkTech
Copy link

@TkTech TkTech commented on 959d372 Mar 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.