diff --git a/paddle/fluid/platform/cpu_info.cc b/paddle/fluid/platform/cpu_info.cc index b7ed66bd36369b0b31df3afbbd18e49fba8e23e1..6b9bacd7b71b0881660b68c7c6248848ac9f34fc 100644 --- a/paddle/fluid/platform/cpu_info.cc +++ b/paddle/fluid/platform/cpu_info.cc @@ -139,6 +139,31 @@ bool MayIUse(const cpu_isa_t cpu_isa) { if (cpu_isa == isa_any) { return true; } else { + int reg[4]; + cpuid(reg, 0); + int nIds = reg[0]; + if (nIds >= 0x00000001) { + // EAX = 1 + cpuid(reg, 0x00000001); + // AVX: ECX Bit 28 + if (cpu_isa == avx) { + int avx_mask = (1 << 28); + return (reg[2] & avx_mask) != 0; + } + } + if (nIds >= 0x00000007) { + // EAX = 7 + cpuid(reg, 0x00000007); + if (cpu_isa == avx2) { + // AVX2: EBX Bit 5 + int avx2_mask = (1 << 5); + return (reg[1] & avx2_mask) != 0; + } else if (cpu_isa == avx512f) { + // AVX512F: EBX Bit 16 + int avx512f_mask = (1 << 16); + return (reg[1] & avx512f_mask) != 0; + } + } return false; } } diff --git a/paddle/fluid/platform/cpu_info.h b/paddle/fluid/platform/cpu_info.h index c70e3be858fe72f298a5e553bcca189641392cdc..3255f50d4a20de6505cbdbe32a485dc69d31c62e 100644 --- a/paddle/fluid/platform/cpu_info.h +++ b/paddle/fluid/platform/cpu_info.h @@ -18,9 +18,9 @@ limitations under the License. */ #ifdef _WIN32 #if defined(__AVX2__) -#include //avx2 +#include // avx2 #elif defined(__AVX__) -#include //avx +#include // avx #endif // AVX #else // WIN32 #ifdef __AVX__ @@ -36,6 +36,17 @@ limitations under the License. */ #define ALIGN32_END __attribute__((aligned(32))) #endif // _WIN32 +#ifndef PADDLE_WITH_XBYAK +#ifdef _WIN32 +#define cpuid(reg, x) __cpuidex(reg, x, 0) +#else +#include +inline void cpuid(int reg[4], int x) { + __cpuid_count(x, 0, reg[0], reg[1], reg[2], reg[3]); +} +#endif +#endif + namespace paddle { namespace platform {