For AVX2 code, also check for AVX, XSAVE, and OS support

pull/585/head
Pieter Wuille 7 years ago
parent 4a7e64fc85
commit 32d153fa36

@ -484,6 +484,14 @@ void inline cpuid(uint32_t leaf, uint32_t subleaf, uint32_t& a, uint32_t& b, uin
{ {
__asm__ ("cpuid" : "=a"(a), "=b"(b), "=c"(c), "=d"(d) : "0"(leaf), "2"(subleaf)); __asm__ ("cpuid" : "=a"(a), "=b"(b), "=c"(c), "=d"(d) : "0"(leaf), "2"(subleaf));
} }
/** Check whether the OS has enabled AVX registers. */
bool AVXEnabled()
{
uint32_t a, d;
__asm__("xgetbv" : "=a"(a), "=d"(d) : "c"(0));
return (a & 6) == 6;
}
#endif #endif
} // namespace } // namespace
@ -492,6 +500,7 @@ std::string SHA256AutoDetect()
{ {
std::string ret = "standard"; std::string ret = "standard";
#if defined(USE_ASM) && (defined(__x86_64__) || defined(__amd64__) || defined(__i386__)) #if defined(USE_ASM) && (defined(__x86_64__) || defined(__amd64__) || defined(__i386__))
(void)AVXEnabled; // Silence unused warning (in case ENABLE_AVX2 is not defined)
uint32_t eax, ebx, ecx, edx; uint32_t eax, ebx, ecx, edx;
cpuid(1, 0, eax, ebx, ecx, edx); cpuid(1, 0, eax, ebx, ecx, edx);
if ((ecx >> 19) & 1) { if ((ecx >> 19) & 1) {
@ -503,10 +512,14 @@ std::string SHA256AutoDetect()
TransformD64_4way = sha256d64_sse41::Transform_4way; TransformD64_4way = sha256d64_sse41::Transform_4way;
ret = "sse4(1way+4way)"; ret = "sse4(1way+4way)";
#if defined(ENABLE_AVX2) && !defined(BUILD_BITCOIN_INTERNAL) #if defined(ENABLE_AVX2) && !defined(BUILD_BITCOIN_INTERNAL)
cpuid(7, 0, eax, ebx, ecx, edx); if (((ecx >> 27) & 1) && ((ecx >> 28) & 1)) { // XSAVE and AVX
if ((ebx >> 5) & 1) { cpuid(7, 0, eax, ebx, ecx, edx);
TransformD64_8way = sha256d64_avx2::Transform_8way; if ((ebx >> 5) & 1) { // AVX2 flag
ret += ",avx2(8way)"; if (AVXEnabled()) { // OS has enabled AVX registers
TransformD64_8way = sha256d64_avx2::Transform_8way;
ret += ",avx2(8way)";
}
}
} }
#endif #endif
#else #else

Loading…
Cancel
Save