Skip to content

Commit 5f72417

Browse files
committed
Add ability to specify SHA256 implementation for benchmark purposes
1 parent 8247a8d commit 5f72417

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

src/crypto/sha256.cpp

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -579,9 +579,15 @@ bool AVXEnabled()
579579
} // namespace
580580

581581

582-
std::string SHA256AutoDetect()
582+
std::string SHA256AutoDetect(sha256_implementation::UseImplementation use_implementation)
583583
{
584584
std::string ret = "standard";
585+
Transform = sha256::Transform;
586+
TransformD64 = sha256::TransformD64;
587+
TransformD64_2way = nullptr;
588+
TransformD64_4way = nullptr;
589+
TransformD64_8way = nullptr;
590+
585591
#if defined(USE_ASM) && defined(HAVE_GETCPUID)
586592
bool have_sse4 = false;
587593
bool have_xsave = false;
@@ -592,16 +598,22 @@ std::string SHA256AutoDetect()
592598

593599
uint32_t eax, ebx, ecx, edx;
594600
GetCPUID(1, 0, eax, ebx, ecx, edx);
595-
have_sse4 = (ecx >> 19) & 1;
601+
if (use_implementation & sha256_implementation::USE_SSE4) {
602+
have_sse4 = (ecx >> 19) & 1;
603+
}
596604
have_xsave = (ecx >> 27) & 1;
597605
have_avx = (ecx >> 28) & 1;
598606
if (have_xsave && have_avx) {
599607
enabled_avx = AVXEnabled();
600608
}
601609
if (have_sse4) {
602610
GetCPUID(7, 0, eax, ebx, ecx, edx);
603-
have_avx2 = (ebx >> 5) & 1;
604-
have_x86_shani = (ebx >> 29) & 1;
611+
if (use_implementation & sha256_implementation::USE_AVX2) {
612+
have_avx2 = (ebx >> 5) & 1;
613+
}
614+
if (use_implementation & sha256_implementation::USE_SHANI) {
615+
have_x86_shani = (ebx >> 29) & 1;
616+
}
605617
}
606618

607619
#if defined(ENABLE_X86_SHANI) && !defined(BUILD_BITCOIN_INTERNAL)
@@ -637,27 +649,28 @@ std::string SHA256AutoDetect()
637649

638650
#if defined(ENABLE_ARM_SHANI) && !defined(BUILD_BITCOIN_INTERNAL)
639651
bool have_arm_shani = false;
640-
652+
if (use_implementation & sha256_implementation::USE_SHANI) {
641653
#if defined(__linux__)
642654
#if defined(__arm__) // 32-bit
643-
if (getauxval(AT_HWCAP2) & HWCAP2_SHA2) {
644-
have_arm_shani = true;
645-
}
655+
if (getauxval(AT_HWCAP2) & HWCAP2_SHA2) {
656+
have_arm_shani = true;
657+
}
646658
#endif
647659
#if defined(__aarch64__) // 64-bit
648-
if (getauxval(AT_HWCAP) & HWCAP_SHA2) {
649-
have_arm_shani = true;
650-
}
660+
if (getauxval(AT_HWCAP) & HWCAP_SHA2) {
661+
have_arm_shani = true;
662+
}
651663
#endif
652664
#endif
653665

654666
#if defined(MAC_OSX)
655-
int val = 0;
656-
size_t len = sizeof(val);
657-
if (sysctlbyname("hw.optional.arm.FEAT_SHA256", &val, &len, nullptr, 0) == 0) {
658-
have_arm_shani = val != 0;
659-
}
667+
int val = 0;
668+
size_t len = sizeof(val);
669+
if (sysctlbyname("hw.optional.arm.FEAT_SHA256", &val, &len, nullptr, 0) == 0) {
670+
have_arm_shani = val != 0;
671+
}
660672
#endif
673+
}
661674

662675
if (have_arm_shani) {
663676
Transform = sha256_arm_shani::Transform;

src/crypto/sha256.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,22 @@ class CSHA256
2626
CSHA256& Reset();
2727
};
2828

29+
namespace sha256_implementation {
30+
enum UseImplementation : uint8_t {
31+
STANDARD = 0,
32+
USE_SSE4 = 1 << 0,
33+
USE_AVX2 = 1 << 1,
34+
USE_SHANI = 1 << 2,
35+
USE_SSE4_AND_AVX2 = USE_SSE4 | USE_AVX2,
36+
USE_SSE4_AND_SHANI = USE_SSE4 | USE_SHANI,
37+
USE_ALL = USE_SSE4 | USE_AVX2 | USE_SHANI,
38+
};
39+
}
40+
2941
/** Autodetect the best available SHA256 implementation.
3042
* Returns the name of the implementation.
3143
*/
32-
std::string SHA256AutoDetect();
44+
std::string SHA256AutoDetect(sha256_implementation::UseImplementation use_implementation = sha256_implementation::USE_ALL);
3345

3446
/** Compute multiple double-SHA256's of 64-byte blobs.
3547
* output: pointer to a blocks*32 byte output buffer

0 commit comments

Comments
 (0)