@@ -552,32 +552,50 @@ std::string SHA256AutoDetect()
552
552
{
553
553
std::string ret = " standard" ;
554
554
#if defined(USE_ASM) && (defined(__x86_64__) || defined(__amd64__) || defined(__i386__))
555
- (void )AVXEnabled; // Silence unused warning (in case ENABLE_AVX2 is not defined)
555
+ bool have_sse4 = false ;
556
+ bool have_xsave = false ;
557
+ bool have_avx = false ;
558
+ bool have_avx2 = false ;
559
+ bool enabled_avx = false ;
560
+
561
+ (void )AVXEnabled;
562
+ (void )have_sse4;
563
+ (void )have_avx;
564
+ (void )have_xsave;
565
+ (void )have_avx2;
566
+ (void )enabled_avx;
567
+
556
568
uint32_t eax, ebx, ecx, edx;
557
569
cpuid (1 , 0 , eax, ebx, ecx, edx);
558
- if ((ecx >> 19 ) & 1 ) {
570
+ have_sse4 = (ecx >> 19 ) & 1 ;
571
+ have_xsave = (ecx >> 27 ) & 1 ;
572
+ have_avx = (ecx >> 28 ) & 1 ;
573
+ if (have_xsave && have_avx) {
574
+ enabled_avx = AVXEnabled ();
575
+ }
576
+ if (have_sse4) {
577
+ cpuid (7 , 0 , eax, ebx, ecx, edx);
578
+ have_avx2 = (ebx >> 5 ) & 1 ;
579
+ }
580
+
581
+ if (have_sse4) {
559
582
#if defined(__x86_64__) || defined(__amd64__)
560
583
Transform = sha256_sse4::Transform;
561
584
TransformD64 = TransformD64Wrapper<sha256_sse4::Transform>;
585
+ ret = " sse4(1way)" ;
562
586
#endif
563
587
#if defined(ENABLE_SSE41) && !defined(BUILD_BITCOIN_INTERNAL)
564
588
TransformD64_4way = sha256d64_sse41::Transform_4way;
565
- ret = " sse4(1way+4way)" ;
566
- #if defined(ENABLE_AVX2) && !defined(BUILD_BITCOIN_INTERNAL)
567
- if (((ecx >> 27 ) & 1 ) && ((ecx >> 28 ) & 1 )) { // XSAVE and AVX
568
- cpuid (7 , 0 , eax, ebx, ecx, edx);
569
- if ((ebx >> 5 ) & 1 ) { // AVX2 flag
570
- if (AVXEnabled ()) { // OS has enabled AVX registers
571
- TransformD64_8way = sha256d64_avx2::Transform_8way;
572
- ret += " ,avx2(8way)" ;
573
- }
574
- }
575
- }
576
- #endif
577
- #else
578
- ret = " sse4" ;
589
+ ret += " ,sse41(4way)" ;
579
590
#endif
580
591
}
592
+
593
+ #if defined(ENABLE_AVX2) && !defined(BUILD_BITCOIN_INTERNAL)
594
+ if (have_avx2 && have_avx && enabled_avx) {
595
+ TransformD64_8way = sha256d64_avx2::Transform_8way;
596
+ ret += " ,avx2(8way)" ;
597
+ }
598
+ #endif
581
599
#endif
582
600
583
601
assert (SelfTest ());
0 commit comments